1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 #include <gtest/gtest.h>
26 #define MOD_NAME ("ocsocket_test")
28 #define TEST_PORT_NUM (8888)
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
36 unsigned int buf1_len = sizeof(buf1);
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,
81 unsigned int buf2_len = sizeof(buf2);
83 TEST(BuildIPv4, Positive) {
85 EXPECT_EQ(ERR_SUCCESS, OCBuildIPv4Address(224,0,0,251,5353, &ipaddr));
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));
94 TEST(DevAddrToIPv4Addr, Positive) {
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);
106 TEST(DevAddrToIPv4Addr, InvalidInput) {
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 ));
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 ));
123 TEST(GetInterfaceAddress, Positive) {
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);
132 TEST(GetInterfaceAddress, Negative) {
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)));
141 TEST(InitUDP, Positive) {
145 uint8_t ifname[] = "eth0";
148 OCBuildIPv4Address(0,0,0,0, 0, &ipaddr);
149 EXPECT_EQ(ERR_SUCCESS, OCInitUDP(&ipaddr, &sockfd));
152 OCBuildIPv4Address(0,0,0,0, 5678, &ipaddr);
153 EXPECT_EQ(ERR_SUCCESS, OCInitUDP(&ipaddr, &sockfd));
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));
164 TEST(InitUDP, Negative) {
168 OCBuildIPv4Address(0,0,0,0, 0, &ipaddr);
169 EXPECT_EQ(ERR_INVALID_INPUT, OCInitUDP(NULL, &sockfd));
171 EXPECT_EQ(ERR_INVALID_INPUT, OCInitUDP(&ipaddr, NULL));
176 TEST(InitUDPMulticast, Positive) {
180 OCBuildIPv4Address(224, 0, 0, 251, 5353, &ipaddr1); //address to which MEMBERSHIP needs to be added
181 EXPECT_EQ(ERR_SUCCESS, OCInitUDPMulticast(&ipaddr1, &sfd));
186 TEST(InitUDPMulticast, Negative) {
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));
196 TEST(SendToRecvfromUnicast, Positive) {
197 OCDevAddr ipaddr1, ipaddr2, ipaddr3;
200 uint8_t ifname[] = "eth0";
204 //Create sending socket
205 OCBuildIPv4Address(0,0,0,0, 0, &ipaddr1);
206 OCInitUDP(&ipaddr1, &ssfd);
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);
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);
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));
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));
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));
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));
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));
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));
249 TEST(SendToRecvfromMulticast, Positive) {
250 OCDevAddr ipaddr1, ipaddr2, ipaddr3, ipaddr4;
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);
259 //build the multicast address to which we need to send the datagram
260 OCBuildIPv4Address(224, 0, 0, 251, 5353, &ipaddr3);
262 //Test 1 -- Send 40 bytes
263 EXPECT_EQ(buf1_len, OCSendTo(ssfd, buf1, buf1_len, 0, &ipaddr3));
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));
270 //Test 2 -- Send 1 byte
271 EXPECT_EQ( 1, OCSendTo(ssfd, buf1, 1, 0, &ipaddr3));
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));
278 //Test 3 -- Send 320 byte
279 EXPECT_EQ(buf2_len, OCSendTo(ssfd, buf2, buf2_len, 0, &ipaddr3));
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));