80-test_ca_internals.t 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #! /usr/bin/env perl
  2. # Copyright 2021-2022 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. use strict;
  9. use warnings;
  10. use POSIX;
  11. use OpenSSL::Test qw/:DEFAULT data_file/;
  12. use File::Copy;
  13. setup('test_ca_internals');
  14. my @updatedb_tests = (
  15. {
  16. description => 'updatedb called before the first certificate expires',
  17. filename => 'index.txt',
  18. copydb => 1,
  19. testdate => '990101000000Z',
  20. need64bit => 0,
  21. expirelist => [ ]
  22. },
  23. {
  24. description => 'updatedb called before Y2k',
  25. filename => 'index.txt',
  26. copydb => 0,
  27. testdate => '991201000000Z',
  28. need64bit => 0,
  29. expirelist => [ '1000' ]
  30. },
  31. {
  32. description => 'updatedb called after year 2020',
  33. filename => 'index.txt',
  34. copydb => 0,
  35. testdate => '211201000000Z',
  36. need64bit => 0,
  37. expirelist => [ '1001' ]
  38. },
  39. {
  40. description => 'updatedb called in year 2049 (last year with 2 digits)',
  41. filename => 'index.txt',
  42. copydb => 0,
  43. testdate => '491201000000Z',
  44. need64bit => 1,
  45. expirelist => [ '1002' ]
  46. },
  47. {
  48. description => 'updatedb called in year 2050 (first year with 4 digits) before the last certificate expires',
  49. filename => 'index.txt',
  50. copydb => 0,
  51. testdate => '20500101000000Z',
  52. need64bit => 1,
  53. expirelist => [ ]
  54. },
  55. {
  56. description => 'updatedb called after the last certificate expired',
  57. filename => 'index.txt',
  58. copydb => 0,
  59. testdate => '20501201000000Z',
  60. need64bit => 1,
  61. expirelist => [ '1003' ]
  62. },
  63. {
  64. description => 'updatedb called for the first time after the last certificate expired',
  65. filename => 'index.txt',
  66. copydb => 1,
  67. testdate => '20501201000000Z',
  68. need64bit => 1,
  69. expirelist => [ '1000',
  70. '1001',
  71. '1002',
  72. '1003' ]
  73. }
  74. );
  75. my @unsupported_commands = (
  76. {
  77. command => 'unsupported'
  78. }
  79. );
  80. # every "test_updatedb" makes 3 checks
  81. plan tests => 3 * scalar(@updatedb_tests) +
  82. 1 * scalar(@unsupported_commands);
  83. foreach my $test (@updatedb_tests) {
  84. test_updatedb($test);
  85. }
  86. foreach my $test (@unsupported_commands) {
  87. test_unsupported_commands($test);
  88. }
  89. ################### subs to do tests per supported command ################
  90. sub test_unsupported_commands {
  91. my ($opts) = @_;
  92. run(
  93. test(['ca_internals_test',
  94. $opts->{command}
  95. ]),
  96. capture => 0,
  97. statusvar => \my $exit
  98. );
  99. is($exit, 0, "command '".$opts->{command}."' completed without an error");
  100. }
  101. sub test_updatedb {
  102. my ($opts) = @_;
  103. my $amtexpectedexpired = scalar(@{$opts->{expirelist}});
  104. my @output;
  105. my $expirelistcorrect = 1;
  106. my $cert;
  107. my $amtexpired = 0;
  108. my $skipped = 0;
  109. if ($opts->{copydb}) {
  110. copy(data_file('index.txt'), 'index.txt');
  111. }
  112. @output = run(
  113. test(['ca_internals_test',
  114. "do_updatedb",
  115. $opts->{filename},
  116. $opts->{testdate},
  117. $opts->{need64bit}
  118. ]),
  119. capture => 1,
  120. statusvar => \my $exit
  121. );
  122. foreach my $tmp (@output) {
  123. ($cert) = $tmp =~ /^[\x20\x23]*[^0-9A-Fa-f]*([0-9A-Fa-f]+)=Expired/;
  124. if ($tmp =~ /^[\x20\x23]*skipping test/) {
  125. $skipped = 1;
  126. }
  127. if (defined($cert) && (length($cert) > 0)) {
  128. $amtexpired++;
  129. my $expirefound = 0;
  130. foreach my $expire (@{$opts->{expirelist}}) {
  131. if ($expire eq $cert) {
  132. $expirefound = 1;
  133. }
  134. }
  135. if ($expirefound != 1) {
  136. $expirelistcorrect = 0;
  137. }
  138. }
  139. }
  140. if ($skipped) {
  141. $amtexpired = $amtexpectedexpired;
  142. $expirelistcorrect = 1;
  143. }
  144. is($exit, 1, "ca_internals_test: returned EXIT_FAILURE (".$opts->{description}.")");
  145. is($amtexpired, $amtexpectedexpired, "ca_internals_test: amount of expired certificates differs from expected amount (".$opts->{description}.")");
  146. is($expirelistcorrect, 1, "ca_internals_test: list of expired certificates differs from expected list (".$opts->{description}.")");
  147. }