xref: /openssl/test/strtoultest.c (revision 7d91d5ba)
1 /*
2  * Copyright 2016-2024 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 <openssl/crypto.h>
11 #include <internal/nelem.h>
12 #include "testutil.h"
13 
14 struct strtoul_test_entry {
15     char *input; /* the input string */
16     int base; /* the base we are converting in */
17     unsigned long expect_val; /* the expected value we should get */
18     int expect_err;  /* the expected error we expect to receive */
19     size_t expect_endptr_offset; /* the expected endptr offset, +1 for NULL */
20 };
21 
22 static struct strtoul_test_entry strtoul_tests[] = {
23     /* pass on conv "0" to 0 */
24     {
25         "0", 0, 0, 1, 1
26     },
27     /* pass on conv "12345" to 12345 */
28     {
29         "12345", 0, 12345, 1, 5
30     },
31     /* pass on conv "0x12345" to 0x12345, base 16 */
32     {
33         "0x12345", 0, 0x12345, 1, 7
34     },
35     /* pass on base 10 translation, endptr points to 'x' */
36     {
37         "0x12345", 10, 0, 1, 1
38     },
39 #if ULONG_MAX == 4294967295
40     /* pass on ULONG_MAX translation */
41     {
42         "4294967295", 0, ULONG_MAX, 1, 10
43     },
44 #else
45     {
46         "18446744073709551615", 0, ULONG_MAX, 1, 20
47     },
48 #endif
49 
50     /* fail on negative input */
51     {
52         "-1", 0, 0, 0, 0
53     },
54     /* fail on non-numerical input */
55     {
56         "abcd", 0, 0, 0, 0
57     },
58     /* pass on decimal input */
59     {
60         "1.0", 0, 1, 1, 1
61     },
62     /* Fail on decimal input without leading number */
63     {
64         ".1", 0, 0, 0, 0
65     }
66 };
67 
test_strtoul(int idx)68 static int test_strtoul(int idx)
69 {
70     unsigned long val;
71     char *endptr = NULL;
72     int err;
73     struct strtoul_test_entry *test = &strtoul_tests[idx];
74 
75     /*
76      * For each test, convert the string to an unsigned long
77      */
78     err = OPENSSL_strtoul(test->input, &endptr, test->base, &val);
79 
80     /*
81      * Check to ensure the error returned is expected
82      */
83     if (!TEST_int_eq(err, test->expect_err))
84         return 0;
85     /*
86      * Confirm that the endptr points to where we expect
87      */
88     if (!TEST_ptr_eq(endptr, &test->input[test->expect_endptr_offset]))
89         return 0;
90     /*
91      * And check that we received the proper translated value
92      * Note, we only check the value if the conversion passed
93      */
94     if (test->expect_err == 1) {
95         if (!TEST_ulong_eq(val, test->expect_val))
96             return 0;
97     }
98     return 1;
99 
100 }
101 
setup_tests(void)102 int setup_tests(void)
103 {
104     ADD_ALL_TESTS(test_strtoul, OSSL_NELEM(strtoul_tests));
105     return 1;
106 }
107