summaryrefslogtreecommitdiff
path: root/t/stepping.t
blob: 1806a30b13ce52a7cdfce5a3510fa2cf3fb9a885 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!perl 
use DAKKAR::p 'test';
 
use Enigmatic::Rotor;
use Enigmatic::Reflector;
use Enigmatic::RotorBox;
use Enigmatic::ReflectorBox;
use Enigmatic::CryptTrain;
 
subtest 'first position' => sub {
    my @in = 'A'..'Z';
 
    my @rots = map { Enigmatic::Rotor->new(@in->join) } 1..3;
    my $refl = Enigmatic::Reflector->new(@in->join);
    my $train = Enigmatic::CryptTrain->new({
        rotors => \@rots,
        reflector => $refl,
        positions => [0,0,0],
    });
 
    my $pos=0;
    for my $c ('A' .. 'Z') {
        is($train->map($c),
           $c,
           "identity train on $c");
        ++$pos;$pos%=26;
        is_deeply([$train->positions],
                  [$pos,0,0],
                  'no notches, only 1st moved');
    }
};
 
subtest 'some stepping' => sub {
    my @in = 'A'..'Z';
 
    my @rots = map { Enigmatic::Rotor->new({
        wiring => @in->join,
        notches => ['Z'],
    }) } 1..3;
    my $refl = Enigmatic::Reflector->new(@in->join);
    my $train = Enigmatic::CryptTrain->new({
        rotors => \@rots,
        reflector => $refl,
        positions => [0,0,0],
    });
 
    my $pos=0;
    for my $c ('A' .. 'Y') {
        $train->step_positions();
        ++$pos;
        is_deeply([$train->positions],
                  [$pos,0,0],
                  'single notch');
    }
    $train->step_positions();
    is_deeply([$train->positions],
              [0,1,0],
              'single notch');
};
 
sub _test_double_stepping {
    my ($train,$init,$steps) = @_;
 
    for my $rot (0..2) {
        $train->set_position($rot,$init->[$rot]);
    }
 
    for my $step_idx (0..$#$steps) {
        $train->step_positions();
 
        my $now_windows = $steps->[$step_idx];
        is_deeply([$train->rotor_windows],
                  $now_windows,
                  'correct step');
    }
}
 
subtest 'double-stepping' => sub {
    my $rotbox = Enigmatic::RotorBox->new();
    my $reflbox = Enigmatic::ReflectorBox->new();
 
    my $train = sub { Enigmatic::CryptTrain->new({
        rotors => [
            $rotbox->get('I'),
            $rotbox->get('II'),
            $rotbox->get('III'),
        ],
        reflector => $reflbox->get('B'),
    }) };
 
    _test_double_stepping($train->(),
                          [qw(O D K)],
                          [
                              [qw(P D K)],
                              [qw(Q D K)],
                              [qw(R E K)],
                              [qw(S F L)],
                              [qw(T F L)],
                          ]);
 
    $train = sub { Enigmatic::CryptTrain->new({
        rotors => [
            $rotbox->get('III'),
            $rotbox->get('II'),
            $rotbox->get('I'),
        ],
        reflector => $reflbox->get('B'),
    }) };
 
    _test_double_stepping($train->(),
                          [qw(T A A)],
                          [
                              [qw(U A A)],
                              [qw(V A A)],
                              [qw(W B A)],
                              [qw(X B A)],
                              [qw(Y B A)],
                          ]);
 
    _test_double_stepping($train->(),
                          [qw(T D A)],
                          [
                              [qw(U D A)],
                              [qw(V D A)],
                              [qw(W E A)],
                              [qw(X F B)],
                              [qw(Y F B)],
                          ]);
};
 
done_testing();
 
__END__
 
=head1 AUTHOR
 
Gianni Ceccarelli <dakkar@thenautilus.net>
 
=head1 COPYRIGHT AND LICENSE
 
This software is copyright (c) 2011 by Gianni Ceccarelli.
 
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, version 3.
 
=cut