]>
Commit | Line | Data |
---|---|---|
1 | package hotline | |
2 | ||
3 | import ( | |
4 | "bytes" | |
5 | "context" | |
6 | "fmt" | |
7 | "github.com/stretchr/testify/assert" | |
8 | "io" | |
9 | "log/slog" | |
10 | "os" | |
11 | "testing" | |
12 | ) | |
13 | ||
14 | type mockReadWriter struct { | |
15 | RBuf bytes.Buffer | |
16 | WBuf *bytes.Buffer | |
17 | } | |
18 | ||
19 | func (mrw mockReadWriter) Read(p []byte) (n int, err error) { | |
20 | return mrw.RBuf.Read(p) | |
21 | } | |
22 | ||
23 | func (mrw mockReadWriter) Write(p []byte) (n int, err error) { | |
24 | return mrw.WBuf.Write(p) | |
25 | } | |
26 | ||
27 | func TestServer_handleFileTransfer(t *testing.T) { | |
28 | type fields struct { | |
29 | ThreadedNews *ThreadedNews | |
30 | FileTransferMgr FileTransferMgr | |
31 | Config Config | |
32 | ConfigDir string | |
33 | Stats *Stats | |
34 | Logger *slog.Logger | |
35 | FS FileStore | |
36 | } | |
37 | type args struct { | |
38 | ctx context.Context | |
39 | rwc io.ReadWriter | |
40 | } | |
41 | tests := []struct { | |
42 | name string | |
43 | fields fields | |
44 | args args | |
45 | wantErr assert.ErrorAssertionFunc | |
46 | wantDump string | |
47 | }{ | |
48 | { | |
49 | name: "with invalid protocol", | |
50 | args: args{ | |
51 | ctx: func() context.Context { | |
52 | ctx := context.Background() | |
53 | ctx = context.WithValue(ctx, contextKeyReq, requestCtx{}) | |
54 | return ctx | |
55 | }(), | |
56 | rwc: func() io.ReadWriter { | |
57 | mrw := mockReadWriter{} | |
58 | mrw.WBuf = &bytes.Buffer{} | |
59 | mrw.RBuf.Write( | |
60 | []byte{ | |
61 | 0, 0, 0, 0, | |
62 | 0, 0, 0, 5, | |
63 | 0, 0, 0x01, 0, | |
64 | 0, 0, 0, 0, | |
65 | }, | |
66 | ) | |
67 | return mrw | |
68 | }(), | |
69 | }, | |
70 | wantErr: assert.Error, | |
71 | }, | |
72 | { | |
73 | name: "with invalid transfer Type", | |
74 | fields: fields{ | |
75 | FileTransferMgr: NewMemFileTransferMgr(), | |
76 | }, | |
77 | args: args{ | |
78 | ctx: func() context.Context { | |
79 | ctx := context.Background() | |
80 | ctx = context.WithValue(ctx, contextKeyReq, requestCtx{}) | |
81 | return ctx | |
82 | }(), | |
83 | rwc: func() io.ReadWriter { | |
84 | mrw := mockReadWriter{} | |
85 | mrw.WBuf = &bytes.Buffer{} | |
86 | mrw.RBuf.Write( | |
87 | []byte{ | |
88 | 0x48, 0x54, 0x58, 0x46, | |
89 | 0, 0, 0, 5, | |
90 | 0, 0, 0x01, 0, | |
91 | 0, 0, 0, 0, | |
92 | }, | |
93 | ) | |
94 | return mrw | |
95 | }(), | |
96 | }, | |
97 | wantErr: assert.Error, | |
98 | }, | |
99 | { | |
100 | name: "file download", | |
101 | fields: fields{ | |
102 | FS: &OSFileStore{}, | |
103 | Logger: NewTestLogger(), | |
104 | Stats: NewStats(), | |
105 | FileTransferMgr: &MemFileTransferMgr{ | |
106 | fileTransfers: map[FileTransferID]*FileTransfer{ | |
107 | {0, 0, 0, 5}: { | |
108 | RefNum: [4]byte{0, 0, 0, 5}, | |
109 | Type: FileDownload, | |
110 | FileName: []byte("testfile-8b"), | |
111 | FilePath: []byte{}, | |
112 | FileRoot: func() string { | |
113 | path, _ := os.Getwd() | |
114 | return path + "/test/config/Files" | |
115 | }(), | |
116 | ClientConn: &ClientConn{ | |
117 | Account: &Account{ | |
118 | Login: "foo", | |
119 | }, | |
120 | ClientFileTransferMgr: ClientFileTransferMgr{ | |
121 | transfers: map[FileTransferType]map[FileTransferID]*FileTransfer{ | |
122 | FileDownload: { | |
123 | [4]byte{0, 0, 0, 5}: &FileTransfer{}, | |
124 | }, | |
125 | }, | |
126 | }, | |
127 | }, | |
128 | bytesSentCounter: &WriteCounter{}, | |
129 | }, | |
130 | }, | |
131 | }, | |
132 | }, | |
133 | args: args{ | |
134 | ctx: func() context.Context { | |
135 | ctx := context.Background() | |
136 | ctx = context.WithValue(ctx, contextKeyReq, requestCtx{}) | |
137 | return ctx | |
138 | }(), | |
139 | rwc: func() io.ReadWriter { | |
140 | mrw := mockReadWriter{} | |
141 | mrw.WBuf = &bytes.Buffer{} | |
142 | mrw.RBuf.Write( | |
143 | []byte{ | |
144 | 0x48, 0x54, 0x58, 0x46, | |
145 | 0, 0, 0, 5, | |
146 | 0, 0, 0x01, 0, | |
147 | 0, 0, 0, 0, | |
148 | }, | |
149 | ) | |
150 | return mrw | |
151 | }(), | |
152 | }, | |
153 | wantErr: assert.NoError, | |
154 | wantDump: `00000000 46 49 4c 50 00 01 00 00 00 00 00 00 00 00 00 00 |FILP............| | |
155 | 00000010 00 00 00 00 00 00 00 02 49 4e 46 4f 00 00 00 00 |........INFO....| | |
156 | 00000020 00 00 00 00 00 00 00 55 41 4d 41 43 54 45 58 54 |.......UAMACTEXT| | |
157 | 00000030 54 54 58 54 00 00 00 00 00 00 01 00 00 00 00 00 |TTXT............| | |
158 | 00000040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| | |
159 | 00000050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| | |
160 | 00000060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0b |................| | |
161 | 00000070 74 65 73 74 66 69 6c 65 2d 38 62 00 00 44 41 54 |testfile-8b..DAT| | |
162 | 00000080 41 00 00 00 00 00 00 00 00 00 00 00 08 7c 39 e0 |A............|9.| | |
163 | 00000090 bc 64 e2 cd de 4d 41 43 52 00 00 00 00 00 00 00 |.d...MACR.......| | |
164 | 000000a0 00 00 00 00 00 |.....| | |
165 | `, | |
166 | }, | |
167 | } | |
168 | for _, tt := range tests { | |
169 | t.Run(tt.name, func(t *testing.T) { | |
170 | s := &Server{ | |
171 | FileTransferMgr: tt.fields.FileTransferMgr, | |
172 | Config: tt.fields.Config, | |
173 | Logger: tt.fields.Logger, | |
174 | Stats: tt.fields.Stats, | |
175 | FS: tt.fields.FS, | |
176 | } | |
177 | ||
178 | tt.wantErr(t, s.handleFileTransfer(tt.args.ctx, tt.args.rwc), fmt.Sprintf("handleFileTransfer(%v, %v)", tt.args.ctx, tt.args.rwc)) | |
179 | ||
180 | assertTransferBytesEqual(t, tt.wantDump, tt.args.rwc.(mockReadWriter).WBuf.Bytes()) | |
181 | }) | |
182 | } | |
183 | } |