ctype_internal_test.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 "testutil.h"
  10. #include "crypto/ctype.h"
  11. #include "internal/nelem.h"
  12. #include <ctype.h>
  13. #include <stdio.h>
  14. /*
  15. * Even though the VMS C RTL claims to be C99 compatible, it's not entirely
  16. * so far (C RTL version 8.4). Same applies to OSF. For the sake of these
  17. * tests, we therefore define our own.
  18. */
  19. #if (defined(__VMS) && __CRTL_VER <= 80400000) || defined(__osf__)
  20. static int isblank(int c)
  21. {
  22. return c == ' ' || c == '\t';
  23. }
  24. #endif
  25. static int test_ctype_chars(int n)
  26. {
  27. if (!TEST_int_eq(isascii((unsigned char)n) != 0, ossl_isascii(n) != 0))
  28. return 0;
  29. if (!ossl_isascii(n))
  30. return 1;
  31. return TEST_int_eq(isalpha(n) != 0, ossl_isalpha(n) != 0)
  32. && TEST_int_eq(isalnum(n) != 0, ossl_isalnum(n) != 0)
  33. #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
  34. && TEST_int_eq(isblank(n) != 0, ossl_isblank(n) != 0)
  35. #endif
  36. && TEST_int_eq(iscntrl(n) != 0, ossl_iscntrl(n) != 0)
  37. && TEST_int_eq(isdigit(n) != 0, ossl_isdigit(n) != 0)
  38. && TEST_int_eq(isgraph(n) != 0, ossl_isgraph(n) != 0)
  39. && TEST_int_eq(islower(n) != 0, ossl_islower(n) != 0)
  40. && TEST_int_eq(isprint(n) != 0, ossl_isprint(n) != 0)
  41. && TEST_int_eq(ispunct(n) != 0, ossl_ispunct(n) != 0)
  42. && TEST_int_eq(isspace(n) != 0, ossl_isspace(n) != 0)
  43. && TEST_int_eq(isupper(n) != 0, ossl_isupper(n) != 0)
  44. && TEST_int_eq(isxdigit(n) != 0, ossl_isxdigit(n) != 0);
  45. }
  46. static struct {
  47. int u;
  48. int l;
  49. } case_change[] = {
  50. { 'A', 'a' },
  51. { 'X', 'x' },
  52. { 'Z', 'z' },
  53. { '0', '0' },
  54. { '%', '%' },
  55. { '~', '~' },
  56. { 0, 0 },
  57. { EOF, EOF }
  58. };
  59. static int test_ctype_toupper(int n)
  60. {
  61. return TEST_int_eq(ossl_toupper(case_change[n].l), case_change[n].u)
  62. && TEST_int_eq(ossl_toupper(case_change[n].u), case_change[n].u);
  63. }
  64. static int test_ctype_tolower(int n)
  65. {
  66. return TEST_int_eq(ossl_tolower(case_change[n].u), case_change[n].l)
  67. && TEST_int_eq(ossl_tolower(case_change[n].l), case_change[n].l);
  68. }
  69. static int test_ctype_eof(void)
  70. {
  71. return test_ctype_chars(EOF);
  72. }
  73. int setup_tests(void)
  74. {
  75. ADD_ALL_TESTS(test_ctype_chars, 256);
  76. ADD_ALL_TESTS(test_ctype_toupper, OSSL_NELEM(case_change));
  77. ADD_ALL_TESTS(test_ctype_tolower, OSSL_NELEM(case_change));
  78. ADD_TEST(test_ctype_eof);
  79. return 1;
  80. }