spec/security_spec.rb in prawn-1.0.0.rc2 vs spec/security_spec.rb in prawn-1.0.0

- old
+ new

@@ -1,9 +1,9 @@ # encoding: utf-8 require "tempfile" -require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper") +require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper") describe "Document encryption" do describe "Password padding" do @@ -29,13 +29,13 @@ padded.length.should == 32 padded.should == Prawn::Document::Security::PasswordPadding end end - + describe "Setting permissions" do - + def doc_with_permissions(permissions) pdf = Prawn::Document.new class << pdf # Make things easier to test @@ -104,23 +104,55 @@ end describe "EncryptedPdfObject" do it "should delegate to PdfObject for simple types" do - Prawn::Core::EncryptedPdfObject(true, nil, nil, nil).should == "true" - Prawn::Core::EncryptedPdfObject(42, nil, nil, nil).should == "42" + PDF::Core::EncryptedPdfObject(true, nil, nil, nil).should == "true" + PDF::Core::EncryptedPdfObject(42, nil, nil, nil).should == "42" end it "should encrypt strings properly" do - Prawn::Core::EncryptedPdfObject("foo", "12345", 123, 0).should == "<4ad6e3>" + PDF::Core::EncryptedPdfObject("foo", "12345", 123, 0).should == "<4ad6e3>" end + it "should encrypt literal strings properly" do + PDF::Core::EncryptedPdfObject(PDF::Core::LiteralString.new("foo"), "12345", 123, 0).should == bin_string("(J\xD6\xE3)") + PDF::Core::EncryptedPdfObject(PDF::Core::LiteralString.new("lhfbqg3do5u0satu3fjf"), nil, 123, 0).should == bin_string("(\xF1\x8B\\(\b\xBB\xE18S\x130~4*#\\(%\x87\xE7\x8E\\\n)") + end + + it "should encrypt time properly" do + PDF::Core::EncryptedPdfObject(Time.utc(2050, 04, 26, 10, 17, 10), "12345", 123, 0).should == bin_string("(h\x83\xBE\xDC\xEC\x99\x0F\xD7\\)%\x13\xD4$\xB8\xF0\x16\xB8\x80\xC5\xE91+\xCF)") + end + it "should properly handle compound types" do - Prawn::Core::EncryptedPdfObject({:Bar => "foo"}, "12345", 123, 0).should == + PDF::Core::EncryptedPdfObject({:Bar => "foo"}, "12345", 123, 0).should == "<< /Bar <4ad6e3>\n>>" - Prawn::Core::EncryptedPdfObject(["foo", "bar"], "12345", 123, 0).should == + PDF::Core::EncryptedPdfObject(["foo", "bar"], "12345", 123, 0).should == "[<4ad6e3> <4ed8fe>]" end - + + end + + describe "Reference#encrypted_object" do + it "should encrypt references properly" do + ref = PDF::Core::Reference(1,["foo"]) + ref.encrypted_object(nil).should == "1 0 obj\n[<4fca3f>]\nendobj\n" + end + + it "should encrypt references with streams properly" do + ref = PDF::Core::Reference(1, {}) + ref << 'foo' + result = bin_string("1 0 obj\n<< /Length 3\n>>\nstream\nO\xCA?\nendstream\nendobj\n") + ref.encrypted_object(nil).should == result + end + end + + describe "String#encrypted_object" do + it "should encrypt stream properly" do + stream = PDF::Core::Stream.new + stream << "foo" + result = bin_string("stream\nO\xCA?\nendstream\n") + stream.encrypted_object(nil, 1, 0).should == result + end end end