strtoultest.c 2.6 KB

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