diff options
Diffstat (limited to 'spec/ruby/library/base64')
| -rw-r--r-- | spec/ruby/library/base64/decode64_spec.rb | 22 | ||||
| -rw-r--r-- | spec/ruby/library/base64/encode64_spec.rb | 11 | ||||
| -rw-r--r-- | spec/ruby/library/base64/strict_decode64_spec.rb | 41 | ||||
| -rw-r--r-- | spec/ruby/library/base64/strict_encode64_spec.rb | 19 | ||||
| -rw-r--r-- | spec/ruby/library/base64/urlsafe_decode64_spec.rb | 14 | ||||
| -rw-r--r-- | spec/ruby/library/base64/urlsafe_encode64_spec.rb | 10 |
6 files changed, 98 insertions, 19 deletions
diff --git a/spec/ruby/library/base64/decode64_spec.rb b/spec/ruby/library/base64/decode64_spec.rb index 34d5ed6989..6dd33dddfe 100644 --- a/spec/ruby/library/base64/decode64_spec.rb +++ b/spec/ruby/library/base64/decode64_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'base64' @@ -6,4 +6,24 @@ describe "Base64#decode64" do it "returns the Base64-decoded version of the given string" do Base64.decode64("U2VuZCByZWluZm9yY2VtZW50cw==\n").should == "Send reinforcements" end + + it "returns the Base64-decoded version of the given shared string" do + Base64.decode64("base64: U2VuZCByZWluZm9yY2VtZW50cw==\n".split(" ").last).should == "Send reinforcements" + end + + it "returns the Base64-decoded version of the given string with wrong padding" do + Base64.decode64("XU2VuZCByZWluZm9yY2VtZW50cw===").should == "]M\x95\xB9\x90\x81\xC9\x95\xA5\xB9\x99\xBD\xC9\x8D\x95\xB5\x95\xB9\xD1\xCC".b + end + + it "returns the Base64-decoded version of the given string that contains an invalid character" do + Base64.decode64("%3D").should == "\xDC".b + end + + it "returns a binary encoded string" do + Base64.decode64("SEk=").encoding.should == Encoding::BINARY + end + + it "decodes without padding suffix ==" do + Base64.decode64("eyJrZXkiOnsibiI6InR0dCJ9fQ").should == "{\"key\":{\"n\":\"ttt\"}}" + end end diff --git a/spec/ruby/library/base64/encode64_spec.rb b/spec/ruby/library/base64/encode64_spec.rb index 08df694e54..64de6257bc 100644 --- a/spec/ruby/library/base64/encode64_spec.rb +++ b/spec/ruby/library/base64/encode64_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'base64' @@ -11,4 +11,13 @@ describe "Base64#encode64" do it "returns the Base64-encoded version of the given string" do Base64.encode64('Send reinforcements').should == "U2VuZCByZWluZm9yY2VtZW50cw==\n" end + + it "returns the Base64-encoded version of the given shared string" do + Base64.encode64("Now is the time for all good coders\nto learn Ruby".split("\n").last).should == + "dG8gbGVhcm4gUnVieQ==\n" + end + + it "returns a US_ASCII encoded string" do + Base64.encode64("HI").encoding.should == Encoding::US_ASCII + end end diff --git a/spec/ruby/library/base64/strict_decode64_spec.rb b/spec/ruby/library/base64/strict_decode64_spec.rb new file mode 100644 index 0000000000..7b52f0c922 --- /dev/null +++ b/spec/ruby/library/base64/strict_decode64_spec.rb @@ -0,0 +1,41 @@ +require_relative '../../spec_helper' + +require 'base64' + +describe "Base64#strict_decode64" do + it "returns the Base64-decoded version of the given string" do + Base64.strict_decode64("U2VuZCByZWluZm9yY2VtZW50cw==").should == "Send reinforcements" + end + + it "returns the Base64-decoded version of the given shared string" do + Base64.strict_decode64("base64: U2VuZCByZWluZm9yY2VtZW50cw==".split(" ").last).should == "Send reinforcements" + end + + it "raises ArgumentError when the given string contains CR" do + -> do + Base64.strict_decode64("U2VuZCByZWluZm9yY2VtZW50cw==\r") + end.should.raise(ArgumentError) + end + + it "raises ArgumentError when the given string contains LF" do + -> do + Base64.strict_decode64("U2VuZCByZWluZm9yY2VtZW50cw==\n") + end.should.raise(ArgumentError) + end + + it "raises ArgumentError when the given string has wrong padding" do + -> do + Base64.strict_decode64("=U2VuZCByZWluZm9yY2VtZW50cw==") + end.should.raise(ArgumentError) + end + + it "raises ArgumentError when the given string contains an invalid character" do + -> do + Base64.strict_decode64("%3D") + end.should.raise(ArgumentError) + end + + it "returns a binary encoded string" do + Base64.strict_decode64("SEk=").encoding.should == Encoding::BINARY + end +end diff --git a/spec/ruby/library/base64/strict_encode64_spec.rb b/spec/ruby/library/base64/strict_encode64_spec.rb new file mode 100644 index 0000000000..7cabcf190c --- /dev/null +++ b/spec/ruby/library/base64/strict_encode64_spec.rb @@ -0,0 +1,19 @@ +require_relative '../../spec_helper' + +require 'base64' + +describe "Base64#strict_encode64" do + it "returns the Base64-encoded version of the given string" do + Base64.strict_encode64("Now is the time for all good coders\nto learn Ruby").should == + "Tm93IGlzIHRoZSB0aW1lIGZvciBhbGwgZ29vZCBjb2RlcnMKdG8gbGVhcm4gUnVieQ==" + end + + it "returns the Base64-encoded version of the given shared string" do + Base64.strict_encode64("Now is the time for all good coders\nto learn Ruby".split("\n").last).should == + "dG8gbGVhcm4gUnVieQ==" + end + + it "returns a US_ASCII encoded string" do + Base64.strict_encode64("HI").encoding.should == Encoding::US_ASCII + end +end diff --git a/spec/ruby/library/base64/urlsafe_decode64_spec.rb b/spec/ruby/library/base64/urlsafe_decode64_spec.rb index bea9a7cefb..1b813ee1b9 100644 --- a/spec/ruby/library/base64/urlsafe_decode64_spec.rb +++ b/spec/ruby/library/base64/urlsafe_decode64_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'base64' @@ -13,15 +13,7 @@ describe "Base64#urlsafe_decode64" do decoded.should == '"Being disintegrated makes me ve-ry an-gry!" <huff, huff>' end - ruby_version_is ""..."2.3" do - it "requires padding" do - lambda { Base64.urlsafe_decode64("MQ") }.should raise_error(ArgumentError) - end - end - - ruby_version_is "2.3" do - it "does not require padding" do - Base64.urlsafe_decode64("MQ").should == "1" - end + it "does not require padding" do + Base64.urlsafe_decode64("MQ").should == "1" end end diff --git a/spec/ruby/library/base64/urlsafe_encode64_spec.rb b/spec/ruby/library/base64/urlsafe_encode64_spec.rb index 32fce72603..de1f235cea 100644 --- a/spec/ruby/library/base64/urlsafe_encode64_spec.rb +++ b/spec/ruby/library/base64/urlsafe_encode64_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'base64' @@ -13,10 +13,8 @@ describe "Base64#urlsafe_encode64" do encoded.should == 'IkJlaW5nIGRpc2ludGVncmF0ZWQgbWFrZXMgbWUgdmUtcnkgYW4tZ3J5ISIgPGh1ZmYsIGh1ZmY-' end - ruby_version_is "2.3" do - it "makes padding optional" do - Base64.urlsafe_encode64("1", padding: false).should == "MQ" - Base64.urlsafe_encode64("1").should == "MQ==" - end + it "makes padding optional" do + Base64.urlsafe_encode64("1", padding: false).should == "MQ" + Base64.urlsafe_encode64("1").should == "MQ==" end end |
