aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tests/common-dict.c
blob: 172efe4f5e1223943a8e5ee0ff1b4a3cd7d46ea6 (plain) (blame)
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
 *  Tiny RADIUS Client Library
 *  Copyright (C) 2022 David M. Syzdek <david@syzdek.net>.
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are
 *  met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of David M. Syzdek nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 *  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 *  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID M. SYZDEK BE LIABLE FOR
 *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 *  SUCH DAMAGE.
 */
#define _TESTS_COMMON_DICT_C 1
#include "common-dict.h"


///////////////
//           //
//  Headers  //
//           //
///////////////
#pragma mark - Headers

#include <stddef.h>
#include <stdio.h>
#include <assert.h>


///////////////////
//               //
//  Definitions  //
//               //
///////////////////
#pragma mark - Definitions


/////////////////
//             //
//  Variables  //
//             //
/////////////////
#pragma mark - Variables

#pragma mark test_dict_data_attrs[]
// RADIUS attributes: https://www.iana.org/assignments/radius-types/radius-types.xhtml
const TinyRadDictAttrDef test_dict_data_attrs[] =
{
   { "Syzdek-HTML-Color",         26,      27893,            0,   TRAD_DATATYPE_INTEGER,     0 },  // custom attribute for testing
   { "Syzdek-Faith",              26,      27893,            1,   TRAD_DATATYPE_INTEGER,     0 },  // custom attribute for testing
   { "Syzdek-Animal-Group",       26,      27893,            2,   TRAD_DATATYPE_INTEGER,     0 },  // custom attribute for testing
   { "TinyrRad-Foobar",          224,          0,            0,   TRAD_DATATYPE_TEXT,        0 },  // custom attribute for testing
   { "TinyrRad-Signature",       225,          0,            0,   TRAD_DATATYPE_STRING,      0 },  // custom attribute for testing
   { "TinyrRad-YoYo-Make",       226,          0,            0,   TRAD_DATATYPE_INTEGER,     0 },  // custom attribute for testing
   { NULL, 0, 0, 0, 0, 0 }
};


#pragma mark test_dict_data_values[]
const TinyRadDictValueDef test_dict_data_values[] =
{
   { "Syzdek-Animal-Group",       "Invertebrates",             1 },  // custom value for testing
   { "Syzdek-Animal-Group",       "Fish",                      2 },  // custom value for testing
   { "Syzdek-Animal-Group",       "Amphibians",                3 },  // custom value for testing
   { "Syzdek-Animal-Group",       "Reptiles",                  4 },  // custom value for testing
   { "Syzdek-Animal-Group",       "Birds",                     5 },  // custom value for testing
   { "Syzdek-Animal-Group",       "Mammals",                   6 },  // custom value for testing

   { "Syzdek-HTML-Color",         "Black",                     1 },  // custom value for testing
   { "Syzdek-HTML-Color",         "White",                     2 },  // custom value for testing
   { "Syzdek-HTML-Color",         "Gray",                      3 },  // custom value for testing
   { "Syzdek-HTML-Color",         "Silver",                    4 },  // custom value for testing
   { "Syzdek-HTML-Color",         "Maroon",                    5 },  // custom value for testing
   { "Syzdek-HTML-Color",         "Red",                       6 },  // custom value for testing
   { "Syzdek-HTML-Color",         "Purple",                    7 },  // custom value for testing
   { "Syzdek-HTML-Color",         "Fushsia",                   8 },  // custom value for testing
   { "Syzdek-HTML-Color",         "Green",                     9 },  // custom value for testing
   { "Syzdek-HTML-Color",         "Lime",                     10 },  // custom value for testing
   { "Syzdek-HTML-Color",         "Olive",                    11 },  // custom value for testing
   { "Syzdek-HTML-Color",         "Yellow",                   12 },  // custom value for testing
   { "Syzdek-HTML-Color",         "Navy",                     13 },  // custom value for testing
   { "Syzdek-HTML-Color",         "Blue",                     14 },  // custom value for testing
   { "Syzdek-HTML-Color",         "Teal",                     15 },  // custom value for testing
   { "Syzdek-HTML-Color",         "Aqua",                     16 },  // custom value for testing

   { "Syzdek-Faith",              "Unknown",                   1 },  // custom value for testing
   { "Syzdek-Faith",              "Other",                     2 },  // custom value for testing
   { "Syzdek-Faith",              "Catholic",                  3 },  // custom value for testing
   { "Syzdek-Faith",              "Roman-Rite",                3 },  // custom value for testing
   { "Syzdek-Faith",              "Ritus-Romanus",             3 },  // custom value for testing
   { "Syzdek-Faith",              "Roman-Catholic",            3 },  // custom value for testing
   { "Syzdek-Faith",              "Christian",                 4 },  // custom value for testing
   { "Syzdek-Faith",              "Judaism",                   5 },  // custom value for testing

   { "TinyrRad-YoYo-Make",        "Unknown",                   0 },  // custom value for testing
   { "TinyrRad-YoYo-Make",        "YoYoFactory",               1 },  // custom value for testing
   { "TinyrRad-YoYo-Make",        "Duncan",                    2 },  // custom value for testing
   { "TinyrRad-YoYo-Make",        "iYoYo",                     3 },  // custom value for testing
   { "TinyrRad-YoYo-Make",        "YoYoTricks-com",            4 },  // custom value for testing
   { "TinyrRad-YoYo-Make",        "Magic-Yoyo",                5 },  // custom value for testing
   { "TinyrRad-YoYo-Make",        "Recess",                    6 },  // custom value for testing
   { "TinyrRad-YoYo-Make",        "Throwback-Skill-Toys",      7 },  // custom value for testing
   { "TinyrRad-YoYo-Make",        "yoyo-friends",              8 },  // custom value for testing

   { NULL, NULL, 0}
};


