# HG changeset patch # User Alessio Caiazza # Date 1269179175 -3600 # Node ID 0a90b0f886e8257dc9777df5c190d5077f0b7e73 # Parent b4445ee7fce119918c5070d644116078379e0a9a Rspec testing diff -r b4445ee7fce119918c5070d644116078379e0a9a -r 0a90b0f886e8257dc9777df5c190d5077f0b7e73 Rakefile --- a/Rakefile Sun Mar 21 12:24:17 2010 +0100 +++ b/Rakefile Sun Mar 21 14:46:15 2010 +0100 @@ -3,6 +3,8 @@ require 'rake' require 'echoe' +ENV['SPEC_OPTS'] ||= '-c' + Echoe.new('YubiRuby') do |p| p.description = <<-EOF Yubikey integration - @@ -41,9 +43,10 @@ p.email = "nolith@abisso.org" p.platform = Gem::Platform::RUBY p.ignore_pattern = ["tmp/**/*", "script/*"] - p.development_dependencies = ['yard'] + p.development_dependencies = ['yard', 'rspec'] p.runtime_dependencies << ['ruby-aes-normal', '~> 1.0'] p.runtime_dependencies << ['bit-struct'] #, '~> 0.13.6'] + p.spec_pattern = ['test/**/*_spec.rb'] end Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext } diff -r b4445ee7fce119918c5070d644116078379e0a9a -r 0a90b0f886e8257dc9777df5c190d5077f0b7e73 test/hex_spec.rb --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/hex_spec.rb Sun Mar 21 14:46:15 2010 +0100 @@ -0,0 +1,52 @@ +require 'yubiruby' + +describe 'YubiRuby::HEX' do + describe '#encode' do + it 'should work as expected' do + str = "ciao\n" + hex = "6369616f0a" + YubiRuby::HEX.encode(str).should == hex + end + + it 'should be a reversible operation' do + str = "ciao\n" + hex = YubiRuby::HEX.encode(str) + YubiRuby::HEX.decode(hex).should == str + end + end + + describe '#decode' do + it 'should work as expected' do + str = "ciao\n" + hex = "6369616f0a" + YubiRuby::HEX.decode(hex).should == str + end + + it 'should be a reversible operation' do + hex = "6369616f0a" + str = YubiRuby::HEX.decode(hex) + YubiRuby::HEX.encode(str).should == hex + end + + it 'should raise NoHexEncodedError if decoding non Hex string' do + lambda { YubiRuby::HEX.decode('a') }.should raise_error(YubiRuby::NoHexEncodedError) + lambda {YubiRuby::HEX.decode('zebr') }.should raise_error(YubiRuby::NoHexEncodedError) + lambda {YubiRuby::HEX.decode('!af5') }.should raise_error(YubiRuby::NoHexEncodedError) + lambda {YubiRuby::HEX.decode('0x12ff') }.should raise_error(YubiRuby::NoHexEncodedError) + end + end + + describe '#hex?' do + it 'should recognize valid hex' do + YubiRuby::HEX.hex?('7af5').should == true + YubiRuby::HEX.hex?('12fF').should == true + end + + it 'should recognize invalid hex' do + YubiRuby::HEX.hex?('a').should == false + YubiRuby::HEX.hex?('zebr').should == false + YubiRuby::HEX.hex?('!af5').should == false + YubiRuby::HEX.hex?('0x12ff').should == false + end + end +end diff -r b4445ee7fce119918c5070d644116078379e0a9a -r 0a90b0f886e8257dc9777df5c190d5077f0b7e73 test/modhex_spec.rb --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/modhex_spec.rb Sun Mar 21 14:46:15 2010 +0100 @@ -0,0 +1,81 @@ +require 'yubiruby' + +describe 'YubiRuby::ModHex' do + + it 'should be a reversible operation' do + str = "test" + hex = "ifhgieif" + YubiRuby::ModHex.decode(YubiRuby::ModHex.encode(str)).should == str + YubiRuby::ModHex.encode(YubiRuby::ModHex.decode(hex)).should == hex + end + + describe '#encode' do + it 'should work as expected' do + str = "test" + hex = "ifhgieif" + YubiRuby::ModHex.encode(str).should == hex + end + + it "should double the string lenght" do + str = '1234zz567890' + str.size.should == 12 + str.modhex_encode.size.should == 24 + end + + end + + describe '#decode' do + it 'should work as expected' do + str = "test" + hex = "ifhgieif" + YubiRuby::ModHex.decode(hex).should == str + end + + it 'should raise NoModHexEncodedError if decoding non ModHex string' do + lambda { YubiRuby::ModHex.decode('a') }.should raise_error(YubiRuby::NoModHexEncodedError) + lambda {YubiRuby::ModHex.decode('zebr') }.should raise_error(YubiRuby::NoModHexEncodedError) + lambda {YubiRuby::ModHex.decode('!af5') }.should raise_error(YubiRuby::NoModHexEncodedError) + lambda {YubiRuby::ModHex.decode('0x12ff') }.should raise_error(YubiRuby::NoModHexEncodedError) + end + + it "should halves the string lenght" do + otp = 'dbkutdgrnlvrhdiregebvkkrgtuefjru' + otp.size.should == 32 + otp.modhex_decode.size.should == 16 + end + end + + describe '#hex?' do + it 'should recognize valid hex' do + YubiRuby::ModHex.modhex?('ifhgieif').should == true + YubiRuby::ModHex.modhex?('ichkicichv').should == true + end + + it 'should recognize invalid hex' do + YubiRuby::ModHex.modhex?('a').should == false + YubiRuby::ModHex.modhex?('zebr').should == false + YubiRuby::ModHex.modhex?('!af5').should == false + YubiRuby::ModHex.modhex?('0x12ff').should == false + end + end +end + +describe 'String' do + describe '#modhex_decode' do + it "should call YubiRuby::ModHex#decode once" do + otp = 'dbkutdgrnlvrhdiregebvkkrgtuefjru' + YubiRuby::ModHex.should_receive(:decode).once.with(otp) + YubiRuby::ModHex.should_not_receive(:encode) + otp.modhex_decode + end + end + + describe '#modhex_encode' do + it "should call YubiRuby::ModHex#encode once" do + str = 'ciaociao ciao' + YubiRuby::ModHex.should_receive(:encode).once.with(str) + YubiRuby::ModHex.should_not_receive(:decode) + str.modhex_encode + end + end +end \ No newline at end of file diff -r b4445ee7fce119918c5070d644116078379e0a9a -r 0a90b0f886e8257dc9777df5c190d5077f0b7e73 test/tc_modhex.rb --- a/test/tc_modhex.rb Sun Mar 21 12:24:17 2010 +0100 +++ b/test/tc_modhex.rb Sun Mar 21 14:46:15 2010 +0100 @@ -15,7 +15,7 @@ assert_equal(YubiRuby::ModHex.encode(YubiRuby::ModHex.decode(hex)), hex) end - def test_no_hex_encoded_exception + def test_no_modhex_encoded_exception assert !YubiRuby::ModHex.modhex?('a') assert !YubiRuby::ModHex.modhex?('zebr') assert !YubiRuby::ModHex.modhex?('!af5')