| 1 | #!/usr/bin/perl |
| 2 | |
| 3 | use strict; |
| 4 | use warnings; |
| 5 | |
| 6 | use Test::More 'no_plan'; |
| 7 | |
| 8 | use ok "Crypt::Util"; |
| 9 | |
| 10 | my $c = Crypt::Util->new; |
| 11 | |
| 12 | my @strings = ( |
| 13 | 'moose', |
| 14 | 'foo bar gorch', |
| 15 | "\x00 \xff thzxgtj j\$/..,.,\"at {}\$2 1 \n \r \t", |
| 16 | # test various paddings |
| 17 | ' ', |
| 18 | ' ', |
| 19 | ' ', |
| 20 | ' ', |
| 21 | ' ', |
| 22 | ); |
| 23 | |
| 24 | SKIP: { |
| 25 | skip "URI::Escape required", @strings * 3 unless eval { require URI::Escape }; |
| 26 | skip "MIME::Base64 required", @strings * 3 unless eval { require MIME::Base64 }; |
| 27 | skip "MIME::Base64::URLSafe required", @strings * 3 unless eval { require MIME::Base64::URLSafe }; |
| 28 | |
| 29 | foreach my $string ( @strings ) { |
| 30 | my $encoded = $c->encode_string_uri_base64( $string ); |
| 31 | my $double_encoded = $c->encode_string_uri_escape( $encoded ); |
| 32 | |
| 33 | like( $encoded, qr/^[\w\*\-]+$/, "only valid chars" ); |
| 34 | is( $encoded, $double_encoded, "no need for further URI escaping" ); |
| 35 | |
| 36 | is( $c->decode_string_uri_base64($encoded), $string, "round trip" ); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | SKIP: foreach my $encoding (qw/hex uri_escape base64 base32 uri_base64/) { |
| 41 | skip "couldn't load $encoding provider", @strings * 2 unless $c->_try_encoding_fallback($encoding); |
| 42 | |
| 43 | foreach my $string ( @strings ) { |
| 44 | ok( defined( my $encoded = $c->encode_string( encoding => $encoding, string => $string ) ), "encode with $encoding" ); |
| 45 | is( $c->decode_string( encoding => $encoding, string => $encoded ), $string, "$encoding round trip" ); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | foreach my $encoding (qw/uri alphanumerical printable/) { |
| 50 | foreach my $string ( @strings ) { |
| 51 | ok( defined( my $encoded = $c->encode_string( encoding => $encoding, string => $string ) ), "encode with $encoding" ); |
| 52 | is( $c->decode_string( encoding => $encoding, string => $encoded ), $string, "$encoding round trip" ); |
| 53 | } |
| 54 | } |