port.c 8.48 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
	bctoolbox
    Copyright (C) 2017  Belledonne Communications SARL


    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
21
#include <inttypes.h>
22 23 24
#include "bctoolbox_tester.h"
#include "bctoolbox/port.h"

25
static void bytes_to_from_hexa_strings(void) {
26 27 28 29 30 31 32 33 34
	const uint8_t a55aBytes[2] = {0xa5, 0x5a};
	const uint8_t a55aString[5] = "a55a";
	const uint8_t upBytes[8] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
	const uint8_t upString[17] = "0123456789abcdef";
	const uint8_t downBytes[8] = {0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10};
	const uint8_t downString[17] = "fedcba9876543210";
	uint8_t outputBytes[16];
	uint8_t outputString[16];

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
	BC_ASSERT_EQUAL(bctbx_char_to_byte("1"[0]), 1, uint8_t, "%d");
	BC_ASSERT_EQUAL(bctbx_char_to_byte("5"[0]), 5, uint8_t, "%d");
	BC_ASSERT_EQUAL(bctbx_char_to_byte("a"[0]), 10, uint8_t, "%d");
	BC_ASSERT_EQUAL(bctbx_char_to_byte("e"[0]), 14, uint8_t, "%d");
	BC_ASSERT_EQUAL(bctbx_char_to_byte("B"[0]), 11, uint8_t, "%d");
	BC_ASSERT_EQUAL(bctbx_char_to_byte("F"[0]), 15, uint8_t, "%d");

	BC_ASSERT_EQUAL(bctbx_byte_to_char(0), "0"[0], char, "%c");
	BC_ASSERT_EQUAL(bctbx_byte_to_char(2), "2"[0], char, "%c");
	BC_ASSERT_EQUAL(bctbx_byte_to_char(5), "5"[0], char, "%c");
	BC_ASSERT_EQUAL(bctbx_byte_to_char(0x0a), "a"[0], char, "%c");
	BC_ASSERT_EQUAL(bctbx_byte_to_char(0x0c), "c"[0], char, "%c");
	BC_ASSERT_EQUAL(bctbx_byte_to_char(0x0e), "e"[0], char, "%c");

	bctbx_str_to_uint8(outputBytes, a55aString, 4);
50
	BC_ASSERT_NSTRING_EQUAL((char *)outputBytes, (char *)a55aBytes, 2);
51
	bctbx_str_to_uint8(outputBytes, upString, 16);
52
	BC_ASSERT_NSTRING_EQUAL((char *)outputBytes, (char *)upBytes, 8);
53
	bctbx_str_to_uint8(outputBytes, downString, 16);
54
	BC_ASSERT_NSTRING_EQUAL((char *)outputBytes, (char *)downBytes, 8);
55

56
	bctbx_int8_to_str(outputString, a55aBytes, 2);
57
	BC_ASSERT_NSTRING_EQUAL((char *)outputString, (char *)a55aString, 4);
58
	bctbx_int8_to_str(outputString, upBytes, 8);
59
	BC_ASSERT_NSTRING_EQUAL((char *)outputString, (char *)upString, 16);
60
	bctbx_int8_to_str(outputString, downBytes, 8);
61
	BC_ASSERT_NSTRING_EQUAL((char *)outputString, (char *)downString, 16);
62

63
	bctbx_uint32_to_str(outputString, 0x5aa5c376);
64
	BC_ASSERT_NSTRING_EQUAL((char *)outputString, "5aa5c376", 8);
65
	bctbx_uint32_to_str(outputString, 0x01234567);
66
	BC_ASSERT_NSTRING_EQUAL((char *)outputString, "01234567", 8);
67
	bctbx_uint32_to_str(outputString, 0xfedcba98);
68
	BC_ASSERT_NSTRING_EQUAL((char *)outputString, "fedcba98", 8);
69

70 71 72
	BC_ASSERT_EQUAL(bctbx_str_to_uint32((uint8_t *)"5aa5c376"), 0x5aa5c376, uint32_t, "0x%08x");
	BC_ASSERT_EQUAL(bctbx_str_to_uint32((uint8_t *)"01234567"), 0x01234567, uint32_t, "0x%08x");
	BC_ASSERT_EQUAL(bctbx_str_to_uint32((uint8_t *)"fedcba98"), 0xfedcba98, uint32_t, "0x%08x");
73

74
	bctbx_uint64_to_str(outputString, 0xfa5c37643cde8de0);
75
	BC_ASSERT_NSTRING_EQUAL((char *)outputString, "fa5c37643cde8de0", 16);
76
	bctbx_uint64_to_str(outputString, 0x0123456789abcdef);
77
	BC_ASSERT_NSTRING_EQUAL((char *)outputString, "0123456789abcdef", 16);
78
	bctbx_uint64_to_str(outputString, 0xfedcba9876543210);
79
	BC_ASSERT_NSTRING_EQUAL((char *)outputString, "fedcba9876543210", 16);
80

81 82 83
	BC_ASSERT_EQUAL(bctbx_str_to_uint64((uint8_t *)"fa5c37643cde8de0"), 0xfa5c37643cde8de0, uint64_t, "0x%" PRIx64);
	BC_ASSERT_EQUAL(bctbx_str_to_uint64((uint8_t *)"0123456789abcdef"), 0x0123456789abcdef, uint64_t, "0x%" PRIx64);
	BC_ASSERT_EQUAL(bctbx_str_to_uint64((uint8_t *)"fedcba9876543210"), 0xfedcba9876543210, uint64_t, "0x%" PRIx64);
84 85
}

