]> git.r.bdr.sh - rbdr/mobius/blame - hotline/server_test.go
patch: v0.10.16
[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
df1ade54 35 fileTransfers map[[4]byte]*FileTransfer
7cd900d6
JH
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
7cd900d6
JH
44 mux sync.Mutex
45 flatNewsMux sync.Mutex
46 FlatNews []byte
47 }
48 type args struct {
49 ctx context.Context
50 rwc io.ReadWriter
51 }
52 tests := []struct {
53 name string
54 fields fields
55 args args
56 wantErr assert.ErrorAssertionFunc
57 wantDump string
58 }{
59 {
60 name: "with invalid protocol",
61 args: args{
62 ctx: func() context.Context {
63 ctx := context.Background()
64 ctx = context.WithValue(ctx, contextKeyReq, requestCtx{})
65 return ctx
66 }(),
67 rwc: func() io.ReadWriter {
68 mrw := mockReadWriter{}
69 mrw.WBuf = &bytes.Buffer{}
70 mrw.RBuf.Write(
71 []byte{
72 0, 0, 0, 0,
73 0, 0, 0, 5,
74 0, 0, 0x01, 0,
75 0, 0, 0, 0,
76 },
77 )
78 return mrw
79 }(),
80 },
81 wantErr: assert.Error,
82 },
83 {
84 name: "with invalid transfer ID",
85 args: args{
86 ctx: func() context.Context {
87 ctx := context.Background()
88 ctx = context.WithValue(ctx, contextKeyReq, requestCtx{})
89 return ctx
90 }(),
91 rwc: func() io.ReadWriter {
92 mrw := mockReadWriter{}
93 mrw.WBuf = &bytes.Buffer{}
94 mrw.RBuf.Write(
95 []byte{
96 0x48, 0x54, 0x58, 0x46,
97 0, 0, 0, 5,
98 0, 0, 0x01, 0,
99 0, 0, 0, 0,
100 },
101 )
102 return mrw
103 }(),
104 },
105 wantErr: assert.Error,
106 },
107 {
108 name: "file download",
109 fields: fields{
110 FS: &OSFileStore{},
111 Config: &Config{
112 FileRoot: func() string {
113 path, _ := os.Getwd()
114 return path + "/test/config/Files"
115 }()},
116 Logger: NewTestLogger(),
117 Stats: &Stats{},
df1ade54
JH
118 fileTransfers: map[[4]byte]*FileTransfer{
119 [4]byte{0, 0, 0, 5}: {
7cd900d6
JH
120 ReferenceNumber: []byte{0, 0, 0, 5},
121 Type: FileDownload,
122 FileName: []byte("testfile-8b"),
123 FilePath: []byte{},
df1ade54
JH
124 ClientConn: &ClientConn{
125 Account: &Account{
126 Login: "foo",
127 },
128 transfersMU: sync.Mutex{},
129 transfers: map[int]map[[4]byte]*FileTransfer{
130 FileDownload: {
131 [4]byte{0, 0, 0, 5}: &FileTransfer{},
132 },
133 },
134 },
135 bytesSentCounter: &WriteCounter{},
7cd900d6
JH
136 },
137 },
138 },
139 args: args{
140 ctx: func() context.Context {
141 ctx := context.Background()
142 ctx = context.WithValue(ctx, contextKeyReq, requestCtx{})
143 return ctx
144 }(),
145 rwc: func() io.ReadWriter {
146 mrw := mockReadWriter{}
147 mrw.WBuf = &bytes.Buffer{}
148 mrw.RBuf.Write(
149 []byte{
150 0x48, 0x54, 0x58, 0x46,
151 0, 0, 0, 5,
152 0, 0, 0x01, 0,
153 0, 0, 0, 0,
154 },
155 )
156 return mrw
157 }(),
158 },
159 wantErr: assert.NoError,
160 wantDump: `00000000 46 49 4c 50 00 01 00 00 00 00 00 00 00 00 00 00 |FILP............|
16100000010 00 00 00 00 00 00 00 02 49 4e 46 4f 00 00 00 00 |........INFO....|
16200000020 00 00 00 00 00 00 00 55 41 4d 41 43 54 45 58 54 |.......UAMACTEXT|
16300000030 54 54 58 54 00 00 00 00 00 00 01 00 00 00 00 00 |TTXT............|
16400000040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
16500000050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
16600000060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0b |................|
16700000070 74 65 73 74 66 69 6c 65 2d 38 62 00 00 44 41 54 |testfile-8b..DAT|
16800000080 41 00 00 00 00 00 00 00 00 00 00 00 08 7c 39 e0 |A............|9.|
16900000090 bc 64 e2 cd de 4d 41 43 52 00 00 00 00 00 00 00 |.d...MACR.......|
170000000a0 00 00 00 00 00 |.....|
171`,
172 },
173 }
174 for _, tt := range tests {
175 t.Run(tt.name, func(t *testing.T) {
176 s := &Server{
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,
df1ade54 182 fileTransfers: tt.fields.fileTransfers,
7cd900d6
JH
183 Config: tt.fields.Config,
184 ConfigDir: tt.fields.ConfigDir,
185 Logger: tt.fields.Logger,
186 Stats: tt.fields.Stats,
187 FS: tt.fields.FS,
188 }
189 tt.wantErr(t, s.handleFileTransfer(tt.args.ctx, tt.args.rwc), fmt.Sprintf("handleFileTransfer(%v, %v)", tt.args.ctx, tt.args.rwc))
190
191 assertTransferBytesEqual(t, tt.wantDump, tt.args.rwc.(mockReadWriter).WBuf.Bytes())
192 })
193 }
194}