From: zriback Date: Fri, 24 Jan 2025 20:43:22 +0000 (-0500) Subject: Add 20-test_prime unit test X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0b7a16fe09ce7d78346587f84146a266a55b63e8;p=thirdparty%2Fopenssl.git Add 20-test_prime unit test Reviewed-by: Paul Dale Reviewed-by: Viktor Dukhovni (Merged from https://github.com/openssl/openssl/pull/26549) --- diff --git a/apps/prime.c b/apps/prime.c index 7ee1a71e268..cbbcd4262ff 100644 --- a/apps/prime.c +++ b/apps/prime.c @@ -145,6 +145,7 @@ opthelp: } else { for ( ; *argv; argv++) { char *check_val; + int bytes_read = 0; int total_read = 0; int r; @@ -157,21 +158,20 @@ opthelp: goto end; } - int i; file_read_buf = (char *)app_malloc(BUFSIZE, "File read buffer"); while (BIO_pending(in) || !BIO_eof(in)) { - i = BIO_read(in, (char *)(file_read_buf + total_read), BUFSIZE); - if (i < 0) { + bytes_read = BIO_read(in, (char *)(file_read_buf + total_read), BUFSIZE); + if (bytes_read < 0) { BIO_printf(bio_err, "Read error in %s\n", argv[0]); goto end; } - if (i == 0) + if (bytes_read == 0) break; - total_read += i; - if (i == BUFSIZE) + total_read += bytes_read; + if (bytes_read == BUFSIZE) file_read_buf = (char *)realloc(file_read_buf, BUFSIZE + total_read); } - + /* Trim trailing newline if present */ if (file_read_buf[total_read - 1] == '\n') file_read_buf[total_read - 1] = '\0'; diff --git a/test/recipes/20-test_prime.t b/test/recipes/20-test_prime.t new file mode 100644 index 00000000000..3d22ae3ffa4 --- /dev/null +++ b/test/recipes/20-test_prime.t @@ -0,0 +1,48 @@ +#! /usr/bin/env perl +# Copyright 2020-2025 The OpenSSL Project Authors. All Rights Reserved. +# +# Licensed under the Apache License 2.0 (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 warnings; + +use OpenSSL::Test qw(:DEFAULT data_file); +use OpenSSL::Test; + +setup("test_prime"); + +plan tests => 8; + +my $prime_file = data_file("prime.txt"); +my $composite_file = data_file("composite.txt"); +my $long_number_file = data_file("long_number.txt"); +my $non_number_file = data_file("non_number.txt"); +my $hex_number_file = data_file("hex_number.txt"); +my $bad_format_file = data_file("bad_format.txt"); + +ok(run(app(["openssl", "prime", "-in", $prime_file])), + "Run openssl prime with prime number -in file"); + +ok(run(app(["openssl", "prime", "-in", $composite_file])), + "Run openssl prime with composite number -in file"); + +ok(run(app(["openssl", "prime", "-in", $long_number_file])), + "Run openssl prime with long number -in file"); + +ok(!run(app(["openssl", "prime", "-in", $non_number_file])), + "Run openssl prime with non number -in file"); + +ok(run(app(["openssl", "prime", "-in", "-hex", $hex_number_file])), + "Run openssl prime with hex number -in file"); + +ok(!run(app(["openssl", "prime", "-in", $bad_format_file])), + "Run openssl prime with bad format -in file"); + +ok(run(app(["openssl", "prime", "-in", $prime_file, $composite_file, $long_number_file])), + "Run openssl prime with multiple -in files"); + +ok(!run(app(["openssl", "prime", "-in", "does_not_exist.txt"])), + "Run openssl prime with -in file that does not exist"); \ No newline at end of file diff --git a/test/recipes/20-test_prime_data/bad_format.txt b/test/recipes/20-test_prime_data/bad_format.txt new file mode 100644 index 00000000000..ce8c77db7f7 --- /dev/null +++ b/test/recipes/20-test_prime_data/bad_format.txt @@ -0,0 +1,2 @@ +123 +456 diff --git a/test/recipes/20-test_prime_data/composite.txt b/test/recipes/20-test_prime_data/composite.txt new file mode 100644 index 00000000000..48082f72f08 --- /dev/null +++ b/test/recipes/20-test_prime_data/composite.txt @@ -0,0 +1 @@ +12 diff --git a/test/recipes/20-test_prime_data/hex_number.txt b/test/recipes/20-test_prime_data/hex_number.txt new file mode 100644 index 00000000000..5da849b5c6f --- /dev/null +++ b/test/recipes/20-test_prime_data/hex_number.txt @@ -0,0 +1 @@ +ABC diff --git a/test/recipes/20-test_prime_data/long_number.txt b/test/recipes/20-test_prime_data/long_number.txt new file mode 100644 index 00000000000..8555ba56847 --- /dev/null +++ b/test/recipes/20-test_prime_data/long_number.txt @@ -0,0 +1 @@ +1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005537073003 diff --git a/test/recipes/20-test_prime_data/non_number.txt b/test/recipes/20-test_prime_data/non_number.txt new file mode 100644 index 00000000000..82a04b397b8 --- /dev/null +++ b/test/recipes/20-test_prime_data/non_number.txt @@ -0,0 +1 @@ +not a number diff --git a/test/recipes/20-test_prime_data/prime.txt b/test/recipes/20-test_prime_data/prime.txt new file mode 100644 index 00000000000..98d9bcb75a6 --- /dev/null +++ b/test/recipes/20-test_prime_data/prime.txt @@ -0,0 +1 @@ +17