86
static void time_functions(void) {
johan's avatar
johan committed
87
	bctoolboxTimeSpec testTs;
johan's avatar
johan committed
88
	bctoolboxTimeSpec y2k,monday6Feb2017;
johan's avatar
johan committed
89 90
	y2k.tv_sec = 946684800;
	y2k.tv_nsec = 123456789;
johan's avatar
johan committed
91 92
	monday6Feb2017.tv_sec = 1486347823;
	monday6Feb2017.tv_nsec = 0;
johan's avatar
johan committed
93 94 95 96

	memcpy(&testTs, &y2k, sizeof(bctoolboxTimeSpec));
	BC_ASSERT_EQUAL(bctbx_timespec_compare(&y2k, &testTs), 0, int, "%d");
	bctbx_timespec_add(&testTs, 604800);
97 98
	BC_ASSERT_EQUAL(testTs.tv_sec, y2k.tv_sec+604800, int64_t, "%" PRIi64);
	BC_ASSERT_EQUAL(testTs.tv_nsec, y2k.tv_nsec, int64_t, "%" PRIi64);
johan's avatar
johan committed
99 100 101 102
	BC_ASSERT_TRUE(bctbx_timespec_compare(&y2k, &testTs)<0);

	memcpy(&testTs, &y2k, sizeof(bctoolboxTimeSpec));
	bctbx_timespec_add(&testTs, -604800);
103 104
	BC_ASSERT_EQUAL(testTs.tv_sec, y2k.tv_sec-604800, int64_t, "%" PRIi64);
	BC_ASSERT_EQUAL(testTs.tv_nsec, y2k.tv_nsec, int64_t, "%" PRIi64);
johan's avatar
johan committed
105 106 107 108
	BC_ASSERT_TRUE(bctbx_timespec_compare(&y2k, &testTs)>0);

	memcpy(&testTs, &y2k, sizeof(bctoolboxTimeSpec));
	bctbx_timespec_add(&testTs, -946684801);
109 110
	BC_ASSERT_EQUAL(testTs.tv_sec, 0, int64_t, "%" PRIi64);
	BC_ASSERT_EQUAL(testTs.tv_nsec, 0, int64_t, "%" PRIi64);
johan's avatar
johan committed
111 112 113 114 115 116 117

	/* test the get utc time function
	 * there is no easy way to ensure we get the correct time, just check it is at least not the time from last boot,
	 * check it is greater than the current time as this test was written(6feb2017) */
	bctbx_get_utc_cur_time(&testTs);
	BC_ASSERT_TRUE(bctbx_timespec_compare(&testTs, &monday6Feb2017)>0);

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
	BC_ASSERT_EQUAL(bctbx_time_string_to_sec(NULL), 0, uint32_t, "%d");
	BC_ASSERT_EQUAL(bctbx_time_string_to_sec(""), 0, uint32_t, "%d");
	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("0"), 0, uint32_t, "%d");
	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("1500"), 1500, uint32_t, "%d");
	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("2500s"), 2500, uint32_t, "%d");
	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("10m"), 600, uint32_t, "%d");
	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("5h"), 5*3600, uint32_t, "%d");
	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("2d"), 2*24*3600, uint32_t, "%d");
	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("3W"), 3*7*24*3600, uint32_t, "%d");
	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("6M"), 6*30*24*3600, uint32_t, "%d");
	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("7Y"), 7*365*24*3600, uint32_t, "%d");
	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("7Y6M2W"), (7*365+6*30+2*7)*24*3600, uint32_t, "%d");
	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("2m30"), 2*60+30, uint32_t, "%d");
	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("15d1M"), (15+1*30)*24*3600, uint32_t, "%d");
	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("15d5z"), 15*24*3600, uint32_t, "%d");
	BC_ASSERT_EQUAL(bctbx_time_string_to_sec("15dM12h"), (15*24+12)*3600, uint32_t, "%d");
