Fix fragile explicit cert date tests.
[openssl.git] / test / list_test.c
1 /*
2  * Copyright 2022 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 <stdio.h>
11 #include <string.h>
12
13 #include <openssl/opensslconf.h>
14 #include <openssl/err.h>
15 #include <openssl/crypto.h>
16
17 #include "internal/list.h"
18 #include "internal/nelem.h"
19 #include "testutil.h"
20
21 typedef struct testl_st TESTL;
22 struct testl_st {
23     int n;
24     OSSL_LIST_MEMBER(fizz, TESTL);
25     OSSL_LIST_MEMBER(buzz, TESTL);
26 };
27
28 DEFINE_LIST_OF(fizz, TESTL);
29 DEFINE_LIST_OF(buzz, TESTL);
30
31 static int test_fizzbuzz(void)
32 {
33     OSSL_LIST(fizz) a;
34     OSSL_LIST(buzz) b;
35     TESTL elem[20];
36     const int nelem = OSSL_NELEM(elem);
37     int i, na = 0, nb = 0;
38
39     ossl_list_fizz_init(&a);
40     ossl_list_buzz_init(&b);
41
42     for (i = 1; i < nelem; i++) {
43         elem[i].n = i;
44         if (i % 3 == 0) {
45             ossl_list_fizz_insert_tail(&a, elem + i);
46             na++;
47         }
48         if (i % 5 == 0) {
49             ossl_list_buzz_insert_head(&b, elem + i);
50             nb++;
51         }
52     }
53
54     if (!TEST_size_t_eq(ossl_list_fizz_num(&a), na)
55             || !TEST_size_t_eq(ossl_list_buzz_num(&b), nb)
56             || !TEST_ptr(ossl_list_fizz_head(&a))
57             || !TEST_ptr(ossl_list_fizz_tail(&a))
58             || !TEST_ptr(ossl_list_buzz_head(&b))
59             || !TEST_ptr(ossl_list_buzz_tail(&b))
60             || !TEST_int_eq(ossl_list_fizz_head(&a)->n, 3)
61             || !TEST_int_eq(ossl_list_fizz_tail(&a)->n, na * 3)
62             || !TEST_int_eq(ossl_list_buzz_head(&b)->n, nb * 5)
63             || !TEST_int_eq(ossl_list_buzz_tail(&b)->n, 5))
64         return 0;
65     ossl_list_fizz_remove(&a, ossl_list_fizz_head(&a));
66     ossl_list_buzz_remove(&b, ossl_list_buzz_tail(&b));
67     if (!TEST_size_t_eq(ossl_list_fizz_num(&a), --na)
68             || !TEST_size_t_eq(ossl_list_buzz_num(&b), --nb)
69             || !TEST_ptr(ossl_list_fizz_head(&a))
70             || !TEST_ptr(ossl_list_buzz_tail(&b))
71             || !TEST_int_eq(ossl_list_fizz_head(&a)->n, 6)
72             || !TEST_int_eq(ossl_list_buzz_tail(&b)->n, 10)
73             || !TEST_ptr(ossl_list_fizz_next(ossl_list_fizz_head(&a)))
74             || !TEST_ptr(ossl_list_fizz_prev(ossl_list_fizz_tail(&a)))
75             || !TEST_int_eq(ossl_list_fizz_next(ossl_list_fizz_head(&a))->n, 9)
76             || !TEST_int_eq(ossl_list_fizz_prev(ossl_list_fizz_tail(&a))->n, 15))
77         return 0;
78     return 1;
79 }
80
81 typedef struct int_st INTL;
82 struct int_st {
83     int n;
84     OSSL_LIST_MEMBER(int, INTL);
85 };
86
87 DEFINE_LIST_OF(int, INTL);
88
89 static int test_insert(void)
90 {
91     INTL *c, *d;
92     OSSL_LIST(int) l;
93     INTL elem[20];
94     size_t i;
95     int n = 1;
96
97     ossl_list_int_init(&l);
98     for (i = 0; i < OSSL_NELEM(elem); i++)
99             elem[i].n = i;
100
101     /* Check various insert options - head, tail, middle */
102     ossl_list_int_insert_head(&l, elem + 3);                /* 3 */
103     ossl_list_int_insert_tail(&l, elem + 6);                /* 3 6 */
104     ossl_list_int_insert_before(&l, elem + 6, elem + 5);    /* 3 5 6 */
105     ossl_list_int_insert_before(&l, elem + 3, elem + 1);    /* 1 3 5 6 */
106     ossl_list_int_insert_after(&l, elem + 1, elem + 2);     /* 1 2 3 5 6 */
107     ossl_list_int_insert_after(&l, elem + 6, elem + 7);     /* 1 2 3 5 6 7 */
108     ossl_list_int_insert_after(&l, elem + 3, elem + 4);     /* 1 2 3 4 5 6 7 */
109     if (!TEST_size_t_eq(ossl_list_int_num(&l), 7))
110         return 0;
111     c = ossl_list_int_head(&l);
112     d = ossl_list_int_tail(&l);
113     while (c != NULL && d != NULL) {
114         if (!TEST_int_eq(c->n, n) || !TEST_int_eq(d->n, 8 - n))
115             return 0;
116         c = ossl_list_int_next(c);
117         d = ossl_list_int_prev(d);
118         n++;
119     }
120     if (!TEST_ptr_null(c) || !TEST_ptr_null(d))
121         return 0;
122
123     /* Check removing head, tail and middle */
124     ossl_list_int_remove(&l, elem + 1);                     /* 2 3 4 5 6 7 */
125     ossl_list_int_remove(&l, elem + 6);                     /* 2 3 4 5 7 */
126     ossl_list_int_remove(&l, elem + 7);                     /* 2 3 4 5 */
127     n = 2;
128     c = ossl_list_int_head(&l);
129     d = ossl_list_int_tail(&l);
130     while (c != NULL && d != NULL) {
131         if (!TEST_int_eq(c->n, n) || !TEST_int_eq(d->n, 7 - n))
132             return 0;
133         c = ossl_list_int_next(c);
134         d = ossl_list_int_prev(d);
135         n++;
136     }
137     if (!TEST_ptr_null(c) || !TEST_ptr_null(d))
138         return 0;
139
140     /* Check removing the head of a two element list works */
141     ossl_list_int_remove(&l, elem + 2);                     /* 3 4 5 */
142     ossl_list_int_remove(&l, elem + 4);                     /* 3 5 */
143     ossl_list_int_remove(&l, elem + 3);                     /* 5 */
144     if (!TEST_ptr(ossl_list_int_head(&l))
145             || !TEST_ptr(ossl_list_int_tail(&l))
146             || !TEST_int_eq(ossl_list_int_head(&l)->n, 5)
147             || !TEST_int_eq(ossl_list_int_tail(&l)->n, 5))
148         return 0;
149
150     /* Check removing the tail of a two element list works */
151     ossl_list_int_insert_head(&l, elem);                    /* 0 5 */
152     ossl_list_int_remove(&l, elem + 5);                     /* 0 */
153     if (!TEST_ptr(ossl_list_int_head(&l))
154             || !TEST_ptr(ossl_list_int_tail(&l))
155             || !TEST_int_eq(ossl_list_int_head(&l)->n, 0)
156             || !TEST_int_eq(ossl_list_int_tail(&l)->n, 0))
157         return 0;
158
159     /* Check removing the only element works */
160     ossl_list_int_remove(&l, elem);
161     if (!TEST_ptr_null(ossl_list_int_head(&l))
162             || !TEST_ptr_null(ossl_list_int_tail(&l)))
163         return 0;
164     return 1;
165 }
166
167 int setup_tests(void)
168 {
169     ADD_TEST(test_fizzbuzz);
170     ADD_TEST(test_insert);
171     return 1;
172 }