1 |
#include "LSCPTest.h" |
#include "LSCPTest.h" |
2 |
|
|
3 |
|
#include "../common/global.h" |
4 |
|
#include "../common/optional.h" |
5 |
|
|
6 |
#include <iostream> |
#include <iostream> |
7 |
#include <stdio.h> |
#include <stdio.h> |
8 |
#include <stdlib.h> |
#include <stdlib.h> |
9 |
|
#include <unistd.h> |
10 |
|
|
11 |
CPPUNIT_TEST_SUITE_REGISTRATION(LSCPTest); |
CPPUNIT_TEST_SUITE_REGISTRATION(LSCPTest); |
12 |
|
|
20 |
static int hSocket = -1; |
static int hSocket = -1; |
21 |
static FILE* hServerIn = NULL; |
static FILE* hServerIn = NULL; |
22 |
|
|
23 |
|
/// Returns first token from \a sentence and removes that token (and evtl. delimiter) from \a sentence. |
24 |
|
static optional<string> __ExtractFirstToken(string* pSentence, const string Delimiter) { |
25 |
|
if (*pSentence == "") return optional<string>::nothing; |
26 |
|
|
27 |
|
string::size_type pos = pSentence->find(Delimiter); |
28 |
|
|
29 |
|
// if sentence has only one token |
30 |
|
if (pos == string::npos) { |
31 |
|
string token = *pSentence; |
32 |
|
*pSentence = ""; |
33 |
|
return token; |
34 |
|
} |
35 |
|
|
36 |
|
// sentence has more than one token, so extract the first token |
37 |
|
string token = pSentence->substr(0, pos); |
38 |
|
*pSentence = pSentence->replace(0, pos + 1, ""); |
39 |
|
return token; |
40 |
|
} |
41 |
|
|
42 |
// split the multi line response string into the individual lines and remove the last (delimiter) line and the line feed characters in all lines |
// split the multi line response string into the individual lines and remove the last (delimiter) line and the line feed characters in all lines |
43 |
static vector<string> __ConvertMultiLineMessage(string msg) { |
static vector<string> __ConvertMultiLineMessage(string msg) { |
44 |
vector<string> res; |
vector<string> res; |
164 |
return msg.substr(0, pos); |
return msg.substr(0, pos); |
165 |
} |
} |
166 |
|
|
167 |
// wait until LSCP server answers with a multi line answer |
/// wait until LSCP server answers with a multi line answer (throws LinuxSamplerException if optional timeout exceeded) |
168 |
vector<string> LSCPTest::receiveMultiLineAnswerFromLSCPServer() { |
vector<string> LSCPTest::receiveMultiLineAnswerFromLSCPServer(uint timeout_seconds) throw (LinuxSamplerException) { |
169 |
string msg = receiveAnswerFromLSCPServer("\n.\r\n"); |
string msg = receiveAnswerFromLSCPServer("\n.\r\n", timeout_seconds); |
170 |
return __ConvertMultiLineMessage(msg); |
return __ConvertMultiLineMessage(msg); |
171 |
} |
} |
172 |
|
|
173 |
// wait until LSCP server answers with the given \a delimiter token at the end |
/// wait until LSCP server answers with the given \a delimiter token at the end (throws LinuxSamplerException if optional timeout exceeded or socket error occured) |
174 |
string LSCPTest::receiveAnswerFromLSCPServer(string delimiter) { |
string LSCPTest::receiveAnswerFromLSCPServer(string delimiter, uint timeout_seconds) throw (LinuxSamplerException) { |
175 |
if (!hServerIn) { |
if (!hServerIn) { |
176 |
cout << "receiveAnswerFromLSCPServer() error: client socket not ready\n" << flush; |
cout << "receiveAnswerFromLSCPServer() error: client socket not ready\n" << flush; |
177 |
return ""; |
return ""; |
178 |
} |
} |
179 |
string message; |
string message; |
180 |
char c; |
char c; |
181 |
while ((c = fgetc(hServerIn)) != EOF) { |
fd_set sockSet; |
182 |
|
timeval timeout; |
183 |
|
|
184 |
|
while (true) { |
185 |
|
if (timeout_seconds) { |
186 |
|
FD_ZERO(&sockSet); |
187 |
|
FD_SET(hSocket, &sockSet); |
188 |
|
timeout.tv_sec = timeout_seconds; |
189 |
|
timeout.tv_usec = 0; |
190 |
|
int res = select(hSocket + 1, &sockSet, NULL, NULL, &timeout); |
191 |
|
if (!res) throw LinuxSamplerException("LSCPTest::receiveAnswerFromLSCPServer(): timeout (" + ToString(timeout_seconds) + "s) exceeded waiting for expected answer (end)"); |
192 |
|
else if (res < 0) throw LinuxSamplerException("LSCPTest::receiveAnswerFromLSCPServer(): select error"); |
193 |
|
} |
194 |
|
|
195 |
|
// there's something to read, so read one character |
196 |
|
c = fgetc(hServerIn); |
197 |
|
if (c == EOF) { |
198 |
|
cout << "receiveAnswerFromLSCPServer() error: EOF reached\n" << flush; |
199 |
|
return ""; |
200 |
|
} |
201 |
message += c; |
message += c; |
202 |
string::size_type pos = message.rfind(delimiter); // ouch, but this is only a test case, right? ;) |
string::size_type pos = message.rfind(delimiter); // ouch, but this is only a test case, right? ;) |
203 |
if (pos != string::npos) return message; |
if (pos != string::npos) return message; |
204 |
} |
} |
|
cout << "receiveAnswerFromLSCPServer() error: EOF reached\n" << flush; |
|
|
return ""; |
|
205 |
} |
} |
206 |
|
|
207 |
|
|
303 |
CPPUNIT_ASSERT(true); // success |
CPPUNIT_ASSERT(true); // success |
304 |
} |
} |
305 |
|
|
306 |
|
// Check "GET AUDIO_OUTPUT_CHANNEL_PARAMETER INFO" LSCP command. |
307 |
|
void LSCPTest::test_GET_AUDIO_OUTPUT_CHANNEL_PARAMETER_INFO() { |
308 |
|
// first check if there's already an audio output device created |
309 |
|
sendCommandToLSCPServer("GET AUDIO_OUTPUT_DEVICES"); |
310 |
|
string answer = receiveSingleLineAnswerFromLSCPServer(); |
311 |
|
int devices = atoi(answer.c_str()); |
312 |
|
CPPUNIT_ASSERT(devices >= 0); |
313 |
|
if (!devices) { // if there's no audio output device yet, try to create one |
314 |
|
sendCommandToLSCPServer("GET AVAILABLE_AUDIO_OUTPUT_DRIVERS"); |
315 |
|
string drivers = receiveSingleLineAnswerFromLSCPServer(); |
316 |
|
CPPUNIT_ASSERT(drivers.size()); |
317 |
|
|
318 |
|
// iterate through all available drivers until device creation was successful |
319 |
|
do { |
320 |
|
optional<string> driver = __ExtractFirstToken(&drivers, ","); |
321 |
|
CPPUNIT_ASSERT(driver); |
322 |
|
|
323 |
|
sendCommandToLSCPServer("CREATE AUDIO_OUTPUT_DEVICE " + *driver); |
324 |
|
answer = receiveSingleLineAnswerFromLSCPServer(); |
325 |
|
} while (answer != "OK[0]"); |
326 |
|
} |
327 |
|
|
328 |
|
// now we can check the "GET AUDIO_OUTPUT_CHANNEL_PARAMETER INFO" command |
329 |
|
const uint timeout_seconds = 2; |
330 |
|
sendCommandToLSCPServer("GET AUDIO_OUTPUT_CHANNEL_PARAMETER INFO 0 0 NAME"); |
331 |
|
vector<string> vAnswer = receiveMultiLineAnswerFromLSCPServer(timeout_seconds); |
332 |
|
CPPUNIT_ASSERT(vAnswer.size() >= 4); // should at least contain tags TYPE, DESCRIPTION, FIX and MULTIPLICITY |
333 |
|
|
334 |
|
sendCommandToLSCPServer("GET AUDIO_OUTPUT_CHANNEL_PARAMETER INFO 0 0 IS_MIX_CHANNEL"); |
335 |
|
vAnswer = receiveMultiLineAnswerFromLSCPServer(timeout_seconds); |
336 |
|
CPPUNIT_ASSERT(vAnswer.size() >= 4); // should at least contain tags TYPE, DESCRIPTION, FIX and MULTIPLICITY |
337 |
|
} |
338 |
|
|
339 |
// Check if we can shutdown the LSCP Server without problems. |
// Check if we can shutdown the LSCP Server without problems. |
340 |
void LSCPTest::testShutdownLSCPServer() { |
void LSCPTest::testShutdownLSCPServer() { |
341 |
//cout << "testShutdownLSCPServer()\n" << flush; |
//cout << "testShutdownLSCPServer()\n" << flush; |