1 /*
2 	bctoolbox
3     Copyright (C) 2017  Belledonne Communications SARL
4 
5 
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include <stdio.h>
21 #include <inttypes.h>
22 #include "bctoolbox_tester.h"
23 #include "bctoolbox/port.h"
24 
bytes_to_from_hexa_strings(void)25 static void bytes_to_from_hexa_strings(void) {
26 	const uint8_t a55aBytes[2] = {0xa5, 0x5a};
27 	const uint8_t a55aString[5] = "a55a";
28 	const uint8_t upBytes[8] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
29 	const uint8_t upString[17] = "0123456789abcdef";
30 	const uint8_t downBytes[8] = {0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10};
31 	const uint8_t downString[17] = "fedcba9876543210";
32 	uint8_t outputBytes[16];
33 	uint8_t outputString[16];
34 
35 	BC_ASSERT_EQUAL(bctbx_char_to_byte("1"[0]), 1, uint8_t, "%d");
36 	BC_ASSERT_EQUAL(bctbx_char_to_byte("5"[0]), 5, uint8_t, "%d");
37 	BC_ASSERT_EQUAL(bctbx_char_to_byte("a"[0]), 10, uint8_t, "%d");
38 	BC_ASSERT_EQUAL(bctbx_char_to_byte("e"[0]), 14, uint8_t, "%d");
39 	BC_ASSERT_EQUAL(bctbx_char_to_byte("B"[0]), 11, uint8_t, "%d");
40 	BC_ASSERT_EQUAL(bctbx_char_to_byte("F"[0]), 15, uint8_t, "%d");
41 
42 	BC_ASSERT_EQUAL(bctbx_byte_to_char(0), "0"[0], char, "%c");
43 	BC_ASSERT_EQUAL(bctbx_byte_to_char(2), "2"[0], char, "%c");
44 	BC_ASSERT_EQUAL(bctbx_byte_to_char(5), "5"[0], char, "%c");
45 	BC_ASSERT_EQUAL(bctbx_byte_to_char(0x0a), "a"[0], char, "%c");
46 	BC_ASSERT_EQUAL(bctbx_byte_to_char(0x0c), "c"[0], char, "%c");
47 	BC_ASSERT_EQUAL(bctbx_byte_to_char(0x0e), "e"[0], char, "%c");
48 
49 	bctbx_str_to_uint8(outputBytes, a55aString, 4);
50 	BC_ASSERT_NSTRING_EQUAL(outputBytes, a55aBytes, 2);
51 	bctbx_str_to_uint8(outputBytes, upString, 16);
52 	BC_ASSERT_NSTRING_EQUAL(outputBytes, upBytes, 8);
53 	bctbx_str_to_uint8(outputBytes, downString, 16);
54 	BC_ASSERT_NSTRING_EQUAL(outputBytes, downBytes, 8);
55 
56 	bctbx_int8_to_str(outputString, a55aBytes, 2);
57 	BC_ASSERT_NSTRING_EQUAL(outputString, a55aString, 4);
58 	bctbx_int8_to_str(outputString, upBytes, 8);
59 	BC_ASSERT_NSTRING_EQUAL(outputString, upString, 16);
60 	bctbx_int8_to_str(outputString, downBytes, 8);
61 	BC_ASSERT_NSTRING_EQUAL(outputString, downString, 16);
62 
63 	bctbx_uint32_to_str(outputString, 0x5aa5c376);
64 	BC_ASSERT_NSTRING_EQUAL(outputString, "5aa5c376", 8);
65 	bctbx_uint32_to_str(outputString, 0x01234567);
66 	BC_ASSERT_NSTRING_EQUAL(outputString, "01234567", 8);
67 	bctbx_uint32_to_str(outputString, 0xfedcba98);
68 	BC_ASSERT_NSTRING_EQUAL(outputString, "fedcba98", 8);
69 
70 	BC_ASSERT_EQUAL(bctbx_str_to_uint32("5aa5c376"), 0x5aa5c376, uint32_t, "0x%08x");
71 	BC_ASSERT_EQUAL(bctbx_str_to_uint32("01234567"), 0x01234567, uint32_t, "0x%08x");
72 	BC_ASSERT_EQUAL(bctbx_str_to_uint32("fedcba98"), 0xfedcba98, uint32_t, "0x%08x");
73 
74 	bctbx_uint64_to_str(outputString, 0xfa5c37643cde8de0);
75 	BC_ASSERT_NSTRING_EQUAL(outputString, "fa5c37643cde8de0", 16);
76 	bctbx_uint64_to_str(outputString, 0x0123456789abcdef);
77 	BC_ASSERT_NSTRING_EQUAL(outputString, "0123456789abcdef", 16);
78 	bctbx_uint64_to_str(outputString, 0xfedcba9876543210);
79 	BC_ASSERT_NSTRING_EQUAL(outputString, "fedcba9876543210", 16);
80 
81 	BC_ASSERT_EQUAL(bctbx_str_to_uint64("fa5c37643cde8de0"), 0xfa5c37643cde8de0, uint64_t, "0x%" PRIx64);
82 	BC_ASSERT_EQUAL(bctbx_str_to_uint64("0123456789abcdef"), 0x0123456789abcdef, uint64_t, "0x%" PRIx64);
83 	BC_ASSERT_EQUAL(bctbx_str_to_uint64("fedcba9876543210"), 0xfedcba9876543210, uint64_t, "0x%" PRIx64);
84 }
85 
time_functions(void)86 static void time_functions(void) {
87 	bctoolboxTimeSpec testTs;
88 	bctoolboxTimeSpec y2k,monday6Feb2017;
89 	y2k.tv_sec = 946684800;
90 	y2k.tv_nsec = 123456789;
91 	monday6Feb2017.tv_sec = 1486347823;
92 	monday6Feb2017.tv_nsec = 0;
93 
94 	memcpy(&testTs, &y2k, sizeof(bctoolboxTimeSpec));
95 	BC_ASSERT_EQUAL(bctbx_timespec_compare(&y2k, &testTs), 0, int, "%d");
96 	bctbx_timespec_add(&testTs, 604800);
97 	BC_ASSERT_EQUAL(testTs.tv_sec, y2k.tv_sec+604800, int64_t, "%" PRIi64);
98 	BC_ASSERT_EQUAL(testTs.tv_nsec, y2k.tv_nsec, int64_t, "%" PRIi64);
99 	BC_ASSERT_TRUE(bctbx_timespec_compare(&y2k, &testTs)<0);
100 
101 	memcpy(&testTs, &y2k, sizeof(bctoolboxTimeSpec));
102 	bctbx_timespec_add(&testTs, -604800);
103 	BC_ASSERT_EQUAL(testTs.tv_sec, y2k.tv_sec-604800, int64_t, "%" PRIi64);
104 	BC_ASSERT_EQUAL(testTs.tv_nsec, y2k.tv_nsec, int64_t, "%" PRIi64);
105 	BC_ASSERT_TRUE(bctbx_timespec_compare(&y2k, &testTs)>0);
106 
107 	memcpy(&testTs, &y2k, sizeof(bctoolboxTimeSpec));
108 	bctbx_timespec_add(&testTs, -946684801);
109 	BC_ASSERT_EQUAL(testTs.tv_sec, 0, int64_t, "%" PRIi64);
110 	BC_ASSERT_EQUAL(testTs.tv_nsec, 0, int64_t, "%" PRIi64);
111 
112 	/* test the get utc time function
113 	 * there is no easy way to ensure we get the correct time, just check it is at least not the time from last boot,
114 	 * check it is greater than the current time as this test was written(6feb2017) */
115 	bctbx_get_utc_cur_time(&testTs);
116 	BC_ASSERT_TRUE(bctbx_timespec_compare(&testTs, &monday6Feb2017)>0);
117 
118 	BC_ASSERT_EQUAL(bctbx_time_string_to_sec(NULL), 0, uint32_t, "%d");
119 	BC_ASSERT_EQUAL(bctbx_time_string_to_sec(""), 0, uint32_t, "%d");
120 	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("0"), 0, uint32_t, "%d");
121 	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("1500"), 1500, uint32_t, "%d");
122 	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("2500s"), 2500, uint32_t, "%d");
123 	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("10m"), 600, uint32_t, "%d");
124 	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("5h"), 5*3600, uint32_t, "%d");
125 	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("2d"), 2*24*3600, uint32_t, "%d");
126 	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("3W"), 3*7*24*3600, uint32_t, "%d");
127 	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("6M"), 6*30*24*3600, uint32_t, "%d");
128 	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("7Y"), 7*365*24*3600, uint32_t, "%d");
129 	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("7Y6M2W"), (7*365+6*30+2*7)*24*3600, uint32_t, "%d");
130 	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("2m30"), 2*60+30, uint32_t, "%d");
131 	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("15d1M"), (15+1*30)*24*3600, uint32_t, "%d");
132 	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("15d5z"), 15*24*3600, uint32_t, "%d");
133 	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("15dM12h"), (15*24+12)*3600, uint32_t, "%d");
134 }
135 
136 static test_t utils_tests[] = {
137 	TEST_NO_TAG("Bytes to/from Hexa strings", bytes_to_from_hexa_strings),
138 	TEST_NO_TAG("Time", time_functions)
139 };
140 
141 test_suite_t utils_test_suite = {"Utils", NULL, NULL, NULL, NULL,
142 							   sizeof(utils_tests) / sizeof(utils_tests[0]), utils_tests};
143