]> git.r.bdr.sh - rbdr/mobius/blame - hotline/server_blackbox_test.go
Prevent account from creating new account with greater permission
[rbdr/mobius] / hotline / server_blackbox_test.go
CommitLineData
6988a057
JH
1package hotline
2
625b0580 3import (
7cd900d6
JH
4 "bytes"
5 "encoding/hex"
00d1ef67 6 "github.com/stretchr/testify/assert"
625b0580
JH
7 "go.uber.org/zap"
8 "go.uber.org/zap/zapcore"
625b0580 9 "os"
625b0580
JH
10 "testing"
11)
12
625b0580
JH
13func 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
7cd900d6
JH
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
35func 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
aebc4d36 48// tranAssertEqual compares equality of transactions slices after stripping out the random ID
00d1ef67
JH
49func tranAssertEqual(t *testing.T, tran1, tran2 []Transaction) bool {
50 var newT1 []Transaction
51 var newT2 []Transaction
7cd900d6 52
92a7e455
JH
53 for _, trans := range tran1 {
54 trans.ID = []byte{0, 0, 0, 0}
7cd900d6
JH
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
00d1ef67
JH
63 newT1 = append(newT1, trans)
64 }
625b0580 65
92a7e455
JH
66 for _, trans := range tran2 {
67 trans.ID = []byte{0, 0, 0, 0}
7cd900d6
JH
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
00d1ef67 76 newT2 = append(newT2, trans)
00d1ef67 77 }
625b0580 78
00d1ef67 79 return assert.Equal(t, newT1, newT2)
92a7e455 80}