Gleb Klochkov / Mbed OS Climatcontroll_Main

Dependencies:   esp8266-driver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers test_util.cpp Source File

test_util.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 "CppUTest/TestHarness.h"
00018 #include "test_util.h"
00019 #include <string.h>
00020 #include "CellularUtil.h"
00021 
00022 using namespace mbed_cellular_util;
00023 
00024 Test_util::Test_util()
00025 {
00026 
00027 }
00028 
00029 Test_util::~Test_util()
00030 {
00031 }
00032 
00033 void Test_util::test_util_uint_to_binary_string()
00034 {
00035     char str[33];
00036     uint_to_binary_str(15, str, 33, 32);
00037     str[32] = '\0';
00038     // 15 is "1111" in binary but we ask all 32 bits so it should return "00000000000000000000000000001111"
00039     STRCMP_EQUAL("00000000000000000000000000001111", str);
00040 
00041     // test NULL pointer
00042     uint_to_binary_str(15, NULL, 0, 32);
00043 
00044     // test give too small buffer
00045     char too_small[5];
00046     uint_to_binary_str(15, too_small, 5, 6);
00047 }
00048 
00049 void Test_util::test_util_char_str_to_hex()
00050 {
00051     // basic conversion test, happy days
00052     char hex_buf[50];
00053     uint16_t number_of_hex_chars = char_str_to_hex_str("1234", 4, hex_buf);
00054     hex_buf[number_of_hex_chars] = '\0';
00055     STRCMP_EQUAL("31323334", hex_buf);
00056     LONGS_EQUAL(8, number_of_hex_chars);
00057 
00058     number_of_hex_chars = char_str_to_hex_str("wuhuu", 5, hex_buf);
00059     hex_buf[number_of_hex_chars] = '\0';
00060     STRCMP_EQUAL("7775687575", hex_buf);
00061     LONGS_EQUAL(10, number_of_hex_chars);
00062 
00063     // First don't omit the leading zero and then omit and check that leading zero is missing
00064     number_of_hex_chars = char_str_to_hex_str("\nwuhuu", 6, hex_buf);
00065     hex_buf[number_of_hex_chars] = '\0';
00066     STRCMP_EQUAL("0A7775687575", hex_buf);
00067     LONGS_EQUAL(12, number_of_hex_chars);
00068 
00069     number_of_hex_chars = char_str_to_hex_str("\nwuhuu", 6, hex_buf, true);
00070     hex_buf[number_of_hex_chars] = '\0';
00071     STRCMP_EQUAL("A7775687575", hex_buf);
00072     LONGS_EQUAL(11, number_of_hex_chars);
00073 
00074     // test giving a null pointer
00075     number_of_hex_chars = char_str_to_hex_str(NULL, 4, hex_buf);
00076     LONGS_EQUAL(0, number_of_hex_chars);
00077     number_of_hex_chars = char_str_to_hex_str("1234", 4, NULL);
00078     LONGS_EQUAL(0, number_of_hex_chars);
00079 }
00080 
00081 void Test_util::test_util_convert_ipv6()
00082 {
00083     // leading zeros omitted
00084     char ipv6[64];
00085     strncpy(ipv6, "1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1", 64);
00086     convert_ipv6(ipv6);
00087     STRCMP_EQUAL("101:101:101:101:101:101:101:101", ipv6);
00088     LONGS_EQUAL(31, strlen(ipv6));
00089 
00090     // some omitted and some not so much
00091     strncpy(ipv6, "255.1.120.2.244.12.55.45.201.110.11.2.233.154.85.96", 64);
00092     convert_ipv6(ipv6);
00093     STRCMP_EQUAL("FF01:7802:F40C:372D:C96E:B02:E99A:5560", ipv6);
00094     LONGS_EQUAL(38, strlen(ipv6));
00095 
00096     // test giving a null pointer
00097     convert_ipv6(NULL);
00098 }
00099 
00100 void Test_util::test_util_prefer_ipv6()
00101 {
00102     char tt[20] = "62.241.198.246";
00103     char temp[64] = "2001:14B8:1000:000:000:000:000:002";
00104 
00105     // not enough space to swap, arrays should stay the same
00106     prefer_ipv6(tt, sizeof(tt), temp, sizeof(temp));
00107     STRCMP_EQUAL("62.241.198.246", tt);
00108     STRCMP_EQUAL("2001:14B8:1000:000:000:000:000:002", temp);
00109 
00110     // should swap as first one was ip4 and later was ipv6 and enough space
00111     char tt2[64] = "62.241.198.246";
00112     prefer_ipv6(tt2, sizeof(tt2), temp, sizeof(temp));
00113     STRCMP_EQUAL("62.241.198.246", temp);
00114     STRCMP_EQUAL("2001:14B8:1000:000:000:000:000:002", tt2);
00115 }
00116 
00117 void Test_util::test_util_separate_ip_addresses()
00118 {
00119     char* s = (char*)malloc(128);
00120 
00121     char ip[64] = {0};
00122     char subnet[64] = {0};
00123 
00124     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", 94);
00125     separate_ip_addresses(s, ip, sizeof(ip), subnet, sizeof(subnet));
00126     STRCMP_EQUAL("2001:14BB:170:8BF5:FB88:E86E:7B33:E68A", ip);
00127     STRCMP_EQUAL("001:203:405:607:809:A0B:C0D:E0F", subnet);
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", 94);
00130     separate_ip_addresses(s, ip, sizeof(ip), subnet, sizeof(subnet));
00131     STRCMP_EQUAL("32:1:20:187:1:112:139:245:251:136:232:110:123:51:230:138", ip);
00132     STRCMP_EQUAL("0:1:2:3:4:5:6:7:8:9:10:11:12:13:14:15", subnet);
00133 
00134     ip[0] = '\0';
00135     subnet[0] = '\0';
00136     strncpy(s, "1.2.3.4\0", 8);
00137     separate_ip_addresses(s, ip, sizeof(ip), subnet, sizeof(subnet));
00138     STRCMP_EQUAL("1.2.3.4", ip);
00139     STRCMP_EQUAL("", subnet);
00140 
00141     ip[0] = '\0';
00142     subnet[0] = '\0';
00143     strncpy(s, "1.2.3.4.5.6.7.8\0", 16);
00144     separate_ip_addresses(s, ip, sizeof(ip), subnet, sizeof(subnet));
00145     STRCMP_EQUAL("1.2.3.4", ip);
00146     STRCMP_EQUAL("5.6.7.8", subnet);
00147 
00148     ip[0] = '\0';
00149     subnet[0] = '\0';
00150     strncpy(s, "1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16\0", 39);
00151     separate_ip_addresses(s, ip, sizeof(ip), subnet, sizeof(subnet));
00152     STRCMP_EQUAL("102:304:506:708:90A:B0C:D0E:F10", ip);
00153     STRCMP_EQUAL("", subnet);
00154 
00155     ip[0] = '\0';
00156     subnet[0] = '\0';
00157     strncpy(s, "32:1:20:187:1:112:139:245:251:136:232:110:123:51:230:138\0", 57);
00158     separate_ip_addresses(s, ip, sizeof(ip), subnet, sizeof(subnet));
00159     STRCMP_EQUAL("32:1:20:187:1:112:139:245:251:136:232:110:123:51:230:138", ip);
00160     STRCMP_EQUAL("", subnet);
00161 
00162     ip[0] = '\0';
00163     subnet[0] = '\0';
00164     strncpy(s, "1.2.3.4 32:1:20:187:1:112:139:245:251:136:232:110:123:51:230:138\0", 65);
00165     separate_ip_addresses(s, ip, sizeof(ip), subnet, sizeof(subnet));
00166     STRCMP_EQUAL("1.2.3.4", ip);
00167     STRCMP_EQUAL("32:1:20:187:1:112:139:245:251:136:232:110:123:51:230:138", subnet);
00168 
00169     ip[0] = '\0';
00170     subnet[0] = '\0';
00171     strncpy(s, "1.2.3.4 5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20\0", 51);
00172     separate_ip_addresses(s, ip, sizeof(ip), subnet, sizeof(subnet));
00173     STRCMP_EQUAL("1.2.3.4", ip);
00174     STRCMP_EQUAL("506:708:90A:B0C:D0E:F10:1112:1314", subnet);
00175     STRCMP_EQUAL("1.2.3.4 5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20", s);
00176 
00177 }