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