]> eyrie.org Git - kerberos/krb5-strength.git/blob - tests/data/passwords/make-c-data
New upstream version 3.3
[kerberos/krb5-strength.git] / tests / data / passwords / make-c-data
1 #!/usr/bin/perl
2 #
3 # Read a JSON file of password tests and generate C data.
4 #
5 # The canonical representation of our password tests is in JSON, but I don't
6 # want to require a JSON parser for the C tests to run.  This script reads the
7 # JSON input and generates a C data structure that holds all of the tests.
8 #
9 # SPDX-License-Identifier: MIT
10
11 use 5.010;
12 use autodie;
13 use strict;
14 use warnings;
15
16 use Carp qw(croak);
17 use Const::Fast qw(const);
18 use Encode qw(encode);
19 use File::Basename qw(basename);
20 use JSON::MaybeXS qw(JSON);
21 use Perl6::Slurp qw(slurp);
22
23 ##############################################################################
24 # Global variables
25 ##############################################################################
26
27 # The header on the generated source file.
28 const my $HEADER => <<'END_HEADER';
29 /*
30  * Automatically generated -- do not edit!
31  *
32  * This file was automatically generated from the original JSON source file
33  * for the use in C test programs.  To make changes, modify the original
34  * JSON source or (more rarely) the make-c-data script and run it again.
35  *
36  * Written by Russ Allbery <eagle@eyrie.org>
37  * Copyright 2020 Russ Allbery <eagle@eyrie.org>
38  * Copyright 2013
39  *     The Board of Trustees of the Leland Stanford Junior University
40  *
41  * SPDX-License-Identifier: MIT
42  */
43
44 #include <tests/data/passwords/tests.h>
45
46 END_HEADER
47
48 # The list of attributes, in order, whose values go into the C struct.
49 const my @ATTRIBUTES => qw(
50     name principal password code error skip_for_system_cracklib
51 );
52
53 # A hash of attributes that should be put in the C struct as they literally
54 # appear in the JSON, rather than as strings.  (In other words, attributes
55 # that are numbers, booleans, or C constants.)  Only the keys are of interest.
56 const my %IS_LITERAL_ATTRIBUTE => (
57     code => 1,
58     skip_for_system_cracklib => 1,
59 );
60
61 ##############################################################################
62 # Functions
63 ##############################################################################
64
65 # print with error checking and an explicit file handle.  autodie
66 # unfortunately can't help us with these because they can't be prototyped and
67 # hence can't be overridden.
68 #
69 # $fh   - Output file handle
70 # @args - Remaining arguments to print
71 #
72 # Returns: undef
73 #  Throws: Text exception on output failure
74 sub print_fh {
75     my ($fh, @args) = @_;
76     print {$fh} @args or croak('print failed');
77     return;
78 }
79
80 # The same for say.
81 sub say_fh {
82     my ($fh, @args) = @_;
83     say {$fh} @args or croak('say failed');
84     return;
85 }
86
87 # Load a password test cases and return them as a list.
88 #
89 # $file - The path to the file containing the test data in JSON
90 #
91 # Returns: List of anonymous hashes representing password test cases
92 #  Throws: Text exception on failure to load the test data
93 sub load_password_tests {
94     my ($file) = @_;
95
96     # Load the test file data into memory.
97     my $testdata = slurp($file);
98
99     # Decode the JSON into Perl objects and return them.
100     my $json = JSON->new->utf8;
101     return $json->decode($testdata);
102 }
103
104 # Output one struct's data, representing a test case.
105 #
106 # $fh       - The output file handle to which to send the C data
107 # $test_ref - The hash reference holding the test data
108 #
109 # Returns: undef
110 #  Throws: Text exception on I/O failure
111 sub output_test {
112     my ($fh, $test_ref) = @_;
113     my $prefix = q{ } x 4;
114
115     # Output the data in the order of @ATTRIBUTES.
116     say_fh($fh, $prefix, "{\n");
117     for my $attr (@ATTRIBUTES) {
118         my $value = $test_ref->{$attr};
119         if (exists($IS_LITERAL_ATTRIBUTE{$attr})) {
120             $value //= 0;
121         } else {
122             $value = defined($value) ? qq{"$value"} : 'NULL';
123         }
124         say_fh($fh, $prefix x 2, encode('utf-8', $value), q{,});
125     }
126     say_fh($fh, $prefix, '},');
127     return;
128 }
129
130 ##############################################################################
131 # Main routine
132 ##############################################################################
133
134 # Parse command-line arguments.
135 if (@ARGV != 1) {
136     die "Syntax: make-c-data <json-file>\n";
137 }
138 my $datafile = $ARGV[0];
139
140 # Load the test data.
141 my $tests_ref = load_password_tests($datafile);
142
143 # Print out the header.
144 my $name = basename($datafile);
145 $name =~ s{ [.]json \z }{}xms;
146 print_fh(\*STDOUT, $HEADER);
147 say_fh(\*STDOUT, "extern const struct password_test ${name}_tests[];");
148 say_fh(\*STDOUT, "const struct password_test ${name}_tests[] = {");
149
150 # Print out the test data.
151 for my $test_ref (@{$tests_ref}) {
152     output_test(\*STDOUT, $test_ref);
153 }
154
155 # Close the struct.
156 say_fh(\*STDOUT, '};');
157
158 __END__
159
160 ##############################################################################
161 # Documentation
162 ##############################################################################
163
164 =for stopwords
165 Allbery JSON krb5-strength struct sublicense MERCHANTABILITY
166 NONINFRINGEMENT
167
168 =head1 NAME
169
170 make-c-data - Generate C data from JSON test data for krb5-strength
171
172 =head1 SYNOPSIS
173
174 B<make-c-data> I<input>
175
176 =head1 DESCRIPTION
177
178 The canonical form of the password test data for the krb5-strength package
179 is in JSON, but requiring a C JSON parser to run the test suite (or
180 writing one) is undesirable.  Hence this script.  B<make-c-data> takes a
181 JSON file as input, interprets it as a list of password test cases, and
182 outputs a C file that defines an array of C<struct password_test>.  That
183 struct is expected to have the following definition:
184
185     struct password_test {
186         const char *name;
187         const char *principal;
188         const char *password;
189         bool skip_for_system_cracklib;
190         krb5_error_code code;
191         const char *error;
192     };
193
194 All JSON objects are expected to have fields corresponding to the above
195 struct element names.  All of them are written as C strings except for
196 code, where the value from JSON is written as a literal.  It should
197 therefore be either a number or a symbolic constant.
198
199 The written file will also include C<tests/data/passwords/tests.h>, which
200 should define the above struct and any constants that will be used for the
201 code field.
202
203 =head1 AUTHOR
204
205 Russ Allbery <eagle@eyrie.org>
206
207 =head1 COPYRIGHT AND LICENSE
208
209 Copyright 2020, 2023 Russ Allbery <eagle@eyrie.org>
210
211 Copyright 2013 The Board of Trustees of the Leland Stanford Junior
212 University
213
214 Permission is hereby granted, free of charge, to any person obtaining a
215 copy of this software and associated documentation files (the "Software"),
216 to deal in the Software without restriction, including without limitation
217 the rights to use, copy, modify, merge, publish, distribute, sublicense,
218 and/or sell copies of the Software, and to permit persons to whom the
219 Software is furnished to do so, subject to the following conditions:
220
221 The above copyright notice and this permission notice shall be included in
222 all copies or substantial portions of the Software.
223
224 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
225 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
226 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
227 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
228 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
229 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
230 DEALINGS IN THE SOFTWARE.
231
232 =cut