From e67d91904853fd8a28585b2c19bf5a43522994da Mon Sep 17 00:00:00 2001 From: Patrik Flykt Date: Fri, 19 Dec 2014 13:25:43 +0200 Subject: [PATCH] unit: Create unit test for SSID UTF8 pretty-printing Write a set of tests that check correct and incorrect UTF8 sequences at the beginning, middle and end of a byte array. Also verify an all zeros (hidden) SSID of varying length. --- Makefile.am | 8 +++- unit/test-ssid-to-utf8.c | 87 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 unit/test-ssid-to-utf8.c diff --git a/Makefile.am b/Makefile.am index 5b861847..dbd18537 100644 --- a/Makefile.am +++ b/Makefile.am @@ -70,7 +70,8 @@ noinst_PROGRAMS = tools/hwsim tools_hwsim_LDADD = ell/libell-internal.la unit_tests = unit/test-pbkdf2-sha1 unit/test-prf-sha1 unit/test-ie \ - unit/test-crypto unit/test-mpdu unit/test-eapol + unit/test-crypto unit/test-mpdu unit/test-eapol \ + unit/test-ssid-to-utf8 if MAINTAINER_MODE noinst_PROGRAMS += $(unit_tests) @@ -99,6 +100,11 @@ unit_test_mpdu_LDADD = ell/libell-internal.la unit_test_eapol_SOURCES = unit/test-eapol.c \ src/eapol.h src/eapol.c unit_test_eapol_LDADD = ell/libell-internal.la + +unit_test_ssid_to_utf8_SOURCES = src/util.h src/util.c \ + unit/test-ssid-to-utf8.c +unit_test_ssid_to_utf8_LDADD = ell/libell-internal.la + TESTS = $(unit_tests) manual_pages = doc/iwmon.1 diff --git a/unit/test-ssid-to-utf8.c b/unit/test-ssid-to-utf8.c new file mode 100644 index 00000000..4978aede --- /dev/null +++ b/unit/test-ssid-to-utf8.c @@ -0,0 +1,87 @@ +/* + * + * Wireless daemon for Linux + * + * Copyright (C) 2014 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 +#endif + +#include +#include +#include +#include + +#include "src/util.h" + +struct ssid_test_data { + size_t len; + uint8_t ssid[32]; + 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 = sizeof(ssid_samples) / sizeof(ssid_samples[0]); + + 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++; + } + +} + +int main(int argc, char *argv[]) +{ + l_test_init(&argc, &argv); + + l_test_add("/util/ssid_to_utf8/", ssid_to_utf8, ssid_samples); + + return l_test_run(); +}