johan's avatar
johan committed
134
}
135

136 137 138 139 140
static void bctbx_addrinfo_sort_test(void) {
	struct addrinfo * res1 = bctbx_name_to_addrinfo(AF_INET6, SOCK_DGRAM, "sip3.linphone.org", 27256);
	struct addrinfo * res2 = bctbx_ip_address_to_addrinfo(AF_INET6, SOCK_DGRAM, "91.121.209.194", 27256);
	struct addrinfo * res3 = bctbx_ip_address_to_addrinfo(AF_INET, SOCK_DGRAM, "91.121.209.194", 27256);
	bool_t searching_for_v6=TRUE;
jehan's avatar
jehan committed
141
	struct addrinfo * ai =  NULL;
142 143
	char printable_ip[256];
	struct addrinfo * res = res3;
jehan's avatar
jehan committed
144 145
	
	for ( ai=res2 ; ai !=NULL; ai=ai->ai_next) {
146 147 148 149 150 151 152 153 154
		if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6*)(ai->ai_addr))->sin6_addr)) {
			res->ai_next=ai;
			break;
		}
	}
	res->ai_next->ai_next=res1;
	res->ai_next->ai_next->ai_next=NULL;
	
	//So now, res as ipv4 first, then v4 mapped, then v6
jehan's avatar
jehan committed
155
	for ( ai=res ; ai !=NULL; ai=ai->ai_next) {
156 157 158 159 160 161
		bctbx_addrinfo_to_printable_ip_address(ai, printable_ip, sizeof(printable_ip));
		bctbx_message("bctbx_getaddrinfo origin address:%s", printable_ip);
	}
	
	//now apply bctbx_addrinfo_sort
	
jehan's avatar
jehan committed
162
	for ( ai=bctbx_addrinfo_sort(res) ; ai !=NULL; ai=ai->ai_next) {
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
		if (ai->ai_family == AF_INET6) {
			if (!searching_for_v6) {
				BC_ASSERT_FALSE(IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6*)(ai->ai_addr))->sin6_addr));
			}
			if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6*)(ai->ai_addr))->sin6_addr)) {
				searching_for_v6 = FALSE; // stating from now, all subsequent IP shall be pure V6
			}
		}
		bctbx_addrinfo_to_printable_ip_address(ai, printable_ip, sizeof(printable_ip));
		bctbx_message("bctbx_getaddrinfo sorted address:%s", printable_ip);
		
	}
	
	bctbx_freeaddrinfo(res);
}
178
static test_t utils_tests[] = {
179
	TEST_NO_TAG("Bytes to/from Hexa strings", bytes_to_from_hexa_strings),
180 181
	TEST_NO_TAG("Time", time_functions),
	TEST_NO_TAG("Addrinfo sort", bctbx_addrinfo_sort_test)
182 183 184 185
};

test_suite_t utils_test_suite = {"Utils", NULL, NULL, NULL, NULL,
							   sizeof(utils_tests) / sizeof(utils_tests[0]), utils_tests};