LCOV - code coverage report
Current view: top level - src/plugins/macaddr - testmod_macaddr.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 113 113 100.0 %
Date: 2019-09-12 12:28:41 Functions: 12 12 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Tests for macaddr plugin
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  *
       8             :  */
       9             : 
      10             : #include <stdlib.h>
      11             : #include <string.h>
      12             : 
      13             : #include <kdbconfig.h>
      14             : 
      15             : #include <tests_plugin.h>
      16             : 
      17             : #define PLUGIN_NAME "macaddr"
      18             : #define META "check/macaddr"
      19             : #define MAXMACINT 281474976710655
      20             : 
      21             : static void convertLong (char * returned, unsigned long long i)
      22             : {
      23          18 :         sprintf (returned, "%llu", i);
      24             : }
      25             : 
      26          82 : static int setKey (KeySet * testKs)
      27             : {
      28          82 :         Key * parent = keyNew ("user/tests/mac", KEY_VALUE, "", KEY_END);
      29             : 
      30          82 :         KeySet * conf = ksNew (0, KS_END);
      31          82 :         PLUGIN_OPEN (PLUGIN_NAME);
      32          82 :         ksRewind (testKs);
      33          82 :         int ret = plugin->kdbSet (plugin, testKs, parent);
      34          82 :         keyDel (parent);
      35          82 :         PLUGIN_CLOSE ();
      36          82 :         return ret;
      37             : }
      38             : 
      39          12 : static const char * getKeyString (KeySet * ks, char * keyName)
      40             : {
      41          12 :         Key * parent = keyNew ("user/tests/mac", KEY_VALUE, "", KEY_END);
      42          12 :         KeySet * conf = ksNew (0, KS_END);
      43          12 :         PLUGIN_OPEN (PLUGIN_NAME);
      44          12 :         ksRewind (ks);
      45          12 :         plugin->kdbGet (plugin, ks, parent);
      46          12 :         keyDel (parent);
      47          12 :         PLUGIN_CLOSE ();
      48          12 :         return keyString (ksLookupByName (ks, keyName, 0));
      49             : }
      50             : 
      51          68 : static void testAddressSet (const char * keyValue, int retValue)
      52             : {
      53          68 :         KeySet * testKs = ksNew (10, keyNew ("user/tests/mac/addr", KEY_VALUE, keyValue, KEY_META, META, "", KEY_END), KS_END);
      54          68 :         succeed_if (setKey (testKs) == retValue, "error");
      55          68 :         ksDel (testKs);
      56          68 : }
      57             : 
      58          10 : static void testAddressesSetGet (const char * keyValue, unsigned long long longValue)
      59             : {
      60             :         char intChar[21];
      61          10 :         Key * key = keyNew ("user/tests/mac/addr", KEY_VALUE, keyValue, KEY_META, META, "", KEY_END);
      62          10 :         KeySet * testKs = ksNew (10, key, KS_END);
      63          10 :         setKey (testKs);
      64          10 :         convertLong (intChar, longValue);
      65          10 :         succeed_if (!strcmp (getKeyString (testKs, "user/tests/mac/addr"), intChar), "error");
      66          10 :         succeed_if (!strcmp (keyString (keyGetMeta (key, "origvalue")), keyValue), "error");
      67          10 :         keyDel (key);
      68          10 :         ksDel (testKs);
      69          10 : }
      70             : 
      71           2 : static void testAddressesReturn (void)
      72             : {
      73           2 :         testAddressesSetGet ("00:00:00:00:00:00", 0);
      74           2 :         testAddressesSetGet ("FF:FF:FF:FF:FF:FF", 281474976710655);
      75           2 :         testAddressesSetGet ("0d:b6:8c:44:cc:f9", 15077688528121);
      76           2 :         testAddressesSetGet ("aB-Cd-8f-f3-e5-d7", 188899371771351);
      77           2 :         testAddressesSetGet ("A1B2C3-4D5E6F", 177789152878191);
      78           2 : }
      79             : 
      80           2 : static void testAddressesStandardColons (void)
      81             : {
      82           2 :         testAddressSet ("00:00:00:00:00:00", 1);
      83           2 :         testAddressSet ("FF:FF:FF:FF:FF:FF", 1);
      84           2 :         testAddressSet ("AA:BB:CC:DD:EE:FF", 1);
      85           2 :         testAddressSet ("99:99:99:99:99:99", 1);
      86           2 :         testAddressSet ("A1:B2:C3:4D:5E:6F", 1);
      87           2 :         testAddressSet ("aB:Cd:8f:f3:e5:d7", 1);
      88           2 :         testAddressSet ("aB:Cd:8f:f3:e5:g7", -1);
      89           2 :         testAddressSet ("aB:Cd:8f:f3:e5e:d7", -1);
      90           2 :         testAddressSet ("aB:Cd:8f:f3:e5", -1);
      91           2 :         testAddressSet ("aB:Cd:8f:f3:e5", -1);
      92           2 : }
      93             : 
      94           2 : static void testAddressesStandardHyphens (void)
      95             : {
      96             : 
      97           2 :         testAddressSet ("00-00-00-00-00-00", 1);
      98           2 :         testAddressSet ("FF-FF-FF-FF-FF-FF", 1);
      99           2 :         testAddressSet ("AA-BB-CC-DD-EE-FF", 1);
     100           2 :         testAddressSet ("99-99-99-99-99-99", 1);
     101           2 :         testAddressSet ("A1-B2-C3-4D-5E-6F", 1);
     102           2 :         testAddressSet ("aB-Cd-8f-f3-e5-d7", 1);
     103           2 :         testAddressSet ("aB-Cd-8f-f3-e5-g7", -1);
     104           2 :         testAddressSet ("aB-Cd-8f-f3-e5e-d7", -1);
     105           2 :         testAddressSet ("aB-Cd-8f-f3-e5", -1);
     106           2 :         testAddressSet ("aB-Cd-8f-f3-e5", -1);
     107           2 : }
     108             : 
     109           2 : static void testAddressesSingleHyphen (void)
     110             : {
     111           2 :         testAddressSet ("000000-000000", 1);
     112           2 :         testAddressSet ("FFFFFF-FFFFFF", 1);
     113           2 :         testAddressSet ("AABBCC-DDEEFF", 1);
     114           2 :         testAddressSet ("999999-999999", 1);
     115           2 :         testAddressSet ("A1B2C3-4D5E6F", 1);
     116           2 :         testAddressSet ("aBCd8f-f3e5d7", 1);
     117           2 :         testAddressSet ("aBCd8f-f3e5g7", -1);
     118           2 :         testAddressSet ("aBCd8f-f3e5ed7", -1);
     119           2 :         testAddressSet ("aBCd8f-f3e5", -1);
     120           2 :         testAddressSet ("aBCd8f-f3e5", -1);
     121           2 : }
     122             : 
     123           2 : static void testAddressesNumber (void)
     124             : {
     125             :         char intChar[21];
     126           2 :         convertLong (intChar, 0);
     127           2 :         testAddressSet (intChar, 1);
     128             : 
     129           2 :         convertLong (intChar, MAXMACINT);
     130           2 :         testAddressSet (intChar, 1);
     131             : 
     132           2 :         convertLong (intChar, -1);
     133           2 :         testAddressSet (intChar, -1);
     134             : 
     135           2 :         convertLong (intChar, MAXMACINT + 1);
     136           2 :         testAddressSet (intChar, -1);
     137           2 : }
     138             : 
     139           2 : static void testRestoreValue (void)
     140             : {
     141           2 :         char * val = "00:11:55:AA:FF:CC";
     142           2 :         Key * key = keyNew ("user/tests/mac/addr", KEY_VALUE, val, KEY_META, META, "", KEY_END);
     143           2 :         KeySet * testKs = ksNew (10, key, KS_END);
     144           2 :         setKey (testKs);
     145           2 :         getKeyString (testKs, "user/tests/mac/addr");
     146           2 :         setKey (testKs);
     147           2 :         succeed_if (!strcmp (keyString (key), val), "error");
     148           2 :         keyDel (key);
     149           2 :         ksDel (testKs);
     150           2 : }
     151             : 
     152           2 : static void testAll (void)
     153             : {
     154           2 :         testAddressesStandardColons ();
     155           2 :         testAddressesStandardHyphens ();
     156           2 :         testAddressesSingleHyphen ();
     157           2 :         testAddressesNumber ();
     158           2 :         testAddressesReturn ();
     159           2 :         testRestoreValue ();
     160           2 : }
     161             : 
     162             : 
     163           2 : int main (int argc, char ** argv)
     164             : {
     165           2 :         printf ("MACADDR     TESTS\n");
     166           2 :         printf ("==================\n\n");
     167             : 
     168           2 :         init (argc, argv);
     169             : 
     170           2 :         testAll ();
     171             : 
     172           2 :         print_result ("testmod_macaddr");
     173             : 
     174           2 :         return nbError;
     175             : }

Generated by: LCOV version 1.13