]>
Commit | Line | Data |
---|---|---|
a2ef262a JH |
1 | package hotline |
2 | ||
3 | import ( | |
4 | "encoding/binary" | |
5 | "github.com/stretchr/testify/assert" | |
fd740bc4 JH |
6 | "io" |
7 | "reflect" | |
a2ef262a JH |
8 | "testing" |
9 | ) | |
10 | ||
11 | func TestFileTransfer_String(t *testing.T) { | |
12 | type fields struct { | |
13 | FileName []byte | |
14 | FilePath []byte | |
15 | refNum [4]byte | |
d9bc63a1 | 16 | Type FileTransferType |
a2ef262a JH |
17 | TransferSize []byte |
18 | FolderItemCount []byte | |
19 | fileResumeData *FileResumeData | |
20 | options []byte | |
21 | bytesSentCounter *WriteCounter | |
22 | ClientConn *ClientConn | |
23 | } | |
24 | tests := []struct { | |
25 | name string | |
26 | fields fields | |
27 | want string | |
28 | }{ | |
29 | { | |
30 | name: "50% complete 198MB file", | |
31 | fields: fields{ | |
32 | FileName: []byte("MasterOfOrionII1.4.0."), | |
33 | TransferSize: func() []byte { b := make([]byte, 4); binary.BigEndian.PutUint32(b, 207618048); return b }(), | |
34 | bytesSentCounter: &WriteCounter{ | |
35 | Total: 103809024, | |
36 | }, | |
37 | }, | |
38 | want: "MasterOfOrionII1.4.0. 50% 198.0M\n", | |
39 | }, | |
40 | { | |
41 | name: "25% complete 512KB file", | |
42 | fields: fields{ | |
43 | FileName: []byte("ExampleFile.txt"), | |
44 | TransferSize: func() []byte { b := make([]byte, 4); binary.BigEndian.PutUint32(b, 524288); return b }(), | |
45 | bytesSentCounter: &WriteCounter{ | |
46 | Total: 131072, | |
47 | }, | |
48 | }, | |
49 | want: "ExampleFile.txt 25% 512K\n", | |
50 | }, | |
51 | { | |
52 | name: "100% complete 2GB file", | |
53 | fields: fields{ | |
54 | FileName: []byte("LargeFile.dat"), | |
55 | TransferSize: func() []byte { b := make([]byte, 4); binary.BigEndian.PutUint32(b, 2147483648); return b }(), | |
56 | bytesSentCounter: &WriteCounter{ | |
57 | Total: 2147483648, | |
58 | }, | |
59 | }, | |
60 | want: "LargeFile.dat 100% 2048.0M\n", | |
61 | }, | |
62 | { | |
63 | name: "0% complete 1MB file", | |
64 | fields: fields{ | |
65 | FileName: []byte("NewDocument.docx"), | |
66 | TransferSize: func() []byte { b := make([]byte, 4); binary.BigEndian.PutUint32(b, 1048576); return b }(), | |
67 | bytesSentCounter: &WriteCounter{ | |
68 | Total: 0, | |
69 | }, | |
70 | }, | |
71 | want: "NewDocument.docx 0% 1.0M\n", | |
72 | }, | |
73 | } | |
74 | for _, tt := range tests { | |
75 | t.Run(tt.name, func(t *testing.T) { | |
76 | ft := &FileTransfer{ | |
77 | FileName: tt.fields.FileName, | |
78 | FilePath: tt.fields.FilePath, | |
fd740bc4 | 79 | RefNum: tt.fields.refNum, |
a2ef262a JH |
80 | Type: tt.fields.Type, |
81 | TransferSize: tt.fields.TransferSize, | |
82 | FolderItemCount: tt.fields.FolderItemCount, | |
fd740bc4 JH |
83 | FileResumeData: tt.fields.fileResumeData, |
84 | Options: tt.fields.options, | |
a2ef262a JH |
85 | bytesSentCounter: tt.fields.bytesSentCounter, |
86 | ClientConn: tt.fields.ClientConn, | |
87 | } | |
88 | assert.Equalf(t, tt.want, ft.String(), "String()") | |
89 | }) | |
90 | } | |
91 | } | |
fd740bc4 JH |
92 | |
93 | func TestNewFileHeader(t *testing.T) { | |
94 | type args struct { | |
95 | fileName string | |
96 | isDir bool | |
97 | } | |
98 | tests := []struct { | |
99 | name string | |
100 | args args | |
101 | want FileHeader | |
102 | }{ | |
103 | { | |
104 | name: "when path is file", | |
105 | args: args{ | |
106 | fileName: "foo", | |
107 | isDir: false, | |
108 | }, | |
109 | want: FileHeader{ | |
110 | Size: [2]byte{0x00, 0x0a}, | |
111 | Type: [2]byte{0x00, 0x00}, | |
112 | FilePath: EncodeFilePath("foo"), | |
113 | }, | |
114 | }, | |
115 | { | |
116 | name: "when path is dir", | |
117 | args: args{ | |
118 | fileName: "foo", | |
119 | isDir: true, | |
120 | }, | |
121 | want: FileHeader{ | |
122 | Size: [2]byte{0x00, 0x0a}, | |
123 | Type: [2]byte{0x00, 0x01}, | |
124 | FilePath: EncodeFilePath("foo"), | |
125 | }, | |
126 | }, | |
127 | } | |
128 | for _, tt := range tests { | |
129 | t.Run(tt.name, func(t *testing.T) { | |
130 | if got := NewFileHeader(tt.args.fileName, tt.args.isDir); !reflect.DeepEqual(got, tt.want) { | |
131 | t.Errorf("NewFileHeader() = %v, want %v", got, tt.want) | |
132 | } | |
133 | }) | |
134 | } | |
135 | } | |
136 | ||
137 | func TestFileHeader_Payload(t *testing.T) { | |
138 | type fields struct { | |
139 | Size [2]byte | |
140 | Type [2]byte | |
141 | FilePath []byte | |
142 | } | |
143 | tests := []struct { | |
144 | name string | |
145 | fields fields | |
146 | want []byte | |
147 | }{ | |
148 | { | |
149 | name: "has expected payload bytes", | |
150 | fields: fields{ | |
151 | Size: [2]byte{0x00, 0x0a}, | |
152 | Type: [2]byte{0x00, 0x00}, | |
153 | FilePath: EncodeFilePath("foo"), | |
154 | }, | |
155 | want: []byte{ | |
156 | 0x00, 0x0a, // total size | |
157 | 0x00, 0x00, // type | |
158 | 0x00, 0x01, // path item count | |
159 | 0x00, 0x00, // path separator | |
160 | 0x03, // pathName len | |
161 | 0x66, 0x6f, 0x6f, // "foo" | |
162 | }, | |
163 | }, | |
164 | } | |
165 | for _, tt := range tests { | |
166 | t.Run(tt.name, func(t *testing.T) { | |
167 | fh := &FileHeader{ | |
168 | Size: tt.fields.Size, | |
169 | Type: tt.fields.Type, | |
170 | FilePath: tt.fields.FilePath, | |
171 | } | |
172 | got, _ := io.ReadAll(fh) | |
173 | if !reflect.DeepEqual(got, tt.want) { | |
174 | t.Errorf("Read() = %v, want %v", got, tt.want) | |
175 | } | |
176 | }) | |
177 | } | |
178 | } |