summaryrefslogtreecommitdiff
path: root/t
diff options
context:
space:
mode:
Diffstat (limited to 't')
-rw-r--r--t/basic-enigma.t28
-rw-r--r--t/full-enigma.t69
-rw-r--r--t/lib/Test/Enigmatic.pm25
-rw-r--r--t/simple-enigma.t23
4 files changed, 145 insertions, 0 deletions
diff --git a/t/basic-enigma.t b/t/basic-enigma.t
new file mode 100644
index 0000000..0760737
--- /dev/null
+++ b/t/basic-enigma.t
@@ -0,0 +1,28 @@
+#!perl
+use DAKKAR::p 'test';
+use Test::Enigmatic;
+use Enigmatic::Machine;
+
+Test::Enigmatic::test_full_machine(
+ sub {
+ Enigmatic::Machine->new({
+ reflector => 'B',
+ rotors => [ 'III', 'II', 'I' ],
+ });
+ },
+ 'AAAAA',
+ 'BDZGO');
+
+Test::Enigmatic::test_full_machine(
+ sub {
+ Enigmatic::Machine->new({
+ reflector => 'B',
+ rotors => [ 'III', 'II', 'I' ],
+ ring_settings => ['B','B','B'],
+ });
+ },
+ 'AAAAA',
+ 'EWTYX');
+
+
+done_testing();
diff --git a/t/full-enigma.t b/t/full-enigma.t
new file mode 100644
index 0000000..0015b46
--- /dev/null
+++ b/t/full-enigma.t
@@ -0,0 +1,69 @@
+#!perl
+use DAKKAR::p 'test';
+
+use Enigmatic::Machine;
+
+sub real_machine {
+ return Enigmatic::Machine->new({
+ reflector => 'B_thin',
+ rotors => [ 'I', 'IV', 'II', 'Beta' ],
+ plugboard => 'AT BL DF GJ HM NW OP QY RZ VX',
+ ring_settings => [ 'V', 'A', 'A', 'A' ],
+ rotor_positions => [ 'A', 'N', 'J', 'V' ],
+ });
+}
+
+my $plaintext = <<'EOPLAIN';
+VONV ONJL OOKS JHFF TTTE
+INSE INSD REIZ WOYY QNNS
+NEUN INHA LTXX BEIA NGRI
+FFUN TERW ASSE RGED RUEC
+KTYW ABOS XLET ZTER GEGN
+ERST ANDN ULAC HTDR EINU
+LUHR MARQ UANT ONJO TANE
+UNAC HTSE YHSD REIY ZWOZ
+WONU LGRA DYAC HTSM YSTO
+SSEN ACHX EKNS VIER MBFA
+ELLT YNNN NNNO OOVI ERYS
+ICHT EINS NULL
+EOPLAIN
+$plaintext =~ s{\s+}{}g;
+
+my $ciphertext = <<'EOCIPHER';
+NCZW VUSX PNYM INHZ XMQX
+SFWX WLKJ AHSH NMCO CCAK
+UQPM KCSM HKSE INJU SBLK
+IOSX CKUB HMLL XCSJ USRR
+DVKO HULX WCCB GVLI YXEO
+AHXR HKKF VDRE WEZL XOBA
+FGYU JQUK GRTV UKAM EURB
+VEKS UHHV OYHA BCJW MAKL
+FKLM YFVN RIZR VVRT KOFD
+ANJM OLBG FFLE OPRG TFLV
+RHOW OPBE KVWM UQFM PWPA
+RMFH AGKX IIBG
+EOCIPHER
+$ciphertext =~ s{\s+}{}g;
+
+note "encrypt real";
+
+my $check = real_machine->map_string($plaintext);
+p $check;
+is($check,$ciphertext,
+ 'ok crypt');
+
+note "round-trip check";
+
+$check = real_machine->map_string($check);
+p $check;
+is($check,$plaintext,
+ 'round tripped');
+
+note "decrypt real";
+
+$check = real_machine->map_string($ciphertext);
+p $check;
+is($check,$plaintext,
+ 'ok plain');
+
+
diff --git a/t/lib/Test/Enigmatic.pm b/t/lib/Test/Enigmatic.pm
index 923ede1..9958667 100644
--- a/t/lib/Test/Enigmatic.pm
+++ b/t/lib/Test/Enigmatic.pm
@@ -13,3 +13,28 @@ sub test_static_map {
"$name on $c");
}
}
+
+
+sub test_full_machine {
+ my ($factory,$input,$exp_output) = @_;
+
+ $input =~ s{\s+}{}g;
+
+ my $output = $factory->()->map_string(uc $input);
+
+ note "output: $output";
+
+ my $back = $factory->()->map_string($output);
+
+ note "back: $back";
+
+ is($back,
+ $input,
+ 'round tripped');
+ if ($exp_output) {
+ $exp_output =~ s{\s+}{}g;
+ is($output,
+ uc($exp_output),
+ 'got expected output');
+ }
+}
diff --git a/t/simple-enigma.t b/t/simple-enigma.t
new file mode 100644
index 0000000..b2bcbdd
--- /dev/null
+++ b/t/simple-enigma.t
@@ -0,0 +1,23 @@
+#!perl
+use DAKKAR::p 'test';
+
+use Enigmatic::Machine;
+
+sub real_machine {
+ return Enigmatic::Machine->new({
+ reflector => 'B_thin',
+ rotors => [ 'I', 'IV', 'II', 'Beta' ],
+ plugboard => 'AT BL DF GJ HM NW OP QY RZ VX',
+ ring_settings => [ 'V', 'A', 'A', 'A' ],
+ rotor_positions => [ 'A', 'N', 'J', 'V' ],
+ });
+}
+
+my $plaintext = 'this is a simple text to be encrypted by an enigma machine'->uc;
+note "encrypt simple";
+my $ciphertext = real_machine->map_string($plaintext);note $ciphertext;
+note "decrypt simple";
+my $check = real_machine->map_string($ciphertext);note $check;
+$plaintext =~ s{\s+}{}g;
+is($check,$plaintext,
+ 'round tripped');