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