takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers utiltest.cpp Source File

utiltest.cpp

00001 /*
00002  * Copyright (c) 2018, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include "gtest/gtest.h"
00018 #include <string.h>
00019 #include "CellularUtil.h"
00020 
00021 using namespace mbed_cellular_util;
00022 
00023 // AStyle ignored as the definition is not clear due to preprocessor usage
00024 // *INDENT-OFF*
00025 class Testutil : public testing::Test {
00026 protected:
00027 
00028     void SetUp()
00029     {
00030     }
00031 
00032     void TearDown()
00033     {
00034     }
00035 };
00036 // *INDENT-ON*
00037 
00038 TEST_F(Testutil, test_util_uint_to_binary_string)
00039 {
00040     char str[33];
00041     uint_to_binary_str(15, str, 33, 32);
00042     str[32] = '\0';
00043     // 15 is "1111" in binary but we ask all 32 bits so it should return "00000000000000000000000000001111"
00044     EXPECT_STREQ("00000000000000000000000000001111", str);
00045 
00046     // test NULL pointer
00047     uint_to_binary_str(15, NULL, 0, 32);
00048 
00049     // test give too small buffer
00050     char too_small[5];
00051     uint_to_binary_str(15, too_small, 5, 6);
00052 }
00053 
00054 TEST_F(Testutil, char_str_to_hex)
00055 {
00056     // basic conversion test, happy days
00057     char hex_buf[50];
00058     uint16_t number_of_hex_chars = char_str_to_hex_str("1234", 4, hex_buf);
00059     hex_buf[number_of_hex_chars] = '\0';
00060     EXPECT_STREQ("31323334", hex_buf);
00061     EXPECT_EQ(8, number_of_hex_chars);
00062 
00063     number_of_hex_chars = char_str_to_hex_str("wuhuu", 5, hex_buf);
00064     hex_buf[number_of_hex_chars] = '\0';
00065     EXPECT_STREQ("7775687575", hex_buf);
00066     EXPECT_EQ(10, number_of_hex_chars);
00067 
00068     // First don't omit the leading zero and then omit and check that leading zero is missing
00069     number_of_hex_chars = char_str_to_hex_str("\nwuhuu", 6, hex_buf);
00070     hex_buf[number_of_hex_chars] = '\0';
00071     EXPECT_STREQ("0A7775687575", hex_buf);
00072     EXPECT_EQ(12, number_of_hex_chars);
00073 
00074     number_of_hex_chars = char_str_to_hex_str("\nwuhuu", 6, hex_buf, true);
00075     hex_buf[number_of_hex_chars] = '\0';
00076     EXPECT_STREQ("A7775687575", hex_buf);
00077     EXPECT_EQ(11, number_of_hex_chars);
00078 
00079     // test giving a null pointer
00080     number_of_hex_chars = char_str_to_hex_str(NULL, 4, hex_buf);
00081     EXPECT_EQ(0, number_of_hex_chars);
00082     number_of_hex_chars = char_str_to_hex_str("1234", 4, NULL);
00083     EXPECT_EQ(0, number_of_hex_chars);
00084 }
00085 
00086 TEST_F(Testutil, convert_ipv6)
00087 {
00088     // leading zeros omitted
00089     char ipv6[64];
00090     strncpy(ipv6, "1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1", 64);
00091     convert_ipv6(ipv6);
00092     EXPECT_STREQ("101:101:101:101:101:101:101:101", ipv6);
00093     EXPECT_EQ(31, strlen(ipv6));
00094 
00095     // some omitted and some not so much
00096     strncpy(ipv6, "255.1.120.2.244.12.55.45.201.110.11.2.233.154.85.96", 64);
00097     convert_ipv6(ipv6);
00098     EXPECT_STREQ("FF01:7802:F40C:372D:C96E:B02:E99A:5560", ipv6);
00099     EXPECT_EQ(38, strlen(ipv6));
00100 
00101     // test giving a null pointer
00102     convert_ipv6(NULL);
00103 }
00104 
00105 TEST_F(Testutil, prefer_ipv6)
00106 {
00107     char tt[20] = "62.241.198.246";
00108     char temp[64] = "2001:14B8:1000:000:000:000:000:002";
00109 
00110     // not enough space to swap, arrays should stay the same
00111     prefer_ipv6(tt, sizeof(tt), temp, sizeof(temp));
00112     EXPECT_STREQ("62.241.198.246", tt);
00113     EXPECT_STREQ("2001:14B8:1000:000:000:000:000:002", temp);
00114 
00115     // should swap as first one was ip4 and later was ipv6 and enough space
00116     char tt2[64] = "62.241.198.246";
00117     prefer_ipv6(tt2, sizeof(tt2), temp, sizeof(temp));
00118     EXPECT_STREQ("62.241.198.246", temp);
00119     EXPECT_STREQ("2001:14B8:1000:000:000:000:000:002", tt2);
00120 }
00121 
00122 TEST_F(Testutil, separate_ip_addresses)
00123 {
00124     char *s = (char *)malloc(128);
00125 
00126     char ip[64] = {0};
00127     char subnet[64] = {0};
00128 
00129     strncpy(s, "32.1.20.187.1.112.139.245.251.136.232.110.123.51.230.138.0.1.2.3.4.5.6.7.8.9.10.11.12.13.14.15\0", 95);
00130     separate_ip_addresses(NULL, ip, sizeof(ip), subnet, sizeof(subnet));
00131 
00132     separate_ip_addresses(s, ip, sizeof(ip), subnet, sizeof(subnet));
00133     EXPECT_STREQ("2001:14BB:170:8BF5:FB88:E86E:7B33:E68A", ip);
00134     EXPECT_STREQ("001:203:405:607:809:A0B:C0D:E0F", subnet);
00135 
00136     strncpy(s, "32:1:20:187:1:112:139:245:251:136:232:110:123:51:230:138 0:1:2:3:4:5:6:7:8:9:10:11:12:13:14:15\0", 95);
00137     separate_ip_addresses(s, ip, sizeof(ip), subnet, sizeof(subnet));
00138     EXPECT_STREQ("32:1:20:187:1:112:139:245:251:136:232:110:123:51:230:138", ip);
00139     EXPECT_STREQ("0:1:2:3:4:5:6:7:8:9:10:11:12:13:14:15", subnet);
00140 
00141     ip[0] = '\0';
00142     subnet[0] = '\0';
00143     strncpy(s, "1.2.3.4\0", 8);
00144     separate_ip_addresses(s, ip, sizeof(ip), subnet, sizeof(subnet));
00145     EXPECT_STREQ("1.2.3.4", ip);
00146     EXPECT_STREQ("", subnet);
00147 
00148     ip[0] = '\0';
00149     subnet[0] = '\0';
00150     strncpy(s, "1.2.3.4.5.6.7.8\0", 16);
00151     separate_ip_addresses(s, ip, sizeof(ip), subnet, sizeof(subnet));
00152     EXPECT_STREQ("1.2.3.4", ip);
00153     EXPECT_STREQ("5.6.7.8", subnet);
00154 
00155     ip[0] = '\0';
00156     subnet[0] = '\0';
00157     strncpy(s, "1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16\0", 39);
00158     separate_ip_addresses(s, ip, sizeof(ip), subnet, sizeof(subnet));
00159     EXPECT_STREQ("102:304:506:708:90A:B0C:D0E:F10", ip);
00160     EXPECT_STREQ("", subnet);
00161 
00162     ip[0] = '\0';
00163     subnet[0] = '\0';
00164     strncpy(s, "32:1:20:187:1:112:139:245:251:136:232:110:123:51:230:138\0", 57);
00165     separate_ip_addresses(s, ip, sizeof(ip), subnet, sizeof(subnet));
00166     EXPECT_STREQ("32:1:20:187:1:112:139:245:251:136:232:110:123:51:230:138", ip);
00167     EXPECT_STREQ("", subnet);
00168 
00169     ip[0] = '\0';
00170     subnet[0] = '\0';
00171     strncpy(s, "1.2.3.4 32:1:20:187:1:112:139:245:251:136:232:110:123:51:230:138\0", 65);
00172     separate_ip_addresses(s, ip, sizeof(ip), subnet, sizeof(subnet));
00173     EXPECT_STREQ("1.2.3.4", ip);
00174     EXPECT_STREQ("32:1:20:187:1:112:139:245:251:136:232:110:123:51:230:138", subnet);
00175 
00176     ip[0] = '\0';
00177     subnet[0] = '\0';
00178     strncpy(s, "1.2.3.4 5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20\0", 51);
00179     separate_ip_addresses(s, ip, sizeof(ip), subnet, sizeof(subnet));
00180     EXPECT_STREQ("1.2.3.4", ip);
00181     EXPECT_STREQ("506:708:90A:B0C:D0E:F10:1112:1314", subnet);
00182     EXPECT_STREQ("1.2.3.4 5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20", s);
00183 }
00184 
00185 TEST_F(Testutil, get_dynamic_ip_port)
00186 {
00187     uint16_t port = get_dynamic_ip_port();
00188     uint16_t port2 = get_dynamic_ip_port();
00189 
00190     EXPECT_TRUE(port != port2);
00191 }
00192 
00193 TEST_F(Testutil, int_to_hex_str)
00194 {
00195     char buf[2];
00196     int_to_hex_str(100, (char*)buf);
00197 
00198     EXPECT_TRUE(buf[0] == '6');
00199     EXPECT_TRUE(buf[1] == '4');
00200 }
00201