Repo Merge: Moving resource API down a directory
[platform/upstream/iotivity.git] / resource / csdk / ocsocket / test / linux / 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
28 #define TEST_PORT_NUM (8888)
29
30 unsigned char buf1[] = {
31   0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
32   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f, 0x6f, 0x67,
33   0x6c, 0x65, 0x63, 0x61, 0x73, 0x74, 0x04, 0x5f, 0x74, 0x63, 0x70, 0x05,
34   0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01
35 };
36 unsigned int buf1_len = sizeof(buf1);
37
38
39 unsigned char buf2[] = {
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 buf2_len = sizeof(buf2);
82
83 TEST(BuildIPv4, Positive) {
84     OCDevAddr ipaddr;
85     EXPECT_EQ(ERR_SUCCESS, OCBuildIPv4Address(224,0,0,251,5353, &ipaddr));
86 }
87
88
89 TEST(BuildIPv4, InvalidInput) {
90     EXPECT_EQ(ERR_INVALID_INPUT, OCBuildIPv4Address(24,24,24,24,2424, NULL));
91     EXPECT_EQ(ERR_INVALID_INPUT, OCBuildIPv4Address(-24,24,-24,24,2424, NULL));
92 }
93
94 TEST(DevAddrToIPv4Addr, Positive) {
95     OCDevAddr ipaddr;
96     uint8_t  a,b,c,d;
97     uint16_t port;
98     OCBuildIPv4Address(1,2,3,4,5353, &ipaddr);
99     EXPECT_EQ(ERR_SUCCESS, OCDevAddrToIPv4Addr(&ipaddr, &a, &b, &c, &d ));
100     EXPECT_TRUE((a == 1) && (b == 2) && (c == 3) && (d ==4));
101     EXPECT_EQ(ERR_SUCCESS, OCDevAddrToPort(&ipaddr, &port ));
102     EXPECT_TRUE(port == 5353);
103 }
104
105
106 TEST(DevAddrToIPv4Addr, InvalidInput) {
107     OCDevAddr ipaddr;
108     uint8_t  a,b,c,d;
109     uint16_t port;
110     OCBuildIPv4Address(1,2,3,4,5353, &ipaddr);
111     EXPECT_EQ(ERR_INVALID_INPUT, OCDevAddrToIPv4Addr(NULL, &a, &b, &c, &d ));
112     EXPECT_EQ(ERR_INVALID_INPUT, OCDevAddrToIPv4Addr(&ipaddr, NULL, &b, &c, &d ));
113     EXPECT_EQ(ERR_INVALID_INPUT, OCDevAddrToIPv4Addr(NULL, NULL, &b, &c, &d ));
114
115     EXPECT_EQ(ERR_INVALID_INPUT, OCDevAddrToPort(NULL, &port ));
116     EXPECT_EQ(ERR_INVALID_INPUT, OCDevAddrToPort(&ipaddr, NULL ));
117     EXPECT_EQ(ERR_INVALID_INPUT, OCDevAddrToPort(NULL, NULL ));
118 }
119
120
121
122
123 TEST(GetInterfaceAddress, Positive) {
124     uint8_t addr[20];
125     uint8_t ifname[] = "eth0";
126     EXPECT_EQ(ERR_SUCCESS, OCGetInterfaceAddress( ifname, sizeof(ifname), AF_INET,  addr, sizeof(addr)));
127     printf("IPv4 Address: %s\n", addr);
128     EXPECT_EQ(ERR_SUCCESS, OCGetInterfaceAddress( NULL, 0,  AF_INET, addr, sizeof(addr)));
129     printf("IPv4 Address: %s\n", addr);
130 }
131
132 TEST(GetInterfaceAddress, Negative) {
133     uint8_t addr[20];
134     uint8_t ifname[] = "ethxx";
135     EXPECT_EQ(ERR_UNKNOWN, OCGetInterfaceAddress( ifname, sizeof(ifname),  AF_INET, addr, sizeof(addr)));
136     EXPECT_EQ(ERR_INVALID_INPUT, OCGetInterfaceAddress( ifname, sizeof(ifname),  AF_INET, NULL, sizeof(addr)));
137     EXPECT_EQ(ERR_UNKNOWN, OCGetInterfaceAddress( ifname, sizeof(ifname),  AF_INET, addr, 0));
138     EXPECT_EQ(ERR_INVALID_INPUT, OCGetInterfaceAddress( ifname, sizeof(ifname),  AF_INET6, addr, sizeof(addr)));
139 }
140
141 TEST(InitUDP, Positive) {
142     OCDevAddr ipaddr;
143     int32_t  sockfd;
144     uint8_t addr[20];
145     uint8_t ifname[] = "eth0";
146     uint8_t a,b,c,d;
147
148     OCBuildIPv4Address(0,0,0,0, 0, &ipaddr);
149     EXPECT_EQ(ERR_SUCCESS, OCInitUDP(&ipaddr, &sockfd));
150     OCClose(sockfd);
151
152     OCBuildIPv4Address(0,0,0,0, 5678, &ipaddr);
153     EXPECT_EQ(ERR_SUCCESS, OCInitUDP(&ipaddr, &sockfd));
154     OCClose(sockfd);
155
156     OCGetInterfaceAddress( ifname, sizeof(ifname),  AF_INET, addr, sizeof(addr));
157     sscanf((const char*)addr, "%d.%d.%d.%d", (int*)&a, (int*)&b, (int*)&c, (int*)&d);
158     OCBuildIPv4Address(a,b,c,d, TEST_PORT_NUM, &ipaddr);
159     EXPECT_EQ(ERR_SUCCESS, OCInitUDP(&ipaddr, &sockfd));
160     OCClose(sockfd);
161 }
162
163
164 TEST(InitUDP, Negative) {
165     OCDevAddr ipaddr;
166     int32_t  sockfd;
167
168     OCBuildIPv4Address(0,0,0,0, 0, &ipaddr);
169     EXPECT_EQ(ERR_INVALID_INPUT, OCInitUDP(NULL, &sockfd));
170
171     EXPECT_EQ(ERR_INVALID_INPUT, OCInitUDP(&ipaddr, NULL));
172 }
173
174
175
176 TEST(InitUDPMulticast, Positive) {
177     OCDevAddr ipaddr1;
178     int32_t sfd;
179
180     OCBuildIPv4Address(224, 0, 0, 251, 5353, &ipaddr1); //address to which MEMBERSHIP needs to be added
181     EXPECT_EQ(ERR_SUCCESS, OCInitUDPMulticast(&ipaddr1, &sfd));
182     OCClose(sfd);
183 }
184
185
186 TEST(InitUDPMulticast, Negative) {
187     OCDevAddr ipaddr1;
188     int32_t sfd;
189
190     OCBuildIPv4Address(224, 0, 0, 251, 5353, &ipaddr1); //address to which MEMBERSHIP needs to be added
191     EXPECT_EQ(ERR_INVALID_INPUT, OCInitUDPMulticast(NULL, &sfd));
192     EXPECT_EQ(ERR_INVALID_INPUT, OCInitUDPMulticast(&ipaddr1, NULL));
193 }
194
195
196 TEST(SendToRecvfromUnicast, Positive) {
197     OCDevAddr ipaddr1, ipaddr2, ipaddr3;
198     int32_t  ssfd, rsfd;
199     uint8_t addr[20];
200     uint8_t ifname[] = "eth0";
201     uint8_t a,b,c,d;
202     uint8_t tmp1[512];
203
204     //Create sending socket
205     OCBuildIPv4Address(0,0,0,0, 0, &ipaddr1);
206     OCInitUDP(&ipaddr1, &ssfd);
207
208     //Create receiving socket...i.e. bind to the specific port
209     OCBuildIPv4Address(0,0,0,0, TEST_PORT_NUM, &ipaddr2);
210     OCInitUDP(&ipaddr2, &rsfd);
211
212     //Since this is a Unit test, we will attempt to send message to ourself at a specific port
213     OCGetInterfaceAddress( ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
214     sscanf((const char*)addr, "%d.%d.%d.%d", (int*)&a, (int*)&b, (int*)&c, (int*)&d);
215     OCBuildIPv4Address(a,b,c,d, TEST_PORT_NUM, &ipaddr2);
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 TEST(SendToRecvfromMulticast, Positive) {
250     OCDevAddr ipaddr1, ipaddr2, ipaddr3, ipaddr4;
251     int32_t  ssfd;
252     uint8_t tmp1[512];
253
254     //Create sending socket
255     OCBuildIPv4Address(0,0,0,0, 5353, &ipaddr1);
256     OCBuildIPv4Address(224, 0, 0, 251, 5353, &ipaddr2); //address to which MEMBERSHIP needs to be added
257     OCInitUDPMulticast(&ipaddr2, &ssfd);
258
259     //build the multicast address to which we need to send the datagram
260     OCBuildIPv4Address(224, 0, 0, 251, 5353, &ipaddr3);
261
262     //Test 1 -- Send 40 bytes
263     EXPECT_EQ(buf1_len, OCSendTo(ssfd, buf1, buf1_len, 0, &ipaddr3));
264     //Receive the packet
265     ipaddr4.size = sizeof(ipaddr4.addr);
266     EXPECT_EQ(buf1_len, OCRecvFrom(ssfd, tmp1, sizeof(tmp1), 0, &ipaddr4));
267     //Compare the received buffer with send buffer
268     EXPECT_EQ(ERR_SUCCESS, memcmp(tmp1, buf1, buf1_len));
269
270     //Test 2 -- Send 1 byte
271     EXPECT_EQ( 1, OCSendTo(ssfd, buf1, 1, 0, &ipaddr3));
272     //Receive the packet
273     ipaddr4.size = sizeof(ipaddr4.addr);
274     EXPECT_EQ( 1, OCRecvFrom(ssfd, tmp1, sizeof(tmp1), 0, &ipaddr4));
275     //Compare the received buffer with send buffer
276     EXPECT_EQ(ERR_SUCCESS, memcmp(tmp1, buf1, 1));
277
278     //Test 3 -- Send 320 byte
279     EXPECT_EQ(buf2_len, OCSendTo(ssfd, buf2, buf2_len, 0, &ipaddr3));
280     //Receive the packet
281     ipaddr4.size = sizeof(ipaddr4.addr);
282     EXPECT_EQ(buf2_len, OCRecvFrom(ssfd, tmp1, sizeof(tmp1), 0, &ipaddr3));
283     //Compare the received buffer with send buffer
284     EXPECT_EQ(ERR_SUCCESS, memcmp(tmp1, buf2, buf2_len));
285
286     OCClose(ssfd);
287 }
288