#pragma mark test_dict_data_vendors[]
// See https://www.iana.org/assignments/enterprise-numbers/enterprise-numbers)
const TinyRadDictVendorDef test_dict_data_vendors[] =
{
   { "ACS",              8838,   1, 1 },  // ACS Internet, Inc. (https://www.iana.org/assignments/enterprise-numbers/enterprise-numbers)
   { "Syzdek",          27893,   1, 1 },  // DMS  (https://www.iana.org/assignments/enterprise-numbers/enterprise-numbers)
   { "RFC-Example",     32473,   1, 1 },  // Enterprise Number for Documentation Use (RFC 5612)
   { NULL, 0, 0, 0 }
};


/////////////////
//             //
//  Functions  //
//             //
/////////////////
#pragma mark - Functions

void
our_dict_diag_attr(
         unsigned                      opts,
         const TinyRadDictAttrDef *    attr_def,
         TinyRadDictAttr *             attr )
{
   char *      attr_name;
   uint8_t     attr_type;
   uint8_t     attr_type_octets;
   uint8_t     attr_len_octets;
   uint32_t    attr_vendor_id;
   uint32_t    attr_vendor_type;
   uint32_t    attr_data_type;
   uint32_t    attr_flags;
   unsigned    attr_ref_count;

   if ((attr_def))
   {
      trutils_verbose(opts, "Attribute Definition");
      trutils_verbose(opts, "   Name:             %s",       attr_def->name);
      trutils_verbose(opts, "   Type:             %" PRIu8,  attr_def->type);
      trutils_verbose(opts, "   Vendor ID:        %" PRIu32, attr_def->vendor_id);
      trutils_verbose(opts, "   Vendor Type:      %" PRIu32, attr_def->vendor_type);
      trutils_verbose(opts, "   Data Type:        %" PRIu32, attr_def->data_type);
      trutils_verbose(opts, "   Flags:            0x%08x",   (unsigned)attr_def->flags);
   };

   if ((attr))
   {
      attr_name         = NULL;
      attr_type         = 0;
      attr_vendor_id    = 0;
      attr_vendor_type  = 0;
      attr_data_type    = 0;
      attr_flags        = 0;
      attr_type_octets  = 0;
      attr_len_octets   = 0;
      attr_ref_count    = 0;
      tinyrad_dict_attr_info(attr, TRAD_DICT_OPT_NAME,         &attr_name);
      tinyrad_dict_attr_info(attr, TRAD_DICT_OPT_TYPE,         &attr_type);
      tinyrad_dict_attr_info(attr, TRAD_DICT_OPT_VEND_ID,      &attr_vendor_id);
      tinyrad_dict_attr_info(attr, TRAD_DICT_OPT_VEND_TYPE,    &attr_vendor_type);
      tinyrad_dict_attr_info(attr, TRAD_DICT_OPT_DATA_TYPE,    &attr_data_type);
      tinyrad_dict_attr_info(attr, TRAD_DICT_OPT_FLAGS,        &attr_flags);
      tinyrad_dict_attr_info(attr, TRAD_DICT_OPT_TYPE_OCTS,    &attr_type_octets);
      tinyrad_dict_attr_info(attr, TRAD_DICT_OPT_LEN_OCTS,     &attr_len_octets);
      tinyrad_dict_attr_info(attr, TRAD_DICT_OPT_REF_COUNT,    &attr_ref_count);
      trutils_verbose(opts, "Attribute Object");
      trutils_verbose(opts, "   Name:             %s",       (((attr_name)) ? attr_name : ""));
      trutils_verbose(opts, "   Type:             %" PRIu8,  attr_type);
      trutils_verbose(opts, "   Vendor ID:        %" PRIu32, attr_vendor_id);
      trutils_verbose(opts, "   Vendor Type:      %" PRIu32, attr_vendor_type);
      trutils_verbose(opts, "   Data Type:        %" PRIu32, attr_data_type);
      trutils_verbose(opts, "   Flags:            0x%08x",   (unsigned)attr_flags);
      trutils_verbose(opts, "   Type Octets:      %" PRIu32, attr_type_octets);
      trutils_verbose(opts, "   Length Octets:    %" PRIu32, attr_len_octets);
      trutils_verbose(opts, "   Reference Count:  %u",       attr_ref_count);
   };

   return;
}


