require_relative 'rspec_helper' describe Sixword do it 'should encode RFC hex vectors correctly' do Sixword::TestVectors::HexTests.each do |section, tests| tests.each do |hex, sentence| words = sentence.split byte_string = Sixword::Hex.decode(hex) debug_puts "Encode 0x#{hex} => #{words.inspect}" Sixword.encode(byte_string).should == words end end end it 'should decode RFC vectors to hex correctly' do Sixword::TestVectors::HexTests.each do |section, tests| tests.each do |hex, sentence| words = sentence.split byte_string = Sixword::Hex.decode(hex) debug_puts "Decode #{words.inspect} => 0x#{hex}" Sixword.decode(words).should == byte_string end end end it 'should decode with correct parity' do Sixword::TestVectors::ParityTest.find_all {|k, v| v}.each do |sentence, _| debug_puts "correct parity: #{sentence.inspect}" expect { Sixword.decode(sentence) }.to_not raise_error end end it 'should raise with incorrect parity' do Sixword::TestVectors::ParityTest.find_all {|k, v| !v}.each do |sentence, _| debug_puts "incorrect parity: #{sentence.inspect}" expect { Sixword.decode(sentence) }. to raise_error(Sixword::InvalidParity) end end it 'should reject encode inputs without padding' do expect { Sixword.encode('123') }.to raise_error(ArgumentError) expect { Sixword.encode('12345678X') }.to raise_error(ArgumentError) end it 'should reject decode inputs of wrong length' do expect { Sixword.decode('A') }.to raise_error(ArgumentError) end it 'should properly encode to sentences' do Sixword::TestVectors::HexTests.fetch('rfc 1751').each do |hex, sentence| # group into 6-word sentences sentences = sentence.split.each_slice(6).map {|s| s.join(' ')} byte_string = Sixword::Hex.decode(hex) debug_puts "Encoding #{hex.inspect} to sentences" debug_puts " => #{sentences.inspect}" Sixword.encode_to_sentences(byte_string).should == sentences end end it 'should convert hex strings to byte strings' do {"03e755bf6982fa55" => "\x03\xe7\x55\xbf\x69\x82\xfa\x55", "19dd19a502ca2d60" => "\x19\xdd\x19\xa5\x02\xca\x2d\x60", "1b208466c4560c93" => "\x1b\x20\x84\x66\xc4\x56\x0c\x93", "40026b4d6008286a" => "\x40\x02\x6b\x4d\x60\x08\x28\x6a", "4002 6b4d 6008 286a" => "\x40\x02\x6b\x4d\x60\x08\x28\x6a", "54686520717569636b2062726f776e20666f78206a756d7073206f7665722074686520" \ "6c617a7920646f672e" => "The quick brown fox jumps over the lazy dog.", "54686520717569636B2062726F776E20666F78206A756D7073206F7665722074686520" \ "6C617A7920646F672E" => "The quick brown fox jumps over the lazy dog.", }.each do |hex_string, byte_string| Sixword::Hex.decode(hex_string).should == byte_string end end end