summaryrefslogtreecommitdiff
path: root/test/dbm
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-02-18 18:01:02 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-02-18 18:01:02 +0000
commit62debc952d23dad49611393bb0889a6c4a75bad2 (patch)
tree2a639f4ea302c0e401246f96c2f9320c842927de /test/dbm
parent0187905af2e16438606ead7618328bf062cc3339 (diff)
* test/dbm/test_dbm.rb (test_dbmfile_suffix): check magic numbers.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34686 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/dbm')
-rw-r--r--test/dbm/test_dbm.rb29
1 files changed, 28 insertions, 1 deletions
diff --git a/test/dbm/test_dbm.rb b/test/dbm/test_dbm.rb
index 99daee6..c041b0a 100644
--- a/test/dbm/test_dbm.rb
+++ b/test/dbm/test_dbm.rb
@@ -68,7 +68,7 @@ if defined? DBM
assert_instance_of(DBM, @dbm = DBM.new(@path))
end
def teardown
- assert_nil(@dbm.close)
+ assert_nil(@dbm.close) unless @dbm.closed?
ObjectSpace.each_object(DBM) do |obj|
obj.close unless obj.closed?
end
@@ -97,17 +97,44 @@ if defined? DBM
end
def test_dbmfile_suffix
+ @dbm.close
prefix = File.basename(@path)
suffixes = Dir.entries(@tmpdir).grep(/\A#{Regexp.escape prefix}/) { $' }.sort
+ pagname = "#{@path}.pag"
+ dirname = "#{@path}.dir"
+ dbname = "#{@path}.db"
case DBM::VERSION
when /\bNDBM\b/
assert_equal(%w[.dir .pag], suffixes)
when /\bGDBM\b/
assert_equal(%w[.dir .pag], suffixes)
+ pag = File.binread(pagname, 16)
+ pag_magics = [
+ 0x13579ace, # GDBM_OMAGIC
+ 0x13579acd, # GDBM_MAGIC32
+ 0x13579acf, # GDBM_MAGIC64
+ ]
+ assert_operator(pag_magics, :include?,
+ pag.unpack("i")[0]) # native endian, native int.
+ if !File.identical?(pagname, dirname)
+ dir = File.binread(dirname, 16)
+ assert_equal("GDBM", dir[0, 4])
+ end
when /\bBerkeley DB\b/
assert_equal(%w[.db], suffixes)
+ db = File.binread(dbname, 16)
+ assert(db[0,4].unpack("N") == [0x00061561] || # Berkeley DB 1
+ db[12,4].unpack("L") == [0x061561]) # Berkeley DBM 2 or later.
when /\bQDBM\b/
assert_equal(%w[.dir .pag], suffixes)
+ dir = File.binread(dirname, 16)
+ assert_equal("[depot]\0\v", dir[0, 9])
+ pag = File.binread(pagname, 16)
+ if [1].pack("s") == "\x00\x01" # big endian
+ assert_equal("[DEPOT]\n\f", pag[0, 9])
+ else # little endian
+ assert_equal("[depot]\n\f", pag[0, 9])
+ end
end
if suffixes == %w[.db]
assert_match(/\bBerkeley DB\b/, DBM::VERSION)