Commit cbf0cfaf authored by Pauli's avatar Pauli Committed by Matt Caswell
Browse files

Update sanitytest to use the test infrastructure

parent deeac6c3
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -35,7 +35,7 @@ IF[{- !$disabled{tests} -}]
  INCLUDE[aborttest]=../include
  DEPEND[aborttest]=../libcrypto

  SOURCE[sanitytest]=sanitytest.c
  SOURCE[sanitytest]=sanitytest.c testutil.c test_main.c
  INCLUDE[sanitytest]=../include
  DEPEND[sanitytest]=../libcrypto

+59 −39
Original line number Diff line number Diff line
/*
 * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
 * Copyright 2015-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
@@ -7,20 +7,25 @@
 * https://www.openssl.org/source/license.html
 */

#include <stdio.h>
#include <string.h>
#include <internal/numbers.h>

#include "test_main.h"
#include "testutil.h"

#define TEST(e) \
    do { \
        if (!(e)) { \
            fprintf(stderr, "Failed " #e "\n"); \
            failures++; \
        } \
    } while (0)
static int test_sanity_null_zero(void)
{
    char *p;
    char bytes[sizeof(p)];

    /* Is NULL equivalent to all-bytes-zero? */
    p = NULL;
    memset(bytes, 0, sizeof bytes);
    return TEST_mem_eq(&p, sizeof(p), bytes, sizeof(bytes));
}

static int test_sanity_enum_size(void)
{
    enum smallchoices { sa, sb, sc };
    enum medchoices { ma, mb, mc, md, me, mf, mg, mh, mi, mj, mk, ml };
    enum largechoices {
@@ -36,32 +41,47 @@ enum largechoices {
        a10, b10, c10, d10, e10, f10, g10, h10, i10, j10,
        xxx };

int main()
{
    char *p;
    char bytes[sizeof(p)];
    int failures = 0;

    /* Is NULL equivalent to all-bytes-zero? */
    p = NULL;
    memset(bytes, 0, sizeof bytes);
    TEST(memcmp(&p, bytes, sizeof(bytes)) == 0);

    /* Enum size */
    TEST(sizeof(enum smallchoices) == sizeof(int));
    TEST(sizeof(enum medchoices) == sizeof(int));
    TEST(sizeof(enum largechoices) == sizeof(int));
    if (!TEST_size_t_eq(sizeof(enum smallchoices), sizeof(int))
        || !TEST_size_t_eq(sizeof(enum medchoices), sizeof(int))
        || !TEST_size_t_eq(sizeof(enum largechoices), sizeof(int)))
        return 0;
    return 1;
}

static int test_sanity_twos_complement(void)
{
    /* Basic two's complement checks. */
    TEST(~(-1) == 0);
    TEST(~(-1L) == 0L);
    if (!TEST_int_eq(~(-1), 0)
        || !TEST_long_eq(~(-1L), 0L))
        return 0;
    return 1;
}

static int test_sanity_sign(void)
{
    /* Check that values with sign bit 1 and value bits 0 are valid */
    TEST(-(INT_MIN + 1) == INT_MAX);
    TEST(-(LONG_MIN + 1) == LONG_MAX);
    if (!TEST_int_eq(-(INT_MIN + 1), INT_MAX)
        || !TEST_long_eq(-(LONG_MIN + 1), LONG_MAX))
        return 0;
    return 1;
}

static int test_sanity_unsigned_convertion(void)
{
    /* Check that unsigned-to-signed conversions preserve bit patterns */
    TEST((int)((unsigned int)INT_MAX + 1) == INT_MIN);
    TEST((long)((unsigned long)LONG_MAX + 1) == LONG_MIN);
    if (!TEST_int_eq((int)((unsigned int)INT_MAX + 1), INT_MIN)
        || !TEST_long_eq((long)((unsigned long)LONG_MAX + 1), LONG_MIN))
        return 0;
    return 1;
}

    return failures;
void register_tests(void)
{
    ADD_TEST(test_sanity_null_zero);
    ADD_TEST(test_sanity_enum_size);
    ADD_TEST(test_sanity_twos_complement);
    ADD_TEST(test_sanity_sign);
    ADD_TEST(test_sanity_unsigned_convertion);
}