Files
graph/x/log_writer_test.go
zeekay f5aaeb712e Hanzo Graph — the engine, on zapdb, ZAP-native
Derived from Dgraph (github.com/dgraph-io/dgraph) at commit 64804bd2, Apache-2.0.
See NOTICE.

WHAT THIS IS. The graph engine as a Go library: posting lists, the DQL parser,
the type and tokenizer system, the index and schema layers, and the wire types.
39,170 lines of the upstream's 166,337.

THE STORE is github.com/luxfi/zapdb, a Badger fork that kept the package name,
the subpackages (options, pb, y) and the API — Open, OpenManaged, DB, Options —
so the 74 import sites are aliased rather than rewritten.

THE CODEC is ZAP. zapdb serializes with ZAP, not protobuf: its pb package builds
the ZAP types by default and the protobuf ones only under `-tags grpc`, a path
that does not compile (table/builder.go calls pb.Marshal, which that tag
removes). One call site in posting needed converting; every other type here is
Hanzo's own protobuf and is untouched.

NO TRANSPORT. An embedded engine dials nothing, so nothing here imports
google.golang.org/grpc. Removed: 20 transport functions in x, the generated
service stubs for Raft, Zero and Worker (1,657 lines), and the JWT helper —
identity belongs to IAM. The core called none of them.

NOT KEPT: worker, conn, raftwal, the cluster commands, the GraphQL layer and the
test harnesses. `query`, the DQL execution engine, went with them because it
depends on worker; parsing DQL is available here, executing it is not.

The history of the upstream project lives at github.com/dgraph-io/dgraph. This
repository begins at the derivation because three quarters of that history
describes code this fork does not carry.
2026-08-20 21:26:27 -07:00

159 lines
3.7 KiB
Go

/*
* SPDX-FileCopyrightText: © 2017-2026 Istari Digital, Inc.
* SPDX-License-Identifier: Apache-2.0
*/
package x
import (
"bufio"
"bytes"
"compress/gzip"
"crypto/aes"
"crypto/cipher"
"encoding/binary"
"io"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestLogWriter(t *testing.T) {
path, _ := filepath.Abs("./log_test/audit.log")
defer os.RemoveAll(filepath.Dir(path))
lw := &LogWriter{
FilePath: path,
MaxSize: 1,
MaxAge: 1,
Compress: false,
}
lw, _ = lw.Init()
writeToLogWriterAndVerify(t, lw, path)
}
func TestLogWriterWithCompression(t *testing.T) {
path, _ := filepath.Abs("./log_test/audit.log")
defer os.RemoveAll(filepath.Dir(path))
lw := &LogWriter{
FilePath: path,
MaxSize: 1,
MaxAge: 1,
Compress: true,
}
lw, _ = lw.Init()
writeToLogWriterAndVerify(t, lw, path)
}
// 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
func TestLogWriterWithEncryption(t *testing.T) {
path, _ := filepath.Abs("./log_test/audit.log.enc")
defer os.RemoveAll(filepath.Dir(path))
lw := &LogWriter{
FilePath: path,
MaxSize: 1,
MaxAge: 1,
Compress: false,
EncryptionKey: []byte("1234567890123456"), // 16 bytes
}
lw, _ = lw.Init()
msg := []byte("abcd")
msg = bytes.Repeat(msg, 256)
msg[1023] = '\n'
for range 10000 {
n, err := lw.Write(msg)
require.NoError(t, err)
require.Equal(t, n, len(msg)+20, "write length is not equal")
}
time.Sleep(time.Second * 10)
require.NoError(t, lw.Close())
file, err := os.Open(path)
require.NoError(t, err)
defer file.Close()
outPath, _ := filepath.Abs("./log_test/audit_out.log")
outfile, err := os.OpenFile(outPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
require.NoError(t, err)
defer outfile.Close()
block, err := aes.NewCipher(lw.EncryptionKey)
require.NoError(t, err)
stat, err := os.Stat(path)
require.NoError(t, err)
iv := make([]byte, aes.BlockSize)
_, err = file.ReadAt(iv, 0)
require.NoError(t, err)
var iterator int64 = 16
for {
length := make([]byte, 4)
_, err = file.ReadAt(length, iterator)
require.Nil(t, err)
iterator = iterator + 4
content := make([]byte, binary.BigEndian.Uint32(length))
_, err = file.ReadAt(content, iterator)
require.NoError(t, err)
iterator = iterator + int64(binary.BigEndian.Uint32(length))
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(content, content)
//require.True(t, bytes.Equal(content, msg))
_, err = outfile.Write(content)
require.NoError(t, err)
if iterator >= stat.Size() {
break
}
iv := make([]byte, 16)
_, err = file.ReadAt(iv, iterator)
require.NoError(t, err)
iterator = iterator + 16
}
}
func writeToLogWriterAndVerify(t *testing.T, lw *LogWriter, path string) {
msg := []byte("abcd")
msg = bytes.Repeat(msg, 256)
msg[1023] = '\n'
for range 10 {
go func() {
for range 1000 {
n, err := lw.Write(msg)
require.NoError(t, err)
require.Equal(t, n, len(msg), "write length is not equal")
}
}()
}
time.Sleep(time.Second * 10)
require.NoError(t, lw.Close())
files, err := os.ReadDir("./log_test")
require.NoError(t, err)
lineCount := 0
for _, f := range files {
file, _ := os.Open(filepath.Join(filepath.Dir(path), f.Name()))
var fileScanner *bufio.Scanner
if strings.HasSuffix(file.Name(), ".gz") {
gz, err := gzip.NewReader(file)
require.NoError(t, err)
all, err := io.ReadAll(gz)
require.NoError(t, err)
fileScanner = bufio.NewScanner(bytes.NewReader(all))
gz.Close()
} else {
fileScanner = bufio.NewScanner(file)
}
for fileScanner.Scan() {
lineCount = lineCount + 1
}
}
require.Equal(t, lineCount, 10000)
}