]> git.r.bdr.sh - rbdr/mobius/blame - hotline/server_test.go
Remove unneeded code
[rbdr/mobius] / hotline / server_test.go
CommitLineData
6988a057
JH
1package hotline
2
7cd900d6
JH
3import (
4 "bytes"
5 "context"
6 "fmt"
7 "github.com/stretchr/testify/assert"
8 "go.uber.org/zap"
9 "io"
10 "os"
11 "sync"
12 "testing"
13)
6988a057 14
7cd900d6
JH
15type mockReadWriter struct {
16 RBuf bytes.Buffer
17 WBuf *bytes.Buffer
18}
19
20func (mrw mockReadWriter) Read(p []byte) (n int, err error) {
21 return mrw.RBuf.Read(p)
22}
23
24func (mrw mockReadWriter) Write(p []byte) (n int, err error) {
25 return mrw.WBuf.Write(p)
26}
27
28func TestServer_handleFileTransfer(t *testing.T) {
29 type fields struct {
30 Port int
31 Accounts map[string]*Account
32 Agreement []byte
33 Clients map[uint16]*ClientConn
34 ThreadedNews *ThreadedNews
35 FileTransfers map[uint32]*FileTransfer
36 Config *Config
37 ConfigDir string
38 Logger *zap.SugaredLogger
39 PrivateChats map[uint32]*PrivateChat
40 NextGuestID *uint16
41 TrackerPassID [4]byte
42 Stats *Stats
43 FS FileStore
44 outbox chan Transaction
45 mux sync.Mutex
46 flatNewsMux sync.Mutex
47 FlatNews []byte
48 }
49 type args struct {
50 ctx context.Context
51 rwc io.ReadWriter
52 }
53 tests := []struct {
54 name string
55 fields fields
56 args args
57 wantErr assert.ErrorAssertionFunc
58 wantDump string
59 }{
60 {
61 name: "with invalid protocol",
62 args: args{
63 ctx: func() context.Context {
64 ctx := context.Background()
65 ctx = context.WithValue(ctx, contextKeyReq, requestCtx{})
66 return ctx
67 }(),
68 rwc: func() io.ReadWriter {
69 mrw := mockReadWriter{}
70 mrw.WBuf = &bytes.Buffer{}
71 mrw.RBuf.Write(
72 []byte{
73 0, 0, 0, 0,
74 0, 0, 0, 5,
75 0, 0, 0x01, 0,
76 0, 0, 0, 0,
77 },
78 )
79 return mrw
80 }(),
81 },
82 wantErr: assert.Error,
83 },
84 {
85 name: "with invalid transfer ID",
86 args: args{
87 ctx: func() context.Context {
88 ctx := context.Background()
89 ctx = context.WithValue(ctx, contextKeyReq, requestCtx{})
90 return ctx
91 }(),
92 rwc: func() io.ReadWriter {
93 mrw := mockReadWriter{}
94 mrw.WBuf = &bytes.Buffer{}
95 mrw.RBuf.Write(
96 []byte{
97 0x48, 0x54, 0x58, 0x46,
98 0, 0, 0, 5,
99 0, 0, 0x01, 0,
100 0, 0, 0, 0,
101 },
102 )
103 return mrw
104 }(),
105 },
106 wantErr: assert.Error,
107 },
108 {
109 name: "file download",
110 fields: fields{
111 FS: &OSFileStore{},
112 Config: &Config{
113 FileRoot: func() string {
114 path, _ := os.Getwd()
115 return path + "/test/config/Files"
116 }()},
117 Logger: NewTestLogger(),
118 Stats: &Stats{},
119 FileTransfers: map[uint32]*FileTransfer{
120 uint32(5): {
121 ReferenceNumber: []byte{0, 0, 0, 5},
122 Type: FileDownload,
123 FileName: []byte("testfile-8b"),
124 FilePath: []byte{},
125 },
126 },
127 },
128 args: args{
129 ctx: func() context.Context {
130 ctx := context.Background()
131 ctx = context.WithValue(ctx, contextKeyReq, requestCtx{})
132 return ctx
133 }(),
134 rwc: func() io.ReadWriter {
135 mrw := mockReadWriter{}
136 mrw.WBuf = &bytes.Buffer{}
137 mrw.RBuf.Write(
138 []byte{
139 0x48, 0x54, 0x58, 0x46,
140 0, 0, 0, 5,
141 0, 0, 0x01, 0,
142 0, 0, 0, 0,
143 },
144 )
145 return mrw
146 }(),
147 },
148 wantErr: assert.NoError,
149 wantDump: `00000000 46 49 4c 50 00 01 00 00 00 00 00 00 00 00 00 00 |FILP............|
15000000010 00 00 00 00 00 00 00 02 49 4e 46 4f 00 00 00 00 |........INFO....|
15100000020 00 00 00 00 00 00 00 55 41 4d 41 43 54 45 58 54 |.......UAMACTEXT|
15200000030 54 54 58 54 00 00 00 00 00 00 01 00 00 00 00 00 |TTXT............|
15300000040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
15400000050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
15500000060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0b |................|
15600000070 74 65 73 74 66 69 6c 65 2d 38 62 00 00 44 41 54 |testfile-8b..DAT|
15700000080 41 00 00 00 00 00 00 00 00 00 00 00 08 7c 39 e0 |A............|9.|
15800000090 bc 64 e2 cd de 4d 41 43 52 00 00 00 00 00 00 00 |.d...MACR.......|
159000000a0 00 00 00 00 00 |.....|
160`,
161 },
162 }
163 for _, tt := range tests {
164 t.Run(tt.name, func(t *testing.T) {
165 s := &Server{
166 Port: tt.fields.Port,
167 Accounts: tt.fields.Accounts,
168 Agreement: tt.fields.Agreement,
169 Clients: tt.fields.Clients,
170 ThreadedNews: tt.fields.ThreadedNews,
171 FileTransfers: tt.fields.FileTransfers,
172 Config: tt.fields.Config,
173 ConfigDir: tt.fields.ConfigDir,
174 Logger: tt.fields.Logger,
175 Stats: tt.fields.Stats,
176 FS: tt.fields.FS,
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}