Skip to content

Commit d76386f

Browse files
committed
Add tests
1 parent 05c8bc7 commit d76386f

6 files changed

Lines changed: 216 additions & 1 deletion

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2636,6 +2636,7 @@ if(WOLFSSL_EXAMPLES)
26362636
tests/api/test_tls_ext.c
26372637
tests/api/test_tls.c
26382638
tests/api/test_x509.c
2639+
tests/api/test_asn.c
26392640
tests/srp.c
26402641
tests/suites.c
26412642
tests/w64wrapper.c

tests/api.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@
332332
#include <tests/api/test_tls_ext.h>
333333
#include <tests/api/test_tls.h>
334334
#include <tests/api/test_x509.h>
335+
#include <tests/api/test_asn.h>
335336

336337
#if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_TLS) && \
337338
!defined(NO_RSA) && !defined(SINGLE_THREADED) && \
@@ -67444,6 +67445,9 @@ TEST_CASE testCases[] = {
6744467445
/* x509 */
6744567446
TEST_X509_DECLS,
6744667447

67448+
/* ASN */
67449+
TEST_ASN_DECLS,
67450+
6744767451
/* PEM and DER APIs. */
6744867452
TEST_DECL(test_wc_PemToDer),
6744967453
TEST_DECL(test_wc_AllocDer),

tests/api/include.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ tests_unit_test_SOURCES += tests/api/test_tls_ext.c
5656
tests_unit_test_SOURCES += tests/api/test_tls.c
5757
# Certs
5858
tests_unit_test_SOURCES += tests/api/test_x509.c
59+
# ASN
60+
tests_unit_test_SOURCES += tests/api/test_asn.c
5961
endif
6062

6163
EXTRA_DIST += tests/api/api.h
@@ -108,4 +110,5 @@ EXTRA_DIST += tests/api/test_evp.h
108110
EXTRA_DIST += tests/api/test_tls_ext.h
109111
EXTRA_DIST += tests/api/test_tls.h
110112
EXTRA_DIST += tests/api/test_x509.h
113+
EXTRA_DIST += tests/api/test_asn.h
111114

tests/api/test_asn.c

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/* test_asn.c
2+
*
3+
* Copyright (C) 2006-2025 wolfSSL Inc.
4+
*
5+
* This file is part of wolfSSL.
6+
*
7+
* wolfSSL is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfSSL is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+
*/
21+
22+
#include <tests/unit.h>
23+
24+
#include <tests/api/test_asn.h>
25+
26+
static int test_SetShortInt_once(word32 val, byte* valDer, word32 valDerSz)
27+
{
28+
EXPECT_DECLS;
29+
30+
#ifndef NO_PWDBASED
31+
#if !defined(WOLFSSL_ASN_TEMPLATE) || defined(HAVE_PKCS8) || \
32+
defined(HAVE_PKCS12)
33+
34+
byte outDer[MAX_SHORT_SZ];
35+
word32 outDerSz = 0;
36+
word32 inOutIdx = 0;
37+
word32 maxIdx = MAX_SHORT_SZ;
38+
39+
ExpectIntLE(2 + valDerSz, MAX_SHORT_SZ);
40+
ExpectIntEQ(outDerSz = SetShortInt(outDer, &inOutIdx, val, maxIdx),
41+
2 + valDerSz);
42+
ExpectIntEQ(outDer[0], ASN_INTEGER);
43+
ExpectIntEQ(outDer[1], valDerSz);
44+
ExpectIntEQ(XMEMCMP(outDer + 2, valDer, valDerSz), 0);
45+
46+
#endif /* !WOLFSSL_ASN_TEMPLATE || HAVE_PKCS8 || HAVE_PKCS12 */
47+
#endif /* !NO_PWDBASED */
48+
49+
(void)val;
50+
(void)valDer;
51+
(void)valDerSz;
52+
53+
return EXPECT_RESULT();
54+
}
55+
56+
int test_SetShortInt(void)
57+
{
58+
EXPECT_DECLS;
59+
60+
byte valDer[MAX_SHORT_SZ] = {0};
61+
62+
/* Corner tests for input size */
63+
{
64+
/* Input 1 byte min */
65+
valDer[0] = 0x00;
66+
EXPECT_TEST(test_SetShortInt_once(0x00, valDer, 1));
67+
68+
/* Input 1 byte max */
69+
valDer[0] = 0x00;
70+
valDer[1] = 0xff;
71+
EXPECT_TEST(test_SetShortInt_once(0xff, valDer, 2));
72+
73+
/* Input 2 bytes min */
74+
valDer[0] = 0x01;
75+
valDer[1] = 0x00;
76+
EXPECT_TEST(test_SetShortInt_once(0x0100, valDer, 2));
77+
78+
/* Input 2 bytes max */
79+
valDer[0] = 0x00;
80+
valDer[1] = 0xff;
81+
valDer[2] = 0xff;
82+
EXPECT_TEST(test_SetShortInt_once(0xffff, valDer, 3));
83+
84+
/* Input 3 bytes min */
85+
valDer[0] = 0x01;
86+
valDer[1] = 0x00;
87+
valDer[2] = 0x00;
88+
EXPECT_TEST(test_SetShortInt_once(0x010000, valDer, 3));
89+
90+
/* Input 3 bytes max */
91+
valDer[0] = 0x00;
92+
valDer[1] = 0xff;
93+
valDer[2] = 0xff;
94+
valDer[3] = 0xff;
95+
EXPECT_TEST(test_SetShortInt_once(0xffffff, valDer, 4));
96+
97+
/* Input 4 bytes min */
98+
valDer[0] = 0x01;
99+
valDer[1] = 0x00;
100+
valDer[2] = 0x00;
101+
valDer[3] = 0x00;
102+
EXPECT_TEST(test_SetShortInt_once(0x01000000, valDer, 4));
103+
104+
/* Input 4 bytes max */
105+
valDer[0] = 0x00;
106+
valDer[1] = 0xff;
107+
valDer[2] = 0xff;
108+
valDer[3] = 0xff;
109+
valDer[4] = 0xff;
110+
EXPECT_TEST(test_SetShortInt_once(0xffffffff, valDer, 5));
111+
}
112+
113+
/* Corner tests for output size */
114+
{
115+
/* Skip "Output 1 byte min" because of same as "Input 1 byte min" */
116+
117+
/* Output 1 byte max */
118+
valDer[0] = 0x7f;
119+
EXPECT_TEST(test_SetShortInt_once(0x7f, valDer, 1));
120+
121+
/* Output 2 bytes min */
122+
valDer[0] = 0x00;
123+
valDer[1] = 0x80;
124+
EXPECT_TEST(test_SetShortInt_once(0x80, valDer, 2));
125+
126+
/* Output 2 bytes max */
127+
valDer[0] = 0x7f;
128+
valDer[1] = 0xff;
129+
EXPECT_TEST(test_SetShortInt_once(0x7fff, valDer, 2));
130+
131+
/* Output 3 bytes min */
132+
valDer[0] = 0x00;
133+
valDer[1] = 0x80;
134+
valDer[2] = 0x00;
135+
EXPECT_TEST(test_SetShortInt_once(0x8000, valDer, 3));
136+
137+
/* Output 3 bytes max */
138+
valDer[0] = 0x7f;
139+
valDer[1] = 0xff;
140+
valDer[2] = 0xff;
141+
EXPECT_TEST(test_SetShortInt_once(0x7fffff, valDer, 3));
142+
143+
/* Output 4 bytes min */
144+
valDer[0] = 0x00;
145+
valDer[1] = 0x80;
146+
valDer[2] = 0x00;
147+
valDer[3] = 0x00;
148+
EXPECT_TEST(test_SetShortInt_once(0x800000, valDer, 4));
149+
150+
/* Output 4 bytes max */
151+
valDer[0] = 0x7f;
152+
valDer[1] = 0xff;
153+
valDer[2] = 0xff;
154+
valDer[3] = 0xff;
155+
EXPECT_TEST(test_SetShortInt_once(0x7fffffff, valDer, 4));
156+
157+
/* Output 5 bytes min */
158+
valDer[0] = 0x00;
159+
valDer[1] = 0x80;
160+
valDer[2] = 0x00;
161+
valDer[3] = 0x00;
162+
valDer[4] = 0x00;
163+
EXPECT_TEST(test_SetShortInt_once(0x80000000, valDer, 5));
164+
165+
/* Skip "Output 5 bytes max" because of same as "Input 4 bytes max" */
166+
}
167+
168+
/* Extra tests */
169+
{
170+
valDer[0] = 0x01;
171+
EXPECT_TEST(test_SetShortInt_once(0x01, valDer, 1));
172+
}
173+
174+
return EXPECT_RESULT();
175+
}

tests/api/test_asn.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* test_asn.h
2+
*
3+
* Copyright (C) 2006-2025 wolfSSL Inc.
4+
*
5+
* This file is part of wolfSSL.
6+
*
7+
* wolfSSL is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfSSL is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+
*/
21+
22+
#ifndef WOLFCRYPT_TEST_ASN_H
23+
#define WOLFCRYPT_TEST_ASN_H
24+
25+
#include <tests/api/api_decl.h>
26+
27+
int test_SetShortInt(void);
28+
29+
#define TEST_ASN_DECLS \
30+
TEST_DECL_GROUP("asn", test_SetShortInt) \
31+
32+
#endif /* WOLFCRYPT_TEST_ASN_H */

wolfssl/wolfcrypt/asn.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2202,7 +2202,7 @@ WOLFSSL_LOCAL byte GetCertNameId(int idx);
22022202
#endif
22032203
WOLFSSL_LOCAL int GetShortInt(const byte* input, word32* inOutIdx, int* number,
22042204
word32 maxIdx);
2205-
WOLFSSL_LOCAL int SetShortInt(byte* input, word32* inOutIdx, word32 number,
2205+
WOLFSSL_TEST_VIS int SetShortInt(byte* input, word32* inOutIdx, word32 number,
22062206
word32 maxIdx);
22072207

22082208
WOLFSSL_LOCAL const char* GetSigName(int oid);

0 commit comments

Comments
 (0)