MCPcopy
hub / github.com/dgraph-io/dgraph / TestEntryReadWrite

Function TestEntryReadWrite

raftwal/encryption_test.go:19–53  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

17)
18
19func TestEntryReadWrite(t *testing.T) {
20 key := []byte("badger16byteskey")
21 dir := t.TempDir()
22 ds, err := InitEncrypted(dir, key)
23 require.NoError(t, err)
24
25 // generate some random data
26 data := make([]byte, 1+rand.Intn(1000))
27 rand.Read(data)
28
29 require.NoError(t, ds.wal.AddEntries([]raftpb.Entry{{Index: 1, Term: 1, Data: data}}))
30 entries := ds.wal.allEntries(0, 100, 10000)
31 require.Equal(t, 1, len(entries))
32 require.Equal(t, uint64(1), entries[0].Index)
33 require.Equal(t, uint64(1), entries[0].Term)
34 require.Equal(t, data, entries[0].Data)
35
36 // Open the wal file again.
37 ds2, err := InitEncrypted(dir, key)
38 require.NoError(t, err)
39 entries = ds2.wal.allEntries(0, 100, 10000)
40 require.Equal(t, 1, len(entries))
41 require.Equal(t, uint64(1), entries[0].Index)
42 require.Equal(t, uint64(1), entries[0].Term)
43 require.Equal(t, data, entries[0].Data)
44
45 // Opening it with a wrong key fails.
46 wrongKey := []byte("other16byteskeys")
47 _, err = InitEncrypted(dir, wrongKey)
48 require.EqualError(t, err, "Encryption key mismatch")
49
50 // Opening it without encryption key fails.
51 _, err = InitEncrypted(dir, nil)
52 require.EqualError(t, err, "Logfile is encrypted but encryption key is nil")
53}
54
55// TestLogRotate writes enough log file entries to cause 1 file rotation.
56func TestLogRotate(t *testing.T) {

Callers

nothing calls this directly

Calls 4

InitEncryptedFunction · 0.85
AddEntriesMethod · 0.80
allEntriesMethod · 0.80
ReadMethod · 0.65

Tested by

no test coverage detected