1#! /usr/bin/env perl
2# Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
3#
4# Licensed under the Apache License 2.0 (the "License").  You may not use
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
10use strict;
11use warnings;
12
13use File::Spec;
14use File::Compare qw/compare_text/;
15use OpenSSL::Glob;
16use OpenSSL::Test qw/:DEFAULT data_file/;
17use OpenSSL::Test::Utils;
18
19setup("test_ecparam");
20
21plan skip_all => "EC or EC2M isn't supported in this build"
22    if disabled("ec") || disabled("ec2m");
23
24my @valid = glob(data_file("valid", "*.pem"));
25my @noncanon = glob(data_file("noncanon", "*.pem"));
26my @invalid = glob(data_file("invalid", "*.pem"));
27
28plan tests => 11;
29
30sub checkload {
31    my $files = shift; # List of files
32    my $valid = shift; # Check should pass or fail?
33    my $app = shift;   # Which application
34    my $opt = shift;   # Additional option
35
36    foreach (@$files) {
37        if ($valid) {
38            ok(run(app(['openssl', $app, '-noout', $opt, '-in', $_])));
39        } else {
40            ok(!run(app(['openssl', $app, '-noout', $opt, '-in', $_])));
41        }
42    }
43}
44
45sub checkcompare {
46    my $files = shift; # List of files
47    my $app = shift;   # Which application
48
49    foreach (@$files) {
50        my $testout = "$app.tst";
51
52        ok(run(app(['openssl', $app, '-out', $testout, '-in', $_])));
53        ok(!compare_text($_, $testout, sub {
54            my $in1 = $_[0];
55            my $in2 = $_[1];
56            $in1 =~ s/\r\n/\n/g;
57            $in2 =~ s/\r\n/\n/g;
58            $in1 ne $in2}), "Original file $_ is the same as new one");
59    }
60}
61
62subtest "Check loading valid parameters by ecparam with -check" => sub {
63    plan tests => scalar(@valid);
64    checkload(\@valid, 1, "ecparam", "-check");
65};
66
67subtest "Check loading valid parameters by ecparam with -check_named" => sub {
68    plan tests => scalar(@valid);
69    checkload(\@valid, 1, "ecparam", "-check_named");
70};
71
72subtest "Check loading valid parameters by pkeyparam with -check" => sub {
73    plan tests => scalar(@valid);
74    checkload(\@valid, 1, "pkeyparam", "-check");
75};
76
77subtest "Check loading non-canonically encoded parameters by ecparam with -check" => sub {
78    plan tests => scalar(@noncanon);
79    checkload(\@noncanon, 1, "ecparam", "-check");
80};
81
82subtest "Check loading non-canonically encoded parameters by ecparam with -check_named" => sub {
83    plan tests => scalar(@noncanon);
84    checkload(\@noncanon, 1, "ecparam", "-check_named");
85};
86
87subtest "Check loading non-canonically encoded parameters by pkeyparam with -check" => sub {
88    plan tests => scalar(@noncanon);
89    checkload(\@noncanon, 1, "pkeyparam", "-check");
90};
91
92subtest "Check loading invalid parameters by ecparam with -check" => sub {
93    plan tests => scalar(@invalid);
94    checkload(\@invalid, 0, "ecparam", "-check");
95};
96
97subtest "Check loading invalid parameters by ecparam with -check_named" => sub {
98    plan tests => scalar(@invalid);
99    checkload(\@invalid, 0, "ecparam", "-check_named");
100};
101
102subtest "Check loading invalid parameters by pkeyparam with -check" => sub {
103    plan tests => scalar(@invalid);
104    checkload(\@invalid, 0, "pkeyparam", "-check");
105};
106
107subtest "Check ecparam does not change the parameter file on output" => sub {
108    plan tests => 2 * scalar(@valid);
109    checkcompare(\@valid, "ecparam");
110};
111
112subtest "Check pkeyparam does not change the parameter file on output" => sub {
113    plan tests => 2 * scalar(@valid);
114    checkcompare(\@valid, "pkeyparam");
115};
116