Modifying version number for building on tizen 3.0
[platform/upstream/iotivity.git] / resource / csdk / ocsocket / test / android / ocsocket_gtest.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
22 #include <gtest/gtest.h>
23 #include <ocsocket.h>
24 #include <logger.h>
25
26 #define MOD_NAME ("ocsocket_test")
27 #define TEST_PORT_NUM (8888)
28
29 unsigned char buf1[] = {
30   0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
31   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f, 0x6f, 0x67,
32   0x6c, 0x65, 0x63, 0x61, 0x73, 0x74, 0x04, 0x5f, 0x74, 0x63, 0x70, 0x05,
33   0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01
34 };
35 unsigned int buf1_len = sizeof(buf1);
36
37
38 unsigned char buf2[] = {
39   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
40   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
41   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
42   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
43   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
44   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
45   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
46   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
47   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
48   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
49   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
50   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
51   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
52   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
53   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
54   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
55   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
56   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
57   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
58   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
59   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
60   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
61   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
62   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
63   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
64   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
65   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
66   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
67   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
68   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
69   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
70   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
71   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
72   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
73   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
74   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
75   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
76   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
77   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
78   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
79 };
80
81 unsigned int buf2_len = sizeof(buf2);
82
83
84 TEST(BuildIPv4, Positive) {
85     OCDevAddr ipaddr;
86     EXPECT_EQ(ERR_SUCCESS, OCBuildIPv4Address(224,0,0,251,5353, &ipaddr));
87 }
88
89
90 TEST(BuildIPv4, InvalidInput) {
91     EXPECT_EQ(ERR_INVALID_INPUT, OCBuildIPv4Address(24,24,24,24,2424, NULL));
92     EXPECT_EQ(ERR_INVALID_INPUT, OCBuildIPv4Address(-24,24,-24,24,2424, NULL));
93 }
94
95
96 TEST(DevAddrToIPv4Addr, Positive) {
97     OCDevAddr ipaddr;
98     uint8_t  a,b,c,d;
99     uint16_t port;
100     OCBuildIPv4Address(1,2,3,4,5353, &ipaddr);
101     EXPECT_EQ(ERR_SUCCESS, OCDevAddrToIPv4Addr(&ipaddr, &a, &b, &c, &d ));
102     EXPECT_TRUE((a == 1) && (b == 2) && (c == 3) && (d ==4));
103     EXPECT_EQ(ERR_SUCCESS, OCDevAddrToPort(&ipaddr, &port ));
104     EXPECT_TRUE(port == 5353);
105 }
106
107
108 TEST(DevAddrToIPv4Addr, InvalidInput) {
109     OCDevAddr ipaddr;
110     uint8_t  a,b,c,d;
111     uint16_t port;
112     OCBuildIPv4Address(1,2,3,4,5353, &ipaddr);
113     EXPECT_EQ(ERR_INVALID_INPUT, OCDevAddrToIPv4Addr(NULL, &a, &b, &c, &d ));
114     EXPECT_EQ(ERR_INVALID_INPUT, OCDevAddrToIPv4Addr(&ipaddr, NULL, &b, &c, &d ));
115     EXPECT_EQ(ERR_INVALID_INPUT, OCDevAddrToIPv4Addr(NULL, NULL, &b, &c, &d ));
116
117     EXPECT_EQ(ERR_INVALID_INPUT, OCDevAddrToPort(NULL, &port ));
118     EXPECT_EQ(ERR_INVALID_INPUT, OCDevAddrToPort(&ipaddr, NULL ));
119     EXPECT_EQ(ERR_INVALID_INPUT, OCDevAddrToPort(NULL, NULL ));
120 }
121
122
123 TEST(GetInterfaceAddress, Positive) {
124     uint8_t addr[20];
125     uint8_t ifname[] = "wlan0";
126     EXPECT_EQ(ERR_SUCCESS, OCGetInterfaceAddress( ifname, sizeof(ifname), AF_INET, addr, sizeof(addr)));
127     OC_LOG_V(INFO, MOD_NAME, "IPv4 Address: %s\n", addr);
128     //On Android, it is MUST to provide interface name
129     //EXPECT_EQ(0, get_ipv4addr( NULL, 0, addr, sizeof(addr)));
130 }
131
132 TEST(GetInterfaceAddress, Negative) {
133     uint8_t addr[20];
134     uint8_t ifname[] = "ethxx";
135     EXPECT_EQ(ERR_INVALID_INPUT, OCGetInterfaceAddress( NULL, 0, AF_INET, addr, sizeof(addr)));
136     EXPECT_EQ(ERR_UNKNOWN, OCGetInterfaceAddress( ifname, sizeof(ifname), AF_INET, addr, sizeof(addr)));
137     EXPECT_EQ(ERR_INVALID_INPUT, OCGetInterfaceAddress( ifname, sizeof(ifname), AF_INET, NULL, sizeof(addr)));
138     EXPECT_EQ(ERR_UNKNOWN, OCGetInterfaceAddress( ifname, sizeof(ifname), AF_INET, addr, 0));
139     EXPECT_EQ(ERR_INVALID_INPUT, OCGetInterfaceAddress( ifname, sizeof(ifname),  AF_INET6, addr, sizeof(addr)));
140 }
141
142 TEST(InitUDP, Positive) {
143     OCDevAddr ipaddr;
144     int32_t  sockfd;
145     uint8_t addr[20];
146     uint8_t ifname[] = "wlan0";
147     uint8_t a,b,c,d;
148
149     OCBuildIPv4Address(0,0,0,0, 0, &ipaddr);
150     EXPECT_EQ(ERR_SUCCESS, OCInitUDP(&ipaddr, &sockfd));
151     OCClose(sockfd);
152
153     OCBuildIPv4Address(0,0,0,0, 5678, &ipaddr);
154     EXPECT_EQ(ERR_SUCCESS, OCInitUDP(&ipaddr, &sockfd));
155     OCClose(sockfd);
156
157     OCGetInterfaceAddress( ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
158     sscanf((const char*)addr, "%d.%d.%d.%d", (int*)&a, (int*)&b, (int*)&c, (int*)&d);
159     OCBuildIPv4Address(a,b,c,d, TEST_PORT_NUM, &ipaddr);
160     EXPECT_EQ(ERR_SUCCESS, OCInitUDP(&ipaddr, &sockfd));
161     OCClose(sockfd);
162 }
163
164
165 TEST(InitUDP, Negative) {
166     OCDevAddr ipaddr;
167     int32_t  sockfd;
168
169     OCBuildIPv4Address(0,0,0,0, 0, &ipaddr);
170     EXPECT_EQ(ERR_INVALID_INPUT, OCInitUDP(NULL, &sockfd));
171
172     EXPECT_EQ(ERR_INVALID_INPUT, OCInitUDP(&ipaddr, NULL));
173 }
174
175
176
177 TEST(InitUDPMulticast, Positive) {
178     OCDevAddr ipaddr1;
179     int32_t sfd;
180
181     OCBuildIPv4Address(224, 0, 0, 251, 5353, &ipaddr1); //address to which MEMBERSHIP needs to be added
182     EXPECT_EQ(ERR_SUCCESS, OCInitUDPMulticast(&ipaddr1, &sfd));
183     OCClose(sfd);
184 }
185
186
187 TEST(InitUDPMulticast, Negative) {
188     OCDevAddr ipaddr1;
189     int32_t sfd;
190
191     OCBuildIPv4Address(224, 0, 0, 251, 5353, &ipaddr1); //address to which MEMBERSHIP needs to be added
192     EXPECT_EQ(ERR_INVALID_INPUT, OCInitUDPMulticast(NULL, &sfd));
193     EXPECT_EQ(ERR_INVALID_INPUT, OCInitUDPMulticast(&ipaddr1, NULL));
194 }
195
196
197 TEST(SendToRecvfromUnicast, Positive) {
198     OCDevAddr ipaddr1, ipaddr2, ipaddr3;
199     int32_t  ssfd, rsfd;
200     uint8_t addr[20];
201     uint8_t ifname[] = "wlan0";
202     uint8_t a,b,c,d;
203     //uint8_t tmp1[512];
204
205
206     OCGetInterfaceAddress( ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
207     sscanf((const char*)addr, "%d.%d.%d.%d", (int*)&a, (int*)&b, (int*)&c, (int*)&d);
208
209     //Create sending socket
210     OCBuildIPv4Address(a, b, c, d, 0, &ipaddr1);
211     OCInitUDP(&ipaddr1, &ssfd);
212
213     //Create receiving socket...i.e. bind to the specific port
214     OCBuildIPv4Address(a,b,c, d+1, TEST_PORT_NUM, &ipaddr2);
215     OCInitUDP(&ipaddr2, &rsfd);
216
217     //Test 1 -- Send 40 bytes
218     //Send the packet to ipaddr2(myself:TEST_PORT_NUM)
219     EXPECT_EQ(buf1_len, OCSendTo(ssfd, buf1, buf1_len, 0, &ipaddr2));
220     //Receive the packet
221     ipaddr3.size = sizeof(ipaddr3.addr);
222     //EXPECT_EQ(buf1_len, OCRecvFrom(rsfd, tmp1, sizeof(tmp1), 0, &ipaddr3));
223     //Compare the received buffer with send buffer
224     //EXPECT_EQ(ERR_SUCCESS, memcmp(tmp1, buf1, buf1_len));
225
226     //Test 2 -- Send 1 byte
227     //Send the packet to ipaddr2(myself:TEST_PORT_NUM)
228     EXPECT_EQ( 1, OCSendTo(ssfd, buf1, 1, 0, &ipaddr2));
229     //Receive the packet
230     ipaddr3.size = sizeof(ipaddr3.addr);
231     //EXPECT_EQ( 1, OCRecvFrom(rsfd, tmp1, sizeof(tmp1), 0, &ipaddr3));
232     //Compare the received buffer with send buffer
233     //EXPECT_EQ(ERR_SUCCESS, memcmp(tmp1, buf1, 1));
234
235     //Test 3 -- Send 320 byte
236     //Send the packet to ipaddr2(myself:TEST_PORT_NUM)
237     EXPECT_EQ(buf2_len, OCSendTo(ssfd, buf2, buf2_len, 0, &ipaddr2));
238     //Receive the packet
239     ipaddr3.size = sizeof(ipaddr3.addr);
240     //EXPECT_EQ(buf2_len, OCRecvFrom(rsfd, tmp1, sizeof(tmp1), 0, &ipaddr3));
241     //Compare the received buffer with send buffer
242     //EXPECT_EQ(ERR_SUCCESS, memcmp(tmp1, buf2, buf2_len));
243
244     OCClose(ssfd);
245     OCClose(rsfd);
246 }
247
248
249
250 TEST(SendToRecvfromMulticast, Positive) {
251     OCDevAddr ipaddr1, ipaddr2, ipaddr3, ipaddr4;
252     int32_t  ssfd;
253     uint8_t tmp1[512];
254
255     //Create sending socket
256     OCBuildIPv4Address(0,0,0,0, 5353, &ipaddr1);
257     OCBuildIPv4Address(224, 0, 0, 251, 5353, &ipaddr2); //address to which MEMBERSHIP needs to be added
258     OCInitUDPMulticast(&ipaddr2, &ssfd);
259
260     //build the multicast address to which we need to send the datagram
261     OCBuildIPv4Address(224, 0, 0, 251, 5353, &ipaddr3);
262
263     //Test 1 -- Send 40 bytes
264     EXPECT_EQ(buf1_len, OCSendTo(ssfd, buf1, buf1_len, 0, &ipaddr3));
265     //Receive the packet
266     ipaddr4.size = sizeof(ipaddr4.addr);
267     EXPECT_EQ(buf1_len, OCRecvFrom(ssfd, tmp1, sizeof(tmp1), 0, &ipaddr4));
268     //Compare the received buffer with send buffer
269     EXPECT_EQ(ERR_SUCCESS, memcmp(tmp1, buf1, buf1_len));
270
271     //Test 2 -- Send 1 byte
272     EXPECT_EQ( 1, OCSendTo(ssfd, buf1, 1, 0, &ipaddr3));
273     //Receive the packet
274     ipaddr4.size = sizeof(ipaddr4.addr);
275     EXPECT_EQ( 1, OCRecvFrom(ssfd, tmp1, sizeof(tmp1), 0, &ipaddr4));
276     //Compare the received buffer with send buffer
277     EXPECT_EQ(ERR_SUCCESS, memcmp(tmp1, buf1, 1));
278
279     //Test 3 -- Send 320 byte
280     EXPECT_EQ(buf2_len, OCSendTo(ssfd, buf2, buf2_len, 0, &ipaddr3));
281     //Receive the packet
282     ipaddr4.size = sizeof(ipaddr4.addr);
283     EXPECT_EQ(buf2_len, OCRecvFrom(ssfd, tmp1, sizeof(tmp1), 0, &ipaddr3));
284     //Compare the received buffer with send buffer
285     EXPECT_EQ(ERR_SUCCESS, memcmp(tmp1, buf2, buf2_len));
286
287     OCClose(ssfd);
288 }
289