eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

test-util.c (6459B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2014-2019  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #ifdef HAVE_CONFIG_H
     24 #include <config.h>
     25 #endif
     26 
     27 #include <stdio.h>
     28 #include <string.h>
     29 #include <assert.h>
     30 #include <netinet/in.h>
     31 #include <arpa/inet.h>
     32 #include <ell/ell.h>
     33 
     34 #include "src/defs.h"
     35 #include "src/util.h"
     36 
     37 struct ssid_test_data {
     38 	size_t len;
     39 	uint8_t ssid[SSID_MAX_SIZE];
     40 	const char *string;
     41 	bool result;
     42 };
     43 
     44 const struct ssid_test_data ssid_samples[] = {
     45 	{ 0, { }, "", true },
     46 	{ 1, { }, "", true },
     47 	{ 32, { }, "", true },
     48 	{ 33, { }, "", true },
     49 	{ 33, { 'a', 'b', 'c', }, "", true },
     50 	{ 42, { }, "", true },
     51 	{ 3, { 'f', 'o', 'o', }, "foo", true },
     52 	{ 3, { }, "", true },
     53 	{ 3, { 'f', 'o', 'o', }, "bar", false },
     54 	{ 3, { 'f', 'o', 'o', 0xff, }, "foo", true },
     55 	{ 5, { 'f', 'o', 'o', 'b', 'a', 'r' }, "fooba", true },
     56 	{ 5, { 'f', 'o', 'o', 'b', 'a', 'r' }, "foobar", false },
     57 	{ 5, { 'f', 'o', 'o', 'b', 'a', 'r' }, "oobar", false },
     58 	{ 4, { 'x', 'y', 'z', 0xff }, "xyz�", true },
     59 	{ 6, { 'x', 'y', 'z', 0xff, '1', '2' }, "xyz�12", true },
     60 	{ 7, { 0xf0, 0xf3, '3', '4', '5', '6', '7', }, "��34567", true },
     61 	{ 6, { 0xc3, 0x96, '3', '4', '5', '6' }, "Ö3456", true },
     62 	{ 6, { '1', 0xc3, 0x96, '4', '5', '6' }, "1Ö456", true },
     63 	{ 6, { '1', '2', '3', '4', 0xc3, 0x96 }, "1234Ö", true },
     64 };
     65 
     66 static void ssid_to_utf8(const void *data)
     67 {
     68 	const struct ssid_test_data *ssid = data;
     69 	int i = 0, samples = L_ARRAY_SIZE(ssid_samples);
     70 
     71 	while (i < samples) {
     72 		const char *result = util_ssid_to_utf8(ssid[i].len,
     73 						ssid[i].ssid);
     74 
     75 		assert(!memcmp(ssid[i].string, result,
     76 				strlen(ssid[i].string)) == ssid[i].result);
     77 
     78 		i++;
     79 	}
     80 
     81 }
     82 
     83 static const char ms_id[] = "domain\\user";
     84 static const char id[] = "user@domain";
     85 static const char no_separator[] = "username";
     86 
     87 static void get_domain_test(const void *data)
     88 {
     89 	const char *test;
     90 
     91 	test = util_get_domain(ms_id);
     92 
     93 	assert(strcmp(test, "domain") == 0);
     94 
     95 	test = util_get_domain(id);
     96 
     97 	assert(strcmp(test, "domain") == 0);
     98 
     99 	test = util_get_domain(no_separator);
    100 
    101 	assert(strcmp(test, "username") == 0);
    102 }
    103 
    104 static void get_username_test(const void *data)
    105 {
    106 	const char *test;
    107 
    108 	test = util_get_username(ms_id);
    109 
    110 	assert(strcmp(test, "user") == 0);
    111 
    112 	test = util_get_username(id);
    113 
    114 	assert(strcmp(test, "user") == 0);
    115 
    116 	test = util_get_username(no_separator);
    117 
    118 	assert(strcmp(test, "username") == 0);
    119 }
    120 
    121 static void ip_prefix_test(const void *data)
    122 {
    123 	unsigned int i;
    124 	char *invalid[] = {
    125 		"192.168.0.0", /* Not prefix notation */
    126 		"192.168./22", /* Incomplete notation */
    127 		"192.168.0.1/255", /* Too long prefix */
    128 		"192.168.0.1/0", /* Too short prefix */
    129 		"192.168.0.1/16", /* Invalid prefix */
    130 		"192.168.1.2.3/24", /* IP too long */
    131 		"192.168.111.222.333.444/20", /* IP way too long */
    132 	};
    133 
    134 	struct {
    135 		char *ip_prefix;
    136 		uint8_t prefix;
    137 		char *start;
    138 		char *end;
    139 		char *mask;
    140 	} valid[] = {
    141 		{"192.168.80.0/22", 22, "192.168.80.1",
    142 				"192.168.83.254", "255.255.252.0"},
    143 		{"192.168.128.0/20", 20, "192.168.128.1",
    144 				"192.168.143.254", "255.255.240.0"},
    145 		{"192.168.0.0/25", 25, "192.168.0.1",
    146 				"192.168.0.126", "255.255.255.128"},
    147 		{"192.168.0.0/29", 29, "192.168.0.1",
    148 				"192.168.0.6", "255.255.255.248"},
    149 		{"192.168.0.128/25", 25, "192.168.0.129",
    150 				"192.168.0.254", "255.255.255.128"},
    151 		/* Valid notation which is maximum length */
    152 		{"192.168.111.108/30", 30, "192.168.111.109",
    153 				"192.168.111.110", "255.255.255.252"},
    154 	};
    155 
    156 	for (i = 0; i < L_ARRAY_SIZE(invalid); i++)
    157 		assert(!util_ip_prefix_tohl(invalid[i], NULL, NULL,
    158 						NULL, NULL));
    159 
    160 	for (i = 0; i < L_ARRAY_SIZE(valid); i++) {
    161 		uint8_t prefix;
    162 		uint32_t start;
    163 		uint32_t end;
    164 		uint32_t mask;
    165 		struct in_addr ia;
    166 		char ip[INET_ADDRSTRLEN];
    167 
    168 		assert(util_ip_prefix_tohl(valid[i].ip_prefix,
    169 						&prefix, &start, &end, &mask));
    170 
    171 		assert(valid[i].prefix == prefix);
    172 
    173 		ia.s_addr = htonl(start);
    174 		assert(inet_ntop(AF_INET, &ia, ip, INET_ADDRSTRLEN));
    175 		assert(strcmp(ip, valid[i].start) == 0);
    176 
    177 		ia.s_addr = htonl(end);
    178 		assert(inet_ntop(AF_INET, &ia, ip, INET_ADDRSTRLEN));
    179 		assert(strcmp(ip, valid[i].end) == 0);
    180 
    181 		ia.s_addr = htonl(mask);
    182 		assert(inet_ntop(AF_INET, &ia, ip, INET_ADDRSTRLEN));
    183 		assert(strcmp(ip, valid[i].mask) == 0);
    184 	}
    185 }
    186 
    187 struct linear_map_case {
    188 	double value;
    189 	double a_start;
    190 	double a_end;
    191 	double b_start;
    192 	double b_end;
    193 	double expected;
    194 
    195 	bool fails : 1;
    196 };
    197 
    198 struct linear_map_case cases[] = {
    199 	/* Value out of range */
    200 	{
    201 		.value = 100,
    202 		.a_start = 1,
    203 		.a_end = 3,
    204 		.b_start = 10,
    205 		.b_end = 12,
    206 		.fails = true,
    207 	},
    208 	/* Ascending start range */
    209 	{
    210 		.value = 1,
    211 		.a_start = 0,
    212 		.a_end = 2,
    213 		.b_start = 10,
    214 		.b_end = 12,
    215 		.expected = 11,
    216 	},
    217 	/* Descending start range */
    218 	{
    219 		.value = 1,
    220 		.a_start = 2,
    221 		.a_end = 0,
    222 		.b_start = 10,
    223 		.b_end = 12,
    224 		.expected = 11,
    225 	},
    226 	/* Negatives */
    227 	{
    228 		.value = -1,
    229 		.a_start = -2,
    230 		.a_end = 0,
    231 		.b_start = 0,
    232 		.b_end = 2,
    233 		.expected = 1,
    234 	},
    235 };
    236 
    237 static void test_linear_map(const void *data)
    238 {
    239 	double mapped;
    240 	unsigned int i;
    241 
    242 	for (i = 0; i < L_ARRAY_SIZE(cases); i++) {
    243 		struct linear_map_case *c = &cases[i];
    244 		bool ret = util_linear_map(c->value, c->a_start, c->a_end,
    245 						c->b_start, c->b_end, &mapped);
    246 		if (c->fails) {
    247 			assert(ret == false);
    248 			continue;
    249 		}
    250 
    251 		assert(ret);
    252 		assert(mapped == c->expected);
    253 	}
    254 }
    255 
    256 int main(int argc, char *argv[])
    257 {
    258 	l_test_init(&argc, &argv);
    259 
    260 	l_test_add("/util/ssid_to_utf8/", ssid_to_utf8, ssid_samples);
    261 	l_test_add("/util/get_domain/", get_domain_test, NULL);
    262 	l_test_add("/util/get_username/", get_username_test, NULL);
    263 	l_test_add("/util/ip_prefix/", ip_prefix_test, NULL);
    264 	l_test_add("/util/test_linear_map", test_linear_map, NULL);
    265 
    266 	return l_test_run();
    267 }