]> git.r.bdr.sh - rbdr/mobius/blob - hotline/server_blackbox_test.go
patch: v0.10.3
[rbdr/mobius] / hotline / server_blackbox_test.go
1 package hotline
2
3 import (
4 "bytes"
5 "encoding/hex"
6 "github.com/stretchr/testify/assert"
7 "go.uber.org/zap"
8 "go.uber.org/zap/zapcore"
9 "os"
10 "testing"
11 )
12
13 func NewTestLogger() *zap.SugaredLogger {
14 encoderCfg := zap.NewProductionEncoderConfig()
15 encoderCfg.TimeKey = "timestamp"
16 encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder
17
18 core := zapcore.NewCore(
19 zapcore.NewConsoleEncoder(encoderCfg),
20 zapcore.Lock(os.Stdout),
21 zap.DebugLevel,
22 )
23
24 cores := []zapcore.Core{core}
25 l := zap.New(zapcore.NewTee(cores...))
26 defer func() { _ = l.Sync() }()
27 return l.Sugar()
28 }
29
30 // assertTransferBytesEqual takes a string with a hexdump in the same format that `hexdump -C` produces and compares with
31 // a hexdump for the bytes in got, after stripping the create/modify timestamps.
32 // I don't love this, but as git does not preserve file create/modify timestamps, we either need to fully mock the
33 // filesystem interactions or work around in this way.
34 // TODO: figure out a better solution
35 func assertTransferBytesEqual(t *testing.T, wantHexDump string, got []byte) bool {
36 if wantHexDump == "" {
37 return true
38 }
39
40 var clean []byte
41 clean = append(clean, got[:92]...) // keep the first 92 bytes
42 clean = append(clean, make([]byte, 16)...) // replace the next 16 bytes for create/modify timestamps
43 clean = append(clean, got[108:]...) // keep the rest
44
45 return assert.Equal(t, wantHexDump, hex.Dump(clean))
46 }
47
48 // tranAssertEqual compares equality of transactions slices after stripping out the random ID
49 func tranAssertEqual(t *testing.T, tran1, tran2 []Transaction) bool {
50 var newT1 []Transaction
51 var newT2 []Transaction
52
53 for _, trans := range tran1 {
54 trans.ID = []byte{0, 0, 0, 0}
55 var fs []Field
56 for _, field := range trans.Fields {
57 if bytes.Equal(field.ID, []byte{0x00, 0x6b}) {
58 continue
59 }
60 fs = append(fs, field)
61 }
62 trans.Fields = fs
63 newT1 = append(newT1, trans)
64 }
65
66 for _, trans := range tran2 {
67 trans.ID = []byte{0, 0, 0, 0}
68 var fs []Field
69 for _, field := range trans.Fields {
70 if bytes.Equal(field.ID, []byte{0x00, 0x6b}) {
71 continue
72 }
73 fs = append(fs, field)
74 }
75 trans.Fields = fs
76 newT2 = append(newT2, trans)
77 }
78
79 return assert.Equal(t, newT1, newT2)
80 }