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