2013-04-17 19:26:16 +00:00
|
|
|
# -*- coding: binary -*-
|
2013-01-15 22:59:01 +00:00
|
|
|
shared_examples_for 'an xor encoder' do |keysize|
|
|
|
|
|
2013-09-30 18:47:53 +00:00
|
|
|
it "should encode one block" do
|
|
|
|
# Yup it returns one of its arguments in an array... Because spoon.
|
|
|
|
encoded, key = described_class.encode("A"*keysize, "A"*keysize)
|
|
|
|
encoded.should eql("\x00"*keysize)
|
2013-01-15 22:59:01 +00:00
|
|
|
|
2013-09-30 18:47:53 +00:00
|
|
|
encoded, key = described_class.encode("\x0f"*keysize, "\xf0"*keysize)
|
|
|
|
encoded.should eql("\xff"*keysize)
|
2013-01-15 22:59:01 +00:00
|
|
|
|
2013-09-30 18:47:53 +00:00
|
|
|
encoded, key = described_class.encode("\xf7"*keysize, "\x7f"*keysize)
|
|
|
|
encoded.should eql("\x88"*keysize)
|
|
|
|
end
|
2013-01-15 22:59:01 +00:00
|
|
|
|
2013-09-30 18:47:53 +00:00
|
|
|
it "should encode multiple blocks" do
|
|
|
|
2.upto 50 do |count|
|
|
|
|
encoded, key = described_class.encode("\xf7"*keysize*count, "\x7f"*keysize)
|
|
|
|
encoded.should eql("\x88"*keysize*count)
|
|
|
|
end
|
|
|
|
end
|
2013-04-25 20:45:06 +00:00
|
|
|
|
2013-09-30 18:47:53 +00:00
|
|
|
if keysize > 1
|
|
|
|
it "should deal with input lengths that aren't a multiple of keysize" do
|
|
|
|
lambda {
|
|
|
|
encoded, key = described_class.encode("A"*(keysize+1), "A"*keysize)
|
|
|
|
encoded.should eql("\x00"*(keysize+1))
|
|
|
|
}.should_not raise_error
|
2013-04-25 20:45:06 +00:00
|
|
|
|
2013-09-30 18:47:53 +00:00
|
|
|
lambda {
|
|
|
|
encoded, key = described_class.encode("A"*(keysize-1), "A"*keysize)
|
|
|
|
encoded.should eql("\x00"*(keysize-1))
|
|
|
|
}.should_not raise_error
|
|
|
|
end
|
|
|
|
end
|
2013-01-15 22:59:01 +00:00
|
|
|
|
|
|
|
end
|