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