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