aboutsummaryrefslogtreecommitdiff
path: root/hotline/file_transfer_test.go
blob: 0b549f86a60c960b19d831d243d2dd40b0e6dd90 (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package hotline

import (
	"encoding/binary"
	"github.com/stretchr/testify/assert"
	"io"
	"testing"
)

func TestFileTransfer_String(t *testing.T) {
	type fields struct {
		FileName         []byte
		FilePath         []byte
		refNum           [4]byte
		Type             FileTransferType
		TransferSize     []byte
		FolderItemCount  []byte
		fileResumeData   *FileResumeData
		options          []byte
		bytesSentCounter *WriteCounter
		ClientConn       *ClientConn
	}
	tests := []struct {
		name   string
		fields fields
		want   string
	}{
		{
			name: "50% complete 198MB file",
			fields: fields{
				FileName:     []byte("MasterOfOrionII1.4.0."),
				TransferSize: func() []byte { b := make([]byte, 4); binary.BigEndian.PutUint32(b, 207618048); return b }(),
				bytesSentCounter: &WriteCounter{
					Total: 103809024,
				},
			},
			want: "MasterOfOrionII1.4.0. 50%  198.0M\n",
		},
		{
			name: "25% complete 512KB file",
			fields: fields{
				FileName:     []byte("ExampleFile.txt"),
				TransferSize: func() []byte { b := make([]byte, 4); binary.BigEndian.PutUint32(b, 524288); return b }(),
				bytesSentCounter: &WriteCounter{
					Total: 131072,
				},
			},
			want: "ExampleFile.txt       25%    512K\n",
		},
		{
			name: "100% complete 2GB file",
			fields: fields{
				FileName:     []byte("LargeFile.dat"),
				TransferSize: func() []byte { b := make([]byte, 4); binary.BigEndian.PutUint32(b, 2147483648); return b }(),
				bytesSentCounter: &WriteCounter{
					Total: 2147483648,
				},
			},
			want: "LargeFile.dat         100%  2048.0M\n",
		},
		{
			name: "0% complete 1MB file",
			fields: fields{
				FileName:     []byte("NewDocument.docx"),
				TransferSize: func() []byte { b := make([]byte, 4); binary.BigEndian.PutUint32(b, 1048576); return b }(),
				bytesSentCounter: &WriteCounter{
					Total: 0,
				},
			},
			want: "NewDocument.docx      0%    1.0M\n",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			ft := &FileTransfer{
				FileName:         tt.fields.FileName,
				FilePath:         tt.fields.FilePath,
				RefNum:           tt.fields.refNum,
				Type:             tt.fields.Type,
				TransferSize:     tt.fields.TransferSize,
				FolderItemCount:  tt.fields.FolderItemCount,
				FileResumeData:   tt.fields.fileResumeData,
				Options:          tt.fields.options,
				bytesSentCounter: tt.fields.bytesSentCounter,
				ClientConn:       tt.fields.ClientConn,
			}
			assert.Equalf(t, tt.want, ft.String(), "String()")
		})
	}
}

func TestNewFileHeader(t *testing.T) {
	type args struct {
		fileName string
		isDir    bool
	}
	tests := []struct {
		name string
		args args
		want FileHeader
	}{
		{
			name: "when path is file",
			args: args{
				fileName: "foo",
				isDir:    false,
			},
			want: FileHeader{
				Size:     [2]byte{0x00, 0x0a},
				Type:     [2]byte{0x00, 0x00},
				FilePath: EncodeFilePath("foo"),
			},
		},
		{
			name: "when path is dir",
			args: args{
				fileName: "foo",
				isDir:    true,
			},
			want: FileHeader{
				Size:     [2]byte{0x00, 0x0a},
				Type:     [2]byte{0x00, 0x01},
				FilePath: EncodeFilePath("foo"),
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := NewFileHeader(tt.args.fileName, tt.args.isDir); !assert.Equal(t, tt.want, got) {
				t.Errorf("NewFileHeader() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestFileHeader_Payload(t *testing.T) {
	type fields struct {
		Size     [2]byte
		Type     [2]byte
		FilePath []byte
	}
	tests := []struct {
		name   string
		fields fields
		want   []byte
	}{
		{
			name: "has expected payload bytes",
			fields: fields{
				Size:     [2]byte{0x00, 0x0a},
				Type:     [2]byte{0x00, 0x00},
				FilePath: EncodeFilePath("foo"),
			},
			want: []byte{
				0x00, 0x0a, // total size
				0x00, 0x00, // type
				0x00, 0x01, // path item count
				0x00, 0x00, // path separator
				0x03,             // pathName len
				0x66, 0x6f, 0x6f, // "foo"
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			fh := &FileHeader{
				Size:     tt.fields.Size,
				Type:     tt.fields.Type,
				FilePath: tt.fields.FilePath,
			}
			got, _ := io.ReadAll(fh)
			if !assert.Equal(t, tt.want, got) {
				t.Errorf("Read() = %v, want %v", got, tt.want)
			}
		})
	}
}