1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Execution Server
3 * ---------------------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief ExecServer Tests.
22 *//*--------------------------------------------------------------------*/
26 #include "xsProtocol.hpp"
27 #include "deSocket.hpp"
28 #include "deRingBuffer.hpp"
29 #include "deFilePath.hpp"
30 #include "deBlockBuffer.hpp"
31 #include "deThread.hpp"
32 #include "deStringUtil.hpp"
33 #include "deUniquePtr.hpp"
36 #include "deProcess.h"
49 typedef de::UniquePtr<Message> ScopedMsgPtr;
51 class SocketError : public Error
54 SocketError (deSocketResult result, const char* message, const char* file, int line)
55 : Error (message, deGetSocketResultName(result), file, line)
60 deSocketResult getResult (void) const
66 deSocketResult m_result;
70 void sendMessage (de::Socket& socket, const Message& message)
78 while (pos < buf.size())
80 size_t numLeft = buf.size() - pos;
82 deSocketResult result = socket.send(&buf[pos], numLeft, &numSent);
84 if (result != DE_SOCKETRESULT_SUCCESS)
85 throw SocketError(result, "send() failed", __FILE__, __LINE__);
91 void readBytes (de::Socket& socket, vector<deUint8>& dst, size_t numBytes)
95 while (numRead < numBytes)
97 size_t numLeft = numBytes - numRead;
98 size_t curNumRead = 0;
99 deSocketResult result = socket.receive(&dst[numRead], numLeft, &curNumRead);
101 if (result != DE_SOCKETRESULT_SUCCESS)
102 throw SocketError(result, "receive() failed", __FILE__, __LINE__);
104 numRead += curNumRead;
108 Message* readMessage (de::Socket& socket)
111 vector<deUint8> header;
112 readBytes(socket, header, MESSAGE_HEADER_SIZE);
116 Message::parseHeader(&header[0], (int)header.size(), type, messageSize);
118 // Simple messages without any data.
121 case MESSAGETYPE_KEEPALIVE: return new KeepAliveMessage();
122 case MESSAGETYPE_PROCESS_STARTED: return new ProcessStartedMessage();
124 break; // Read message with data.
127 vector<deUint8> messageBuf;
128 readBytes(socket, messageBuf, messageSize-MESSAGE_HEADER_SIZE);
132 case MESSAGETYPE_HELLO: return new HelloMessage(&messageBuf[0], (int)messageBuf.size());
133 case MESSAGETYPE_TEST: return new TestMessage(&messageBuf[0], (int)messageBuf.size());
134 case MESSAGETYPE_PROCESS_LOG_DATA: return new ProcessLogDataMessage(&messageBuf[0], (int)messageBuf.size());
135 case MESSAGETYPE_INFO: return new InfoMessage(&messageBuf[0], (int)messageBuf.size());
136 case MESSAGETYPE_PROCESS_LAUNCH_FAILED: return new ProcessLaunchFailedMessage(&messageBuf[0], (int)messageBuf.size());
137 case MESSAGETYPE_PROCESS_FINISHED: return new ProcessFinishedMessage(&messageBuf[0], (int)messageBuf.size());
139 XS_FAIL("Unknown message");
146 inline TestClock (void)
151 inline void reset (void)
153 m_initTime = deGetMicroseconds();
156 inline int getMilliseconds (void)
158 return (int)((deGetMicroseconds() - m_initTime) / 1000);
168 TestContext (void) : startServer(false) {}
170 std::string serverPath;
171 std::string testerPath;
172 de::SocketAddress address;
175 // Passed from execserver.
176 std::string logFileName;
177 std::string caseList;
180 TestContext (const TestContext& other);
181 TestContext& operator= (const TestContext& other);
187 TestCase (TestContext& testCtx, const char* name) : m_testCtx(testCtx), m_name(name) {}
188 virtual ~TestCase (void) {}
190 const char* getName (void) const { return m_name.c_str(); }
192 virtual void runClient (de::Socket& socket) = DE_NULL;
193 virtual void runProgram (void) = DE_NULL;
196 TestContext& m_testCtx;
203 TestExecutor (TestContext& testCtx);
204 ~TestExecutor (void);
206 void runCases (const std::vector<TestCase*>& testCases);
207 bool runCase (TestCase* testCase);
210 TestContext& m_testCtx;
213 TestExecutor::TestExecutor (TestContext& testCtx)
218 TestExecutor::~TestExecutor (void)
222 void TestExecutor::runCases (const std::vector<TestCase*>& testCases)
225 int numCases = (int)testCases.size();
227 for (std::vector<TestCase*>::const_iterator i = testCases.begin(); i != testCases.end(); i++)
233 printf("\n %d/%d passed!\n", numPassed, numCases);
236 class FilePrinter : public de::Thread
244 void start (deFile* file)
246 DE_ASSERT(!m_curFile);
256 while (deFile_read(m_curFile, &buf[0], (deInt64)sizeof(buf), &numRead) == DE_FILERESULT_SUCCESS)
257 fwrite(&buf[0], 1, (size_t)numRead, stdout);
266 bool TestExecutor::runCase (TestCase* testCase)
268 printf("%s\n", testCase->getName());
270 bool success = false;
271 deProcess* serverProc = DE_NULL;
272 FilePrinter stdoutPrinter;
273 FilePrinter stderrPrinter;
277 if (m_testCtx.startServer)
279 string cmdLine = m_testCtx.serverPath + " --port=" + de::toString(m_testCtx.address.getPort());
280 serverProc = deProcess_create();
281 XS_CHECK(serverProc);
283 if (!deProcess_start(serverProc, cmdLine.c_str(), DE_NULL))
285 string errMsg = deProcess_getLastError(serverProc);
286 deProcess_destroy(serverProc);
287 XS_FAIL(errMsg.c_str());
290 deSleep(200); /* Give 200ms for server to start. */
291 XS_CHECK(deProcess_isRunning(serverProc));
293 // Start stdout/stderr printers.
294 stdoutPrinter.start(deProcess_getStdOut(serverProc));
295 stderrPrinter.start(deProcess_getStdErr(serverProc));
300 socket.connect(m_testCtx.address);
303 socket.setFlags(DE_SOCKET_CLOSE_ON_EXEC);
306 testCase->runClient(socket);
309 if (socket.isConnected())
313 if (serverProc && deProcess_isRunning(serverProc))
315 XS_CHECK(deProcess_terminate(serverProc));
317 XS_CHECK(deProcess_waitForFinish(serverProc));
319 stdoutPrinter.join();
320 stderrPrinter.join();
325 catch (const std::exception& e)
327 printf("FAIL: %s\n\n", e.what());
331 deProcess_destroy(serverProc);
336 class ConnectTest : public TestCase
339 ConnectTest (TestContext& testCtx)
340 : TestCase(testCtx, "connect")
344 void runClient (de::Socket& socket)
349 void runProgram (void) { /* nothing */ }
352 class HelloTest : public TestCase
355 HelloTest (TestContext& testCtx)
356 : TestCase(testCtx, "hello")
360 void runClient (de::Socket& socket)
362 xs::HelloMessage msg;
363 sendMessage(socket, (const xs::Message&)msg);
366 void runProgram (void) { /* nothing */ }
369 class ExecFailTest : public TestCase
372 ExecFailTest (TestContext& testCtx)
373 : TestCase(testCtx, "exec-fail")
377 void runClient (de::Socket& socket)
379 xs::ExecuteBinaryMessage execMsg;
380 execMsg.name = "foobar-notfound";
382 execMsg.caseList = "";
383 execMsg.workDir = "";
385 sendMessage(socket, execMsg);
387 const int timeout = 100; // 100ms.
392 if (clock.getMilliseconds() > timeout)
393 XS_FAIL("Didn't receive PROCESS_LAUNCH_FAILED");
395 ScopedMsgPtr msg(readMessage(socket));
397 if (msg->type == MESSAGETYPE_PROCESS_LAUNCH_FAILED)
399 else if (msg->type == MESSAGETYPE_KEEPALIVE)
402 XS_FAIL("Invalid message");
406 void runProgram (void) { /* nothing */ }
409 class SimpleExecTest : public TestCase
412 SimpleExecTest (TestContext& testCtx)
413 : TestCase(testCtx, "simple-exec")
417 void runClient (de::Socket& socket)
419 xs::ExecuteBinaryMessage execMsg;
420 execMsg.name = m_testCtx.testerPath;
421 execMsg.params = "--program=simple-exec";
422 execMsg.caseList = "";
423 execMsg.workDir = "";
425 sendMessage(socket, execMsg);
427 const int timeout = 5000; // 5s.
430 bool gotProcessStarted = false;
431 bool gotProcessFinished = false;
435 if (clock.getMilliseconds() > timeout)
438 ScopedMsgPtr msg(readMessage(socket));
440 if (msg->type == MESSAGETYPE_PROCESS_STARTED)
441 gotProcessStarted = true;
442 else if (msg->type == MESSAGETYPE_PROCESS_LAUNCH_FAILED)
443 XS_FAIL("Got PROCESS_LAUNCH_FAILED");
444 else if (gotProcessStarted && msg->type == MESSAGETYPE_PROCESS_FINISHED)
446 gotProcessFinished = true;
449 else if (msg->type == MESSAGETYPE_KEEPALIVE || msg->type == MESSAGETYPE_INFO)
452 XS_FAIL((string("Invalid message: ") + de::toString(msg->type)).c_str());
455 if (!gotProcessStarted)
456 XS_FAIL("Did't get PROCESS_STARTED message");
458 if (!gotProcessFinished)
459 XS_FAIL("Did't get PROCESS_FINISHED message");
462 void runProgram (void) { /* print nothing. */ }
465 class InfoTest : public TestCase
470 InfoTest (TestContext& testCtx)
471 : TestCase (testCtx, "info")
472 , infoStr ("Hello, World")
476 void runClient (de::Socket& socket)
478 xs::ExecuteBinaryMessage execMsg;
479 execMsg.name = m_testCtx.testerPath;
480 execMsg.params = "--program=info";
481 execMsg.caseList = "";
482 execMsg.workDir = "";
484 sendMessage(socket, execMsg);
486 const int timeout = 10000; // 10s.
489 bool gotProcessStarted = false;
490 bool gotProcessFinished = false;
491 std::string receivedInfo = "";
495 if (clock.getMilliseconds() > timeout)
498 ScopedMsgPtr msg(readMessage(socket));
500 if (msg->type == MESSAGETYPE_PROCESS_STARTED)
501 gotProcessStarted = true;
502 else if (msg->type == MESSAGETYPE_PROCESS_LAUNCH_FAILED)
503 XS_FAIL("Got PROCESS_LAUNCH_FAILED");
504 else if (gotProcessStarted && msg->type == MESSAGETYPE_INFO)
505 receivedInfo += static_cast<const InfoMessage*>(msg.get())->info;
506 else if (gotProcessStarted && msg->type == MESSAGETYPE_PROCESS_FINISHED)
508 gotProcessFinished = true;
511 else if (msg->type == MESSAGETYPE_KEEPALIVE)
514 XS_FAIL("Invalid message");
517 if (!gotProcessStarted)
518 XS_FAIL("Did't get PROCESS_STARTED message");
520 if (!gotProcessFinished)
521 XS_FAIL("Did't get PROCESS_FINISHED message");
523 if (receivedInfo != infoStr)
524 XS_FAIL("Info data doesn't match");
527 void runProgram (void) { printf("%s", infoStr.c_str()); }
530 class LogDataTest : public TestCase
533 LogDataTest (TestContext& testCtx)
534 : TestCase(testCtx, "logdata")
538 void runClient (de::Socket& socket)
540 xs::ExecuteBinaryMessage execMsg;
541 execMsg.name = m_testCtx.testerPath;
542 execMsg.params = "--program=logdata";
543 execMsg.caseList = "";
544 execMsg.workDir = "";
546 sendMessage(socket, execMsg);
548 const int timeout = 10000; // 10s.
551 bool gotProcessStarted = false;
552 bool gotProcessFinished = false;
553 std::string receivedData = "";
557 if (clock.getMilliseconds() > timeout)
560 ScopedMsgPtr msg(readMessage(socket));
562 if (msg->type == MESSAGETYPE_PROCESS_STARTED)
563 gotProcessStarted = true;
564 else if (msg->type == MESSAGETYPE_PROCESS_LAUNCH_FAILED)
565 XS_FAIL("Got PROCESS_LAUNCH_FAILED");
566 else if (gotProcessStarted && msg->type == MESSAGETYPE_PROCESS_LOG_DATA)
567 receivedData += static_cast<const ProcessLogDataMessage*>(msg.get())->logData;
568 else if (gotProcessStarted && msg->type == MESSAGETYPE_PROCESS_FINISHED)
570 gotProcessFinished = true;
573 else if (msg->type == MESSAGETYPE_KEEPALIVE)
575 else if (msg->type == MESSAGETYPE_INFO)
576 XS_FAIL(static_cast<const InfoMessage*>(msg.get())->info.c_str());
578 XS_FAIL("Invalid message");
581 if (!gotProcessStarted)
582 XS_FAIL("Did't get PROCESS_STARTED message");
584 if (!gotProcessFinished)
585 XS_FAIL("Did't get PROCESS_FINISHED message");
587 const char* expected = "Foo\nBar\n";
588 if (receivedData != expected)
590 printf(" received: '%s'\n expected: '%s'\n", receivedData.c_str(), expected);
591 XS_FAIL("Log data doesn't match");
595 void runProgram (void)
597 deFile* file = deFile_create(m_testCtx.logFileName.c_str(), DE_FILEMODE_OPEN|DE_FILEMODE_CREATE|DE_FILEMODE_TRUNCATE|DE_FILEMODE_WRITE);
600 const char line0[] = "Foo\n";
601 const char line1[] = "Bar\n";
602 deInt64 numWritten = 0;
605 XS_CHECK(deFile_write(file, line0, sizeof(line0)-1, &numWritten) == DE_FILERESULT_SUCCESS);
606 XS_CHECK(numWritten == sizeof(line0)-1);
608 // Sleep for 0.5s and write line 2.
610 XS_CHECK(deFile_write(file, line1, sizeof(line1)-1, &numWritten) == DE_FILERESULT_SUCCESS);
611 XS_CHECK(numWritten == sizeof(line1)-1);
613 deFile_destroy(file);
617 class BigLogDataTest : public TestCase
622 DATA_SIZE = 100*1024*1024
625 BigLogDataTest (TestContext& testCtx)
626 : TestCase(testCtx, "biglogdata")
630 void runClient (de::Socket& socket)
632 xs::ExecuteBinaryMessage execMsg;
633 execMsg.name = m_testCtx.testerPath;
634 execMsg.params = "--program=biglogdata";
635 execMsg.caseList = "";
636 execMsg.workDir = "";
638 sendMessage(socket, execMsg);
640 const int timeout = 30000; // 30s.
643 bool gotProcessStarted = false;
644 bool gotProcessFinished = false;
645 int receivedBytes = 0;
649 if (clock.getMilliseconds() > timeout)
652 ScopedMsgPtr msg(readMessage(socket));
654 if (msg->type == MESSAGETYPE_PROCESS_STARTED)
655 gotProcessStarted = true;
656 else if (msg->type == MESSAGETYPE_PROCESS_LAUNCH_FAILED)
657 XS_FAIL("Got PROCESS_LAUNCH_FAILED");
658 else if (gotProcessStarted && msg->type == MESSAGETYPE_PROCESS_LOG_DATA)
659 receivedBytes += (int)static_cast<const ProcessLogDataMessage*>(msg.get())->logData.length();
660 else if (gotProcessStarted && msg->type == MESSAGETYPE_PROCESS_FINISHED)
662 gotProcessFinished = true;
665 else if (msg->type == MESSAGETYPE_KEEPALIVE)
667 // Reply with keepalive.
668 sendMessage(socket, KeepAliveMessage());
671 else if (msg->type == MESSAGETYPE_INFO)
672 printf("%s", static_cast<const InfoMessage*>(msg.get())->info.c_str());
674 XS_FAIL("Invalid message");
677 if (!gotProcessStarted)
678 XS_FAIL("Did't get PROCESS_STARTED message");
680 if (!gotProcessFinished)
681 XS_FAIL("Did't get PROCESS_FINISHED message");
683 if (receivedBytes != DATA_SIZE)
685 printf(" received: %d bytes\n expected: %d bytes\n", receivedBytes, DATA_SIZE);
686 XS_FAIL("Log data size doesn't match");
689 int timeMs = clock.getMilliseconds();
690 printf(" Streamed %d bytes in %d ms: %.2f MiB/s\n", DATA_SIZE, timeMs, ((float)DATA_SIZE / (float)(1024*1024)) / ((float)timeMs / 1000.0f));
693 void runProgram (void)
695 deFile* file = deFile_create(m_testCtx.logFileName.c_str(), DE_FILEMODE_OPEN|DE_FILEMODE_CREATE|DE_FILEMODE_TRUNCATE|DE_FILEMODE_WRITE);
698 deUint8 tmpBuf[1024*16];
701 deMemset(&tmpBuf, 'a', sizeof(tmpBuf));
703 while (numWritten < DATA_SIZE)
705 deInt64 numWrittenInBatch = 0;
706 XS_CHECK(deFile_write(file, &tmpBuf[0], de::min((int)sizeof(tmpBuf), DATA_SIZE-numWritten), &numWrittenInBatch) == DE_FILERESULT_SUCCESS);
707 numWritten += (int)numWrittenInBatch;
710 deFile_destroy(file);
714 class KeepAliveTest : public TestCase
717 KeepAliveTest (TestContext& testCtx)
718 : TestCase(testCtx, "keepalive")
722 void runClient (de::Socket& socket)
725 const int sendInterval = 5000;
726 const int minReceiveInterval = 10000;
727 const int testTime = 30000;
728 const int sleepTime = 200;
729 const int expectedTimeout = 40000;
731 int lastSendTime = 0;
732 int lastReceiveTime = 0;
735 DE_ASSERT(sendInterval < minReceiveInterval);
737 curTime = clock.getMilliseconds();
739 while (curTime < testTime)
741 bool tryGetKeepalive = false;
743 if (curTime-lastSendTime > sendInterval)
745 printf(" %d ms: sending keepalive\n", curTime);
746 sendMessage(socket, KeepAliveMessage());
747 curTime = clock.getMilliseconds();
748 lastSendTime = curTime;
749 tryGetKeepalive = true;
754 // Try to acquire keepalive.
755 printf(" %d ms: waiting for keepalive\n", curTime);
756 ScopedMsgPtr msg(readMessage(socket));
757 int recvTime = clock.getMilliseconds();
759 if (msg->type != MESSAGETYPE_KEEPALIVE)
760 XS_FAIL("Got invalid message");
762 printf(" %d ms: got keepalive\n", curTime);
764 if (recvTime-lastReceiveTime > minReceiveInterval)
765 XS_FAIL("Server doesn't send keepalives");
767 lastReceiveTime = recvTime;
771 curTime = clock.getMilliseconds();
774 // Verify that server actually kills the connection upon timeout.
775 sendMessage(socket, KeepAliveMessage());
776 printf(" waiting %d ms for keepalive timeout...\n", expectedTimeout);
777 bool isClosed = false;
783 curTime = clock.getMilliseconds();
785 while (curTime < expectedTimeout)
787 // Try to get keepalive message.
788 ScopedMsgPtr msg(readMessage(socket));
789 if (msg->type != MESSAGETYPE_KEEPALIVE)
790 XS_FAIL("Got invalid message");
792 curTime = clock.getMilliseconds();
793 printf(" %d ms: got keepalive\n", curTime);
796 catch (const SocketError& e)
798 if (e.getResult() == DE_SOCKETRESULT_CONNECTION_CLOSED)
800 printf(" %d ms: server closed connection", clock.getMilliseconds());
810 XS_FAIL("Server didn't close connection");
813 void runProgram (void) { /* nothing */ }
816 void printHelp (const char* binName)
818 printf("%s:\n", binName);
819 printf(" --client=[name] Run test [name]\n");
820 printf(" --program=[name] Run program for test [name]\n");
821 printf(" --host=[host] Connect to host [host]\n");
822 printf(" --port=[name] Use port [port]\n");
823 printf(" --tester-cmd=[cmd] Launch tester with [cmd]\n");
824 printf(" --server-cmd=[cmd] Launch server with [cmd]\n");
825 printf(" --start-server Start server for test execution\n");
828 struct CompareCaseName
832 CompareCaseName (const string& name_) : name(name_) {}
834 bool operator() (const TestCase* testCase) const
836 return name == testCase->getName();
840 void runExecServerTests (int argc, const char* const* argv)
842 // Construct test context.
845 testCtx.serverPath = "execserver";
846 testCtx.testerPath = argv[0];
847 testCtx.startServer = false;
848 testCtx.address.setHost("127.0.0.1");
849 testCtx.address.setPort(50016);
851 std::string runClient = "";
852 std::string runProgram = "";
854 // Parse command line.
855 for (int argNdx = 1; argNdx < argc; argNdx++)
857 const char* arg = argv[argNdx];
859 if (deStringBeginsWith(arg, "--client="))
861 else if (deStringBeginsWith(arg, "--program="))
863 else if (deStringBeginsWith(arg, "--port="))
864 testCtx.address.setPort(atoi(arg+7));
865 else if (deStringBeginsWith(arg, "--host="))
866 testCtx.address.setHost(arg+7);
867 else if (deStringBeginsWith(arg, "--server-cmd="))
868 testCtx.serverPath = arg+13;
869 else if (deStringBeginsWith(arg, "--tester-cmd="))
870 testCtx.testerPath = arg+13;
871 else if (deStringBeginsWith(arg, "--deqp-log-filename="))
872 testCtx.logFileName = arg+20;
873 else if (deStringBeginsWith(arg, "--deqp-caselist="))
874 testCtx.caseList = arg+16;
875 else if (deStringEqual(arg, "--deqp-stdin-caselist"))
877 // \todo [pyry] This is rather brute-force solution...
879 while (fread(&c, 1, 1, stdin) == 1 && c != 0)
880 testCtx.caseList += c;
882 else if (deStringEqual(arg, "--start-server"))
883 testCtx.startServer = true;
892 std::vector<TestCase*> testCases;
893 testCases.push_back(new ConnectTest(testCtx));
894 testCases.push_back(new HelloTest(testCtx));
895 testCases.push_back(new ExecFailTest(testCtx));
896 testCases.push_back(new SimpleExecTest(testCtx));
897 testCases.push_back(new InfoTest(testCtx));
898 testCases.push_back(new LogDataTest(testCtx));
899 testCases.push_back(new KeepAliveTest(testCtx));
900 testCases.push_back(new BigLogDataTest(testCtx));
904 if (!runClient.empty())
907 vector<TestCase*>::iterator casePos = std::find_if(testCases.begin(), testCases.end(), CompareCaseName(runClient));
908 XS_CHECK(casePos != testCases.end());
909 TestExecutor executor(testCtx);
910 executor.runCase(*casePos);
912 else if (!runProgram.empty())
915 vector<TestCase*>::iterator casePos = std::find_if(testCases.begin(), testCases.end(), CompareCaseName(runProgram));
916 XS_CHECK(casePos != testCases.end());
917 (*casePos)->runProgram();
918 fflush(stdout); // Make sure handles are flushed.
924 TestExecutor executor(testCtx);
925 executor.runCases(testCases);
928 catch (const std::exception& e)
930 printf("ERROR: %s\n", e.what());
934 for (std::vector<TestCase*>::const_iterator i = testCases.begin(); i != testCases.end(); i++)
941 void testProcFile (void)
944 if (deFileExists("test.txt"))
945 deDeleteFile("test.txt");
946 deFile* file = deFile_create("test.txt", DE_FILEMODE_CREATE|DE_FILEMODE_WRITE);
947 const char test[] = "Hello";
948 XS_CHECK(deFile_write(file, test, sizeof(test), DE_NULL) == DE_FILERESULT_SUCCESS);
949 deFile_destroy(file);
952 char buf[10] = { 0 };
953 file = deFile_create("test.txt", DE_FILEMODE_OPEN|DE_FILEMODE_READ);
954 XS_CHECK(deFile_read(file, buf, sizeof(test), DE_NULL) == DE_FILERESULT_SUCCESS);
955 printf("buf: %s\n", buf);
956 deFile_destroy(file);
959 deProcess* proc = deProcess_create("ls -lah /Users/pyry", DE_NULL);
960 deFile* out = deProcess_getStdOut(proc);
964 while (deFile_read(out, buf, sizeof(buf)-1, &numRead) == DE_FILERESULT_SUCCESS)
969 deProcess_destroy(proc);
974 void testBlockingFile (const char* filename)
977 int dataSize = 1024*1024;
978 deUint8* data = (deUint8*)deCalloc(dataSize);
981 deRandom_init(&rnd, 0);
983 if (deFileExists(filename))
984 DE_VERIFY(deDeleteFile(filename));
986 /* Fill in with random data. */
987 DE_ASSERT(dataSize % sizeof(int) == 0);
988 for (int ndx = 0; ndx < (int)(dataSize/sizeof(int)); ndx++)
989 ((deUint32*)data)[ndx] = deRandom_getUint32(&rnd);
991 /* Write with random-sized blocks. */
992 file = deFile_create(filename, DE_FILEMODE_CREATE|DE_FILEMODE_WRITE);
996 while (curPos < dataSize)
998 int blockSize = 1 + deRandom_getUint32(&rnd) % (dataSize-curPos);
999 deInt64 numWritten = 0;
1000 deFileResult result = deFile_write(file, &data[curPos], blockSize, &numWritten);
1002 DE_VERIFY(result == DE_FILERESULT_SUCCESS);
1003 DE_VERIFY(numWritten == blockSize);
1005 curPos += blockSize;
1008 deFile_destroy(file);
1010 /* Read and verify file. */
1011 file = deFile_create(filename, DE_FILEMODE_OPEN|DE_FILEMODE_READ);
1013 while (curPos < dataSize)
1015 deUint8 block[1024];
1016 int numToRead = 1 + deRandom_getUint32(&rnd) % deMin(dataSize-curPos, DE_LENGTH_OF_ARRAY(block));
1017 deInt64 numRead = 0;
1018 deFileResult result = deFile_read(file, block, numToRead, &numRead);
1020 DE_VERIFY(result == DE_FILERESULT_SUCCESS);
1021 DE_VERIFY((int)numRead == numToRead);
1022 DE_VERIFY(deMemCmp(block, &data[curPos], numToRead) == 0);
1024 curPos += numToRead;
1026 deFile_destroy(file);
1030 int main (int argc, const char* const* argv)
1032 xs::runExecServerTests(argc, argv);