Add a namemap test
[oweals/openssl.git] / test / namemap_internal_test.c
1 /*
2  * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include "internal/namemap.h"
11 #include "testutil.h"
12
13 #define NAME1 "name1"
14 #define NAME2 "name2"
15 #define ALIAS1 "alias1"
16
17 static int test_namemap(OSSL_NAMEMAP *nm)
18 {
19     int num1 = ossl_namemap_add(nm, 0, NAME1);
20     int num2 = ossl_namemap_add(nm, 0, NAME2);
21     int num3 = ossl_namemap_add(nm, num1, ALIAS1);
22     int check1 = ossl_namemap_name2num(nm, NAME1);
23     int check2 = ossl_namemap_name2num(nm, NAME2);
24     int check3 = ossl_namemap_name2num(nm, ALIAS1);
25     int false1 = ossl_namemap_name2num(nm, "foo");
26
27     return TEST_int_ne(num1, 0)
28         && TEST_int_ne(num2, 0)
29         && TEST_int_eq(num1, num3)
30         && TEST_int_eq(num1, check1)
31         && TEST_int_eq(num2, check2)
32         && TEST_int_eq(num3, check3)
33         && TEST_int_eq(false1, 0);
34 }
35
36 static int test_namemap_independent(void)
37 {
38     OSSL_NAMEMAP *nm = ossl_namemap_new();
39     int ok = nm != NULL && test_namemap(nm);
40
41     ossl_namemap_free(nm);
42     return ok;
43 }
44
45 static int test_namemap_stored(void)
46 {
47     OSSL_NAMEMAP *nm = ossl_namemap_stored(NULL);
48
49     return nm != NULL
50         && test_namemap(nm);
51 }
52
53 int setup_tests(void)
54 {
55     ADD_TEST(test_namemap_independent);
56     ADD_TEST(test_namemap_stored);
57     return 1;
58 }