void
our_dict_diag_value(
         unsigned                      opts,
         const TinyRadDictValueDef *   value_def,
         TinyRadDictValue *            value )
{
   char *      value_name;
   char *      attr_name;
   unsigned    value_ref_count;
   uint8_t     value_type;
   uint32_t    vendor_id;
   uint32_t    vendor_type;
   uint64_t    value_data;

   if ((value_def))
   {
      trutils_verbose(opts, "Value Definition");
      trutils_verbose(opts, "   Attribute Name:   %s",       value_def->attr_name);
      trutils_verbose(opts, "   Value Name:       %s",       value_def->value_name);
      trutils_verbose(opts, "   Value Data:       %" PRIu64, value_def->data);
   };

   if ((value))
   {
      attr_name            = NULL;
      value_name           = NULL;
      value_type           = 0;
      vendor_id            = 0;
      vendor_type          = 0;
      value_data           = 0;
      value_ref_count      = 0;
      tinyrad_dict_value_info(value, TRAD_DICT_OPT_NAME,         &value_name);
      tinyrad_dict_value_info(value, TRAD_DICT_OPT_ATTR_NAME,    &attr_name);
      tinyrad_dict_value_info(value, TRAD_DICT_OPT_TYPE,         &value_type);
      tinyrad_dict_value_info(value, TRAD_DICT_OPT_VEND_ID,      &vendor_id);
      tinyrad_dict_value_info(value, TRAD_DICT_OPT_VEND_TYPE,    &vendor_type);
      tinyrad_dict_value_info(value, TRAD_DICT_OPT_DATA,         &value_data);
      tinyrad_dict_value_info(value, TRAD_DICT_OPT_REF_COUNT,    &value_ref_count);
      trutils_verbose(opts, "Value Object");
      trutils_verbose(opts, "   Value Name:       %s",         (((value_name)) ? value_name : ""));
      trutils_verbose(opts, "   Attribute Name:   %s",         (((attr_name))  ? attr_name  : ""));
      trutils_verbose(opts, "   Attribute Type:   %" PRIu8,    value_type);
      trutils_verbose(opts, "   Vendor ID:        %" PRIu32,   vendor_id);
      trutils_verbose(opts, "   Vendor Type:      %" PRIu32,   vendor_type);
      trutils_verbose(opts, "   Value Data:       %" PRIu64,   value_data);
      trutils_verbose(opts, "   Reference Count:  %u",         value_ref_count);
   };

   return;
}


