xref: /src/crypto/openssl/test/strtoultest.c (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d)
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     /* pass on conv "12345" to 12345 */
27     {
28         "12345", 0, 12345, 1, 5 },
29     /* pass on conv "0x12345" to 0x12345, base 16 */
30     {
31         "0x12345", 0, 0x12345, 1, 7 },
32     /* pass on base 10 translation, endptr points to 'x' */
33     {
34         "0x12345", 10, 0, 1, 1 },
35 #if ULONG_MAX == 4294967295
36     /* pass on ULONG_MAX translation */
37     {
38         "4294967295", 0, ULONG_MAX, 1, 10 },
39 #else
40     { "18446744073709551615", 0, ULONG_MAX, 1, 20 },
41 #endif
42 
43     /* fail on negative input */
44     {
45         "-1", 0, 0, 0, 0 },
46     /* fail on non-numerical input */
47     {
48         "abcd", 0, 0, 0, 0 },
49     /* pass on decimal input */
50     {
51         "1.0", 0, 1, 1, 1 },
52     /* Fail on decimal input without leading number */
53     {
54         ".1", 0, 0, 0, 0 }
55 };
56 
test_strtoul(int idx)57 static int test_strtoul(int idx)
58 {
59     unsigned long val;
60     char *endptr = NULL;
61     int err;
62     struct strtoul_test_entry *test = &strtoul_tests[idx];
63 
64     /*
65      * For each test, convert the string to an unsigned long
66      */
67     err = OPENSSL_strtoul(test->input, &endptr, test->base, &val);
68 
69     /*
70      * Check to ensure the error returned is expected
71      */
72     if (!TEST_int_eq(err, test->expect_err))
73         return 0;
74     /*
75      * Confirm that the endptr points to where we expect
76      */
77     if (!TEST_ptr_eq(endptr, &test->input[test->expect_endptr_offset]))
78         return 0;
79     /*
80      * And check that we received the proper translated value
81      * Note, we only check the value if the conversion passed
82      */
83     if (test->expect_err == 1) {
84         if (!TEST_ulong_eq(val, test->expect_val))
85             return 0;
86     }
87     return 1;
88 }
89 
setup_tests(void)90 int setup_tests(void)
91 {
92     ADD_ALL_TESTS(test_strtoul, OSSL_NELEM(strtoul_tests));
93     return 1;
94 }
95