1e1051a39Sopenharmony_ci#! /usr/bin/env perl
2e1051a39Sopenharmony_ci# Copyright 2007-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci# Copyright Nokia 2007-2019
4e1051a39Sopenharmony_ci# Copyright Siemens AG 2015-2019
5e1051a39Sopenharmony_ci#
6e1051a39Sopenharmony_ci# Licensed under the Apache License 2.0 (the "License").  You may not use
7e1051a39Sopenharmony_ci# this file except in compliance with the License.  You can obtain a copy
8e1051a39Sopenharmony_ci# in the file LICENSE in the source distribution or at
9e1051a39Sopenharmony_ci# https://www.openssl.org/source/license.html
10e1051a39Sopenharmony_ci
11e1051a39Sopenharmony_ciuse strict;
12e1051a39Sopenharmony_ciuse warnings;
13e1051a39Sopenharmony_ci
14e1051a39Sopenharmony_ciuse POSIX;
15e1051a39Sopenharmony_ciuse File::Compare qw/compare_text/;
16e1051a39Sopenharmony_ciuse OpenSSL::Test qw/:DEFAULT with srctop_file srctop_dir bldtop_dir result_file/;
17e1051a39Sopenharmony_ciuse OpenSSL::Test::Utils;
18e1051a39Sopenharmony_ci
19e1051a39Sopenharmony_ciBEGIN {
20e1051a39Sopenharmony_ci    setup("test_cmp_cli");
21e1051a39Sopenharmony_ci}
22e1051a39Sopenharmony_ciuse lib srctop_dir('Configurations');
23e1051a39Sopenharmony_ciuse lib bldtop_dir('.');
24e1051a39Sopenharmony_ci
25e1051a39Sopenharmony_ciplan skip_all => "These tests are not supported in a fuzz build"
26e1051a39Sopenharmony_ci    if config('options') =~ /-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION/;
27e1051a39Sopenharmony_ci
28e1051a39Sopenharmony_ciplan skip_all => "These tests are not supported in a no-cmp build"
29e1051a39Sopenharmony_ci    if disabled("cmp");
30e1051a39Sopenharmony_ci
31e1051a39Sopenharmony_ci# Prevent MSys2 filename munging for arguments that look like file paths but
32e1051a39Sopenharmony_ci# aren't
33e1051a39Sopenharmony_ci$ENV{MSYS2_ARG_CONV_EXCL} = "/CN=";
34e1051a39Sopenharmony_ci
35e1051a39Sopenharmony_cimy @app = qw(openssl cmp);
36e1051a39Sopenharmony_ci
37e1051a39Sopenharmony_cimy @cmp_basic_tests = (
38e1051a39Sopenharmony_ci    [ "show help",                        [ "-help"               ], 1 ],
39e1051a39Sopenharmony_ci    [ "CLI option not starting with '-'", [  "days", "1"          ], 0 ],
40e1051a39Sopenharmony_ci    [ "unknown CLI option",               [ "-dayss"              ], 0 ],
41e1051a39Sopenharmony_ci    [ "bad int syntax: non-digit",        [ "-days", "a/"         ], 0 ],
42e1051a39Sopenharmony_ci    [ "bad int syntax: float",            [ "-days", "3.14"       ], 0 ],
43e1051a39Sopenharmony_ci    [ "bad int syntax: trailing garbage", [ "-days", "314_+"      ], 0 ],
44e1051a39Sopenharmony_ci    [ "bad int: out of range",            [ "-days", "2147483648" ], 0 ],
45e1051a39Sopenharmony_ci    );
46e1051a39Sopenharmony_ci
47e1051a39Sopenharmony_cimy @cmp_server_tests = (
48e1051a39Sopenharmony_ci    [ "with polling",             [ "-poll_count", "1"       ], 1 ]
49e1051a39Sopenharmony_ci    );
50e1051a39Sopenharmony_ci
51e1051a39Sopenharmony_ci# loader_attic doesn't build on VMS, so we don't test it
52e1051a39Sopenharmony_cipush @cmp_server_tests, (
53e1051a39Sopenharmony_ci    [ "with loader_attic engine", [ "-engine", "loader_attic"], 1 ]
54e1051a39Sopenharmony_ci    )
55e1051a39Sopenharmony_ci    unless disabled('loadereng');
56e1051a39Sopenharmony_ci
57e1051a39Sopenharmony_ciplan tests => @cmp_basic_tests + @cmp_server_tests;
58e1051a39Sopenharmony_ci
59e1051a39Sopenharmony_ciforeach (@cmp_basic_tests) {
60e1051a39Sopenharmony_ci    my $title = $$_[0];
61e1051a39Sopenharmony_ci    my $params = $$_[1];
62e1051a39Sopenharmony_ci    my $expected = $$_[2];
63e1051a39Sopenharmony_ci    ok($expected == run(app([@app, "-config", '', @$params])),
64e1051a39Sopenharmony_ci       $title);
65e1051a39Sopenharmony_ci}
66e1051a39Sopenharmony_ci
67e1051a39Sopenharmony_ci# these use the mock server directly in the cmp app, without TCP
68e1051a39Sopenharmony_ciforeach (@cmp_server_tests) {
69e1051a39Sopenharmony_ci    my $title = $$_[0];
70e1051a39Sopenharmony_ci    my $extra_args = $$_[1];
71e1051a39Sopenharmony_ci    my $expected = $$_[2];
72e1051a39Sopenharmony_ci    my $secret = "pass:test";
73e1051a39Sopenharmony_ci    my $rsp_cert = srctop_file('test',  'certs', 'ee-cert-1024.pem');
74e1051a39Sopenharmony_ci    my $outfile = result_file("test.certout.pem");
75e1051a39Sopenharmony_ci    ok($expected ==
76e1051a39Sopenharmony_ci       run(app([@app, "-config", '', @$extra_args,
77e1051a39Sopenharmony_ci                "-use_mock_srv", "-srv_ref", "mock server",
78e1051a39Sopenharmony_ci                "-srv_secret", $secret,
79e1051a39Sopenharmony_ci                "-rsp_cert", $rsp_cert,
80e1051a39Sopenharmony_ci                "-cmd", "cr",
81e1051a39Sopenharmony_ci                "-subject", "/CN=any",
82e1051a39Sopenharmony_ci                "-newkey", srctop_file('test', 'certs', 'ee-key-1024.pem'),
83e1051a39Sopenharmony_ci                "-secret", $secret,
84e1051a39Sopenharmony_ci                "-ref", "client under test",
85e1051a39Sopenharmony_ci                "-certout", $outfile]))
86e1051a39Sopenharmony_ci       && compare_text($outfile, $rsp_cert) == 0,
87e1051a39Sopenharmony_ci       $title);
88e1051a39Sopenharmony_ci    # not unlinking $outfile
89e1051a39Sopenharmony_ci}
90