forked from zach-klippenstein/goadb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync_file_writer_test.go
101 lines (76 loc) · 2.81 KB
/
sync_file_writer_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
package adb
import (
"bytes"
"testing"
"time"
"encoding/binary"
"strings"
"github.com/yosemite-open/go-adb/wire"
"github.com/stretchr/testify/assert"
)
func TestFileWriterWriteSingleChunk(t *testing.T) {
var buf bytes.Buffer
writer := newSyncFileWriter(wire.NewSyncSender(&buf), MtimeOfClose)
n, err := writer.Write([]byte("hello"))
assert.NoError(t, err)
assert.Equal(t, 5, n)
assert.Equal(t, "DATA\005\000\000\000hello", buf.String())
}
func TestFileWriterWriteMultiChunk(t *testing.T) {
var buf bytes.Buffer
writer := newSyncFileWriter(wire.NewSyncSender(&buf), MtimeOfClose)
n, err := writer.Write([]byte("hello"))
assert.NoError(t, err)
assert.Equal(t, 5, n)
n, err = writer.Write([]byte(" world"))
assert.NoError(t, err)
assert.Equal(t, 6, n)
assert.Equal(t, "DATA\005\000\000\000helloDATA\006\000\000\000 world", buf.String())
}
func TestFileWriterWriteLargeChunk(t *testing.T) {
var buf bytes.Buffer
writer := newSyncFileWriter(wire.NewSyncSender(&buf), MtimeOfClose)
// Send just enough data to get 2 chunks.
data := make([]byte, wire.SyncMaxChunkSize+1)
n, err := writer.Write(data)
assert.NoError(t, err)
assert.Equal(t, wire.SyncMaxChunkSize+1, n)
assert.Equal(t, 8+8+wire.SyncMaxChunkSize+1, buf.Len())
// First header.
chunk := buf.Bytes()[:8+wire.SyncMaxChunkSize]
expectedHeader := []byte("DATA----")
binary.LittleEndian.PutUint32(expectedHeader[4:], wire.SyncMaxChunkSize)
assert.Equal(t, expectedHeader, chunk[:8])
assert.Equal(t, data[:wire.SyncMaxChunkSize], chunk[8:])
// Second header.
chunk = buf.Bytes()[wire.SyncMaxChunkSize+8 : wire.SyncMaxChunkSize+8+1]
expectedHeader = []byte("DATA\000\000\000\000")
binary.LittleEndian.PutUint32(expectedHeader[4:], 1)
assert.Equal(t, expectedHeader, chunk[:8])
}
func TestFileWriterCloseEmpty(t *testing.T) {
var buf bytes.Buffer
mtime := time.Unix(1, 0)
writer := newSyncFileWriter(wire.NewSyncSender(&buf), mtime)
assert.NoError(t, writer.Close())
assert.Equal(t, "DONE\x01\x00\x00\x00", buf.String())
}
func TestFileWriterWriteClose(t *testing.T) {
var buf bytes.Buffer
mtime := time.Unix(1, 0)
writer := newSyncFileWriter(wire.NewSyncSender(&buf), mtime)
writer.Write([]byte("hello"))
assert.NoError(t, writer.Close())
assert.Equal(t, "DATA\005\000\000\000helloDONE\x01\x00\x00\x00", buf.String())
}
func TestFileWriterCloseAutoMtime(t *testing.T) {
var buf bytes.Buffer
writer := newSyncFileWriter(wire.NewSyncSender(&buf), MtimeOfClose)
assert.NoError(t, writer.Close())
assert.Len(t, buf.String(), 8)
assert.True(t, strings.HasPrefix(buf.String(), "DONE"))
mtimeBytes := buf.Bytes()[4:]
mtimeActual := time.Unix(int64(binary.LittleEndian.Uint32(mtimeBytes)), 0)
// Delta has to be a whole second since adb only supports second granularity for mtimes.
assert.WithinDuration(t, time.Now(), mtimeActual, 1*time.Second)
}