From 2b28ab91084f37a5e9014737d0df694587fe60cf Mon Sep 17 00:00:00 2001 From: dakkar Date: Mon, 29 Aug 2011 19:16:26 +0100 Subject: fixed rotor ring settings and removed debug prints --- lib/Enigmatic/CryptTrain.pm | 30 +++++++++++------------------- lib/Enigmatic/Machine.pm | 13 ------------- lib/Enigmatic/Rotor.pm | 9 ++++----- t/basic-enigma.t | 1 - t/full-enigma.t | 29 ++++++----------------------- t/lib/Test/Enigmatic.pm | 8 ++++---- t/rotors.t | 31 +++++++++++-------------------- t/simple-enigma.t | 15 ++++++--------- 8 files changed, 42 insertions(+), 94 deletions(-) diff --git a/lib/Enigmatic/CryptTrain.pm b/lib/Enigmatic/CryptTrain.pm index 4d4b6c6..97332f4 100644 --- a/lib/Enigmatic/CryptTrain.pm +++ b/lib/Enigmatic/CryptTrain.pm @@ -56,13 +56,9 @@ around BUILDARGS => sub { if ($args->{positions}) { - warn "pos: @{$args->{positions}}\n"; - for my $pos (@{$args->{positions}}) { $pos = to_RotorPos($pos); } - - warn "pos: @{$args->{positions}}\n"; } return $args; @@ -83,32 +79,28 @@ sub map { my $max_idx = $self->rotor_count -1; - my $log=''; - $self->step_positions(); for my $idx (0..$max_idx) { - my $rotor = $self->rotor_at($idx);$log.="($idx:"; - my $position = $self->position_at($idx);$log.="$position)"; + my $rotor = $self->rotor_at($idx); + my $position = $self->position_at($idx); - $letter = _rotate_by($letter,$position);$log.=$letter; - $letter = $rotor->map($letter);$log.=$letter; - $letter = _rotate_by($letter,-$position);$log.=$letter; + $letter = _rotate_by($letter,$position); + $letter = $rotor->map($letter); + $letter = _rotate_by($letter,-$position); } - $letter = $self->reflector->map($letter);$log.="r${letter}r"; + $letter = $self->reflector->map($letter); for my $idx (reverse 0..$max_idx) { - my $rotor = $self->rotor_at($idx);$log.="($idx:"; - my $position = $self->position_at($idx);$log.="$position)"; + my $rotor = $self->rotor_at($idx); + my $position = $self->position_at($idx); - $letter = _rotate_by($letter,$position);$log.=$letter; - $letter = $rotor->inverse_map($letter);$log.=$letter; - $letter = _rotate_by($letter,-$position);$log.=$letter; + $letter = _rotate_by($letter,$position); + $letter = $rotor->inverse_map($letter); + $letter = _rotate_by($letter,-$position); } - warn "$log\n"; - return $letter; } diff --git a/lib/Enigmatic/Machine.pm b/lib/Enigmatic/Machine.pm index f5d5081..cc736e0 100644 --- a/lib/Enigmatic/Machine.pm +++ b/lib/Enigmatic/Machine.pm @@ -55,23 +55,10 @@ sub map { { isa => Letter }, ); -warn "mapping $letter\n"; -warn " initial positions: @{[ $self->train->positions ]}\n"; - $letter = $self->plugboard->map($letter); - -warn " after plugboard: $letter\n"; - $letter = $self->train->map($letter); - -warn " after rotors: $letter\n"; - $letter = $self->plugboard->map($letter); -warn " after plugboard: $letter\n"; - -warn " new positions: @{[ $self->train->positions ]}\n"; - return $letter; } diff --git a/lib/Enigmatic/Rotor.pm b/lib/Enigmatic/Rotor.pm index 9a88718..f725160 100644 --- a/lib/Enigmatic/Rotor.pm +++ b/lib/Enigmatic/Rotor.pm @@ -68,10 +68,9 @@ sub _real_map { my $setting = $self->ring_setting; - my $log = "{$letter"; - $letter = _rotate_by($letter,$setting);$log.=$letter; - $letter = $self->$wiring->at($letter);$log.=$letter; -# $letter = _rotate_by($letter,-$setting);$log.=$letter; - $log .= "}\n";warn $log; + $letter = _rotate_by($letter,-$setting); + $letter = $self->$wiring->at($letter); + $letter = _rotate_by($letter,$setting); + return $letter; } diff --git a/t/basic-enigma.t b/t/basic-enigma.t index 0760737..9ed8c90 100644 --- a/t/basic-enigma.t +++ b/t/basic-enigma.t @@ -24,5 +24,4 @@ Test::Enigmatic::test_full_machine( 'AAAAA', 'EWTYX'); - done_testing(); diff --git a/t/full-enigma.t b/t/full-enigma.t index 0015b46..872b199 100644 --- a/t/full-enigma.t +++ b/t/full-enigma.t @@ -1,6 +1,6 @@ #!perl use DAKKAR::p 'test'; - +use Test::Enigmatic; use Enigmatic::Machine; sub real_machine { @@ -27,7 +27,6 @@ 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 @@ -43,27 +42,11 @@ 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'); +Test::Enigmatic::test_full_machine( + \&real_machine, + $plaintext, + $ciphertext); +done_testing(); diff --git a/t/lib/Test/Enigmatic.pm b/t/lib/Test/Enigmatic.pm index 9958667..6e35070 100644 --- a/t/lib/Test/Enigmatic.pm +++ b/t/lib/Test/Enigmatic.pm @@ -18,9 +18,9 @@ sub test_static_map { sub test_full_machine { my ($factory,$input,$exp_output) = @_; - $input =~ s{\s+}{}g; + $input =~ s{\s+}{}g;$input=$input->uc; - my $output = $factory->()->map_string(uc $input); + my $output = $factory->()->map_string($input); note "output: $output"; @@ -32,9 +32,9 @@ sub test_full_machine { $input, 'round tripped'); if ($exp_output) { - $exp_output =~ s{\s+}{}g; + $exp_output =~ s{\s+}{}g;$exp_output=$exp_output->uc; is($output, - uc($exp_output), + $exp_output, 'got expected output'); } } diff --git a/t/rotors.t b/t/rotors.t index 4676a55..27b6eba 100644 --- a/t/rotors.t +++ b/t/rotors.t @@ -19,26 +19,6 @@ subtest 'scramble rotor' => sub { Test::Enigmatic::test_static_map($r,\@out,'shuffle'); }; -subtest 'identity rotor, non-default ring setting' => sub { - my @out = (('B'..'Z'),'A'); - - my $r = Enigmatic::Rotor->new( - wiring => ['A'..'Z']->join, - ring_setting => 1, - ); - Test::Enigmatic::test_static_map($r,\@out,'ring=1'); -}; - -subtest 'identity rotor, moved ring setting' => sub { - my @out = (('B'..'Z'),'A'); - - my $r = Enigmatic::Rotor->new( - wiring => ['A'..'Z']->join, - ); - $r->ring_setting(1); - Test::Enigmatic::test_static_map($r,\@out,'ring moved to 1'); -}; - subtest 'constraints' => sub { my @bad_params = ( 'ABCD', @@ -107,4 +87,15 @@ subtest 'rotor box' => sub { }); }; +subtest 'ring-setting on real rotor' => sub { + my $r = Enigmatic::RotorBox->new->get('I'); + $r->ring_setting(1); + + my $out = 'EKMFLGDQVZNTOWYHXUSPAIBRCJ'->split(qr//); + $out->unshift($out->pop); + $out->each_value(sub{$_[0] =~ tr[A-Z][B-ZA]}); + + Test::Enigmatic::test_static_map($r,$out,'ring I moved to 1'); +}; + done_testing(); diff --git a/t/simple-enigma.t b/t/simple-enigma.t index b2bcbdd..889f0c0 100644 --- a/t/simple-enigma.t +++ b/t/simple-enigma.t @@ -1,6 +1,6 @@ #!perl use DAKKAR::p 'test'; - +use Test::Enigmatic; use Enigmatic::Machine; sub real_machine { @@ -13,11 +13,8 @@ sub real_machine { }); } -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'); +Test::Enigmatic::test_full_machine( + \&real_machine, + 'this is a simple text to be encrypted by an enigma machine'); + +done_testing(); -- cgit v1.2.3