]>
Commit | Line | Data |
---|---|---|
cc75cbc4 | 1 | /* |
1212818e | 2 | * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved. |
cc75cbc4 | 3 | * |
909f1a2e | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
cc75cbc4 RS |
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 | ||
cc75cbc4 | 10 | #include <string.h> |
cbf0cfaf | 11 | #include "testutil.h" |
3a63c0ed | 12 | #include "internal/numbers.h" |
cc75cbc4 | 13 | |
cbf0cfaf | 14 | static int test_sanity_null_zero(void) |
cc75cbc4 RS |
15 | { |
16 | char *p; | |
17 | char bytes[sizeof(p)]; | |
cc75cbc4 RS |
18 | |
19 | /* Is NULL equivalent to all-bytes-zero? */ | |
20 | p = NULL; | |
cbe29648 | 21 | memset(bytes, 0, sizeof(bytes)); |
cbf0cfaf P |
22 | return TEST_mem_eq(&p, sizeof(p), bytes, sizeof(bytes)); |
23 | } | |
24 | ||
25 | static int test_sanity_enum_size(void) | |
26 | { | |
27 | enum smallchoices { sa, sb, sc }; | |
28 | enum medchoices { ma, mb, mc, md, me, mf, mg, mh, mi, mj, mk, ml }; | |
29 | enum largechoices { | |
30 | a01, b01, c01, d01, e01, f01, g01, h01, i01, j01, | |
31 | a02, b02, c02, d02, e02, f02, g02, h02, i02, j02, | |
32 | a03, b03, c03, d03, e03, f03, g03, h03, i03, j03, | |
33 | a04, b04, c04, d04, e04, f04, g04, h04, i04, j04, | |
34 | a05, b05, c05, d05, e05, f05, g05, h05, i05, j05, | |
35 | a06, b06, c06, d06, e06, f06, g06, h06, i06, j06, | |
36 | a07, b07, c07, d07, e07, f07, g07, h07, i07, j07, | |
37 | a08, b08, c08, d08, e08, f08, g08, h08, i08, j08, | |
38 | a09, b09, c09, d09, e09, f09, g09, h09, i09, j09, | |
39 | a10, b10, c10, d10, e10, f10, g10, h10, i10, j10, | |
40 | xxx }; | |
cc75cbc4 RS |
41 | |
42 | /* Enum size */ | |
cbf0cfaf P |
43 | if (!TEST_size_t_eq(sizeof(enum smallchoices), sizeof(int)) |
44 | || !TEST_size_t_eq(sizeof(enum medchoices), sizeof(int)) | |
45 | || !TEST_size_t_eq(sizeof(enum largechoices), sizeof(int))) | |
46 | return 0; | |
47 | return 1; | |
48 | } | |
49 | ||
50 | static int test_sanity_twos_complement(void) | |
51 | { | |
cc75cbc4 | 52 | /* Basic two's complement checks. */ |
cbf0cfaf P |
53 | if (!TEST_int_eq(~(-1), 0) |
54 | || !TEST_long_eq(~(-1L), 0L)) | |
55 | return 0; | |
56 | return 1; | |
57 | } | |
cc75cbc4 | 58 | |
cbf0cfaf P |
59 | static int test_sanity_sign(void) |
60 | { | |
cc75cbc4 | 61 | /* Check that values with sign bit 1 and value bits 0 are valid */ |
cbf0cfaf P |
62 | if (!TEST_int_eq(-(INT_MIN + 1), INT_MAX) |
63 | || !TEST_long_eq(-(LONG_MIN + 1), LONG_MAX)) | |
64 | return 0; | |
65 | return 1; | |
66 | } | |
cc75cbc4 | 67 | |
46f4e1be | 68 | static int test_sanity_unsigned_conversion(void) |
cbf0cfaf | 69 | { |
cc75cbc4 | 70 | /* Check that unsigned-to-signed conversions preserve bit patterns */ |
cbf0cfaf P |
71 | if (!TEST_int_eq((int)((unsigned int)INT_MAX + 1), INT_MIN) |
72 | || !TEST_long_eq((long)((unsigned long)LONG_MAX + 1), LONG_MIN)) | |
73 | return 0; | |
74 | return 1; | |
75 | } | |
cc75cbc4 | 76 | |
1b3e2bbf P |
77 | static int test_sanity_range(void) |
78 | { | |
79 | /* This isn't possible to check using the framework functions */ | |
80 | if (SIZE_MAX < INT_MAX) { | |
81 | TEST_error("int must not be wider than size_t"); | |
82 | return 0; | |
83 | } | |
84 | return 1; | |
85 | } | |
86 | ||
3ef97bd8 P |
87 | static int test_sanity_memcmp(void) |
88 | { | |
89 | return CRYPTO_memcmp("ab","cd",2); | |
90 | } | |
91 | ||
ad887416 | 92 | int setup_tests(void) |
cbf0cfaf P |
93 | { |
94 | ADD_TEST(test_sanity_null_zero); | |
95 | ADD_TEST(test_sanity_enum_size); | |
96 | ADD_TEST(test_sanity_twos_complement); | |
97 | ADD_TEST(test_sanity_sign); | |
46f4e1be | 98 | ADD_TEST(test_sanity_unsigned_conversion); |
1b3e2bbf | 99 | ADD_TEST(test_sanity_range); |
3ef97bd8 | 100 | ADD_TEST(test_sanity_memcmp); |
ad887416 | 101 | return 1; |
cc75cbc4 | 102 | } |
cbf0cfaf | 103 |