]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Add 20-test_prime unit test
authorzriback <zacharythecdr@gmail.com>
Fri, 24 Jan 2025 20:43:22 +0000 (15:43 -0500)
committerViktor Dukhovni <openssl-users@dukhovni.org>
Tue, 29 Apr 2025 04:17:06 +0000 (14:17 +1000)
Reviewed-by: Paul Dale <ppzgs1@gmail.com>
Reviewed-by: Viktor Dukhovni <viktor@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/26549)

apps/prime.c
test/recipes/20-test_prime.t [new file with mode: 0644]
test/recipes/20-test_prime_data/bad_format.txt [new file with mode: 0644]
test/recipes/20-test_prime_data/composite.txt [new file with mode: 0644]
test/recipes/20-test_prime_data/hex_number.txt [new file with mode: 0644]
test/recipes/20-test_prime_data/long_number.txt [new file with mode: 0644]
test/recipes/20-test_prime_data/non_number.txt [new file with mode: 0644]
test/recipes/20-test_prime_data/prime.txt [new file with mode: 0644]

index 7ee1a71e26851c7dd49793e5b23ef65f66ed2fa5..cbbcd4262ff45da57fae28a9d67fcbfc7221a56a 100644 (file)
@@ -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 (file)
index 0000000..3d22ae3
--- /dev/null
@@ -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 (file)
index 0000000..ce8c77d
--- /dev/null
@@ -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 (file)
index 0000000..48082f7
--- /dev/null
@@ -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 (file)
index 0000000..5da849b
--- /dev/null
@@ -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 (file)
index 0000000..8555ba5
--- /dev/null
@@ -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 (file)
index 0000000..82a04b3
--- /dev/null
@@ -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 (file)
index 0000000..98d9bcb
--- /dev/null
@@ -0,0 +1 @@
+17