/
corrupt.lua
80 lines (61 loc) · 1.86 KB
/
corrupt.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package.path = package.path .. ";tests/?.lua"
local testlib = require("testlib")
function simpletrunc(dbname)
local db = couch.open(dbname, true)
db:save("k", "original value", 1)
db:commit()
testlib.check_doc(db, "k", "original value")
db:save("k", "second value", 1)
db:commit()
testlib.check_doc(db, "k", "second value")
db:truncate(-10)
db:close()
db = couch.open(dbname)
testlib.check_doc(db, "k", "original value")
db:save("k", "third value", 1)
db:commit()
db:close()
db = couch.open(dbname)
testlib.check_doc(db, "k", "third value")
end
function do_mangle(dbname, offset)
local db = couch.open(dbname, true)
db:save("k", "original value", 1)
db:commit()
testlib.check_doc(db, "k", "original value")
db:save("k", "second value", 1)
db:commit()
testlib.check_doc(db, "k", "second value")
db:close()
local f = io.open(dbname, "r+")
local corruptWith = "here be some garbage"
local treelen = f:seek("end") % 4096
if offset > treelen then
return false
end
f:seek("end", 0 - treelen)
f:write(corruptWith)
f:close()
db = couch.open(dbname)
testlib.check_doc(db, "k", "original value")
db:save("k", "third value", 1)
db:commit()
db:close()
db = couch.open(dbname)
testlib.check_doc(db, "k", "third value")
return true
end
-- Mangle the header with some arbitrary data from the beginning of
-- the header boundary to the end. The header is located by the EOF %
-- 4096. Should probably do something special when EOF *is* at a 4k
-- boundary, but this test is valuable without it.
function header_mangling(dbname)
local i = 0
while do_mangle(dbname, i) do
i = i + 1
os.remove(dbname)
end
end
testlib.run_test("Simple truncation test", simpletrunc)
testlib.run_test("Various mangling of headers", header_mangling)
return testlib.fail_count()