Sha256: 15eb03c6727d5f122744143596ede14fd68cda660cde4508160f000dfbe66d80

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

# encoding: utf-8
require 'test_helper'

class TestURLcrypt < TestClass
  def test_empty_string
    assert_encode_and_decode('', '')
  end

  def test_encode
    assert_encode_and_decode(
      '111gc86f4nxw5zj1b3qmhpb14n5h25l4m7111',
      "\0\0awesome \n ΓΌ string\0\0")
  end

  def test_invalid_encoding
    assert_decoding('ZZZZZ', '')
  end

  def test_arbitrary_byte_strings
    0.step(1500,17) do |n|
      original = (0..n).map{rand(256).chr}.join
      encoded = URLcrypt::encode(original)
      assert_decoding(encoded, original)
    end
  end
  
  def test_encryption
    # pack() converts this secret into a byte array
    secret = ['d25883a27b9a639da85ea7e159b661218799c9efa63069fac13a6778c954fb6d'].pack('H*')
    URLcrypt::key = secret

    assert_equal  OpenSSL::Cipher.new('aes-256-cbc').key_len, secret.bytesize

    original  = "hello world!"
    encrypted = URLcrypt::encrypt(original)
    assert_equal(URLcrypt::decrypt(encrypted), original)
  end

  def test_decrypt_error
    error = assert_raises(URLcrypt::DecryptError) do
      ::URLcrypt::decrypt("just some plaintext")
    end
    assert_equal error.message, "not a valid string to decrypt"
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
urlcrypt-0.1.2 test/URLcrypt_test.rb