1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
4*e0c4386eSCy Schubert *
5*e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use
6*e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy
7*e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at
8*e0c4386eSCy Schubert * https://www.openssl.org/source/license.html
9*e0c4386eSCy Schubert */
10*e0c4386eSCy Schubert
11*e0c4386eSCy Schubert #include <stdio.h>
12*e0c4386eSCy Schubert #include <string.h>
13*e0c4386eSCy Schubert #include <limits.h>
14*e0c4386eSCy Schubert
15*e0c4386eSCy Schubert #include <openssl/crypto.h>
16*e0c4386eSCy Schubert #include "internal/nelem.h"
17*e0c4386eSCy Schubert #include "crypto/sparse_array.h"
18*e0c4386eSCy Schubert #include "testutil.h"
19*e0c4386eSCy Schubert
20*e0c4386eSCy Schubert /* The macros below generate unused functions which error out one of the clang
21*e0c4386eSCy Schubert * builds. We disable this check here.
22*e0c4386eSCy Schubert */
23*e0c4386eSCy Schubert #ifdef __clang__
24*e0c4386eSCy Schubert #pragma clang diagnostic ignored "-Wunused-function"
25*e0c4386eSCy Schubert #endif
26*e0c4386eSCy Schubert
27*e0c4386eSCy Schubert DEFINE_SPARSE_ARRAY_OF(char);
28*e0c4386eSCy Schubert
test_sparse_array(void)29*e0c4386eSCy Schubert static int test_sparse_array(void)
30*e0c4386eSCy Schubert {
31*e0c4386eSCy Schubert static const struct {
32*e0c4386eSCy Schubert ossl_uintmax_t n;
33*e0c4386eSCy Schubert char *v;
34*e0c4386eSCy Schubert } cases[] = {
35*e0c4386eSCy Schubert { 22, "a" }, { 0, "z" }, { 1, "b" }, { 290, "c" },
36*e0c4386eSCy Schubert { INT_MAX, "m" }, { 6666666, "d" }, { (ossl_uintmax_t)-1, "H" },
37*e0c4386eSCy Schubert { 99, "e" }
38*e0c4386eSCy Schubert };
39*e0c4386eSCy Schubert SPARSE_ARRAY_OF(char) *sa;
40*e0c4386eSCy Schubert size_t i, j;
41*e0c4386eSCy Schubert int res = 0;
42*e0c4386eSCy Schubert
43*e0c4386eSCy Schubert if (!TEST_ptr(sa = ossl_sa_char_new())
44*e0c4386eSCy Schubert || !TEST_ptr_null(ossl_sa_char_get(sa, 3))
45*e0c4386eSCy Schubert || !TEST_ptr_null(ossl_sa_char_get(sa, 0))
46*e0c4386eSCy Schubert || !TEST_ptr_null(ossl_sa_char_get(sa, UINT_MAX)))
47*e0c4386eSCy Schubert goto err;
48*e0c4386eSCy Schubert
49*e0c4386eSCy Schubert for (i = 0; i < OSSL_NELEM(cases); i++) {
50*e0c4386eSCy Schubert if (!TEST_true(ossl_sa_char_set(sa, cases[i].n, cases[i].v))) {
51*e0c4386eSCy Schubert TEST_note("iteration %zu", i + 1);
52*e0c4386eSCy Schubert goto err;
53*e0c4386eSCy Schubert }
54*e0c4386eSCy Schubert for (j = 0; j <= i; j++)
55*e0c4386eSCy Schubert if (!TEST_str_eq(ossl_sa_char_get(sa, cases[j].n), cases[j].v)) {
56*e0c4386eSCy Schubert TEST_note("iteration %zu / %zu", i + 1, j + 1);
57*e0c4386eSCy Schubert goto err;
58*e0c4386eSCy Schubert }
59*e0c4386eSCy Schubert }
60*e0c4386eSCy Schubert
61*e0c4386eSCy Schubert res = 1;
62*e0c4386eSCy Schubert err:
63*e0c4386eSCy Schubert ossl_sa_char_free(sa);
64*e0c4386eSCy Schubert return res;
65*e0c4386eSCy Schubert }
66*e0c4386eSCy Schubert
test_sparse_array_num(void)67*e0c4386eSCy Schubert static int test_sparse_array_num(void)
68*e0c4386eSCy Schubert {
69*e0c4386eSCy Schubert static const struct {
70*e0c4386eSCy Schubert size_t num;
71*e0c4386eSCy Schubert ossl_uintmax_t n;
72*e0c4386eSCy Schubert char *v;
73*e0c4386eSCy Schubert } cases[] = {
74*e0c4386eSCy Schubert { 1, 22, "a" }, { 2, 1021, "b" }, { 3, 3, "c" }, { 2, 22, NULL },
75*e0c4386eSCy Schubert { 2, 3, "d" }, { 3, 22, "e" }, { 3, 666, NULL }, { 4, 666, "f" },
76*e0c4386eSCy Schubert { 3, 3, NULL }, { 2, 22, NULL }, { 1, 666, NULL }, { 2, 64000, "g" },
77*e0c4386eSCy Schubert { 1, 1021, NULL }, { 0, 64000, NULL }, { 1, 23, "h" }, { 0, 23, NULL }
78*e0c4386eSCy Schubert };
79*e0c4386eSCy Schubert SPARSE_ARRAY_OF(char) *sa = NULL;
80*e0c4386eSCy Schubert size_t i;
81*e0c4386eSCy Schubert int res = 0;
82*e0c4386eSCy Schubert
83*e0c4386eSCy Schubert if (!TEST_size_t_eq(ossl_sa_char_num(NULL), 0)
84*e0c4386eSCy Schubert || !TEST_ptr(sa = ossl_sa_char_new())
85*e0c4386eSCy Schubert || !TEST_size_t_eq(ossl_sa_char_num(sa), 0))
86*e0c4386eSCy Schubert goto err;
87*e0c4386eSCy Schubert for (i = 0; i < OSSL_NELEM(cases); i++)
88*e0c4386eSCy Schubert if (!TEST_true(ossl_sa_char_set(sa, cases[i].n, cases[i].v))
89*e0c4386eSCy Schubert || !TEST_size_t_eq(ossl_sa_char_num(sa), cases[i].num))
90*e0c4386eSCy Schubert goto err;
91*e0c4386eSCy Schubert res = 1;
92*e0c4386eSCy Schubert err:
93*e0c4386eSCy Schubert ossl_sa_char_free(sa);
94*e0c4386eSCy Schubert return res;
95*e0c4386eSCy Schubert }
96*e0c4386eSCy Schubert
97*e0c4386eSCy Schubert struct index_cases_st {
98*e0c4386eSCy Schubert ossl_uintmax_t n;
99*e0c4386eSCy Schubert char *v;
100*e0c4386eSCy Schubert int del;
101*e0c4386eSCy Schubert };
102*e0c4386eSCy Schubert
103*e0c4386eSCy Schubert struct doall_st {
104*e0c4386eSCy Schubert SPARSE_ARRAY_OF(char) *sa;
105*e0c4386eSCy Schubert size_t num_cases;
106*e0c4386eSCy Schubert const struct index_cases_st *cases;
107*e0c4386eSCy Schubert int res;
108*e0c4386eSCy Schubert int all;
109*e0c4386eSCy Schubert };
110*e0c4386eSCy Schubert
leaf_check_all(ossl_uintmax_t n,char * value,void * arg)111*e0c4386eSCy Schubert static void leaf_check_all(ossl_uintmax_t n, char *value, void *arg)
112*e0c4386eSCy Schubert {
113*e0c4386eSCy Schubert struct doall_st *doall_data = (struct doall_st *)arg;
114*e0c4386eSCy Schubert const struct index_cases_st *cases = doall_data->cases;
115*e0c4386eSCy Schubert size_t i;
116*e0c4386eSCy Schubert
117*e0c4386eSCy Schubert doall_data->res = 0;
118*e0c4386eSCy Schubert for (i = 0; i < doall_data->num_cases; i++)
119*e0c4386eSCy Schubert if ((doall_data->all || !cases[i].del)
120*e0c4386eSCy Schubert && n == cases[i].n && strcmp(value, cases[i].v) == 0) {
121*e0c4386eSCy Schubert doall_data->res = 1;
122*e0c4386eSCy Schubert return;
123*e0c4386eSCy Schubert }
124*e0c4386eSCy Schubert TEST_error("Index %ju with value %s not found", n, value);
125*e0c4386eSCy Schubert }
126*e0c4386eSCy Schubert
leaf_delete(ossl_uintmax_t n,char * value,void * arg)127*e0c4386eSCy Schubert static void leaf_delete(ossl_uintmax_t n, char *value, void *arg)
128*e0c4386eSCy Schubert {
129*e0c4386eSCy Schubert struct doall_st *doall_data = (struct doall_st *)arg;
130*e0c4386eSCy Schubert const struct index_cases_st *cases = doall_data->cases;
131*e0c4386eSCy Schubert size_t i;
132*e0c4386eSCy Schubert
133*e0c4386eSCy Schubert doall_data->res = 0;
134*e0c4386eSCy Schubert for (i = 0; i < doall_data->num_cases; i++)
135*e0c4386eSCy Schubert if (n == cases[i].n && strcmp(value, cases[i].v) == 0) {
136*e0c4386eSCy Schubert doall_data->res = 1;
137*e0c4386eSCy Schubert ossl_sa_char_set(doall_data->sa, n, NULL);
138*e0c4386eSCy Schubert return;
139*e0c4386eSCy Schubert }
140*e0c4386eSCy Schubert TEST_error("Index %ju with value %s not found", n, value);
141*e0c4386eSCy Schubert }
142*e0c4386eSCy Schubert
test_sparse_array_doall(void)143*e0c4386eSCy Schubert static int test_sparse_array_doall(void)
144*e0c4386eSCy Schubert {
145*e0c4386eSCy Schubert static const struct index_cases_st cases[] = {
146*e0c4386eSCy Schubert { 22, "A", 1 }, { 1021, "b", 0 }, { 3, "c", 0 }, { INT_MAX, "d", 1 },
147*e0c4386eSCy Schubert { (ossl_uintmax_t)-1, "H", 0 }, { (ossl_uintmax_t)-2, "i", 1 },
148*e0c4386eSCy Schubert { 666666666, "s", 1 }, { 1234567890, "t", 0 },
149*e0c4386eSCy Schubert };
150*e0c4386eSCy Schubert struct doall_st doall_data;
151*e0c4386eSCy Schubert size_t i;
152*e0c4386eSCy Schubert SPARSE_ARRAY_OF(char) *sa = NULL;
153*e0c4386eSCy Schubert int res = 0;
154*e0c4386eSCy Schubert
155*e0c4386eSCy Schubert if (!TEST_ptr(sa = ossl_sa_char_new()))
156*e0c4386eSCy Schubert goto err;
157*e0c4386eSCy Schubert doall_data.num_cases = OSSL_NELEM(cases);
158*e0c4386eSCy Schubert doall_data.cases = cases;
159*e0c4386eSCy Schubert doall_data.all = 1;
160*e0c4386eSCy Schubert doall_data.sa = NULL;
161*e0c4386eSCy Schubert for (i = 0; i < OSSL_NELEM(cases); i++)
162*e0c4386eSCy Schubert if (!TEST_true(ossl_sa_char_set(sa, cases[i].n, cases[i].v))) {
163*e0c4386eSCy Schubert TEST_note("failed at iteration %zu", i + 1);
164*e0c4386eSCy Schubert goto err;
165*e0c4386eSCy Schubert }
166*e0c4386eSCy Schubert
167*e0c4386eSCy Schubert ossl_sa_char_doall_arg(sa, &leaf_check_all, &doall_data);
168*e0c4386eSCy Schubert if (doall_data.res == 0) {
169*e0c4386eSCy Schubert TEST_info("while checking all elements");
170*e0c4386eSCy Schubert goto err;
171*e0c4386eSCy Schubert }
172*e0c4386eSCy Schubert doall_data.all = 0;
173*e0c4386eSCy Schubert doall_data.sa = sa;
174*e0c4386eSCy Schubert ossl_sa_char_doall_arg(sa, &leaf_delete, &doall_data);
175*e0c4386eSCy Schubert if (doall_data.res == 0) {
176*e0c4386eSCy Schubert TEST_info("while deleting selected elements");
177*e0c4386eSCy Schubert goto err;
178*e0c4386eSCy Schubert }
179*e0c4386eSCy Schubert ossl_sa_char_doall_arg(sa, &leaf_check_all, &doall_data);
180*e0c4386eSCy Schubert if (doall_data.res == 0) {
181*e0c4386eSCy Schubert TEST_info("while checking for deleted elements");
182*e0c4386eSCy Schubert goto err;
183*e0c4386eSCy Schubert }
184*e0c4386eSCy Schubert res = 1;
185*e0c4386eSCy Schubert
186*e0c4386eSCy Schubert err:
187*e0c4386eSCy Schubert ossl_sa_char_free(sa);
188*e0c4386eSCy Schubert return res;
189*e0c4386eSCy Schubert }
190*e0c4386eSCy Schubert
setup_tests(void)191*e0c4386eSCy Schubert int setup_tests(void)
192*e0c4386eSCy Schubert {
193*e0c4386eSCy Schubert ADD_TEST(test_sparse_array);
194*e0c4386eSCy Schubert ADD_TEST(test_sparse_array_num);
195*e0c4386eSCy Schubert ADD_TEST(test_sparse_array_doall);
196*e0c4386eSCy Schubert return 1;
197*e0c4386eSCy Schubert }
198