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

Function TestLogWriterWithEncryption

x/log_writer_test.go:55–117  ·  view source on GitHub ↗

if this test failed and you changed anything, please check the dgraph audit decrypt command. The dgraph audit decrypt command uses the same decryption method

(t *testing.T)

Source from the content-addressed store, hash-verified

53// if this test failed and you changed anything, please check the dgraph audit decrypt command.
54// The dgraph audit decrypt command uses the same decryption method
55func TestLogWriterWithEncryption(t *testing.T) {
56 path, _ := filepath.Abs("./log_test/audit.log.enc")
57 defer os.RemoveAll(filepath.Dir(path))
58 lw := &LogWriter{
59 FilePath: path,
60 MaxSize: 1,
61 MaxAge: 1,
62 Compress: false,
63 EncryptionKey: []byte("1234567890123456"), // 16 bytes
64 }
65
66 lw, _ = lw.Init()
67 msg := []byte("abcd")
68 msg = bytes.Repeat(msg, 256)
69 msg[1023] = '\n'
70 for range 10000 {
71 n, err := lw.Write(msg)
72 require.NoError(t, err)
73 require.Equal(t, n, len(msg)+20, "write length is not equal")
74 }
75
76 time.Sleep(time.Second * 10)
77 require.NoError(t, lw.Close())
78 file, err := os.Open(path)
79 require.NoError(t, err)
80 defer file.Close()
81 outPath, _ := filepath.Abs("./log_test/audit_out.log")
82 outfile, err := os.OpenFile(outPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
83 require.NoError(t, err)
84 defer outfile.Close()
85
86 block, err := aes.NewCipher(lw.EncryptionKey)
87 require.NoError(t, err)
88 stat, err := os.Stat(path)
89 require.NoError(t, err)
90 iv := make([]byte, aes.BlockSize)
91 _, err = file.ReadAt(iv, 0)
92 require.NoError(t, err)
93
94 var iterator int64 = 16
95 for {
96 length := make([]byte, 4)
97 _, err = file.ReadAt(length, iterator)
98 require.Nil(t, err)
99 iterator = iterator + 4
100 content := make([]byte, binary.BigEndian.Uint32(length))
101 _, err = file.ReadAt(content, iterator)
102 require.NoError(t, err)
103 iterator = iterator + int64(binary.BigEndian.Uint32(length))
104 stream := cipher.NewCTR(block, iv)
105 stream.XORKeyStream(content, content)
106 //require.True(t, bytes.Equal(content, msg))
107 _, err = outfile.Write(content)
108 require.NoError(t, err)
109 if iterator >= stat.Size() {
110 break
111 }
112 iv := make([]byte, 16)

Callers

nothing calls this directly

Calls 9

InitMethod · 0.95
WriteMethod · 0.95
CloseMethod · 0.95
RemoveAllMethod · 0.80
OpenMethod · 0.65
CloseMethod · 0.65
OpenFileMethod · 0.65
WriteMethod · 0.65
SizeMethod · 0.45

Tested by

no test coverage detected