increased chunk size
Fork of MTS-Socket by
Test/TestUDPSocket.h@35:c85bea9cb713, 2015-03-11 (annotated)
- Committer:
- Vanger
- Date:
- Wed Mar 11 17:27:16 2015 +0000
- Revision:
- 35:c85bea9cb713
- Parent:
- 16:dbe80ac199f5
Added HTTPS unit test.; Removed APN declarations, as there is only one static APN declaration now (consolidated the APN's to the same location for all tests)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfiore | 9:b2e3862705fc | 1 | #ifndef TESTUDPSOCKET_H |
mfiore | 9:b2e3862705fc | 2 | #define TESTUDPSOCKET_H |
mfiore | 9:b2e3862705fc | 3 | |
mfiore | 9:b2e3862705fc | 4 | #include "mtsas.h" |
mfiore | 9:b2e3862705fc | 5 | |
mfiore | 9:b2e3862705fc | 6 | using namespace mts; |
mfiore | 9:b2e3862705fc | 7 | |
mfiore | 9:b2e3862705fc | 8 | class TestUDPSocket : public TestCollection |
mfiore | 9:b2e3862705fc | 9 | { |
mfiore | 9:b2e3862705fc | 10 | public: |
mfiore | 9:b2e3862705fc | 11 | TestUDPSocket(); |
mfiore | 9:b2e3862705fc | 12 | virtual void run(); |
mfiore | 9:b2e3862705fc | 13 | |
mfiore | 9:b2e3862705fc | 14 | private: |
mfiore | 9:b2e3862705fc | 15 | MTSSerialFlowControl* io; |
mfiore | 9:b2e3862705fc | 16 | Cellular* radio; |
mfiore | 9:b2e3862705fc | 17 | UDPSocket* sock; |
mfiore | 9:b2e3862705fc | 18 | }; |
mfiore | 9:b2e3862705fc | 19 | |
Vanger | 14:7643ed024fe8 | 20 | //Must set test server and test port before running socket test |
mfiore | 9:b2e3862705fc | 21 | const char UDP_TEST_SERVER[] = ""; |
mfiore | 9:b2e3862705fc | 22 | const int UDP_TEST_PORT = -1; |
mfiore | 9:b2e3862705fc | 23 | |
Vanger | 14:7643ed024fe8 | 24 | //Test pattern can be changed to any alphanumeric pattern wanted |
mfiore | 9:b2e3862705fc | 25 | const char TEST_PATTERN[] = "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|\r\n" |
mfiore | 9:b2e3862705fc | 26 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}/\r\n" |
mfiore | 9:b2e3862705fc | 27 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}-\r\n" |
mfiore | 9:b2e3862705fc | 28 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}\\\r\n" |
mfiore | 9:b2e3862705fc | 29 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|\r\n" |
mfiore | 9:b2e3862705fc | 30 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}/\r\n" |
mfiore | 9:b2e3862705fc | 31 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}-\r\n" |
mfiore | 9:b2e3862705fc | 32 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}\\\r\n" |
mfiore | 9:b2e3862705fc | 33 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}*\r\n"; |
mfiore | 9:b2e3862705fc | 34 | |
mfiore | 9:b2e3862705fc | 35 | TestUDPSocket::TestUDPSocket() : TestCollection("TestUDPSocket") {} |
mfiore | 9:b2e3862705fc | 36 | |
mfiore | 9:b2e3862705fc | 37 | void TestUDPSocket::run() { |
mfiore | 10:42220b7df921 | 38 | |
mfiore | 9:b2e3862705fc | 39 | Endpoint remote; |
mfiore | 9:b2e3862705fc | 40 | remote.set_address(UDP_TEST_SERVER, UDP_TEST_PORT); |
mfiore | 9:b2e3862705fc | 41 | |
mfiore | 9:b2e3862705fc | 42 | MTSLog::setLogLevel(MTSLog::TRACE_LEVEL); |
mfiore | 9:b2e3862705fc | 43 | Test::start("Setup"); |
mfiore | 9:b2e3862705fc | 44 | io = new MTSSerialFlowControl(D8, D2, D3, D6); |
mfiore | 9:b2e3862705fc | 45 | io->baud(115200); |
mfiore | 9:b2e3862705fc | 46 | radio = CellularFactory::create(io); |
mfiore | 9:b2e3862705fc | 47 | if (! radio) { |
mfiore | 9:b2e3862705fc | 48 | Test::assertTrue(false); |
mfiore | 9:b2e3862705fc | 49 | } |
mfiore | 9:b2e3862705fc | 50 | radio->configureSignals(D4, D7, RESET); |
mfiore | 9:b2e3862705fc | 51 | Transport::setTransport(radio); |
mfiore | 9:b2e3862705fc | 52 | |
mfiore | 9:b2e3862705fc | 53 | for (int i = 0; i < 10; i++) { |
mfiore | 9:b2e3862705fc | 54 | if (i >= 10) { |
mfiore | 9:b2e3862705fc | 55 | Test::assertTrue(false); |
mfiore | 9:b2e3862705fc | 56 | } |
mfiore | 12:9efe3cc3cb6c | 57 | if (radio->setApn(APN) == MTS_SUCCESS) { |
mfiore | 9:b2e3862705fc | 58 | break; |
mfiore | 9:b2e3862705fc | 59 | } else { |
mfiore | 9:b2e3862705fc | 60 | wait(1); |
mfiore | 9:b2e3862705fc | 61 | } |
mfiore | 9:b2e3862705fc | 62 | } |
Vanger | 14:7643ed024fe8 | 63 | |
mfiore | 9:b2e3862705fc | 64 | for (int i = 0; i < 3; i++) { |
mfiore | 9:b2e3862705fc | 65 | if (i >= 3) { |
mfiore | 9:b2e3862705fc | 66 | Test::assertTrue(false); |
mfiore | 9:b2e3862705fc | 67 | } |
mfiore | 9:b2e3862705fc | 68 | if (radio->connect()) { |
mfiore | 9:b2e3862705fc | 69 | break; |
mfiore | 9:b2e3862705fc | 70 | } else { |
mfiore | 9:b2e3862705fc | 71 | wait(1); |
mfiore | 9:b2e3862705fc | 72 | } |
mfiore | 9:b2e3862705fc | 73 | } |
mfiore | 9:b2e3862705fc | 74 | |
mfiore | 9:b2e3862705fc | 75 | for (int i = 0; i < 5; i++) { |
mfiore | 9:b2e3862705fc | 76 | if (i >= 5) { |
mfiore | 9:b2e3862705fc | 77 | Test::assertTrue(false); |
mfiore | 9:b2e3862705fc | 78 | } |
mfiore | 9:b2e3862705fc | 79 | if (radio->ping()) { |
mfiore | 9:b2e3862705fc | 80 | break; |
mfiore | 9:b2e3862705fc | 81 | } else { |
mfiore | 9:b2e3862705fc | 82 | wait(1); |
mfiore | 9:b2e3862705fc | 83 | } |
mfiore | 9:b2e3862705fc | 84 | } |
mfiore | 9:b2e3862705fc | 85 | |
mfiore | 9:b2e3862705fc | 86 | sock = new UDPSocket(); |
Vanger | 14:7643ed024fe8 | 87 | sock->set_blocking(false, 20000); |
mfiore | 9:b2e3862705fc | 88 | Test::end(); |
mfiore | 9:b2e3862705fc | 89 | |
Vanger | 14:7643ed024fe8 | 90 | /** set up an UDP echo server using netcat |
mfiore | 9:b2e3862705fc | 91 | * nc -e /bin/cat -l -u -p TEST_PORT |
Vanger | 14:7643ed024fe8 | 92 | * this should echo back anything you send to it. |
Vanger | 14:7643ed024fe8 | 93 | * Note: Listen port might close after each UDP packet |
Vanger | 14:7643ed024fe8 | 94 | * if testing UIP-type radios. |
mfiore | 9:b2e3862705fc | 95 | */ |
mfiore | 9:b2e3862705fc | 96 | |
mfiore | 9:b2e3862705fc | 97 | int bufsize = 1024; |
mfiore | 9:b2e3862705fc | 98 | char buf[bufsize]; |
mfiore | 9:b2e3862705fc | 99 | int size = 0; |
mfiore | 9:b2e3862705fc | 100 | |
mfiore | 9:b2e3862705fc | 101 | for (int i = 0; i < 10; i++) { |
mfiore | 9:b2e3862705fc | 102 | Test::start("Test UDP"); |
mfiore | 9:b2e3862705fc | 103 | logInfo("sending to remote"); |
mfiore | 9:b2e3862705fc | 104 | size = sock->sendTo(remote, (char*) TEST_PATTERN, sizeof(TEST_PATTERN)); |
mfiore | 9:b2e3862705fc | 105 | if (size != sizeof(TEST_PATTERN)) { |
mfiore | 9:b2e3862705fc | 106 | logError("failed to send - only sent %d bytes", size); |
mfiore | 9:b2e3862705fc | 107 | Test::assertTrue(false); |
mfiore | 9:b2e3862705fc | 108 | } |
mfiore | 9:b2e3862705fc | 109 | logInfo("receiving from remote"); |
mfiore | 9:b2e3862705fc | 110 | size = sock->receiveFrom(remote, buf, bufsize); |
mfiore | 9:b2e3862705fc | 111 | if (size != sizeof(TEST_PATTERN)) { |
mfiore | 9:b2e3862705fc | 112 | logError("failed to receive - only received %d bytes", size); |
mfiore | 9:b2e3862705fc | 113 | Test::assertTrue(false); |
mfiore | 9:b2e3862705fc | 114 | } else { |
mfiore | 9:b2e3862705fc | 115 | for (int j = 0; j < sizeof(TEST_PATTERN); j++) { |
mfiore | 9:b2e3862705fc | 116 | if (buf[j] != TEST_PATTERN[j]) { |
mfiore | 9:b2e3862705fc | 117 | logError("patterns don't match at %d, pattern [%#02X] received [%#02X]", j, TEST_PATTERN[j], buf[j]); |
mfiore | 9:b2e3862705fc | 118 | Test::assertTrue(false); |
mfiore | 9:b2e3862705fc | 119 | } |
mfiore | 9:b2e3862705fc | 120 | } |
mfiore | 9:b2e3862705fc | 121 | } |
mfiore | 9:b2e3862705fc | 122 | Test::end(); |
mfiore | 9:b2e3862705fc | 123 | } |
Vanger | 14:7643ed024fe8 | 124 | sock->close(); |
Vanger | 14:7643ed024fe8 | 125 | radio->disconnect(); |
mfiore | 9:b2e3862705fc | 126 | } |
mfiore | 9:b2e3862705fc | 127 | |
mfiore | 9:b2e3862705fc | 128 | #endif |