2019-09-01 11:51:48 +02:00
|
|
|
require "../spec_helper"
|
|
|
|
require "../../src/sodium/secure_buffer"
|
2021-06-16 23:50:26 +02:00
|
|
|
require "crypto-secret/test"
|
2019-09-01 11:51:48 +02:00
|
|
|
|
2019-09-01 19:31:36 +02:00
|
|
|
class FakeError < Exception
|
|
|
|
end
|
|
|
|
|
2021-06-16 23:50:26 +02:00
|
|
|
test_secret_class Sodium::SecureBuffer
|
|
|
|
|
2019-09-01 11:51:48 +02:00
|
|
|
describe Sodium::SecureBuffer do
|
|
|
|
it "allocates empty" do
|
|
|
|
buf = Sodium::SecureBuffer.new 5
|
2021-06-15 02:02:37 +02:00
|
|
|
buf.readonly do |slice|
|
|
|
|
slice.each do |b|
|
|
|
|
b.should eq 0xdb_u8
|
|
|
|
end
|
2019-09-01 11:51:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
buf.noaccess
|
|
|
|
buf.readonly
|
|
|
|
buf.readwrite
|
|
|
|
end
|
|
|
|
|
|
|
|
it "dups without crashing" do
|
2021-06-15 02:02:37 +02:00
|
|
|
buf1 = Sodium::SecureBuffer.new 5
|
|
|
|
buf1.noaccess
|
2019-09-01 11:51:48 +02:00
|
|
|
|
2021-06-15 02:02:37 +02:00
|
|
|
buf2 = buf1.dup
|
|
|
|
buf2.@state.should eq Sodium::SecureBuffer::State::Noaccess
|
2019-09-01 11:51:48 +02:00
|
|
|
|
2021-06-15 02:02:37 +02:00
|
|
|
buf1.readwrite do |slice|
|
|
|
|
slice[0] = 1_u8
|
|
|
|
end
|
|
|
|
buf1.hexstring.should_not eq buf2.hexstring
|
|
|
|
|
|
|
|
buf2.readwrite do |slice|
|
|
|
|
slice[0] = 1_u8
|
|
|
|
end
|
|
|
|
buf1.hexstring.should eq buf2.hexstring
|
2019-09-01 11:51:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "transitions correctly" do
|
|
|
|
buf = Sodium::SecureBuffer.new 5
|
|
|
|
|
|
|
|
buf.noaccess
|
|
|
|
buf.@state.should eq Sodium::SecureBuffer::State::Noaccess
|
2019-09-14 06:33:21 +02:00
|
|
|
buf.readonly { buf.@state.should eq Sodium::SecureBuffer::State::Readonly }
|
2019-09-01 11:51:48 +02:00
|
|
|
buf.@state.should eq Sodium::SecureBuffer::State::Noaccess
|
|
|
|
|
|
|
|
buf.readonly
|
|
|
|
buf.@state.should eq Sodium::SecureBuffer::State::Readonly
|
2019-09-14 06:33:21 +02:00
|
|
|
buf.readwrite { buf.@state.should eq Sodium::SecureBuffer::State::Readwrite }
|
2019-09-01 11:51:48 +02:00
|
|
|
buf.@state.should eq Sodium::SecureBuffer::State::Readonly
|
|
|
|
|
|
|
|
buf.readwrite
|
|
|
|
buf.@state.should eq Sodium::SecureBuffer::State::Readwrite
|
2019-09-14 06:33:21 +02:00
|
|
|
buf.readonly { buf.@state.should eq Sodium::SecureBuffer::State::Readwrite }
|
2019-09-01 19:31:36 +02:00
|
|
|
buf.@state.should eq Sodium::SecureBuffer::State::Readwrite
|
2019-09-01 11:51:48 +02:00
|
|
|
|
|
|
|
buf.wipe
|
|
|
|
buf.@state.should eq Sodium::SecureBuffer::State::Wiped
|
|
|
|
end
|
|
|
|
|
2019-09-01 19:31:36 +02:00
|
|
|
it "temporarily transitions correctly with exceptions" do
|
|
|
|
buf = Sodium::SecureBuffer.new(5).noaccess
|
|
|
|
begin
|
|
|
|
buf.readonly { raise FakeError.new }
|
|
|
|
rescue FakeError
|
|
|
|
end
|
|
|
|
buf.@state.should eq Sodium::SecureBuffer::State::Noaccess
|
|
|
|
end
|
|
|
|
|
2019-09-01 11:51:48 +02:00
|
|
|
it "can wipe more than once" do
|
|
|
|
buf = Sodium::SecureBuffer.new 5
|
|
|
|
3.times { buf.wipe }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can't transition from wiped" do
|
|
|
|
buf = Sodium::SecureBuffer.new 5
|
|
|
|
buf.wipe
|
|
|
|
expect_raises Sodium::SecureBuffer::Error::KeyWiped do
|
|
|
|
buf.readwrite
|
|
|
|
end
|
|
|
|
expect_raises Sodium::SecureBuffer::Error::KeyWiped do
|
|
|
|
buf.readonly
|
|
|
|
end
|
|
|
|
expect_raises Sodium::SecureBuffer::Error::KeyWiped do
|
|
|
|
buf.noaccess
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|