Added safety checks requested in code review.
[platform/upstream/iotivity.git] / resource / csdk / ocsocket / test / arduino / ocsocket_test.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include <SPI.h>
22 #include <Ethernet.h>
23 #include <ocsocket.h>
24 #include <logger.h>
25
26 //Mac address of my ethernet shield
27 static uint8_t ETHERNET_MAC[] = {0x90, 0xA2, 0xDA, 0x0F, 0x2B, 0x72 };
28 //IP address of the peer whom I wish to send some data
29 static uint8_t PEER_IP_ADDR[] = {192, 168, 1, 125};
30 static uint8_t MULTICAST_IP_ADDR[] = {224, 0, 1, 187};
31
32 //Set below to 0 to disable multicast testing
33 //#define MCAST_TESTING_EN 1
34
35 #define TEST_NUM_PKTS (20)
36 #define TEST_PORT_NUM (8888)
37 #define MAX_BUF_SIZE  (256)
38
39 unsigned char TEST_BUF[] = {
40   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
41   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
42   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
43   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
44   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
45   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
46   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
47   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
48   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
49   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
50   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
51   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
52   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
53   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
54   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
55   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
56   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
57   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
58   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
59   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
60   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
61   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
62   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
63   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
64   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
65   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
66   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
67   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
68   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
69   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
70   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
71   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
72   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
73   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
74   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
75   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
76   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
77   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
78   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
79   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
80 };
81 unsigned int TEST_BUF_LEN = sizeof(TEST_BUF);
82
83 #define PCF(str) ((PROGMEM const char *)(F(str)))
84
85 #define MOD_NAME PCF("ocsocket_test")
86
87 #define VERIFY_SUCCESS(op, res) { if (op == res) {OC_LOG(DEBUG, MOD_NAME, PCF(#op " SUCCEEDED"));} \
88      else {OC_LOG(FATAL, MOD_NAME, PCF(#op "!!!!  FAILED FAILED FAILED !!!!"));} }
89
90 //OCBuildIPv4Address tests
91 void test20() {
92     OCDevAddr ipAddr;
93     VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM, &ipAddr), ERR_SUCCESS);
94     VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM, NULL), ERR_INVALID_INPUT);
95     OC_LOG(DEBUG, MOD_NAME, PCF("test20 - Completed"));
96 }
97
98
99 void test30() {
100
101     OCDevAddr ipAddr;
102     uint8_t a, b, c, d;
103     uint16_t port;
104     VERIFY_SUCCESS(OCBuildIPv4Address( 1, 2, 3, 4, TEST_PORT_NUM, &ipAddr), ERR_SUCCESS);
105     VERIFY_SUCCESS(OCDevAddrToIPv4Addr( &ipAddr, &a, &b, &c, &d), ERR_SUCCESS);
106     if ((a != 1) || (b != 2) || (c !=3) || (d !=4)) {
107         OC_LOG(DEBUG, MOD_NAME, PCF("test30 - Failed !!!"));
108         return;
109     }
110
111     VERIFY_SUCCESS(OCDevAddrToPort( &ipAddr, &port), ERR_SUCCESS);
112     if (port != TEST_PORT_NUM) {
113         OC_LOG(DEBUG, MOD_NAME, PCF("test30 - Failed !!!"));
114         return;
115     }
116     VERIFY_SUCCESS(OCDevAddrToIPv4Addr( NULL, &a, &b, &c, &d), ERR_INVALID_INPUT);
117     VERIFY_SUCCESS(OCDevAddrToPort( &ipAddr, NULL), ERR_INVALID_INPUT);
118     OC_LOG(DEBUG, MOD_NAME, PCF("test30 - Completed"));
119 }
120
121 //OCInitUDP tests
122 void test40() {
123     OCDevAddr ipAddr;
124     int32_t sfd1, sfd2, sfd3, sfd4, sfd5;
125
126     //We should be able to successfully open 4 sockets.
127     VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM, &ipAddr), ERR_SUCCESS);
128     VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd1), ERR_SUCCESS);
129
130     VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM + 1, &ipAddr), ERR_SUCCESS);
131     VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd2), ERR_SUCCESS);
132
133     VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM + 2, &ipAddr), ERR_SUCCESS);
134     VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd3), ERR_SUCCESS);
135
136     VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM + 3, &ipAddr), ERR_SUCCESS);
137     VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd4), ERR_SUCCESS);
138
139     //5th socket creation call should FAIL
140     VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM + 4, &ipAddr), ERR_SUCCESS);
141     VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd5), ERR_UNKNOWN);
142
143     VERIFY_SUCCESS(OCClose( sfd4), ERR_SUCCESS);
144     VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd5), ERR_SUCCESS);
145
146     VERIFY_SUCCESS(OCClose( sfd1), ERR_SUCCESS);
147     VERIFY_SUCCESS(OCClose( sfd2), ERR_SUCCESS);
148     VERIFY_SUCCESS(OCClose( sfd3), ERR_SUCCESS);
149     VERIFY_SUCCESS(OCClose( sfd5), ERR_SUCCESS);
150     OC_LOG(DEBUG, MOD_NAME, PCF("test40 - Completed"));
151 }
152
153 //OCSendTo -- Unicast tests
154 void test60() {
155     OCDevAddr ipAddr, peerAddr;
156     int32_t sfd;
157
158     VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM, &ipAddr), ERR_SUCCESS);
159     VERIFY_SUCCESS(OCBuildIPv4Address(PEER_IP_ADDR[0], PEER_IP_ADDR[0],
160                 PEER_IP_ADDR[0], PEER_IP_ADDR[0], TEST_PORT_NUM, &peerAddr), ERR_SUCCESS);
161
162     VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd), ERR_SUCCESS);
163     for (int i = 2; i < 300; ) {
164         VERIFY_SUCCESS(OCSendTo(sfd, TEST_BUF, i, 0, &peerAddr), i);
165         i = i + 12;
166
167         delay(100);
168     }
169     VERIFY_SUCCESS(OCClose( sfd), ERR_SUCCESS);
170     OC_LOG(DEBUG, MOD_NAME, PCF("test60 - Completed"));
171 }
172
173 //End to End - Unicast Send/Recv test
174 //Start sample app 'sendrecv' on different Linux box.
175 void test80() {
176     OCDevAddr ipAddr, peerAddr;
177     int32_t sfd;
178     int32_t recvLen;
179     uint8_t buf[MAX_BUF_SIZE];
180     uint8_t pktrecv = 0;
181
182     //Unicast
183     VERIFY_SUCCESS(OCBuildIPv4Address( 0, 0, 0, 0, TEST_PORT_NUM, &ipAddr), ERR_SUCCESS);
184     VERIFY_SUCCESS(OCInitUDP( &ipAddr, &sfd), ERR_SUCCESS);
185
186     do {
187         recvLen = OCRecvFrom(sfd, buf, MAX_BUF_SIZE, 0, &peerAddr);
188         if (recvLen > 0) {
189             pktrecv++;
190             OC_LOG(DEBUG, MOD_NAME, PCF("Rcvd data from :"));
191             OC_LOG_BUFFER(INFO, MOD_NAME, peerAddr.addr, peerAddr.size);
192             OC_LOG(DEBUG, MOD_NAME, PCF("Data Length :"));
193             OC_LOG_BUFFER(INFO, MOD_NAME, (uint8_t*)&recvLen, sizeof(recvLen));
194
195             VERIFY_SUCCESS(OCSendTo(sfd, buf, recvLen, 0, &peerAddr), recvLen);
196         } else {
197             OC_LOG(DEBUG, MOD_NAME, PCF("No data received"));
198         }
199         delay(500); //delay  secs
200     } while (pktrecv < TEST_NUM_PKTS);
201
202     VERIFY_SUCCESS(OCClose(sfd), ERR_SUCCESS);
203     OC_LOG(DEBUG, MOD_NAME, PCF("test80 - Completed"));
204 }
205
206
207 //End to End - Multicast Send/Recv test
208 //Start sample app 'sendrecv' on different Linux box.
209 void test90() {
210     OCDevAddr ipAddrMcast, peerAddr;
211     int32_t sfd;
212     int32_t recvLen;
213     uint8_t buf[MAX_BUF_SIZE];
214     uint8_t pktrecv = 0;
215
216     //Multicast
217     VERIFY_SUCCESS(OCBuildIPv4Address( MULTICAST_IP_ADDR[0], MULTICAST_IP_ADDR[1],
218                 MULTICAST_IP_ADDR[2], MULTICAST_IP_ADDR[3], TEST_PORT_NUM, &ipAddrMcast), ERR_SUCCESS);
219
220     do {
221
222         recvLen = OCRecvFrom(sfd, buf, MAX_BUF_SIZE, 0, &peerAddr);
223         if (recvLen > 0) {
224             pktrecv++;
225             OC_LOG(DEBUG, MOD_NAME, PCF("Rcvd data from :"));
226             OC_LOG_BUFFER(INFO, MOD_NAME, peerAddr.addr, peerAddr.size);
227             OC_LOG(DEBUG, MOD_NAME, PCF("Data Length :"));
228             OC_LOG_BUFFER(INFO, MOD_NAME, (uint8_t*)&recvLen, sizeof(recvLen));
229
230             VERIFY_SUCCESS(OCSendTo(sfd, buf, recvLen, 0, &ipAddrMcast), recvLen);
231         } else {
232             OC_LOG(DEBUG, MOD_NAME, PCF("No data received"));
233         }
234         delay(500); //delay  secs
235     } while (pktrecv < TEST_NUM_PKTS);
236
237     VERIFY_SUCCESS(OCClose(sfd), ERR_SUCCESS);
238     OC_LOG(DEBUG, MOD_NAME, PCF("test90 - Completed"));
239 }
240
241
242
243 //End to End - 'Simultaneous' Unicast-Multicast Send/Recv test
244 //This tests if Arduino Wiznet shield can open 2 independent sockets listening on same port:
245 //  one for unicast traffic and another for multicast traffic.
246 //Start sample app 'sendrecv' on different Linux box.
247 void test100() {
248     //TBD
249     //TBD
250     //TBD
251 }
252
253
254
255
256
257 void setup() {
258
259     Serial.begin(115200);
260
261     Serial.println("Trying to get an IP address using DHCP");
262     if (Ethernet.begin(ETHERNET_MAC) == 0) {
263         Serial.println("Failed to configure Ethernet using DHCP");
264     }
265 }
266
267 void loop() {
268     test10();
269     test20();
270     test30();
271     test40();
272     test50();
273     test60(); //SendTo --Unicast
274     test70(); //SendTo --Multicast
275     test80(); //End-to-End  --Unicast
276     delay(5000);
277     test90(); //End-to-End  --Multicast
278
279     do {
280         OC_LOG(DEBUG, MOD_NAME, PCF("All tests Completed"));
281         delay(10000);
282     } while (1);
283 }
284
285