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
44 outbox chan Transaction
46 flatNewsMux sync.Mutex
57 wantErr assert.ErrorAssertionFunc
61 name: "with invalid protocol",
63 ctx: func() context.Context {
64 ctx := context.Background()
65 ctx = context.WithValue(ctx, contextKeyReq, requestCtx{})
68 rwc: func() io.ReadWriter {
69 mrw := mockReadWriter{}
70 mrw.WBuf = &bytes.Buffer{}
82 wantErr: assert.Error,
85 name: "with invalid transfer ID",
87 ctx: func() context.Context {
88 ctx := context.Background()
89 ctx = context.WithValue(ctx, contextKeyReq, requestCtx{})
92 rwc: func() io.ReadWriter {
93 mrw := mockReadWriter{}
94 mrw.WBuf = &bytes.Buffer{}
97 0x48, 0x54, 0x58, 0x46,
106 wantErr: assert.Error,
109 name: "file download",
113 FileRoot: func() string {
114 path, _ := os.Getwd()
115 return path + "/test/config/Files"
117 Logger: NewTestLogger(),
119 fileTransfers: map[[4]byte]*FileTransfer{
120 [4]byte{0, 0, 0, 5}: {
121 ReferenceNumber: []byte{0, 0, 0, 5},
123 FileName: []byte("testfile-8b"),
125 ClientConn: &ClientConn{
129 transfersMU: sync.Mutex{},
130 transfers: map[int]map[[4]byte]*FileTransfer{
132 [4]byte{0, 0, 0, 5}: &FileTransfer{},
136 bytesSentCounter: &WriteCounter{},
141 ctx: func() context.Context {
142 ctx := context.Background()
143 ctx = context.WithValue(ctx, contextKeyReq, requestCtx{})
146 rwc: func() io.ReadWriter {
147 mrw := mockReadWriter{}
148 mrw.WBuf = &bytes.Buffer{}
151 0x48, 0x54, 0x58, 0x46,
160 wantErr: assert.NoError,
161 wantDump: `00000000 46 49 4c 50 00 01 00 00 00 00 00 00 00 00 00 00 |FILP............|
162 00000010 00 00 00 00 00 00 00 02 49 4e 46 4f 00 00 00 00 |........INFO....|
163 00000020 00 00 00 00 00 00 00 55 41 4d 41 43 54 45 58 54 |.......UAMACTEXT|
164 00000030 54 54 58 54 00 00 00 00 00 00 01 00 00 00 00 00 |TTXT............|
165 00000040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
166 00000050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
167 00000060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0b |................|
168 00000070 74 65 73 74 66 69 6c 65 2d 38 62 00 00 44 41 54 |testfile-8b..DAT|
169 00000080 41 00 00 00 00 00 00 00 00 00 00 00 08 7c 39 e0 |A............|9.|
170 00000090 bc 64 e2 cd de 4d 41 43 52 00 00 00 00 00 00 00 |.d...MACR.......|
171 000000a0 00 00 00 00 00 |.....|
175 for _, tt := range tests {
176 t.Run(tt.name, func(t *testing.T) {
178 Port: tt.fields.Port,
179 Accounts: tt.fields.Accounts,
180 Agreement: tt.fields.Agreement,
181 Clients: tt.fields.Clients,
182 ThreadedNews: tt.fields.ThreadedNews,
183 fileTransfers: tt.fields.fileTransfers,
184 Config: tt.fields.Config,
185 ConfigDir: tt.fields.ConfigDir,
186 Logger: tt.fields.Logger,
187 Stats: tt.fields.Stats,
190 tt.wantErr(t, s.handleFileTransfer(tt.args.ctx, tt.args.rwc), fmt.Sprintf("handleFileTransfer(%v, %v)", tt.args.ctx, tt.args.rwc))
192 assertTransferBytesEqual(t, tt.wantDump, tt.args.rwc.(mockReadWriter).WBuf.Bytes())