This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
testmod_macaddr.c
171 lines (144 loc) · 4.46 KB
/
testmod_macaddr.c
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
/**
* @file
*
* @brief Tests for macaddr plugin
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#include <stdlib.h>
#include <string.h>
#include <kdbconfig.h>
#include <tests_plugin.h>
#define PLUGIN_NAME "macaddr"
#define META "check/macaddr"
#define MAXMACINT 281474976710655
static void convertLong (char * returned, unsigned long long i)
{
sprintf (returned, "%llu", i);
}
static int setKey (KeySet * testKs)
{
Key * parent = keyNew ("user:/tests/mac", KEY_VALUE, "", KEY_END);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN (PLUGIN_NAME);
int ret = plugin->kdbSet (plugin, testKs, parent);
keyDel (parent);
PLUGIN_CLOSE ();
return ret;
}
static const char * getKeyString (KeySet * ks, char * keyName)
{
Key * parent = keyNew ("user:/tests/mac", KEY_VALUE, "", KEY_END);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN (PLUGIN_NAME);
plugin->kdbGet (plugin, ks, parent);
keyDel (parent);
PLUGIN_CLOSE ();
return keyString (ksLookupByName (ks, keyName, 0));
}
static void testAddressSet (const char * keyValue, int retValue)
{
KeySet * testKs = ksNew (10, keyNew ("user:/tests/mac/addr", KEY_VALUE, keyValue, KEY_META, META, "", KEY_END), KS_END);
succeed_if (setKey (testKs) == retValue, "error");
ksDel (testKs);
}
static void testAddressesSetGet (const char * keyValue, unsigned long long longValue)
{
char intChar[21];
Key * key = keyNew ("user:/tests/mac/addr", KEY_VALUE, keyValue, KEY_META, META, "", KEY_END);
KeySet * testKs = ksNew (10, key, KS_END);
setKey (testKs);
convertLong (intChar, longValue);
succeed_if (!strcmp (getKeyString (testKs, "user:/tests/mac/addr"), intChar), "error");
succeed_if (!strcmp (keyString (keyGetMeta (key, "origvalue")), keyValue), "error");
ksDel (testKs);
}
static void testAddressesReturn (void)
{
testAddressesSetGet ("00:00:00:00:00:00", 0);
testAddressesSetGet ("FF:FF:FF:FF:FF:FF", 281474976710655);
testAddressesSetGet ("0d:b6:8c:44:cc:f9", 15077688528121);
testAddressesSetGet ("aB-Cd-8f-f3-e5-d7", 188899371771351);
testAddressesSetGet ("A1B2C3-4D5E6F", 177789152878191);
}
static void testAddressesStandardColons (void)
{
testAddressSet ("00:00:00:00:00:00", 1);
testAddressSet ("FF:FF:FF:FF:FF:FF", 1);
testAddressSet ("AA:BB:CC:DD:EE:FF", 1);
testAddressSet ("99:99:99:99:99:99", 1);
testAddressSet ("A1:B2:C3:4D:5E:6F", 1);
testAddressSet ("aB:Cd:8f:f3:e5:d7", 1);
testAddressSet ("aB:Cd:8f:f3:e5:g7", -1);
testAddressSet ("aB:Cd:8f:f3:e5e:d7", -1);
testAddressSet ("aB:Cd:8f:f3:e5", -1);
testAddressSet ("aB:Cd:8f:f3:e5", -1);
}
static void testAddressesStandardHyphens (void)
{
testAddressSet ("00-00-00-00-00-00", 1);
testAddressSet ("FF-FF-FF-FF-FF-FF", 1);
testAddressSet ("AA-BB-CC-DD-EE-FF", 1);
testAddressSet ("99-99-99-99-99-99", 1);
testAddressSet ("A1-B2-C3-4D-5E-6F", 1);
testAddressSet ("aB-Cd-8f-f3-e5-d7", 1);
testAddressSet ("aB-Cd-8f-f3-e5-g7", -1);
testAddressSet ("aB-Cd-8f-f3-e5e-d7", -1);
testAddressSet ("aB-Cd-8f-f3-e5", -1);
testAddressSet ("aB-Cd-8f-f3-e5", -1);
}
static void testAddressesSingleHyphen (void)
{
testAddressSet ("000000-000000", 1);
testAddressSet ("FFFFFF-FFFFFF", 1);
testAddressSet ("AABBCC-DDEEFF", 1);
testAddressSet ("999999-999999", 1);
testAddressSet ("A1B2C3-4D5E6F", 1);
testAddressSet ("aBCd8f-f3e5d7", 1);
testAddressSet ("aBCd8f-f3e5g7", -1);
testAddressSet ("aBCd8f-f3e5ed7", -1);
testAddressSet ("aBCd8f-f3e5", -1);
testAddressSet ("aBCd8f-f3e5", -1);
}
static void testAddressesNumber (void)
{
char intChar[21];
convertLong (intChar, 0);
testAddressSet (intChar, 1);
convertLong (intChar, MAXMACINT);
testAddressSet (intChar, 1);
convertLong (intChar, -1);
testAddressSet (intChar, -1);
convertLong (intChar, MAXMACINT + 1);
testAddressSet (intChar, -1);
}
static void testRestoreValue (void)
{
char * val = "00:11:55:AA:FF:CC";
Key * key = keyNew ("user:/tests/mac/addr", KEY_VALUE, val, KEY_META, META, "", KEY_END);
KeySet * testKs = ksNew (10, key, KS_END);
setKey (testKs);
getKeyString (testKs, "user:/tests/mac/addr");
setKey (testKs);
succeed_if (!strcmp (keyString (key), val), "error");
ksDel (testKs);
}
static void testAll (void)
{
testAddressesStandardColons ();
testAddressesStandardHyphens ();
testAddressesSingleHyphen ();
testAddressesNumber ();
testAddressesReturn ();
testRestoreValue ();
}
int main (int argc, char ** argv)
{
printf ("MACADDR TESTS\n");
printf ("==================\n\n");
init (argc, argv);
testAll ();
print_result ("testmod_macaddr");
return nbError;
}