forked from zach-klippenstein/goadb
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sync_file_reader_test.go
114 lines (96 loc) · 3.12 KB
/
sync_file_reader_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
package adb
import (
"io"
"io/ioutil"
"strings"
"testing"
"github.com/jt6562/go-adb/wire"
"github.com/stretchr/testify/assert"
)
func TestReadNextChunk(t *testing.T) {
s := wire.NewSyncScanner(strings.NewReader(
"DATA\006\000\000\000hello DATA\005\000\000\000worldDONE"))
// Read 1st chunk
reader, err := readNextChunk(s)
assert.NoError(t, err)
assert.Equal(t, int64(6), reader.(*io.LimitedReader).N)
buf := make([]byte, 10)
n, err := reader.Read(buf)
assert.NoError(t, err)
assert.Equal(t, 6, n)
assert.Equal(t, "hello ", string(buf[:6]))
// Read 2nd chunk
reader, err = readNextChunk(s)
assert.NoError(t, err)
assert.Equal(t, int64(5), reader.(*io.LimitedReader).N)
buf = make([]byte, 10)
n, err = reader.Read(buf)
assert.NoError(t, err)
assert.Equal(t, 5, n)
assert.Equal(t, "world", string(buf[:5]))
// Read DONE
_, err = readNextChunk(s)
assert.Equal(t, io.EOF, err)
}
func TestReadNextChunkInvalidChunkId(t *testing.T) {
s := wire.NewSyncScanner(strings.NewReader(
"ATAD\006\000\000\000hello "))
// Read 1st chunk
_, err := readNextChunk(s)
assert.EqualError(t, err, "AssertionError: expected chunk id 'DATA' or 'DONE', but got 'ATAD'")
}
func TestReadMultipleCalls(t *testing.T) {
s := wire.NewSyncScanner(strings.NewReader(
"DATA\006\000\000\000hello DATA\005\000\000\000worldDONE"))
reader, err := newSyncFileReader(s)
assert.NoError(t, err)
firstByte := make([]byte, 1)
_, err = io.ReadFull(reader, firstByte)
assert.NoError(t, err)
assert.Equal(t, "h", string(firstByte))
restFirstChunkBytes := make([]byte, 5)
_, err = io.ReadFull(reader, restFirstChunkBytes)
assert.NoError(t, err)
assert.Equal(t, "ello ", string(restFirstChunkBytes))
secondChunkBytes := make([]byte, 5)
_, err = io.ReadFull(reader, secondChunkBytes)
assert.NoError(t, err)
assert.Equal(t, "world", string(secondChunkBytes))
_, err = io.ReadFull(reader, make([]byte, 5))
assert.Equal(t, io.EOF, err)
}
func TestReadAll(t *testing.T) {
s := wire.NewSyncScanner(strings.NewReader(
"DATA\006\000\000\000hello DATA\005\000\000\000worldDONE"))
reader, err := newSyncFileReader(s)
assert.NoError(t, err)
buf := make([]byte, 20)
_, err = io.ReadFull(reader, buf)
assert.Equal(t, io.ErrUnexpectedEOF, err)
assert.Equal(t, "hello world\000", string(buf[:12]))
}
func TestReadError(t *testing.T) {
s := wire.NewSyncScanner(strings.NewReader(
"FAIL\004\000\000\000fail"))
_, err := newSyncFileReader(s)
assert.EqualError(t, err, "AdbError: server error for read-chunk request: fail ({Request:read-chunk ServerMsg:fail})")
}
func TestReadEmpty(t *testing.T) {
s := wire.NewSyncScanner(strings.NewReader(
"DONE"))
r, err := newSyncFileReader(s)
assert.NoError(t, err)
// Multiple read calls that return EOF is a valid case.
for i := 0; i < 5; i++ {
data, err := ioutil.ReadAll(r)
assert.NoError(t, err)
assert.Empty(t, data)
}
}
func TestReadErrorNotFound(t *testing.T) {
s := wire.NewSyncScanner(strings.NewReader(
"FAIL\031\000\000\000No such file or directory"))
_, err := newSyncFileReader(s)
assert.True(t, HasErrCode(err, FileNoExistError))
assert.EqualError(t, err, "FileNoExistError: no such file or directory")
}