LCOV - code coverage report
Current view: top level - src/utils - utils_module_tests.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1401872338 Lines: 106 146 72.6 %
Date: 2014-06-04 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /*
       2             :  * utils module tests
       3             :  * Copyright (c) 2014, Jouni Malinen <j@w1.fi>
       4             :  *
       5             :  * This software may be distributed under the terms of the BSD license.
       6             :  * See README for more details.
       7             :  */
       8             : 
       9             : #include "utils/includes.h"
      10             : 
      11             : #include "utils/common.h"
      12             : #include "utils/bitfield.h"
      13             : #include "utils/ext_password.h"
      14             : #include "utils/trace.h"
      15             : 
      16             : 
      17             : struct printf_test_data {
      18             :         u8 *data;
      19             :         size_t len;
      20             :         char *encoded;
      21             : };
      22             : 
      23             : static const struct printf_test_data printf_tests[] = {
      24             :         { (u8 *) "abcde", 5, "abcde" },
      25             :         { (u8 *) "a\0b\nc\ed\re\tf\"\\", 13, "a\\0b\\nc\\ed\\re\\tf\\\"\\\\" },
      26             :         { (u8 *) "\x00\x31\x00\x32\x00\x39", 6, "\\x001\\0002\\09" },
      27             :         { (u8 *) "\n\n\n", 3, "\n\12\x0a" },
      28             :         { (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
      29             :           "\\xc3\\xa5\xc3\\xa4\\xc3\\xb6\\xc3\\x85\\xc3\\x84\\xc3\\x96" },
      30             :         { (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
      31             :           "\\303\\245\\303\\244\\303\\266\\303\\205\\303\\204\\303\\226" },
      32             :         { (u8 *) "\xe5\xe4\xf6\xc5\xc4\xd6", 6,
      33             :           "\\xe5\\xe4\\xf6\\xc5\\xc4\\xd6" },
      34             :         { NULL, 0, NULL }
      35             : };
      36             : 
      37             : 
      38           1 : static int printf_encode_decode_tests(void)
      39             : {
      40             :         int i;
      41             :         size_t binlen;
      42             :         char buf[100];
      43             :         u8 bin[100];
      44           1 :         int errors = 0;
      45             : 
      46           1 :         wpa_printf(MSG_INFO, "printf encode/decode tests");
      47             : 
      48           8 :         for (i = 0; printf_tests[i].data; i++) {
      49           7 :                 const struct printf_test_data *test = &printf_tests[i];
      50           7 :                 printf_encode(buf, sizeof(buf), test->data, test->len);
      51           7 :                 wpa_printf(MSG_INFO, "%d: -> \"%s\"", i, buf);
      52             : 
      53           7 :                 binlen = printf_decode(bin, sizeof(bin), buf);
      54          14 :                 if (binlen != test->len ||
      55           7 :                     os_memcmp(bin, test->data, binlen) != 0) {
      56           0 :                         wpa_hexdump(MSG_ERROR, "Error in decoding#1",
      57             :                                     bin, binlen);
      58           0 :                         errors++;
      59             :                 }
      60             : 
      61           7 :                 binlen = printf_decode(bin, sizeof(bin), test->encoded);
      62          14 :                 if (binlen != test->len ||
      63           7 :                     os_memcmp(bin, test->data, binlen) != 0) {
      64           0 :                         wpa_hexdump(MSG_ERROR, "Error in decoding#2",
      65             :                                     bin, binlen);
      66           0 :                         errors++;
      67             :                 }
      68             :         }
      69             : 
      70           1 :         buf[5] = 'A';
      71           1 :         printf_encode(buf, 5, (const u8 *) "abcde", 5);
      72           1 :         if (buf[5] != 'A') {
      73           0 :                 wpa_printf(MSG_ERROR, "Error in bounds checking#1");
      74           0 :                 errors++;
      75             :         }
      76             : 
      77           6 :         for (i = 5; i < 10; i++) {
      78           5 :                 buf[i] = 'A';
      79           5 :                 printf_encode(buf, i, (const u8 *) "\xdd\xdd\xdd\xdd\xdd", 5);
      80           5 :                 if (buf[i] != 'A') {
      81           0 :                         wpa_printf(MSG_ERROR, "Error in bounds checking#2(%d)",
      82             :                                    i);
      83           0 :                         errors++;
      84             :                 }
      85             :         }
      86             : 
      87           1 :         if (errors) {
      88           0 :                 wpa_printf(MSG_ERROR, "%d printf test(s) failed", errors);
      89           0 :                 return -1;
      90             :         }
      91             : 
      92           1 :         return 0;
      93             : }
      94             : 
      95             : 
      96           1 : static int bitfield_tests(void)
      97             : {
      98             :         struct bitfield *bf;
      99             :         int i;
     100           1 :         int errors = 0;
     101             : 
     102           1 :         wpa_printf(MSG_INFO, "bitfield tests");
     103             : 
     104           1 :         bf = bitfield_alloc(123);
     105           1 :         if (bf == NULL)
     106           0 :                 return -1;
     107             : 
     108         124 :         for (i = 0; i < 123; i++) {
     109         123 :                 if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
     110           0 :                         errors++;
     111         123 :                 if (i > 0 && bitfield_is_set(bf, i - 1))
     112           0 :                         errors++;
     113         123 :                 bitfield_set(bf, i);
     114         123 :                 if (!bitfield_is_set(bf, i))
     115           0 :                         errors++;
     116         123 :                 bitfield_clear(bf, i);
     117         123 :                 if (bitfield_is_set(bf, i))
     118           0 :                         errors++;
     119             :         }
     120             : 
     121          78 :         for (i = 123; i < 200; i++) {
     122          77 :                 if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
     123           0 :                         errors++;
     124          77 :                 if (i > 0 && bitfield_is_set(bf, i - 1))
     125           0 :                         errors++;
     126          77 :                 bitfield_set(bf, i);
     127          77 :                 if (bitfield_is_set(bf, i))
     128           0 :                         errors++;
     129          77 :                 bitfield_clear(bf, i);
     130          77 :                 if (bitfield_is_set(bf, i))
     131           0 :                         errors++;
     132             :         }
     133             : 
     134         124 :         for (i = 0; i < 123; i++) {
     135         123 :                 if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
     136           0 :                         errors++;
     137         123 :                 bitfield_set(bf, i);
     138         123 :                 if (!bitfield_is_set(bf, i))
     139           0 :                         errors++;
     140             :         }
     141             : 
     142         124 :         for (i = 0; i < 123; i++) {
     143         123 :                 if (!bitfield_is_set(bf, i))
     144           0 :                         errors++;
     145         123 :                 bitfield_clear(bf, i);
     146         123 :                 if (bitfield_is_set(bf, i))
     147           0 :                         errors++;
     148             :         }
     149             : 
     150         124 :         for (i = 0; i < 123; i++) {
     151         123 :                 if (bitfield_get_first_zero(bf) != i)
     152           0 :                         errors++;
     153         123 :                 bitfield_set(bf, i);
     154             :         }
     155           1 :         if (bitfield_get_first_zero(bf) != -1)
     156           0 :                 errors++;
     157         124 :         for (i = 0; i < 123; i++) {
     158         123 :                 if (!bitfield_is_set(bf, i))
     159           0 :                         errors++;
     160         123 :                 bitfield_clear(bf, i);
     161         123 :                 if (bitfield_get_first_zero(bf) != i)
     162           0 :                         errors++;
     163         123 :                 bitfield_set(bf, i);
     164             :         }
     165           1 :         if (bitfield_get_first_zero(bf) != -1)
     166           0 :                 errors++;
     167             : 
     168           1 :         bitfield_free(bf);
     169             : 
     170           1 :         if (errors) {
     171           0 :                 wpa_printf(MSG_ERROR, "%d bitfield test(s) failed", errors);
     172           0 :                 return -1;
     173             :         }
     174             : 
     175           1 :         return 0;
     176             : }
     177             : 
     178             : 
     179           1 : static int int_array_tests(void)
     180             : {
     181           1 :         int test1[] = { 1, 2, 3, 4, 5, 6, 0 };
     182           1 :         int test2[] = { 1, -1, 0 };
     183           1 :         int test3[] = { 1, 1, 1, -1, 2, 3, 4, 1, 2, 0 };
     184           1 :         int test3_res[] = { -1, 1, 2, 3, 4, 0 };
     185           1 :         int errors = 0;
     186             :         int len;
     187             : 
     188           1 :         wpa_printf(MSG_INFO, "int_array tests");
     189             : 
     190           2 :         if (int_array_len(test1) != 6 ||
     191           1 :             int_array_len(test2) != 2)
     192           0 :                 errors++;
     193             : 
     194           1 :         int_array_sort_unique(test3);
     195           1 :         len = int_array_len(test3_res);
     196           1 :         if (int_array_len(test3) != len)
     197           0 :                 errors++;
     198           1 :         else if (os_memcmp(test3, test3_res, len * sizeof(int)) != 0)
     199           0 :                 errors++;
     200             : 
     201           1 :         if (errors) {
     202           0 :                 wpa_printf(MSG_ERROR, "%d int_array test(s) failed", errors);
     203           0 :                 return -1;
     204             :         }
     205             : 
     206           1 :         return 0;
     207             : }
     208             : 
     209             : 
     210           1 : static int ext_password_tests(void)
     211             : {
     212             :         struct ext_password_data *data;
     213           1 :         int ret = 0;
     214             :         struct wpabuf *pw;
     215             : 
     216           1 :         wpa_printf(MSG_INFO, "ext_password tests");
     217             : 
     218           1 :         data = ext_password_init("unknown", "foo");
     219           1 :         if (data != NULL)
     220           0 :                 return -1;
     221             : 
     222           1 :         data = ext_password_init("test", NULL);
     223           1 :         if (data == NULL)
     224           0 :                 return -1;
     225           1 :         pw = ext_password_get(data, "foo");
     226           1 :         if (pw != NULL)
     227           0 :                 ret = -1;
     228           1 :         ext_password_free(pw);
     229             : 
     230           1 :         ext_password_deinit(data);
     231             : 
     232           1 :         pw = ext_password_get(NULL, "foo");
     233           1 :         if (pw != NULL)
     234           0 :                 ret = -1;
     235           1 :         ext_password_free(pw);
     236             : 
     237           1 :         return ret;
     238             : }
     239             : 
     240             : 
     241           1 : static int trace_tests(void)
     242             : {
     243           1 :         wpa_printf(MSG_INFO, "trace tests");
     244             : 
     245           1 :         wpa_trace_show("test backtrace");
     246           1 :         wpa_trace_dump_funcname("test funcname", trace_tests);
     247             : 
     248           1 :         return 0;
     249             : }
     250             : 
     251             : 
     252           1 : int utils_module_tests(void)
     253             : {
     254           1 :         int ret = 0;
     255             : 
     256           1 :         wpa_printf(MSG_INFO, "utils module tests");
     257             : 
     258           2 :         if (printf_encode_decode_tests() < 0 ||
     259           2 :             ext_password_tests() < 0 ||
     260           2 :             trace_tests() < 0 ||
     261           2 :             bitfield_tests() < 0 ||
     262           1 :             int_array_tests() < 0)
     263           0 :                 ret = -1;
     264             : 
     265           1 :         return ret;
     266             : }

Generated by: LCOV version 1.10