Initial merge-commit of the OIC code. Should successfully do discovery for single...
[platform/upstream/iotivity.git] / csdk / ocsocket / test / linux / ocsocket_gtest.cpp
1 //******************************************************************
2 //
3 // copyright 2014 intel corporation all rights reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
5
6
7 #include <gtest/gtest.h>
8 #include <ocsocket.h>
9 #include <logger.h>
10
11 #define MOD_NAME ("ocsocket_test")
12
13 #define TEST_PORT_NUM (8888)
14
15 unsigned char buf1[] = {
16   0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
17   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f, 0x6f, 0x67,
18   0x6c, 0x65, 0x63, 0x61, 0x73, 0x74, 0x04, 0x5f, 0x74, 0x63, 0x70, 0x05,
19   0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01
20 };
21 unsigned int buf1_len = sizeof(buf1);
22
23
24 unsigned char buf2[] = {
25   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
26   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
27   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
28   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
29   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
30   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
31   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
32   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
33   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
34   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
35   0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
36   0x00, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x67, 0x6f,
37   0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74,
38   0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
39   0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
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 };
66 unsigned int buf2_len = sizeof(buf2);
67
68 TEST(BuildIPv4, Positive) {
69     OCDevAddr ipaddr;
70     EXPECT_EQ(ERR_SUCCESS, OCBuildIPv4Address(224,0,0,251,5353, &ipaddr));
71 }
72
73
74 TEST(BuildIPv4, InvalidInput) {
75     EXPECT_EQ(ERR_INVALID_INPUT, OCBuildIPv4Address(24,24,24,24,2424, NULL));
76     EXPECT_EQ(ERR_INVALID_INPUT, OCBuildIPv4Address(-24,24,-24,24,2424, NULL));
77 }
78
79 TEST(DevAddrToIPv4Addr, Positive) {
80     OCDevAddr ipaddr;
81     uint8_t  a,b,c,d;
82     uint16_t port;
83     OCBuildIPv4Address(1,2,3,4,5353, &ipaddr);
84     EXPECT_EQ(ERR_SUCCESS, OCDevAddrToIPv4Addr(&ipaddr, &a, &b, &c, &d ));
85     EXPECT_TRUE((a == 1) && (b == 2) && (c == 3) && (d ==4));
86     EXPECT_EQ(ERR_SUCCESS, OCDevAddrToPort(&ipaddr, &port ));
87     EXPECT_TRUE(port == 5353);
88 }
89
90
91 TEST(DevAddrToIPv4Addr, InvalidInput) {
92     OCDevAddr ipaddr;
93     uint8_t  a,b,c,d;
94     uint16_t port;
95     OCBuildIPv4Address(1,2,3,4,5353, &ipaddr);
96     EXPECT_EQ(ERR_INVALID_INPUT, OCDevAddrToIPv4Addr(NULL, &a, &b, &c, &d ));
97     EXPECT_EQ(ERR_INVALID_INPUT, OCDevAddrToIPv4Addr(&ipaddr, NULL, &b, &c, &d ));
98     EXPECT_EQ(ERR_INVALID_INPUT, OCDevAddrToIPv4Addr(NULL, NULL, &b, &c, &d ));
99
100     EXPECT_EQ(ERR_INVALID_INPUT, OCDevAddrToPort(NULL, &port ));
101     EXPECT_EQ(ERR_INVALID_INPUT, OCDevAddrToPort(&ipaddr, NULL ));
102     EXPECT_EQ(ERR_INVALID_INPUT, OCDevAddrToPort(NULL, NULL ));
103 }
104
105
106
107
108 TEST(GetInterfaceAddress, Positive) {
109     uint8_t addr[20];
110     uint8_t ifname[] = "eth0";
111     EXPECT_EQ(ERR_SUCCESS, OCGetInterfaceAddress( ifname, sizeof(ifname), AF_INET,  addr, sizeof(addr)));
112     printf("IPv4 Address: %s\n", addr);
113     EXPECT_EQ(ERR_SUCCESS, OCGetInterfaceAddress( NULL, 0,  AF_INET, addr, sizeof(addr)));
114     printf("IPv4 Address: %s\n", addr);
115 }
116
117 TEST(GetInterfaceAddress, Negative) {
118     uint8_t addr[20];
119     uint8_t ifname[] = "ethxx";
120     EXPECT_EQ(ERR_UNKNOWN, OCGetInterfaceAddress( ifname, sizeof(ifname),  AF_INET, addr, sizeof(addr)));
121     EXPECT_EQ(ERR_INVALID_INPUT, OCGetInterfaceAddress( ifname, sizeof(ifname),  AF_INET, NULL, sizeof(addr)));
122     EXPECT_EQ(ERR_UNKNOWN, OCGetInterfaceAddress( ifname, sizeof(ifname),  AF_INET, addr, 0));
123     EXPECT_EQ(ERR_INVALID_INPUT, OCGetInterfaceAddress( ifname, sizeof(ifname),  AF_INET6, addr, sizeof(addr)));
124 }
125
126 TEST(InitUDP, Positive) {
127     OCDevAddr ipaddr;
128     int32_t  sockfd;
129     uint8_t addr[20];
130     uint8_t ifname[] = "eth0";
131     uint8_t a,b,c,d;
132
133     OCBuildIPv4Address(0,0,0,0, 0, &ipaddr);
134     EXPECT_EQ(ERR_SUCCESS, OCInitUDP(&ipaddr, &sockfd));
135     OCClose(sockfd);
136
137     OCBuildIPv4Address(0,0,0,0, 5678, &ipaddr);
138     EXPECT_EQ(ERR_SUCCESS, OCInitUDP(&ipaddr, &sockfd));
139     OCClose(sockfd);
140
141     OCGetInterfaceAddress( ifname, sizeof(ifname),  AF_INET, addr, sizeof(addr));
142     sscanf((const char*)addr, "%d.%d.%d.%d", (int*)&a, (int*)&b, (int*)&c, (int*)&d);
143     OCBuildIPv4Address(a,b,c,d, TEST_PORT_NUM, &ipaddr);
144     EXPECT_EQ(ERR_SUCCESS, OCInitUDP(&ipaddr, &sockfd));
145     OCClose(sockfd);
146 }
147
148
149 TEST(InitUDP, Negative) {
150     OCDevAddr ipaddr;
151     int32_t  sockfd;
152
153     OCBuildIPv4Address(0,0,0,0, 0, &ipaddr);
154     EXPECT_EQ(ERR_INVALID_INPUT, OCInitUDP(NULL, &sockfd));
155
156     EXPECT_EQ(ERR_INVALID_INPUT, OCInitUDP(&ipaddr, NULL));
157 }
158
159
160
161 TEST(InitUDPMulticast, Positive) {
162     OCDevAddr ipaddr1;
163     int32_t sfd;
164
165     OCBuildIPv4Address(224, 0, 0, 251, 5353, &ipaddr1); //address to which MEMBERSHIP needs to be added
166     EXPECT_EQ(ERR_SUCCESS, OCInitUDPMulticast(&ipaddr1, &sfd));
167     OCClose(sfd);
168 }
169
170
171 TEST(InitUDPMulticast, Negative) {
172     OCDevAddr ipaddr1;
173     int32_t sfd;
174
175     OCBuildIPv4Address(224, 0, 0, 251, 5353, &ipaddr1); //address to which MEMBERSHIP needs to be added
176     EXPECT_EQ(ERR_INVALID_INPUT, OCInitUDPMulticast(NULL, &sfd));
177     EXPECT_EQ(ERR_INVALID_INPUT, OCInitUDPMulticast(&ipaddr1, NULL));
178 }
179
180
181 TEST(SendToRecvfromUnicast, Positive) {
182     OCDevAddr ipaddr1, ipaddr2, ipaddr3;
183     int32_t  ssfd, rsfd;
184     uint8_t addr[20];
185     uint8_t ifname[] = "eth0";
186     uint8_t a,b,c,d;
187     uint8_t tmp1[512];
188
189     //Create sending socket
190     OCBuildIPv4Address(0,0,0,0, 0, &ipaddr1);
191     OCInitUDP(&ipaddr1, &ssfd);
192
193     //Create receiving socket...i.e. bind to the specific port
194     OCBuildIPv4Address(0,0,0,0, TEST_PORT_NUM, &ipaddr2);
195     OCInitUDP(&ipaddr2, &rsfd);
196
197     //Since this is a Unit test, we will attempt to send message to ourself at a specific port
198     OCGetInterfaceAddress( ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
199     sscanf((const char*)addr, "%d.%d.%d.%d", (int*)&a, (int*)&b, (int*)&c, (int*)&d);
200     OCBuildIPv4Address(a,b,c,d, TEST_PORT_NUM, &ipaddr2);
201
202     //Test 1 -- Send 40 bytes
203     //Send the packet to ipaddr2(myself:TEST_PORT_NUM)
204     EXPECT_EQ(buf1_len, OCSendTo(ssfd, buf1, buf1_len, 0, &ipaddr2));
205     //Receive the packet
206     ipaddr3.size = sizeof(ipaddr3.addr);
207     EXPECT_EQ(buf1_len, OCRecvFrom(rsfd, tmp1, sizeof(tmp1), 0, &ipaddr3));
208     //Compare the received buffer with send buffer
209     EXPECT_EQ(ERR_SUCCESS, memcmp(tmp1, buf1, buf1_len));
210
211     //Test 2 -- Send 1 byte
212     //Send the packet to ipaddr2(myself:TEST_PORT_NUM)
213     EXPECT_EQ( 1, OCSendTo(ssfd, buf1, 1, 0, &ipaddr2));
214     //Receive the packet
215     ipaddr3.size = sizeof(ipaddr3.addr);
216     EXPECT_EQ( 1, OCRecvFrom(rsfd, tmp1, sizeof(tmp1), 0, &ipaddr3));
217     //Compare the received buffer with send buffer
218     EXPECT_EQ(ERR_SUCCESS, memcmp(tmp1, buf1, 1));
219
220     //Test 3 -- Send 320 byte
221     //Send the packet to ipaddr2(myself:TEST_PORT_NUM)
222     EXPECT_EQ(buf2_len, OCSendTo(ssfd, buf2, buf2_len, 0, &ipaddr2));
223     //Receive the packet
224     ipaddr3.size = sizeof(ipaddr3.addr);
225     EXPECT_EQ(buf2_len, OCRecvFrom(rsfd, tmp1, sizeof(tmp1), 0, &ipaddr3));
226     //Compare the received buffer with send buffer
227     EXPECT_EQ(ERR_SUCCESS, memcmp(tmp1, buf2, buf2_len));
228
229     OCClose(ssfd);
230     OCClose(rsfd);
231 }
232
233
234 TEST(SendToRecvfromMulticast, Positive) {
235     OCDevAddr ipaddr1, ipaddr2, ipaddr3, ipaddr4;
236     int32_t  ssfd;
237     uint8_t tmp1[512];
238
239     //Create sending socket
240     OCBuildIPv4Address(0,0,0,0, 5353, &ipaddr1);
241     OCBuildIPv4Address(224, 0, 0, 251, 5353, &ipaddr2); //address to which MEMBERSHIP needs to be added
242     OCInitUDPMulticast(&ipaddr2, &ssfd);
243
244     //build the multicast address to which we need to send the datagram
245     OCBuildIPv4Address(224, 0, 0, 251, 5353, &ipaddr3);
246
247     //Test 1 -- Send 40 bytes
248     EXPECT_EQ(buf1_len, OCSendTo(ssfd, buf1, buf1_len, 0, &ipaddr3));
249     //Receive the packet
250     ipaddr4.size = sizeof(ipaddr4.addr);
251     EXPECT_EQ(buf1_len, OCRecvFrom(ssfd, tmp1, sizeof(tmp1), 0, &ipaddr4));
252     //Compare the received buffer with send buffer
253     EXPECT_EQ(ERR_SUCCESS, memcmp(tmp1, buf1, buf1_len));
254
255     //Test 2 -- Send 1 byte
256     EXPECT_EQ( 1, OCSendTo(ssfd, buf1, 1, 0, &ipaddr3));
257     //Receive the packet
258     ipaddr4.size = sizeof(ipaddr4.addr);
259     EXPECT_EQ( 1, OCRecvFrom(ssfd, tmp1, sizeof(tmp1), 0, &ipaddr4));
260     //Compare the received buffer with send buffer
261     EXPECT_EQ(ERR_SUCCESS, memcmp(tmp1, buf1, 1));
262
263     //Test 3 -- Send 320 byte
264     EXPECT_EQ(buf2_len, OCSendTo(ssfd, buf2, buf2_len, 0, &ipaddr3));
265     //Receive the packet
266     ipaddr4.size = sizeof(ipaddr4.addr);
267     EXPECT_EQ(buf2_len, OCRecvFrom(ssfd, tmp1, sizeof(tmp1), 0, &ipaddr3));
268     //Compare the received buffer with send buffer
269     EXPECT_EQ(ERR_SUCCESS, memcmp(tmp1, buf2, buf2_len));
270
271     OCClose(ssfd);
272 }
273