-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilestream_test.go
89 lines (81 loc) · 1.89 KB
/
filestream_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
package binstream
import (
"bytes"
"testing"
)
func TestFileStream(t *testing.T) {
t.Run("TestNewFileStream", func(t *testing.T) {
testCases := []struct {
input string
err error
}{
{
input: "/bin/ls",
err: nil,
},
}
for _, tt := range testCases {
bs, err := NewFileStream(tt.input)
if bs == nil || err != nil {
t.Fatal("failed to create new BinaryStream instance with error", err)
}
bs.Close()
}
})
t.Run("TestRead", func(t *testing.T) {
testCases := []struct {
input string
expected []byte
err error
}{
{
input: "/bin/ls",
expected: []byte("\177ELF"),
err: nil,
},
}
for _, tt := range testCases {
bs, err := NewFileStream(tt.input)
if bs == nil || err != nil {
t.Fatal("failed to create new BinaryStream instance with error", err)
}
b := make([]byte, 4)
n, err := bs.Read(b)
if n != len(b) || err != nil {
t.Fatal("failed to read from BinaryStream with error", err)
}
if !bytes.Equal(b, tt.expected) {
t.Fatalf("copied bytes from BinaryStream are not consistent with input expected %v got %v", tt.expected, b)
}
bs.Close()
}
})
t.Run("TestReadAt", func(t *testing.T) {
testCases := []struct {
input string
expected []byte
err error
}{
{
input: "/bin/ls",
expected: []byte{0x2, 0x1, 0x1, 0x0},
err: nil,
},
}
for _, tt := range testCases {
bs, err := NewFileStream(tt.input)
if bs == nil || err != nil {
t.Fatal("failed to create new BinaryStream instance with error", err)
}
b := make([]byte, 4)
n, err := bs.ReadAt(b, 4)
if n != len(b) || err != nil {
t.Fatal("failed to read from BinaryStream with error", err)
}
if !bytes.Equal(b, tt.expected) {
t.Fatalf("copied bytes from BinaryStream are not consistent with input expected %v got %v", tt.expected, b)
}
bs.Close()
}
})
}