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