blib/lib/App/FuguSeed/Check.pm

filter: Statementstmt 54.9% 127 / 231 Branchbran 48.0% 25 / 52 Conditioncond 33.3% 4 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 65.6% 21 / 32 Podpod 100% 2 / 2 totaltotal 53.7% 181 / 337 CC 47 SCAR 16.2
CC
47
Cov
53%
_want
8.6
_want
8.6
_block
8.1
CRAP 5.0
« lib/App/FuguSeed/Matrix.pm ↑ Summary lib/App/FuguSeed/List.pm »
1 # ex:ts=8 sw=4:
2 # $OpenBSD$
3 #
4 # Copyright (c) 2026 Dick Olsson <hi@senzilla.io>
5 #
6 # Permission to use, copy, modify, and distribute this software for any
7 # purpose with or without fee is hereby granted, provided that the above
8 # copyright notice and this permission notice appear in all copies.
9 #
10 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  
18 package App::FuguSeed::Check;
19 14 our $VERSION = '0.2.0';
20  
21 14 ▶use v5.34;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 14 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 14 ▶no feature qw(indirect multidimensional bareword_filehandles);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # App::FuguSeed::Check - prove that a word sheet is correct
27 # (WORDS-CHECK).
28 #
29 # The module loads no builder module, so a defect of the builder
30 # cannot hide in the checker (WORDS-CHECK-3). It therefore holds its
31 # own copy of the grammar of the sheet. The two copies must agree,
32 # and t/fuguseed/sheet.t holds them to each other: the check runs on
33 # a built sheet on each test run.
34 #
35 # The check consumes the sheet as one strict grammar from the first
36 # byte to the last (D-09, WORDS-CHECK-4). Every byte of the sheet
37 # belongs to a literal of the grammar, to one of the 2048 cells, or
38 # to the digest and the date of a footer. A byte that the grammar
39 # does not expect ends the walk, and the defect names its offset. An
40 # attribute, a script, a comment, an entity, and a byte outside
41 # printable ASCII are therefore defects, and no rule of them needs a
42 # pattern of its own (WORDS-BUILD-3, WORDS-CHECK-5).
43 #
44 # The style sheet is the one part that the check reads from a share
45 # file. It compares the inlined text with share/fuguseed/sheet.css
46 # byte for byte, because a style rule can reverse or hide a row on
47 # paper while the text order stays correct (D-09, WORDS-CHECK-5).
48  
49 # SIDES, BLOCKS, ROWS and COLUMNS:
50 # The shape of the sheet (WORDS-BUILD-4, WORDS-BUILD-5).
51 14 ▶use constant SIDES => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 14 ▶use constant BLOCKS => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 14 ▶use constant ROWS => 16;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 14 ▶use constant COLUMNS => 16;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 # WINDOW:
57 # The bytes that the walk reads ahead of the cursor for a
58 # variable part. The longest variable part is the SHA-256 of 64
59 # hex digits.
60 14 ▶use constant WINDOW => 128;
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 # SHOW:
63 # The bytes of a defect message that name the text at the
64 # cursor, and the bytes that name the expected text.
65 14 ▶use constant SHOW => 16;
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 # $class->defects($sheet, $list, $style):
68 # Each defect of the sheet $sheet, as one line each, or an empty
69 # list for a correct sheet (WORDS-CHECK-2).
70 #
71 # $list is the list of App::FuguSeed::ListFile, and $style is
72 # the text of share/fuguseed/sheet.css.
73 16 ▶sub defects ( $class, $sheet, $list, $style )
Subroutine defects: called CC=2 SCAR=9.2
Pod documented
74 0 ▶{
Subroutine defects: not called CC=2 SCAR=9.2
Pod documented
75 0 my $self = bless {
76 text => $sheet,
77 at => 0,
78 stop => 0,
79 defects => [],
80 cells => [],
81 footers => [],
82 }, $class;
83  
84 0 $self->_document($style);
85  
86 # A walk that stopped holds no cell and no footer of the bytes
87 # after the first unexpected one, so the checks below would
88 # report a defect of that one defect.
89 0 ▶ return @{ $self->{defects} } if @{ $self->{defects} };
Branch: 2 branches
BranchTrueFalseTotal
if @{$self->{"defects"};} 8 8 16
if @{$self->{"defects"};} 0 0 0
90  
91 0 $self->_words( $list->{words} );
92 0 $self->_footers( $list->{digest} );
93  
94 0 return @{ $self->{defects} };
95 }
96  
97 # $self->_document($style):
98 # The whole sheet, from the first byte to the last.
99 16 ▶sub _document ( $self, $style )
Subroutine _document: called CC=2 SCAR=9.2
100 0 ▶{
Subroutine _document: not called CC=2 SCAR=9.2
101 0 $self->_want( "<!DOCTYPE html>\n"
102 . qq(<html lang="en">\n)
103 . "<head>\n"
104 . qq(<meta charset="utf-8">\n)
105 . "<title>FuguSeed word sheet</title>\n"
106 . "<style>\n" );
107 0 $self->_want($style);
108 0 $self->_want( "</style>\n" . "</head>\n" . "<body>\n" );
109 0 $self->_side($_) for 1 .. SIDES;
110 0 $self->_want( "</body>\n" . "</html>\n" );
111 0 $self->_last;
112  
113 0 return;
114 }
115  
116 # $self->_side($side):
117 # One side: the heading, the legend, the four blocks, and the
118 # footer (WORDS-BUILD-4, WORDS-BUILD-7).
119 32 ▶sub _side ( $self, $side )
Subroutine _side: called CC=2 SCAR=9.2
120 0 ▶{
Subroutine _side: not called CC=2 SCAR=9.2
121 0 $self->_want( "<section>\n"
122 . "<h1>FuguSeed word sheet, side $side of "
123 . SIDES
124 . "</h1>\n"
125 . "<ul>\n"
126 . "<li>YELLOW d8 gives the block number, 1 to 8.</li>\n"
127 . "<li>BLUE d16 gives the row number, 1 to 16.</li>\n"
128 . "<li>RED d16 gives the column number, 1 to 16.</li>\n"
129 . "</ul>\n" );
130  
131 0 my $first = ( $side - 1 ) * BLOCKS + 1;
132 0 $self->_block($_) for $first .. $first + BLOCKS - 1;
133 0 $self->_footer($side);
134  
135 0 return;
136 }
137  
138 # $self->_block($block):
139 # One block: the caption, the header row of the RED columns,
140 # and the 16 rows of the BLUE die. Each cell reaches the cell
141 # list with its block, its row, and its column
142 # (WORDS-CHECK-6).
143 128 ▶sub _block ( $self, $block )
Subroutine _block: called CC=5 SCAR=20.9
144 0 ▶{
Subroutine _block: not called CC=5 SCAR=20.9
145 0 $self->_want(
146 "<table>\n<caption>YELLOW $block</caption>\n<tr><td></td>");
147 0 $self->_want("<th>RED $_</th>") for 1 .. COLUMNS;
148 0 $self->_want("</tr>\n");
149  
150 0 for my $row ( 1 .. ROWS ) {
151 0 $self->_want("<tr><th>BLUE $row</th>");
152 0 for my $column ( 1 .. COLUMNS ) {
153 0 $self->_want('<td>');
154 0 my $word =
155 $self->_take( qr/[a-z]{3,8}/,
156 'a word of the list' );
157 0 $self->_want('</td>');
158 0 ▶ push @{ $self->{cells} },
Branch: 2 branches
BranchTrueFalseTotal
if defined $word 23393 9375 32768
if defined $word 0 0 0
159 [ $block, $row, $column, $word ]
160 if defined $word;
161 }
162 0 $self->_want("</tr>\n");
163 }
164 0 $self->_want("</table>\n");
165  
166 0 return;
167 }
168  
169 # $self->_footer($side):
170 # The footer of one side. It names the side, the standard, the
171 # SHA-256 of the list, and the build date (WORDS-BUILD-7). The
172 # digest and the date reach the footer list, and _footers holds
173 # them to the list file below.
174 32 ▶sub _footer ( $self, $side )
Subroutine _footer: called CC=3 SCAR=14.8
175 0 ▶{
Subroutine _footer: not called CC=3 SCAR=14.8
176 0 $self->_want( "<footer>\n<p>Side $side of "
177 . SIDES
178 . '. The word list is the English list of BIP39.'
179 . ' Its SHA-256 is ' );
180 0 my $digest =
181 $self->_take( qr/[0-9a-f]{64}/, 'a SHA-256 of 64 hex digits' );
182 0 $self->_want('. The build date is ');
183 0 my $date =
184 $self->_take( qr/[0-9]{4}-[0-9]{2}-[0-9]{2}/, 'a date YYYY-MM-DD' );
185 0 $self->_want( ".</p>\n" . "</footer>\n" . "</section>\n" );
186  
187 0 ▶ push @{ $self->{footers} }, { digest => $digest, date => $date }
Branch: 2 branches
BranchTrueFalseTotal
if defined $digest and defined $date 22 10 32
if defined $digest and defined $date 0 0 0
Condition33%2/6
defined $digest and defined $date
!ll&&!rl&&r
10022
defined $digest and defined $date
!ll&&!rl&&r
000
MC/DC50%1/2
defined $digest and defined $date
defined $digest defined $date
MC/DC0%0/2
defined $digest and defined $date
defined $digest defined $date
Truth table66%2/3
A and B
A= defined $digestB= defined $date
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $digestB= defined $date
ABresult
0X0
100
111
188 if defined $digest && defined $date;
189  
190 0 return;
191 }
192  
193 # $self->_words($words):
194 # Hold each of the 2048 cells to the word of its position, and
195 # hold each word to one appearance (WORDS-CHECK-6).
196 8 ▶sub _words ( $self, $words )
Subroutine _words: called CC=4 SCAR=17.9
197 0 ▶{
Subroutine _words: not called CC=4 SCAR=17.9
198 0 my %count;
199 0 for my $cell ( @{ $self->{cells} } ) {
200 0 my ( $block, $row, $column, $word ) = @{$cell};
201 0 $count{$word}++;
202  
203 0 my $index =
204 ( $block - 1 ) * ROWS * COLUMNS +
205 ( $row - 1 ) * COLUMNS +
206 ( $column - 1 );
207 0 ▶ next if $word eq $words->[$index];
Branch: 2 branches
BranchTrueFalseTotal
if $word eq $words->[$index] 16378 6 16384
if $word eq $words->[$index] 0 0 0
208  
209 0 push @{ $self->{defects} },
210 sprintf 'YELLOW %d BLUE %d RED %d: the sheet holds %s, '
211 . 'and the list holds %s',
212 $block, $row, $column, $word, $words->[$index];
213 }
214  
215 0 for my $word ( sort grep { $count{$_} > 1 } keys %count ) {
216 0 push @{ $self->{defects} },
217 sprintf 'the sheet holds the word %s %d times', $word,
218 0 $count{$word};
219 }
220  
221 0 return;
222 }
223  
224 # $self->_footers($digest):
225 # Hold each footer to the digest of the list file, and hold the
226 # two sides to one build date (WORDS-CHECK-6).
227 8 ▶sub _footers ( $self, $digest )
Subroutine _footers: called CC=4 SCAR=17.9
228 0 ▶{
Subroutine _footers: not called CC=4 SCAR=17.9
229 0 my $side = 0;
230 0 for my $footer ( @{ $self->{footers} } ) {
231 0 $side++;
232 0 ▶ next if $footer->{digest} eq $digest;
Branch: 2 branches
BranchTrueFalseTotal
if $footer->{"digest"} eq $digest 15 1 16
if $footer->{"digest"} eq $digest 0 0 0
233 0 push @{ $self->{defects} },
234 sprintf 'side %d: the footer names the SHA-256 %s, '
235 . 'and the list file has the SHA-256 %s',
236 0 $side, $footer->{digest}, $digest;
237 }
238  
239 0 push @{ $self->{defects} }, 'the two sides name two build dates'
240 0 ▶ if $self->{footers}[0]{date} ne $self->{footers}[1]{date};
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"footers"}[0]{"date"} ne $self->{"footers"}[1]{"date"} 1 7 8
if $self->{"footers"}[0]{"date"} ne $self->{"footers"}[1]{"date"} 0 0 0
241  
242 0 return;
243 }
244  
245 # $self->_want($literal):
246 # Consume $literal at the cursor. Another byte is a defect: the
247 # message names the offset of the first unexpected byte, and the
248 # walk stops (WORDS-CHECK-4).
249 72256 ▶sub _want ( $self, $literal )
Subroutine _want: called CC=5 SCAR=21.5
250 0 ▶{
Subroutine _want: not called CC=5 SCAR=21.5
251 0 ▶ return if $self->{stop};
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"stop"} 20627 51629 72256
if $self->{"stop"} 0 0 0
252  
253 0 my $length = length $literal;
254 0 my $found = substr $self->{text}, $self->{at}, $length;
255 0 ▶ if ( $found eq $literal ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($found eq $literal) 51622 7 51629
if ($found eq $literal) 0 0 0
256 0 $self->{at} += $length;
257 0 return;
258 }
259  
260 0 my $same = 0;
261 0 ▶ $same++
Branch: 2 branches
BranchTrueFalseTotal
$same < length $found and substr($found, $same, 1) eq substr($literal, $same, 1) and do { ++$same } 169 7 176
$same < length $found and substr($found, $same, 1) eq substr($literal, $same, 1) and do { ++$same } 0 0 0
Condition33%2/6
$same < length $found and substr($found, $same, 1) eq substr($literal, $same, 1)
!ll&&!rl&&r
07169
$same < length $found and substr($found, $same, 1) eq substr($literal, $same, 1)
!ll&&!rl&&r
000
MC/DC50%1/2
$same < length $found and substr($found, $same, 1) eq substr($literal, $same, 1)
$same < length $found substr($found, $same, 1) eq substr($literal, $same, 1)
MC/DC0%0/2
$same < length $found and substr($found, $same, 1) eq substr($literal, $same, 1)
$same < length $found substr($found, $same, 1) eq substr($literal, $same, 1)
Truth table66%2/3
A and B
A= $same < length $foundB= substr($found, $same, 1) eq substr($literal, $same, 1)
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $same < length $foundB= substr($found, $same, 1) eq substr($literal, $same, 1)
ABresult
0X0
100
111
262 while $same < length $found
263 && substr( $found, $same, 1 ) eq substr( $literal, $same, 1 );
264  
265 0 my $at = $self->{at} + $same;
266 0 push @{ $self->{defects} },
267 sprintf
268 'byte %d: the sheet holds "%s", and the sheet must hold "%s"',
269 0 $at, _show( substr $self->{text}, $at, SHOW ),
270 _show( substr $literal, $same, SHOW );
271 0 $self->{stop} = 1;
272  
273 0 return;
274 }
275  
276 # $self->_take($pattern, $form):
277 # Consume one variable part at the cursor and give its text.
278 # Text that $pattern does not match is a defect, and the walk
279 # stops (WORDS-CHECK-4).
280 32832 ▶sub _take ( $self, $pattern, $form )
Subroutine _take: called CC=3 SCAR=17.9
281 0 ▶{
Subroutine _take: not called CC=3 SCAR=17.9
282 0 ▶ return if $self->{stop};
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"stop"} 9395 23437 32832
if $self->{"stop"} 0 0 0
283  
284 0 my $rest = substr $self->{text}, $self->{at}, WINDOW;
285 0 ▶ if ( $rest =~ /\A($pattern)/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rest =~ /\A($pattern)/u) 23437 0 23437
if ($rest =~ /\A($pattern)/u) 0 0 0
286 0 $self->{at} += length $1;
287 0 return $1;
288 }
289  
290 0 push @{ $self->{defects} },
291 sprintf 'byte %d: the sheet holds "%s", and the sheet must hold %s',
292 0 $self->{at}, _show( substr $rest, 0, SHOW ), $form;
293 0 $self->{stop} = 1;
294  
295 0 return;
296 }
297  
298 # $self->_last:
299 # The grammar ends at the last byte of the sheet. A byte after
300 # it is a defect (WORDS-CHECK-4).
301 16 ▶sub _last ($self)
Subroutine _last: called CC=3 SCAR=14.2
302 0 ▶{
Subroutine _last: not called CC=3 SCAR=14.2
303 0 ▶ return if $self->{stop};
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"stop"} 7 9 16
if $self->{"stop"} 0 0 0
304  
305 0 my $length = length $self->{text};
306 0 ▶ return if $self->{at} == $length;
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"at"} == $length 8 1 9
if $self->{"at"} == $length 0 0 0
307  
308 0 push @{ $self->{defects} },
309 sprintf 'byte %d: the sheet holds a byte after the last '
310 . 'byte of the grammar',
311 0 $self->{at};
312 0 $self->{stop} = 1;
313  
314 0 return;
315 }
316  
317 # _show($text):
318 # $text for a defect message: one line of printable ASCII. A
319 # defect holds one line, so each other byte reads as its
320 # hexadecimal value (WORDS-CHECK-2).
321 14 ▶sub _show ($text)
Subroutine _show: called CC=1 SCAR=1.2
322 0 ▶{
Subroutine _show: not called CC=1 SCAR=1.2
323 0 $text =~ s/([^\x20-\x7e])/sprintf '\\x%02x', ord $1/ge;
324  
325 0 return $text;
326 }
327  
328 14 1;
« lib/App/FuguSeed/Matrix.pm ↑ Summary lib/App/FuguSeed/List.pm »