void
our_dict_diag_vendor(
         unsigned                      opts,
         const TinyRadDictVendorDef *  vendor_def,
         TinyRadDictVendor *           vendor )
{
   char *      vendor_name;
   uint8_t     vendor_type_octets;
   uint8_t     vendor_len_octets;
   uint32_t    vendor_id;
   unsigned    vendor_ref_count;

   if ((vendor_def))
   {
      trutils_verbose(opts, "Vendor Definition");
      trutils_verbose(opts, "   Name:             %s",       vendor_def->name);
      trutils_verbose(opts, "   Vendor ID:        %" PRIu32, vendor_def->vendor_id);
      trutils_verbose(opts, "   Vendor Type:      %" PRIu32, vendor_def->vendor_type_octs);
      trutils_verbose(opts, "   Data Type:        %" PRIu32, vendor_def->vendor_len_octs);
   };

   if ((vendor))
   {
      vendor_name          = NULL;
      vendor_id            = 0;
      vendor_type_octets   = 0;
      vendor_len_octets    = 0;
      vendor_ref_count     = 0;
      tinyrad_dict_vendor_info(vendor, TRAD_DICT_OPT_NAME,         &vendor_name);
      tinyrad_dict_vendor_info(vendor, TRAD_DICT_OPT_VEND_ID,      &vendor_id);
      tinyrad_dict_vendor_info(vendor, TRAD_DICT_OPT_TYPE_OCTS,    &vendor_type_octets);
      tinyrad_dict_vendor_info(vendor, TRAD_DICT_OPT_LEN_OCTS,     &vendor_len_octets);
      tinyrad_dict_vendor_info(vendor, TRAD_DICT_OPT_REF_COUNT,    &vendor_ref_count);
      trutils_verbose(opts, "Vendor Object");
      trutils_verbose(opts, "   Name:             %s",       (((vendor_name)) ? vendor_name : ""));
      trutils_verbose(opts, "   Vendor ID:        %" PRIu32, vendor_id);
      trutils_verbose(opts, "   Type Octets:      %" PRIu32, vendor_type_octets);
      trutils_verbose(opts, "   Length Octets:    %" PRIu32, vendor_len_octets);
      trutils_verbose(opts, "   Reference Count:  %u",       vendor_ref_count);
   };

   return;
}


int
our_dict_initialize(
         unsigned                      opts,
         TinyRadDict **                dictp )
{
   int            rc;
   char **        errs;

   assert(dictp != NULL);

   *dictp   = NULL;
   errs     = NULL;

   // initialize dictionary
   if (tinyrad_dict_initialize(dictp, 0) != TRAD_SUCCESS)
   {
      trutils_error(opts, NULL, "out of virtual memory");
      return(1);
   };

   // load builtin/default dictionary objects
   if ((opts & TEST_OPT_DFLT_DICT))
   {
      trutils_verbose(opts, "loading default dictionary ...");
      if ((rc = tinyrad_dict_defaults(*dictp, &errs, 0)) != TRAD_SUCCESS)
      {
         trutils_error(opts, errs, "tinyrad_dict_defaults(): %s", tinyrad_strerror(rc));
         tinyrad_strsfree(errs);
         tinyrad_free(*dictp);
         return(1);
      };
   };

   // load test dictionary vendors
   trutils_verbose(opts, "adding test dictionary vendors ...");
   if ((rc = tinyrad_dict_import(*dictp, test_dict_data_vendors, NULL, NULL, &errs)) != TRAD_SUCCESS)
   {
      trutils_error(opts, errs, "tinyrad_dict_import(vendors): %s", tinyrad_strerror(rc));
      tinyrad_strsfree(errs);
      tinyrad_free(*dictp);
      return(1);
   };

   // load test dictionary attributes
   trutils_verbose(opts, "adding test dictionary attributes ...");
   if ((rc = tinyrad_dict_import(*dictp, NULL, test_dict_data_attrs, NULL, &errs)) != TRAD_SUCCESS)
   {
      trutils_error(opts, errs, "tinyrad_dict_import(attributes): %s", tinyrad_strerror(rc));
      tinyrad_strsfree(errs);
      tinyrad_free(*dictp);
      return(1);
   };

   // load test dictionary values
   trutils_verbose(opts, "adding test dictionary values ...");
   if ((rc = tinyrad_dict_import(*dictp, NULL, NULL, test_dict_data_values, &errs)) != TRAD_SUCCESS)
   {
      trutils_error(opts, errs, "tinyrad_dict_import(values): %s", tinyrad_strerror(rc));
      tinyrad_strsfree(errs);
      tinyrad_free(*dictp);
      return(1);
   };

   return(0);
}


/* end of source */