diff options
author | Pauli <paul.dale@oracle.com> | 2017-08-21 07:19:17 +1000 |
---|---|---|
committer | Pauli <paul.dale@oracle.com> | 2017-08-22 09:45:25 +1000 |
commit | a1df06b36347a31c17d09e6ca3e1464bdf7eb4d5 (patch) | |
tree | beb260df46896a5c8668dd7f64c5dcefe677b1e6 /test | |
parent | 00dfbaad88a69ed8294d6039bf5f7d722f72bf39 (diff) | |
download | openssl-a1df06b36347a31c17d09e6ca3e1464bdf7eb4d5.tar.gz |
This has been added to avoid the situation where some host ctype.h functions
return true for characters > 127. I.e. they are allowing extended ASCII
characters through which then cause problems. E.g. marking superscript '2' as
a number then causes the common (ch - '0') conversion to number to fail
miserably. Likewise letters with diacritical marks can also cause problems.
If a non-ASCII character set is being used (currently only EBCDIC), it is
adjusted for.
The implementation uses a single table with a bit for each of the defined
classes. These functions accept an int argument and fail for
values out of range or for characters outside of the ASCII set. They will
work for both signed and unsigned character inputs.
Reviewed-by: Andy Polyakov <appro@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/4102)
Diffstat (limited to 'test')
-rw-r--r-- | test/build.info | 6 | ||||
-rw-r--r-- | test/ctype_internal_test.c | 74 | ||||
-rw-r--r-- | test/recipes/02-test_internal_ctype.t | 20 |
3 files changed, 99 insertions, 1 deletions
diff --git a/test/build.info b/test/build.info index 6abd635231..f1f26afef2 100644 --- a/test/build.info +++ b/test/build.info @@ -385,7 +385,7 @@ INCLUDE_MAIN___test_libtestutil_OLB = /INCLUDE=MAIN # names with the DLL import libraries. IF[{- $disabled{shared} || $target{build_scheme}->[1] ne 'windows' -}] PROGRAMS_NO_INST=asn1_internal_test modes_internal_test x509_internal_test \ - tls13encryptiontest wpackettest + tls13encryptiontest wpackettest ctype_internal_test IF[{- !$disabled{poly1305} -}] PROGRAMS_NO_INST=poly1305_internal_test ENDIF @@ -424,6 +424,10 @@ INCLUDE_MAIN___test_libtestutil_OLB = /INCLUDE=MAIN INCLUDE[wpackettest]=../include DEPEND[wpackettest]=../libcrypto ../libssl.a libtestutil.a + SOURCE[ctype_internal_test]=ctype_internal_test.c + INCLUDE[ctype_internal_test]=.. ../crypto/include ../include + DEPEND[ctype_internal_test]=../libcrypto.a libtestutil.a + SOURCE[siphash_internal_test]=siphash_internal_test.c INCLUDE[siphash_internal_test]=.. ../include ../crypto/include DEPEND[siphash_internal_test]=../libcrypto.a libtestutil.a diff --git a/test/ctype_internal_test.c b/test/ctype_internal_test.c new file mode 100644 index 0000000000..0a30c3dd84 --- /dev/null +++ b/test/ctype_internal_test.c @@ -0,0 +1,74 @@ +/* + * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#include "testutil.h" +#include "internal/ctype.h" +#include "../e_os.h" +#include <ctype.h> +#include <stdio.h> + +static int test_ctype_chars(int n) +{ + return TEST_int_eq(isalnum(n) != 0, ossl_isalnum(n) != 0) + && TEST_int_eq(isalpha(n) != 0, ossl_isalpha(n) != 0) + && TEST_int_eq(isascii(n) != 0, ossl_isascii(n) != 0) + && TEST_int_eq(isblank(n) != 0, ossl_isblank(n) != 0) + && TEST_int_eq(iscntrl(n) != 0, ossl_iscntrl(n) != 0) + && TEST_int_eq(isdigit(n) != 0, ossl_isdigit(n) != 0) + && TEST_int_eq(isgraph(n) != 0, ossl_isgraph(n) != 0) + && TEST_int_eq(islower(n) != 0, ossl_islower(n) != 0) + && TEST_int_eq(isprint(n) != 0, ossl_isprint(n) != 0) + && TEST_int_eq(ispunct(n) != 0, ossl_ispunct(n) != 0) + && TEST_int_eq(isspace(n) != 0, ossl_isspace(n) != 0) + && TEST_int_eq(isupper(n) != 0, ossl_isupper(n) != 0) + && TEST_int_eq(isxdigit(n) != 0, ossl_isxdigit(n) != 0); +} + +static int test_ctype_negative(int n) +{ + return test_ctype_chars(-n); +} + +static struct { + int u; + int l; +} case_change[] = { + { 'A', 'a' }, + { 'X', 'x' }, + { 'Z', 'z' }, + { '0', '0' }, + { '%', '%' }, + { '~', '~' }, + { 0, 0 }, + { EOF, EOF }, + { 333, 333 }, + { -333, -333 }, + { -128, -128 } +}; + +static int test_ctype_toupper(int n) +{ + return TEST_int_eq(ossl_toupper(case_change[n].l), case_change[n].u) + && TEST_int_eq(ossl_toupper(case_change[n].u), case_change[n].u); +} + +static int test_ctype_tolower(int n) +{ + return TEST_int_eq(ossl_tolower(case_change[n].u), case_change[n].l) + && TEST_int_eq(ossl_tolower(case_change[n].l), case_change[n].l); +} + +int setup_tests(void) +{ + ADD_ALL_TESTS(test_ctype_chars, 256); + ADD_ALL_TESTS(test_ctype_negative, 128); + ADD_ALL_TESTS(test_ctype_toupper, OSSL_NELEM(case_change)); + ADD_ALL_TESTS(test_ctype_tolower, OSSL_NELEM(case_change)); + return 1; +} diff --git a/test/recipes/02-test_internal_ctype.t b/test/recipes/02-test_internal_ctype.t new file mode 100644 index 0000000000..5bf81bdff1 --- /dev/null +++ b/test/recipes/02-test_internal_ctype.t @@ -0,0 +1,20 @@ +#! /usr/bin/env perl +# Copyright 2017 The OpenSSL Project Authors. All Rights Reserved. +# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. +# +# Licensed under the OpenSSL license (the "License"). You may not use +# this file except in compliance with the License. You can obtain a copy +# in the file LICENSE in the source distribution or at +# https://www.openssl.org/source/license.html + +use strict; +use OpenSSL::Test; # get 'plan' +use OpenSSL::Test::Simple; +use OpenSSL::Test::Utils; + +setup("test_internal_ctype"); + +plan skip_all => "This test is unsupported in a shared library build on Windows" + if $^O eq 'MSWin32' && !disabled("shared"); + +simple_test("test_internal_ctype", "ctype_internal_test"); |