Sha256: 788fbd257673678a7746f89d02f02f81d8d6397cfd013b3f69a2ae97f8ff82f3

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

# encoding: utf-8
require 'test/unit'
require 'URLcrypt'

class TestURLcrypt < Test::Unit::TestCase
  def assert_bytes_equal(string1, string2)
    bytes1 = string1.bytes.to_a.join(':')
    bytes2 = string2.bytes.to_a.join(':')
    assert_equal(bytes1, bytes2)
  end
  
  def assert_decoding(encoded, plain)
    decoded = URLcrypt.decode(encoded)
    assert_bytes_equal(plain, decoded)
  end

  def assert_encoding(encoded, plain)
    actual = URLcrypt.encode(plain)
    assert_bytes_equal(encoded, actual)
  end

  def assert_encode_and_decode(encoded, plain)
    assert_encoding(encoded, plain)
    assert_decoding(encoded, plain)
  end

  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
    # this key was generated via rake secret in a rails app, the pack() converts it into a byte array
    URLcrypt::key =
['d25883a27b9a639da85ea7e159b661218799c9efa63069fac13a6778c954fb6d721968887a19bdb01af8f59eb5a90d256bd9903355c20b0b4b39bf4048b9b17b'].pack('H*')
    
    original  = "hello world!"
    encrypted = URLcrypt::encrypt(original)
    assert_equal(URLcrypt::decrypt(encrypted), original)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
urlcrypt-0.1.0 test/URLcrypt_test.rb