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