1e1051a39Sopenharmony_ci#! /usr/bin/env perl
2e1051a39Sopenharmony_ci# Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci#
4e1051a39Sopenharmony_ci# Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci# this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci# in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci# https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci
9e1051a39Sopenharmony_ciuse strict;
10e1051a39Sopenharmony_ciuse warnings;
11e1051a39Sopenharmony_ci
12e1051a39Sopenharmony_ciuse OpenSSL::Test qw(:DEFAULT data_file);
13e1051a39Sopenharmony_ciuse File::Compare qw(compare_text);
14e1051a39Sopenharmony_ci
15e1051a39Sopenharmony_cisetup('test_bio_prefix');
16e1051a39Sopenharmony_ci
17e1051a39Sopenharmony_cimy %input_result = (
18e1051a39Sopenharmony_ci    'in1.txt'  => [ 'args1.pl', 'out1.txt' ],
19e1051a39Sopenharmony_ci    'in2.txt' => [ 'args2.pl', 'out2.txt' ],
20e1051a39Sopenharmony_ci);
21e1051a39Sopenharmony_ci
22e1051a39Sopenharmony_ciplan tests => 2 * scalar(keys %input_result);
23e1051a39Sopenharmony_ci
24e1051a39Sopenharmony_ciforeach (sort keys %input_result) {
25e1051a39Sopenharmony_ci  SKIP: {
26e1051a39Sopenharmony_ci      my $input_path = data_file($_);
27e1051a39Sopenharmony_ci      my $args_path = data_file($input_result{$_}->[0]);
28e1051a39Sopenharmony_ci      my $expected_path = data_file($input_result{$_}->[1]);
29e1051a39Sopenharmony_ci      my $result_path = "test_bio_prefix-$_-stdout";
30e1051a39Sopenharmony_ci      my @args = do $args_path;
31e1051a39Sopenharmony_ci
32e1051a39Sopenharmony_ci      skip "Problem prefixing $_", 1
33e1051a39Sopenharmony_ci          unless ok(run(test([ 'bio_prefix_text', @args ],
34e1051a39Sopenharmony_ci                             stdin => $input_path, stdout => $result_path)),
35e1051a39Sopenharmony_ci                    "prefixing $_ with args " . join(' ', @args));
36e1051a39Sopenharmony_ci      is(compare_text($result_path, $expected_path, \&cmp_line), 0,
37e1051a39Sopenharmony_ci         "comparing the dump of $_ with $expected_path");
38e1051a39Sopenharmony_ci    }
39e1051a39Sopenharmony_ci}
40e1051a39Sopenharmony_ci
41e1051a39Sopenharmony_cisub cmp_line {
42e1051a39Sopenharmony_ci    return 0 if scalar @_ == 0;
43e1051a39Sopenharmony_ci
44e1051a39Sopenharmony_ci    if (scalar @_ != 2) {
45e1051a39Sopenharmony_ci        diag "Lines to compare less than 2: ", scalar @_;
46e1051a39Sopenharmony_ci        return -1;
47e1051a39Sopenharmony_ci    }
48e1051a39Sopenharmony_ci
49e1051a39Sopenharmony_ci    $_[0] =~ s|\R$||;
50e1051a39Sopenharmony_ci    $_[1] =~ s|\R$||;
51e1051a39Sopenharmony_ci    my $r = $_[0] cmp $_[1];
52e1051a39Sopenharmony_ci
53e1051a39Sopenharmony_ci    diag "Lines differ:\n<: $_[0]\n>: $_[1]\n" unless $r == 0;
54e1051a39Sopenharmony_ci    return $r;
55e1051a39Sopenharmony_ci}
56