-
Notifications
You must be signed in to change notification settings - Fork 0
/
goxxd_test.go
213 lines (180 loc) · 4.89 KB
/
goxxd_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package goxxd
import (
"bytes"
"encoding/hex"
"fmt"
"strings"
"testing"
)
func TestDump(t *testing.T) {
reader := strings.NewReader("File 1 contents")
columns := 16
group := 1
seek := 0
length := -1
littleEndian := false
expected := "00000000: 46 69 6c 65 20 31 20 63 6f 6e 74 65 6e 74 73 File 1 contents\n"
result, err := Dump(reader, columns, group, seek, length, littleEndian)
if err != nil {
t.Errorf("Dump error: %v", err)
return
}
if result != expected {
t.Errorf("expected dump [%s], got [%s]", expected, result)
}
}
func TestDumpColumn(t *testing.T) {
reader := strings.NewReader(`File 1 contents: lorem ipsum dolor sit amet
`)
columns := 6
group := 1
seek := 0
length := -1
littleEndian := false
expected := `00000000: 46 69 6c 65 20 31 File 1
00000006: 20 63 6f 6e 74 65 conte
0000000c: 6e 74 73 3a 20 6c nts: l
00000012: 6f 72 65 6d 20 69 orem i
00000018: 70 73 75 6d 20 64 psum d
0000001e: 6f 6c 6f 72 20 73 olor s
00000024: 69 74 20 61 6d 65 it ame
0000002a: 74 0a t.
`
result, err := Dump(reader, columns, group, seek, length, littleEndian)
if err != nil {
t.Errorf("Dump error: %v", err)
return
}
if result != expected {
t.Errorf("expected dump [%s], got [%s]", expected, result)
}
}
func TestDumpLength(t *testing.T) {
reader := strings.NewReader(`File 1 contents: lorem ipsum dolor sit amet
`)
columns := 16
group := 1
seek := 0
length := 18
littleEndian := false
expected := `00000000: 46 69 6c 65 20 31 20 63 6f 6e 74 65 6e 74 73 3a File 1 contents:
00000010: 20 6c l
`
result, err := Dump(reader, columns, group, seek, length, littleEndian)
if err != nil {
t.Errorf("Dump error: %v", err)
return
}
if result != expected {
t.Errorf("expected dump [%s], got [%s]", expected, result)
}
}
func TestDumpGroup(t *testing.T) {
reader := strings.NewReader(`File 1 contents: lorem ipsum dolor sit amet
`)
columns := 16
group := 4
seek := 0
length := -1
littleEndian := false
expected := `00000000: 46696c65 20312063 6f6e7465 6e74733a File 1 contents:
00000010: 206c6f72 656d2069 7073756d 20646f6c lorem ipsum dol
00000020: 6f722073 69742061 6d65740a or sit amet.
`
result, err := Dump(reader, columns, group, seek, length, littleEndian)
if err != nil {
t.Errorf("Dump error: %v", err)
return
}
if result != expected {
t.Errorf("expected dump [%s], got [%s]", expected, result)
}
}
func TestDumpLittleEndian(t *testing.T) {
reader := strings.NewReader(`File 1 contents: lorem ipsum dolor sit amet
`)
columns := 16
group := 2
seek := 0
length := -1
littleEndian := true
expected := `00000000: 6946 656c 3120 6320 6e6f 6574 746e 3a73 File 1 contents:
00000010: 6c20 726f 6d65 6920 7370 6d75 6420 6c6f lorem ipsum dol
00000020: 726f 7320 7469 6120 656d 0a74 or sit amet.
`
result, err := Dump(reader, columns, group, seek, length, littleEndian)
if err != nil {
t.Errorf("Dump error: %v", err)
return
}
if result != expected {
t.Errorf("expected dump [%s], got [%s]", expected, result)
}
}
func TestDumpSeek(t *testing.T) {
reader := strings.NewReader(`File 1 contents: lorem ipsum dolor sit amet
`)
columns := 16
group := 1
seek := 12
length := -1
littleEndian := true
expected := `0000000c: 6e 74 73 3a 20 6c 6f 72 65 6d 20 69 70 73 75 6d nts: lorem ipsum
0000001c: 20 64 6f 6c 6f 72 20 73 69 74 20 61 6d 65 74 0a dolor sit amet.
`
result, err := Dump(reader, columns, group, seek, length, littleEndian)
if err != nil {
t.Errorf("Dump error: %v", err)
return
}
if result != expected {
t.Errorf("expected dump [%s], got [%s]", expected, result)
}
}
func TestRevertDump(t *testing.T) {
reader := strings.NewReader(`00000000: 46 69 6c 65 20 31 20 63 6f 6e 74 65 6e 74 73 3a File 1 contents:
00000010: 20 6c 6f 72 65 6d 20 69 70 73 75 6d 20 64 6f 6c lorem ipsum dol
00000020: 6f 72 20 73 69 74 20 61 6d 65 74 0a or sit amet.
`)
expected := `File 1 contents: lorem ipsum dolor sit amet
`
result, err := RevertDump(reader)
if err != nil {
t.Errorf("Dump error: %v", err)
return
}
if result != expected {
t.Errorf("expected [%s], got [%s]", expected, result)
}
}
func BenchmarkDumpStandard(b *testing.B) {
for _, size := range []int{256, 1024, 4096, 16384} {
src := bytes.Repeat([]byte{2, 3, 5, 7, 9, 11, 13, 17}, size/8)
b.Run(fmt.Sprintf("%v", size), func(b *testing.B) {
b.SetBytes(int64(size))
for i := 0; i < b.N; i++ {
hex.Dump(src)
}
})
}
}
func BenchmarkDump(b *testing.B) {
config := &DumperConfig{
Columns: 16,
GroupSize: 1,
LittleEndian: false,
}
for _, size := range []int{256, 1024, 4096, 16384} {
bts := bytes.Repeat([]byte{2, 3, 5, 7, 9, 11, 13, 17}, size/8)
b.Run(fmt.Sprintf("%v", size), func(b *testing.B) {
b.SetBytes(int64(size))
for i := 0; i < b.N; i++ {
_, err := dump(bts, config)
if err != nil {
b.Error(err)
return
}
}
})
}
}