-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcorruption_test.go
143 lines (106 loc) · 3.82 KB
/
corruption_test.go
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package graviton
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
// this creates a persistant store in tmp dir
// then commits and does a number of tests
// the closes the store and reopens and does all the tests again
func TestCorruptionStore_basic(t *testing.T) {
key := []byte("key1key1key1key1key1key1key1key1key1key1key1key1key1key1")
value := []byte("This value is good") // an 100 KB value, which will be corrupted later on disk to showcase detection
value_corrupted := []byte("Corrupted value !!")
dir, err := ioutil.TempDir("", "graviton_showcase_")
require.NoError(t, err)
defer os.RemoveAll(dir) // clean up
store, err := NewDiskStore(dir)
require.NoError(t, err)
gv, err := store.LoadSnapshot(0)
require.NoError(t, err)
tree, err := gv.GetTree("treename")
require.NoError(t, err)
err = tree.Put(key, value)
require.NoError(t, err)
_, err = Commit(tree)
require.NoError(t, err)
gv, err = store.LoadSnapshot(0)
require.NoError(t, err)
tree, err = gv.GetTree("treename")
require.NoError(t, err)
value, err = tree.Get(key)
//fmt.Printf("Reading value before corruption \"%s\" err %s\n", string(value), err)
// now the key has been persisted and verified now we will corrupt the value
file, err := os.OpenFile(filepath.Join(dir, "0", "0", "0", "0.dfs"), os.O_RDWR, 0644)
require.NoError(t, err)
file_contents, err := ioutil.ReadFile(filepath.Join(dir, "0", "0", "0", "0.dfs"))
require.NoError(t, err)
index := bytes.Index(file_contents, value)
if index < 0 {
panic("err could not find value in DB")
}
//fmt.Printf("contain count %d\n", bytes.Count(file_contents,value))
file.WriteAt(value_corrupted, int64(index))
file.Close()
gv, err = store.LoadSnapshot(0)
require.NoError(t, err)
tree1, err := gv.GetTree("treename")
require.NoError(t, err)
_, err = tree1.Get(key)
require.Error(t, err)
dummytree, err := gv.GetTree("dummytree")
require.NoError(t, err)
require.Error(t, Diff(tree1, dummytree, nil, nil, nil))
}
func TestCorruptionStore_Left(t *testing.T) {
key := []byte("key1key1key1key1key1key1key1key1key1key1key1key1key1key1")
value := []byte("This value is good") // an 100 KB value, which will be corrupted later on disk to showcase detection
value_corrupted := []byte("Corrupted value !!")
dir, err := ioutil.TempDir("", "graviton_showcase1_")
require.NoError(t, err)
defer os.RemoveAll(dir) // clean up
store, err := NewDiskStore(dir)
require.NoError(t, err)
gv, err := store.LoadSnapshot(0)
require.NoError(t, err)
tree, err := gv.GetTree("treename")
require.NoError(t, err)
for i := 0; i < 8; i++ {
key1 := randStr(60)
value1 := randStr(512)
tree.Put([]byte(key1), []byte(value1))
}
err = tree.Put(key, value)
require.NoError(t, err)
tree.Commit() // commit the tree
gv, err = store.LoadSnapshot(0)
require.NoError(t, err)
tree, err = gv.GetTree("treename")
require.NoError(t, err)
value, err = tree.Get(key)
//fmt.Printf("Reading value before corruption \"%s\" err %s\n", string(value), err)
// now the key has been persisted and verified now we will corrupt the value
file, err := os.OpenFile(filepath.Join(dir, "0", "0", "0", "0.dfs"), os.O_RDWR, 0644)
require.NoError(t, err)
file_contents, err := ioutil.ReadFile(filepath.Join(dir, "0", "0", "0", "0.dfs"))
require.NoError(t, err)
index := bytes.Index(file_contents, value)
if index < 0 {
panic("err could not find value in DB")
}
//fmt.Printf("contain count %d\n", bytes.Count(file_contents,value))
file.WriteAt(value_corrupted, int64(index))
file.Close()
gv, err = store.LoadSnapshot(0)
require.NoError(t, err)
tree1, err := gv.GetTree("treename")
require.NoError(t, err)
_, err = tree1.Get(key)
require.Error(t, err)
dummytree, err := gv.GetTree("dummytree")
require.NoError(t, err)
require.Error(t, Diff(tree1, dummytree, nil, nil, nil))
}