7 "github.com/stretchr/testify/assert"
14 type mockReadWriter struct {
19 func (mrw mockReadWriter) Read(p []byte) (n int, err error) {
20 return mrw.RBuf.Read(p)
23 func (mrw mockReadWriter) Write(p []byte) (n int, err error) {
24 return mrw.WBuf.Write(p)
27 func TestServer_handleFileTransfer(t *testing.T) {
29 ThreadedNews *ThreadedNews
30 FileTransferMgr FileTransferMgr
45 wantErr assert.ErrorAssertionFunc
49 name: "with invalid protocol",
51 ctx: func() context.Context {
52 ctx := context.Background()
53 ctx = context.WithValue(ctx, contextKeyReq, requestCtx{})
56 rwc: func() io.ReadWriter {
57 mrw := mockReadWriter{}
58 mrw.WBuf = &bytes.Buffer{}
70 wantErr: assert.Error,
73 name: "with invalid transfer Type",
75 FileTransferMgr: NewMemFileTransferMgr(),
78 ctx: func() context.Context {
79 ctx := context.Background()
80 ctx = context.WithValue(ctx, contextKeyReq, requestCtx{})
83 rwc: func() io.ReadWriter {
84 mrw := mockReadWriter{}
85 mrw.WBuf = &bytes.Buffer{}
88 0x48, 0x54, 0x58, 0x46,
97 wantErr: assert.Error,
100 name: "file download",
104 FileRoot: func() string {
105 path, _ := os.Getwd()
106 return path + "/test/config/Files"
108 Logger: NewTestLogger(),
110 FileTransferMgr: &MemFileTransferMgr{
111 fileTransfers: map[FileTransferID]*FileTransfer{
113 RefNum: [4]byte{0, 0, 0, 5},
115 FileName: []byte("testfile-8b"),
117 ClientConn: &ClientConn{
121 ClientFileTransferMgr: ClientFileTransferMgr{
122 transfers: map[FileTransferType]map[FileTransferID]*FileTransfer{
124 [4]byte{0, 0, 0, 5}: &FileTransfer{},
129 bytesSentCounter: &WriteCounter{},
135 ctx: func() context.Context {
136 ctx := context.Background()
137 ctx = context.WithValue(ctx, contextKeyReq, requestCtx{})
140 rwc: func() io.ReadWriter {
141 mrw := mockReadWriter{}
142 mrw.WBuf = &bytes.Buffer{}
145 0x48, 0x54, 0x58, 0x46,
154 wantErr: assert.NoError,
155 wantDump: `00000000 46 49 4c 50 00 01 00 00 00 00 00 00 00 00 00 00 |FILP............|
156 00000010 00 00 00 00 00 00 00 02 49 4e 46 4f 00 00 00 00 |........INFO....|
157 00000020 00 00 00 00 00 00 00 55 41 4d 41 43 54 45 58 54 |.......UAMACTEXT|
158 00000030 54 54 58 54 00 00 00 00 00 00 01 00 00 00 00 00 |TTXT............|
159 00000040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
160 00000050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
161 00000060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0b |................|
162 00000070 74 65 73 74 66 69 6c 65 2d 38 62 00 00 44 41 54 |testfile-8b..DAT|
163 00000080 41 00 00 00 00 00 00 00 00 00 00 00 08 7c 39 e0 |A............|9.|
164 00000090 bc 64 e2 cd de 4d 41 43 52 00 00 00 00 00 00 00 |.d...MACR.......|
165 000000a0 00 00 00 00 00 |.....|
169 for _, tt := range tests {
170 t.Run(tt.name, func(t *testing.T) {
172 FileTransferMgr: tt.fields.FileTransferMgr,
173 Config: tt.fields.Config,
174 Logger: tt.fields.Logger,
175 Stats: tt.fields.Stats,
179 tt.wantErr(t, s.handleFileTransfer(tt.args.ctx, tt.args.rwc), fmt.Sprintf("handleFileTransfer(%v, %v)", tt.args.ctx, tt.args.rwc))
181 assertTransferBytesEqual(t, tt.wantDump, tt.args.rwc.(mockReadWriter).WBuf.Bytes())