7 "github.com/stretchr/testify/assert"
15 type mockReadWriter struct {
20 func (mrw mockReadWriter) Read(p []byte) (n int, err error) {
21 return mrw.RBuf.Read(p)
24 func (mrw mockReadWriter) Write(p []byte) (n int, err error) {
25 return mrw.WBuf.Write(p)
28 func TestServer_handleFileTransfer(t *testing.T) {
31 Accounts map[string]*Account
33 Clients map[uint16]*ClientConn
34 ThreadedNews *ThreadedNews
35 fileTransfers map[[4]byte]*FileTransfer
38 Logger *zap.SugaredLogger
39 PrivateChats map[uint32]*PrivateChat
45 flatNewsMux sync.Mutex
56 wantErr assert.ErrorAssertionFunc
60 name: "with invalid protocol",
62 ctx: func() context.Context {
63 ctx := context.Background()
64 ctx = context.WithValue(ctx, contextKeyReq, requestCtx{})
67 rwc: func() io.ReadWriter {
68 mrw := mockReadWriter{}
69 mrw.WBuf = &bytes.Buffer{}
81 wantErr: assert.Error,
84 name: "with invalid transfer ID",
86 ctx: func() context.Context {
87 ctx := context.Background()
88 ctx = context.WithValue(ctx, contextKeyReq, requestCtx{})
91 rwc: func() io.ReadWriter {
92 mrw := mockReadWriter{}
93 mrw.WBuf = &bytes.Buffer{}
96 0x48, 0x54, 0x58, 0x46,
105 wantErr: assert.Error,
108 name: "file download",
112 FileRoot: func() string {
113 path, _ := os.Getwd()
114 return path + "/test/config/Files"
116 Logger: NewTestLogger(),
118 fileTransfers: map[[4]byte]*FileTransfer{
119 [4]byte{0, 0, 0, 5}: {
120 ReferenceNumber: []byte{0, 0, 0, 5},
122 FileName: []byte("testfile-8b"),
124 ClientConn: &ClientConn{
128 transfersMU: sync.Mutex{},
129 transfers: map[int]map[[4]byte]*FileTransfer{
131 [4]byte{0, 0, 0, 5}: &FileTransfer{},
135 bytesSentCounter: &WriteCounter{},
140 ctx: func() context.Context {
141 ctx := context.Background()
142 ctx = context.WithValue(ctx, contextKeyReq, requestCtx{})
145 rwc: func() io.ReadWriter {
146 mrw := mockReadWriter{}
147 mrw.WBuf = &bytes.Buffer{}
150 0x48, 0x54, 0x58, 0x46,
159 wantErr: assert.NoError,
160 wantDump: `00000000 46 49 4c 50 00 01 00 00 00 00 00 00 00 00 00 00 |FILP............|
161 00000010 00 00 00 00 00 00 00 02 49 4e 46 4f 00 00 00 00 |........INFO....|
162 00000020 00 00 00 00 00 00 00 55 41 4d 41 43 54 45 58 54 |.......UAMACTEXT|
163 00000030 54 54 58 54 00 00 00 00 00 00 01 00 00 00 00 00 |TTXT............|
164 00000040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
165 00000050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
166 00000060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0b |................|
167 00000070 74 65 73 74 66 69 6c 65 2d 38 62 00 00 44 41 54 |testfile-8b..DAT|
168 00000080 41 00 00 00 00 00 00 00 00 00 00 00 08 7c 39 e0 |A............|9.|
169 00000090 bc 64 e2 cd de 4d 41 43 52 00 00 00 00 00 00 00 |.d...MACR.......|
170 000000a0 00 00 00 00 00 |.....|
174 for _, tt := range tests {
175 t.Run(tt.name, func(t *testing.T) {
177 Port: tt.fields.Port,
178 Accounts: tt.fields.Accounts,
179 Agreement: tt.fields.Agreement,
180 Clients: tt.fields.Clients,
181 ThreadedNews: tt.fields.ThreadedNews,
182 fileTransfers: tt.fields.fileTransfers,
183 Config: tt.fields.Config,
184 ConfigDir: tt.fields.ConfigDir,
185 Logger: tt.fields.Logger,
186 Stats: tt.fields.Stats,
189 tt.wantErr(t, s.handleFileTransfer(tt.args.ctx, tt.args.rwc), fmt.Sprintf("handleFileTransfer(%v, %v)", tt.args.ctx, tt.args.rwc))
191 assertTransferBytesEqual(t, tt.wantDump, tt.args.rwc.(mockReadWriter).WBuf.Bytes())