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
|
gem 'minitest'
require 'minitest/autorun'
require 'base32'
class TestBase32 < Minitest::Test
def assert_decoding(encoded, plain)
decoded = Base32.decode(encoded)
assert_equal(plain, decoded)
end
def assert_encoding(encoded, plain)
actual = Base32.encode(plain)
assert_equal(encoded, actual)
end
def assert_encode_and_decode(encoded, plain)
assert_encoding(encoded, plain)
assert_decoding(encoded, plain)
end
def assert_hex_encode_and_decode(encoded, hex)
plain = [hex].pack('H*')
assert_encode_and_decode(encoded, plain)
end
def test_empty_string
assert_encode_and_decode('', '')
end
def test_a
assert_encode_and_decode('ME======', 'a')
end
def test_12345
assert_encode_and_decode('GEZDGNBV', '12345')
end
def test_abcde
assert_encode_and_decode('MFRGGZDF', 'abcde')
end
def test_constitution_preamble
plaintext =<<-EOT
We the people of the United States, in order to form a more perfect union,
establish justice, insure domestic tranquility, provide for the common
defense, promote the general welfare, and secure the blessings of liberty
to ourselves and our posterity, do ordain and establish this Constitution
for the United States of America.
EOT
encoded = %W(
EAQCAIBAEBLWKIDUNBSSA4DFN5YGYZJAN5TCA5DIMUQFK3TJORSWIICTORQXIZLTFQQGS3RA
N5ZGIZLSEB2G6IDGN5ZG2IDBEBWW64TFEBYGK4TGMVRXIIDVNZUW63RMBIQCAIBAEAQGK43U
MFRGY2LTNAQGU5LTORUWGZJMEBUW443VOJSSAZDPNVSXG5DJMMQHI4TBNZYXK2LMNF2HSLBA
OBZG65TJMRSSAZTPOIQHI2DFEBRW63LNN5XAUIBAEAQCAIDEMVTGK3TTMUWCA4DSN5WW65DF
EB2GQZJAM5SW4ZLSMFWCA53FNRTGC4TFFQQGC3TEEBZWKY3VOJSSA5DIMUQGE3DFONZWS3TH
OMQG6ZRANRUWEZLSOR4QUIBAEAQCAIDUN4QG65LSONSWY5TFOMQGC3TEEBXXK4RAOBXXG5DF
OJUXI6JMEBSG6IDPOJSGC2LOEBQW4ZBAMVZXIYLCNRUXG2BAORUGS4ZAINXW443UNF2HK5DJ
N5XAUIBAEAQCAIDGN5ZCA5DIMUQFK3TJORSWIICTORQXIZLTEBXWMICBNVSXE2LDMEXAU===).join
assert_encode_and_decode(encoded, plaintext)
end
def test_hex_byte_encoding
assert_hex_encode_and_decode('FA======', '28')
assert_hex_encode_and_decode('2Y======', 'd6')
assert_hex_encode_and_decode('234A====', 'd6f8')
assert_hex_encode_and_decode('234AA===', 'd6f800')
assert_hex_encode_and_decode('234BA===', 'd6f810')
assert_hex_encode_and_decode('234BCDA=', 'd6f8110c')
assert_hex_encode_and_decode('234BCDEA', 'd6f8110c80')
assert_hex_encode_and_decode('234BCDEFGA======', 'd6f8110c8530')
assert_hex_encode_and_decode('234BCDEFG234BCDEFE======', 'd6f8110c8536b7c0886429')
end
def test_random_base32
assert_equal(16, Base32.random_base32.length)
assert_match(/^[A-Z2-7]+$/, Base32.random_base32)
end
def test_random_base32_length
assert_equal(32, Base32.random_base32(32).length)
assert_equal(40, Base32.random_base32(40).length)
assert_equal(32, Base32.random_base32(29).length)
assert_match(/^[A-Z2-7]{1}={7}$/, Base32.random_base32(1))
assert_match(/^[A-Z2-7]{29}={3}$/, Base32.random_base32(29))
end
def test_random_base32_padding
assert_equal(32, Base32.random_base32(32, false).length)
assert_equal(40, Base32.random_base32(40, false).length)
assert_equal(29, Base32.random_base32(29, false).length)
assert_match(/^[A-Z2-7]{1}$/, Base32.random_base32(1, false))
assert_match(/^[A-Z2-7]{29}$/, Base32.random_base32(29, false))
end
def test_assign_new_table
new_table = 'abcdefghijklmnopqrstuvwxyz234567'
Base32.table = new_table
assert_equal(new_table, Base32.table)
Base32.table = Base32::TABLE # so as not to ruin other tests
end
def test_check_table_length
assert_raises(ArgumentError) { Base32.table = ('a' * 31) }
assert_raises(ArgumentError) { Base32.table = ('a' * 32) }
assert_raises(ArgumentError) { Base32.table = ('a' * 33) }
assert_raises(ArgumentError) { Base32.table = ('abcdefghijklmnopqrstuvwxyz234567' * 2) }
Base32.table = Base32::TABLE # so as not to ruin other tests
end
def test_encode_decode_with_alternate_table
Base32.table = 'abcdefghijklmnopqrstuvwxyz234567'
assert_hex_encode_and_decode('fa======', '28')
assert_hex_encode_and_decode('2y======', 'd6')
assert_hex_encode_and_decode('234a====', 'd6f8')
assert_hex_encode_and_decode('234aa===', 'd6f800')
assert_hex_encode_and_decode('234ba===', 'd6f810')
assert_hex_encode_and_decode('234bcda=', 'd6f8110c')
assert_hex_encode_and_decode('234bcdea', 'd6f8110c80')
assert_hex_encode_and_decode('234bcdefga======', 'd6f8110c8530')
assert_hex_encode_and_decode('234bcdefg234bcdefe======', 'd6f8110c8536b7c0886429')
Base32.table = Base32::TABLE # so as not to ruin other tests
end
end
|