For bug fixes
Fork of MTS-Socket by
Test/TestTCPSocketConnection.h@8:a3b41ec82e63, 2014-06-16 (annotated)
- Committer:
- mfiore
- Date:
- Mon Jun 16 14:05:19 2014 +0000
- Revision:
- 8:a3b41ec82e63
- Parent:
- 7:08b474178245
- Child:
- 9:b2e3862705fc
update receive() in TCPSocketConnection; update and improve TCP and HTTP unit tests
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfiore | 7:08b474178245 | 1 | #ifndef TESTTCPSOCKETCONNECTION_H |
mfiore | 7:08b474178245 | 2 | #define TESTTCPSOCKETCONNECTION_H |
mfiore | 7:08b474178245 | 3 | |
mfiore | 7:08b474178245 | 4 | #include "mtsas.h" |
mfiore | 7:08b474178245 | 5 | #include <string> |
mfiore | 7:08b474178245 | 6 | |
mfiore | 7:08b474178245 | 7 | using namespace mts; |
mfiore | 7:08b474178245 | 8 | |
mfiore | 7:08b474178245 | 9 | class TestTCPSocketConnection : public TestCollection |
mfiore | 7:08b474178245 | 10 | { |
mfiore | 7:08b474178245 | 11 | public: |
mfiore | 7:08b474178245 | 12 | TestTCPSocketConnection(); |
mfiore | 7:08b474178245 | 13 | virtual void run(); |
mfiore | 8:a3b41ec82e63 | 14 | |
mfiore | 7:08b474178245 | 15 | private: |
mfiore | 8:a3b41ec82e63 | 16 | bool runIteration(); |
mfiore | 7:08b474178245 | 17 | MTSSerialFlowControl* io; |
mfiore | 7:08b474178245 | 18 | Cellular* radio; |
mfiore | 7:08b474178245 | 19 | TCPSocketConnection* sock; |
mfiore | 7:08b474178245 | 20 | }; |
mfiore | 7:08b474178245 | 21 | |
mfiore | 8:a3b41ec82e63 | 22 | const char PATTERN_LINE1[] = "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|"; |
mfiore | 8:a3b41ec82e63 | 23 | const char PATTERN[] = "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|\r\n" |
mfiore | 8:a3b41ec82e63 | 24 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}/\r\n" |
mfiore | 8:a3b41ec82e63 | 25 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}-\r\n" |
mfiore | 8:a3b41ec82e63 | 26 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}\\\r\n" |
mfiore | 8:a3b41ec82e63 | 27 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|\r\n" |
mfiore | 8:a3b41ec82e63 | 28 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}/\r\n" |
mfiore | 8:a3b41ec82e63 | 29 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}-\r\n" |
mfiore | 8:a3b41ec82e63 | 30 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}\\\r\n" |
mfiore | 8:a3b41ec82e63 | 31 | "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}*\r\n"; |
mfiore | 7:08b474178245 | 32 | |
mfiore | 8:a3b41ec82e63 | 33 | const char MENU_LINE1[] = "send ascii pattern until keypress"; |
mfiore | 7:08b474178245 | 34 | const char MENU[] = "1 send ascii pattern until keypress" |
mfiore | 7:08b474178245 | 35 | "2 send ascii pattern (numbered)" |
mfiore | 7:08b474178245 | 36 | "3 send pattern and close socket" |
mfiore | 7:08b474178245 | 37 | "4 send [ETX] and wait for keypress" |
mfiore | 7:08b474178245 | 38 | "5 send [DLE] and wait for keypress" |
mfiore | 7:08b474178245 | 39 | "6 send all hex values (00-FF)" |
mfiore | 7:08b474178245 | 40 | "q quit" |
mfiore | 7:08b474178245 | 41 | ">:"; |
mfiore | 7:08b474178245 | 42 | |
mfiore | 7:08b474178245 | 43 | const char TEST_SERVER[] = "204.26.122.5"; |
mfiore | 7:08b474178245 | 44 | const int TEST_PORT = 7000; |
mfiore | 7:08b474178245 | 45 | |
mfiore | 7:08b474178245 | 46 | TestTCPSocketConnection::TestTCPSocketConnection() : TestCollection("TestTCPSocketConnection") {} |
mfiore | 7:08b474178245 | 47 | |
mfiore | 8:a3b41ec82e63 | 48 | void TestTCPSocketConnection::run() |
mfiore | 8:a3b41ec82e63 | 49 | { |
mfiore | 7:08b474178245 | 50 | MTSLog::setLogLevel(MTSLog::TRACE_LEVEL); |
mfiore | 7:08b474178245 | 51 | Test::start("Setup"); |
mfiore | 8:a3b41ec82e63 | 52 | io = new MTSSerialFlowControl(D8, D2, D3, D6); |
mfiore | 7:08b474178245 | 53 | io->baud(115200); |
mfiore | 7:08b474178245 | 54 | radio = CellularFactory::create(io); |
mfiore | 7:08b474178245 | 55 | if (! radio) { |
mfiore | 7:08b474178245 | 56 | Test::assertTrue(false); |
mfiore | 7:08b474178245 | 57 | } |
mfiore | 8:a3b41ec82e63 | 58 | radio->configureSignals(D4, D7, RESET); |
mfiore | 7:08b474178245 | 59 | Transport::setTransport(radio); |
mfiore | 8:a3b41ec82e63 | 60 | |
mfiore | 7:08b474178245 | 61 | for (int i = 0; i < 10; i++) { |
mfiore | 7:08b474178245 | 62 | if (i >= 10) { |
mfiore | 7:08b474178245 | 63 | Test::assertTrue(false); |
mfiore | 7:08b474178245 | 64 | } |
mfiore | 7:08b474178245 | 65 | if (radio->setApn("wap.cingular") == SUCCESS) { |
mfiore | 7:08b474178245 | 66 | break; |
mfiore | 7:08b474178245 | 67 | } else { |
mfiore | 7:08b474178245 | 68 | wait(1); |
mfiore | 7:08b474178245 | 69 | } |
mfiore | 7:08b474178245 | 70 | } |
mfiore | 7:08b474178245 | 71 | for (int i = 0; i < 3; i++) { |
mfiore | 7:08b474178245 | 72 | if (i >= 3) { |
mfiore | 7:08b474178245 | 73 | Test::assertTrue(false); |
mfiore | 7:08b474178245 | 74 | } |
mfiore | 7:08b474178245 | 75 | if (radio->connect()) { |
mfiore | 7:08b474178245 | 76 | break; |
mfiore | 7:08b474178245 | 77 | } else { |
mfiore | 7:08b474178245 | 78 | wait(1); |
mfiore | 7:08b474178245 | 79 | } |
mfiore | 7:08b474178245 | 80 | } |
mfiore | 8:a3b41ec82e63 | 81 | |
mfiore | 7:08b474178245 | 82 | for (int i = 0; i < 5; i++) { |
mfiore | 7:08b474178245 | 83 | if (i >= 5) { |
mfiore | 7:08b474178245 | 84 | Test::assertTrue(false); |
mfiore | 7:08b474178245 | 85 | } |
mfiore | 7:08b474178245 | 86 | if (radio->ping()) { |
mfiore | 7:08b474178245 | 87 | break; |
mfiore | 7:08b474178245 | 88 | } else { |
mfiore | 7:08b474178245 | 89 | wait(1); |
mfiore | 7:08b474178245 | 90 | } |
mfiore | 7:08b474178245 | 91 | } |
mfiore | 8:a3b41ec82e63 | 92 | |
mfiore | 7:08b474178245 | 93 | sock = new TCPSocketConnection(); |
mfiore | 8:a3b41ec82e63 | 94 | sock->set_blocking(false, 2); |
mfiore | 7:08b474178245 | 95 | Test::end(); |
mfiore | 8:a3b41ec82e63 | 96 | |
mfiore | 8:a3b41ec82e63 | 97 | for (int i = 0; i < 10; i++) { |
mfiore | 8:a3b41ec82e63 | 98 | Test::start("Test TCP"); |
mfiore | 8:a3b41ec82e63 | 99 | Test::assertTrue(runIteration()); |
mfiore | 8:a3b41ec82e63 | 100 | Test::end(); |
mfiore | 7:08b474178245 | 101 | } |
mfiore | 7:08b474178245 | 102 | } |
mfiore | 7:08b474178245 | 103 | |
mfiore | 8:a3b41ec82e63 | 104 | bool TestTCPSocketConnection::runIteration() |
mfiore | 8:a3b41ec82e63 | 105 | { |
mfiore | 7:08b474178245 | 106 | Timer tmr; |
mfiore | 7:08b474178245 | 107 | int bytesRead = 0; |
mfiore | 7:08b474178245 | 108 | const int readSize = 1024; |
mfiore | 7:08b474178245 | 109 | char buffer[readSize] = {0}; |
mfiore | 7:08b474178245 | 110 | string result; |
mfiore | 8:a3b41ec82e63 | 111 | |
mfiore | 7:08b474178245 | 112 | for (int i = 0; i < 5; i++) { |
mfiore | 7:08b474178245 | 113 | if (i >= 5) { |
mfiore | 7:08b474178245 | 114 | return false; |
mfiore | 7:08b474178245 | 115 | } |
mfiore | 7:08b474178245 | 116 | if (! sock->connect(TEST_SERVER, TEST_PORT)) { |
mfiore | 7:08b474178245 | 117 | break; |
mfiore | 7:08b474178245 | 118 | } else { |
mfiore | 7:08b474178245 | 119 | wait(1); |
mfiore | 7:08b474178245 | 120 | } |
mfiore | 7:08b474178245 | 121 | } |
mfiore | 8:a3b41ec82e63 | 122 | |
mfiore | 8:a3b41ec82e63 | 123 | logInfo("Receiving Menu"); |
mfiore | 8:a3b41ec82e63 | 124 | tmr.reset(); |
mfiore | 8:a3b41ec82e63 | 125 | tmr.start(); |
mfiore | 8:a3b41ec82e63 | 126 | do { |
mfiore | 8:a3b41ec82e63 | 127 | bytesRead = sock->receive(buffer, readSize); |
mfiore | 8:a3b41ec82e63 | 128 | if (bytesRead > 0) { |
mfiore | 8:a3b41ec82e63 | 129 | result.append(buffer, bytesRead); |
mfiore | 8:a3b41ec82e63 | 130 | } |
mfiore | 8:a3b41ec82e63 | 131 | logInfo("Total Bytes Read: %d", result.size()); |
mfiore | 8:a3b41ec82e63 | 132 | } while(tmr.read() <= 5); |
mfiore | 7:08b474178245 | 133 | |
mfiore | 8:a3b41ec82e63 | 134 | wait(5); |
mfiore | 8:a3b41ec82e63 | 135 | |
mfiore | 8:a3b41ec82e63 | 136 | logInfo("Received: [%d] [%s]", result.size(), result.c_str()); |
mfiore | 8:a3b41ec82e63 | 137 | |
mfiore | 8:a3b41ec82e63 | 138 | size_t pos = result.find(MENU_LINE1); |
mfiore | 8:a3b41ec82e63 | 139 | if(pos != string::npos) { |
mfiore | 8:a3b41ec82e63 | 140 | logInfo("Found Menu 1st Line"); |
mfiore | 8:a3b41ec82e63 | 141 | } else { |
mfiore | 8:a3b41ec82e63 | 142 | logError("Failed To Find Menu 1st Line"); |
mfiore | 8:a3b41ec82e63 | 143 | sock->close(); |
mfiore | 8:a3b41ec82e63 | 144 | return false; |
mfiore | 8:a3b41ec82e63 | 145 | } |
mfiore | 8:a3b41ec82e63 | 146 | |
mfiore | 8:a3b41ec82e63 | 147 | result.clear(); |
mfiore | 8:a3b41ec82e63 | 148 | |
mfiore | 8:a3b41ec82e63 | 149 | logInfo("Writing To Socket: 2"); |
mfiore | 8:a3b41ec82e63 | 150 | if(sock->send("2\r\n", 3) == 3) { |
mfiore | 8:a3b41ec82e63 | 151 | logInfo("Successfully Wrote '2'"); |
mfiore | 8:a3b41ec82e63 | 152 | } else { |
mfiore | 8:a3b41ec82e63 | 153 | logError("Failed To Write '2'"); |
mfiore | 8:a3b41ec82e63 | 154 | sock->close(); |
mfiore | 8:a3b41ec82e63 | 155 | return false; |
mfiore | 8:a3b41ec82e63 | 156 | } |
mfiore | 8:a3b41ec82e63 | 157 | logInfo("Expecting 'how many ? >:'"); |
mfiore | 8:a3b41ec82e63 | 158 | tmr.reset(); |
mfiore | 8:a3b41ec82e63 | 159 | tmr.start(); |
mfiore | 8:a3b41ec82e63 | 160 | do { |
mfiore | 8:a3b41ec82e63 | 161 | bytesRead = sock->receive(buffer, readSize); |
mfiore | 8:a3b41ec82e63 | 162 | if (bytesRead > 0) { |
mfiore | 8:a3b41ec82e63 | 163 | result.append(buffer, bytesRead); |
mfiore | 8:a3b41ec82e63 | 164 | } |
mfiore | 8:a3b41ec82e63 | 165 | logInfo("Total Bytes Read: %d", result.size()); |
mfiore | 8:a3b41ec82e63 | 166 | } while(tmr.read() <= 3); |
mfiore | 8:a3b41ec82e63 | 167 | |
mfiore | 8:a3b41ec82e63 | 168 | logInfo("Received: [%d] [%s]", result.size(), result.c_str()); |
mfiore | 8:a3b41ec82e63 | 169 | |
mfiore | 8:a3b41ec82e63 | 170 | if(result.find("how many") != std::string::npos) { |
mfiore | 8:a3b41ec82e63 | 171 | logInfo("Successfully Found 'how many'"); |
mfiore | 8:a3b41ec82e63 | 172 | logInfo("Writing To Socket: 2"); |
mfiore | 8:a3b41ec82e63 | 173 | if(sock->send("2\r\n", 3) == 3) { |
mfiore | 8:a3b41ec82e63 | 174 | logInfo("Successfully wrote '2'"); |
mfiore | 8:a3b41ec82e63 | 175 | } else { |
mfiore | 8:a3b41ec82e63 | 176 | logError("Failed to write '2'"); |
mfiore | 8:a3b41ec82e63 | 177 | sock->close(); |
mfiore | 8:a3b41ec82e63 | 178 | return false; |
mfiore | 8:a3b41ec82e63 | 179 | } |
mfiore | 8:a3b41ec82e63 | 180 | } else { |
mfiore | 8:a3b41ec82e63 | 181 | logError("didn't receive 'how many'"); |
mfiore | 8:a3b41ec82e63 | 182 | sock->close(); |
mfiore | 8:a3b41ec82e63 | 183 | return false; |
mfiore | 8:a3b41ec82e63 | 184 | } |
mfiore | 8:a3b41ec82e63 | 185 | |
mfiore | 8:a3b41ec82e63 | 186 | result.clear(); |
mfiore | 8:a3b41ec82e63 | 187 | |
mfiore | 7:08b474178245 | 188 | logInfo("Receiving Data"); |
mfiore | 7:08b474178245 | 189 | tmr.reset(); |
mfiore | 7:08b474178245 | 190 | tmr.start(); |
mfiore | 7:08b474178245 | 191 | do { |
mfiore | 7:08b474178245 | 192 | bytesRead = sock->receive(buffer, readSize); |
mfiore | 7:08b474178245 | 193 | if (bytesRead > 0) { |
mfiore | 7:08b474178245 | 194 | result.append(buffer, bytesRead); |
mfiore | 7:08b474178245 | 195 | } |
mfiore | 7:08b474178245 | 196 | logInfo("Total Bytes Read: %d", result.size()); |
mfiore | 8:a3b41ec82e63 | 197 | } while(tmr.read() <= 8); |
mfiore | 8:a3b41ec82e63 | 198 | |
mfiore | 7:08b474178245 | 199 | logInfo("Received Data: [%d] [%s]", result.size(), result.c_str()); |
mfiore | 8:a3b41ec82e63 | 200 | |
mfiore | 7:08b474178245 | 201 | pos = result.find(PATTERN_LINE1); |
mfiore | 7:08b474178245 | 202 | if(pos != string::npos) { |
mfiore | 7:08b474178245 | 203 | int patternSize = sizeof(PATTERN) - 1; |
mfiore | 7:08b474178245 | 204 | const char* ptr = &result.data()[pos]; |
mfiore | 7:08b474178245 | 205 | bool match = true; |
mfiore | 7:08b474178245 | 206 | for(int i = 0; i < patternSize; i++) { |
mfiore | 7:08b474178245 | 207 | if(PATTERN[i] != ptr[i]) { |
mfiore | 7:08b474178245 | 208 | logError("1st Pattern Doesn't Match At [%d]", i); |
mfiore | 7:08b474178245 | 209 | logError("Pattern [%02X] Buffer [%02X]", PATTERN[i], ptr[i]); |
mfiore | 7:08b474178245 | 210 | match = false; |
mfiore | 8:a3b41ec82e63 | 211 | break; |
mfiore | 7:08b474178245 | 212 | } |
mfiore | 7:08b474178245 | 213 | } |
mfiore | 7:08b474178245 | 214 | if(match) { |
mfiore | 8:a3b41ec82e63 | 215 | logInfo("Found 1st Pattern"); |
mfiore | 8:a3b41ec82e63 | 216 | } else { |
mfiore | 8:a3b41ec82e63 | 217 | logError("Failed To Find 1st Pattern"); |
mfiore | 8:a3b41ec82e63 | 218 | sock->close(); |
mfiore | 8:a3b41ec82e63 | 219 | return false; |
mfiore | 7:08b474178245 | 220 | } |
mfiore | 8:a3b41ec82e63 | 221 | |
mfiore | 7:08b474178245 | 222 | pos = result.find(PATTERN_LINE1, pos + patternSize); |
mfiore | 7:08b474178245 | 223 | if(pos != std::string::npos) { |
mfiore | 7:08b474178245 | 224 | ptr = &result.data()[pos]; |
mfiore | 7:08b474178245 | 225 | match = true; |
mfiore | 7:08b474178245 | 226 | for(int i = 0; i < patternSize; i++) { |
mfiore | 7:08b474178245 | 227 | if(PATTERN[i] != ptr[i]) { |
mfiore | 7:08b474178245 | 228 | logError("2nd Pattern Doesn't Match At [%d]", i); |
mfiore | 7:08b474178245 | 229 | logError("Pattern [%02X] Buffer [%02X]", PATTERN[i], ptr[i]); |
mfiore | 7:08b474178245 | 230 | match = false; |
mfiore | 8:a3b41ec82e63 | 231 | break; |
mfiore | 7:08b474178245 | 232 | } |
mfiore | 7:08b474178245 | 233 | } |
mfiore | 7:08b474178245 | 234 | if(match) { |
mfiore | 8:a3b41ec82e63 | 235 | logInfo("Found 2nd Pattern"); |
mfiore | 8:a3b41ec82e63 | 236 | } else { |
mfiore | 8:a3b41ec82e63 | 237 | logError("Failed To Find 2nd Pattern"); |
mfiore | 8:a3b41ec82e63 | 238 | sock->close(); |
mfiore | 8:a3b41ec82e63 | 239 | return false; |
mfiore | 7:08b474178245 | 240 | } |
mfiore | 7:08b474178245 | 241 | } |
mfiore | 8:a3b41ec82e63 | 242 | } else { |
mfiore | 8:a3b41ec82e63 | 243 | logError("Failed To Find Pattern 1st Line"); |
mfiore | 8:a3b41ec82e63 | 244 | sock->close(); |
mfiore | 8:a3b41ec82e63 | 245 | return false; |
mfiore | 7:08b474178245 | 246 | } |
mfiore | 8:a3b41ec82e63 | 247 | |
mfiore | 7:08b474178245 | 248 | result.clear(); |
mfiore | 7:08b474178245 | 249 | sock->close(); |
mfiore | 7:08b474178245 | 250 | return true; |
mfiore | 7:08b474178245 | 251 | } |
mfiore | 7:08b474178245 | 252 | |
mfiore | 7:08b474178245 | 253 | #endif |