/*
* 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
* https://www.openssl.org/source/license.html
*/
-/*
- * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
- */
-
#include <stdio.h>
#include <string.h>
#include <openssl/opensslconf.h>
#include <openssl/err.h>
#include <openssl/crypto.h>
+#include <openssl/bn.h>
#include "e_os.h"
#include "testutil.h"
| !TEST(0, TEST_str_eq("\1\2\3\4\5", "\1x\3\6\5"))
| !TEST(0, TEST_str_ne("abc", buf))
| !TEST(1, TEST_str_ne("abc", NULL))
- | !TEST(1, TEST_str_ne(NULL, buf)))
+ | !TEST(1, TEST_str_ne(NULL, buf))
+ | !TEST(0, TEST_str_eq("abcdef", "abcdefghijk")))
goto err;
return 1;
return TEST(0, TEST_mem_eq(p, strlen(p), q, strlen(q)));
}
+static int test_bignum(void)
+{
+ BIGNUM *a = NULL, *b = NULL, *c = NULL;
+ int r = 0;
+
+ if (!TEST(1, TEST_int_eq(BN_dec2bn(&a, "0"), 1))
+ | !TEST(1, TEST_BN_eq_word(a, 0))
+ | !TEST(0, TEST_BN_eq_word(a, 30))
+ | !TEST(1, TEST_BN_abs_eq_word(a, 0))
+ | !TEST(0, TEST_BN_eq_one(a))
+ | !TEST(1, TEST_BN_eq_zero(a))
+ | !TEST(0, TEST_BN_ne_zero(a))
+ | !TEST(1, TEST_BN_le_zero(a))
+ | !TEST(0, TEST_BN_lt_zero(a))
+ | !TEST(1, TEST_BN_ge_zero(a))
+ | !TEST(0, TEST_BN_gt_zero(a))
+ | !TEST(1, TEST_BN_even(a))
+ | !TEST(0, TEST_BN_odd(a))
+ | !TEST(1, TEST_BN_eq(b, c))
+ | !TEST(0, TEST_BN_eq(a, b))
+ | !TEST(0, TEST_BN_ne(NULL, c))
+ | !TEST(1, TEST_int_eq(BN_dec2bn(&b, "1"), 1))
+ | !TEST(1, TEST_BN_eq_word(b, 1))
+ | !TEST(1, TEST_BN_eq_one(b))
+ | !TEST(0, TEST_BN_abs_eq_word(b, 0))
+ | !TEST(1, TEST_BN_abs_eq_word(b, 1))
+ | !TEST(0, TEST_BN_eq_zero(b))
+ | !TEST(1, TEST_BN_ne_zero(b))
+ | !TEST(0, TEST_BN_le_zero(b))
+ | !TEST(0, TEST_BN_lt_zero(b))
+ | !TEST(1, TEST_BN_ge_zero(b))
+ | !TEST(1, TEST_BN_gt_zero(b))
+ | !TEST(0, TEST_BN_even(b))
+ | !TEST(1, TEST_BN_odd(b))
+ | !TEST(1, TEST_int_eq(BN_dec2bn(&c, "-334739439"), 10))
+ | !TEST(0, TEST_BN_eq_word(c, 334739439))
+ | !TEST(1, TEST_BN_abs_eq_word(c, 334739439))
+ | !TEST(0, TEST_BN_eq_zero(c))
+ | !TEST(1, TEST_BN_ne_zero(c))
+ | !TEST(1, TEST_BN_le_zero(c))
+ | !TEST(1, TEST_BN_lt_zero(c))
+ | !TEST(0, TEST_BN_ge_zero(c))
+ | !TEST(0, TEST_BN_gt_zero(c))
+ | !TEST(0, TEST_BN_even(c))
+ | !TEST(1, TEST_BN_odd(c))
+ | !TEST(1, TEST_BN_eq(a, a))
+ | !TEST(0, TEST_BN_ne(a, a))
+ | !TEST(0, TEST_BN_eq(a, b))
+ | !TEST(1, TEST_BN_ne(a, b))
+ | !TEST(0, TEST_BN_lt(a, c))
+ | !TEST(1, TEST_BN_lt(c, b))
+ | !TEST(0, TEST_BN_lt(b, c))
+ | !TEST(0, TEST_BN_le(a, c))
+ | !TEST(1, TEST_BN_le(c, b))
+ | !TEST(0, TEST_BN_le(b, c))
+ | !TEST(1, TEST_BN_gt(a, c))
+ | !TEST(0, TEST_BN_gt(c, b))
+ | !TEST(1, TEST_BN_gt(b, c))
+ | !TEST(1, TEST_BN_ge(a, c))
+ | !TEST(0, TEST_BN_ge(c, b))
+ | !TEST(1, TEST_BN_ge(b, c)))
+ goto err;
+
+ r = 1;
+err:
+ BN_free(a);
+ BN_free(b);
+ BN_free(c);
+ return r;
+}
+
static int test_long_output(void)
{
const char *p = "1234567890123456789012345678901234567890123456789012";
& TEST(0, TEST_mem_eq(r, strlen(r), s, strlen(s)));
}
+static int test_long_bignum(void)
+{
+ int r;
+ BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
+ const char as[] = "1234567890123456789012345678901234567890123456789012"
+ "1234567890123456789012345678901234567890123456789012"
+ "1234567890123456789012345678901234567890123456789012"
+ "1234567890123456789012345678901234567890123456789012"
+ "1234567890123456789012345678901234567890123456789012"
+ "1234567890123456789012345678901234567890123456789012"
+ "FFFFFF";
+ const char bs[] = "1234567890123456789012345678901234567890123456789012"
+ "1234567890123456789012345678901234567890123456789013"
+ "987657";
+ const char cs[] = "-" /* 64 characters plus sign */
+ "123456789012345678901234567890"
+ "123456789012345678901234567890"
+ "ABCD";
+ const char ds[] = "-" /* 63 characters plus sign */
+ "23456789A123456789B123456789C"
+ "123456789D123456789E123456789F"
+ "ABCD";
+
+ r = TEST_true(BN_hex2bn(&a, as))
+ && TEST_true(BN_hex2bn(&b, bs))
+ && TEST_true(BN_hex2bn(&c, cs))
+ && TEST_true(BN_hex2bn(&d, ds))
+ && (TEST(0, TEST_BN_eq(a, b))
+ & TEST(0, TEST_BN_eq(b, a))
+ & TEST(0, TEST_BN_eq(b, NULL))
+ & TEST(0, TEST_BN_eq(NULL, a))
+ & TEST(1, TEST_BN_ne(a, NULL))
+ & TEST(0, TEST_BN_eq(c, d)));
+ BN_free(a);
+ BN_free(b);
+ BN_free(c);
+ BN_free(d);
+ return r;
+}
+
static int test_messages(void)
{
TEST_info("This is an %s message.", "info");
&& TEST_mem_eq(p--, sizeof("456"), "456", sizeof("456"));
}
-void register_tests(void)
+static int test_output(void)
+{
+ const char s[] = "1234567890123456789012345678901234567890123456789012"
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
+
+ test_output_string("test", s, sizeof(s) - 1);
+ test_output_memory("test", (const unsigned char *)s, sizeof(s));
+ return 1;
+}
+
+static const char *bn_output_tests[] = {
+ NULL,
+ "0",
+ "-12345678",
+ "1234567890123456789012345678901234567890123456789012"
+ "1234567890123456789012345678901234567890123456789013"
+ "987657"
+};
+
+static int test_bn_output(int n)
+{
+ BIGNUM *b = NULL;
+
+ if (bn_output_tests[n] != NULL
+ && !TEST_true(BN_hex2bn(&b, bn_output_tests[n])))
+ return 0;
+ test_output_bignum(bn_output_tests[n], b);
+ BN_free(b);
+ return 1;
+}
+
+
+int setup_tests(void)
{
ADD_TEST(test_int);
ADD_TEST(test_uint);
ADD_TEST(test_string);
ADD_TEST(test_memory);
ADD_TEST(test_memory_overflow);
+ ADD_TEST(test_bignum);
+ ADD_TEST(test_long_bignum);
ADD_TEST(test_long_output);
ADD_TEST(test_messages);
ADD_TEST(test_single_eval);
+ ADD_TEST(test_output);
+ ADD_ALL_TESTS(test_bn_output, OSSL_NELEM(bn_output_tests));
+ return 1;
}