github.com/iotexproject/iotex-core@v1.14.1-rc1/db/trie/mptrie/log_test.go (about) 1 package mptrie 2 3 import ( 4 "testing" 5 6 "github.com/iotexproject/iotex-core/testutil" 7 "github.com/pkg/errors" 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestNodeEvent(t *testing.T) { 12 require := require.New(t) 13 tests := []struct { 14 event nodeEvent 15 }{ 16 { 17 event: nodeEvent{ 18 NodeType: _nodeTypeBranch, 19 ActionType: _actionTypeNew, 20 KeyLen: 2, 21 Key: []byte{3, 4}, 22 PathLen: 5, 23 Path: []byte{6, 7, 8, 9, 10}, 24 ChildrenLen: 11, 25 Children: []byte{12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22}, 26 HashLen: 23, 27 HashVal: []byte{24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46}, 28 }, 29 }, 30 { 31 event: nodeEvent{ 32 NodeType: _nodeTypeLeaf, 33 ActionType: _actionTypeSearch, 34 KeyLen: 6, 35 Key: []byte("123456"), 36 PathLen: 6, 37 Path: []byte("abcdef"), 38 ChildrenLen: 6, 39 Children: []byte("ABCDEF"), 40 HashLen: 6, 41 HashVal: []byte("abcdef"), 42 }, 43 }, 44 } 45 46 for _, test := range tests { 47 b := test.event.Bytes() 48 event, err := parseNodeEvent(b) 49 require.NoError(err) 50 require.Equal(test.event.NodeType, event.NodeType) 51 require.Equal(test.event.ActionType, event.ActionType) 52 require.Equal(test.event.KeyLen, event.KeyLen) 53 require.Equal(test.event.Key, event.Key) 54 require.Equal(test.event.PathLen, event.PathLen) 55 require.Equal(test.event.Path, event.Path) 56 require.Equal(test.event.ChildrenLen, event.ChildrenLen) 57 require.Equal(test.event.Children, event.Children) 58 require.Equal(test.event.HashLen, event.HashLen) 59 require.Equal(test.event.HashVal, event.HashVal) 60 } 61 } 62 63 // parseNodeEvent parse the node event 64 func parseNodeEvent(b []byte) (nodeEvent, error) { 65 if len(b) < 1 { 66 return nodeEvent{}, errors.New("invalid node event") 67 } 68 event := nodeEvent{ 69 NodeType: nodeType(b[0]), 70 } 71 if len(b) < 2 { 72 return event, nil 73 } 74 event.ActionType = actionType(b[1]) 75 if len(b) < 3 { 76 return event, nil 77 } 78 event.KeyLen = uint8(b[2]) 79 if len(b) < 3+int(event.KeyLen) { 80 return event, nil 81 } 82 event.Key = b[3 : 3+event.KeyLen] 83 if len(b) < 3+int(event.KeyLen)+1 { 84 return event, nil 85 } 86 event.PathLen = b[3+event.KeyLen] 87 if len(b) < 3+int(event.KeyLen)+1+int(event.PathLen) { 88 return event, nil 89 } 90 event.Path = b[3+event.KeyLen+1 : 3+event.KeyLen+1+event.PathLen] 91 if len(b) < 3+int(event.KeyLen)+1+int(event.PathLen)+1 { 92 return event, nil 93 } 94 event.ChildrenLen = b[3+event.KeyLen+1+event.PathLen] 95 if len(b) < 3+int(event.KeyLen)+1+int(event.PathLen)+1+int(event.ChildrenLen) { 96 return event, nil 97 } 98 event.Children = b[3+event.KeyLen+1+event.PathLen+1 : 3+event.KeyLen+1+event.PathLen+1+event.ChildrenLen] 99 if len(b) < 3+int(event.KeyLen)+1+int(event.PathLen)+1+int(event.ChildrenLen)+1 { 100 return event, nil 101 } 102 event.HashLen = b[3+event.KeyLen+1+event.PathLen+1+event.ChildrenLen] 103 if len(b) < 3+int(event.KeyLen)+1+int(event.PathLen)+1+int(event.ChildrenLen)+1+int(event.HashLen) { 104 return event, nil 105 } 106 event.HashVal = b[3+event.KeyLen+1+event.PathLen+1+event.ChildrenLen+1 : 3+event.KeyLen+1+event.PathLen+1+event.ChildrenLen+1+event.HashLen] 107 return event, nil 108 } 109 110 func TestLogDB(t *testing.T) { 111 require := require.New(t) 112 testPath, err := testutil.PathOfTempFile("test-log-db") 113 require.NoError(err) 114 defer func() { 115 testutil.CleanupPath(testPath) 116 }() 117 require.NoError(OpenLogDB(testPath)) 118 require.NoError(CloseLogDB()) 119 enabledLogMptrie = false 120 }