]> git.r.bdr.sh - rbdr/mobius/blame - hotline/server_test.go
Merge pull request #62 from jhalter/add_basic_stats
[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
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{},
df1ade54
JH
119 fileTransfers: map[[4]byte]*FileTransfer{
120 [4]byte{0, 0, 0, 5}: {
7cd900d6
JH
121 ReferenceNumber: []byte{0, 0, 0, 5},
122 Type: FileDownload,
123 FileName: []byte("testfile-8b"),
124 FilePath: []byte{},
df1ade54
JH
125 ClientConn: &ClientConn{
126 Account: &Account{
127 Login: "foo",
128 },
129 transfersMU: sync.Mutex{},
130 transfers: map[int]map[[4]byte]*FileTransfer{
131 FileDownload: {
132 [4]byte{0, 0, 0, 5}: &FileTransfer{},
133 },
134 },
135 },
136 bytesSentCounter: &WriteCounter{},
7cd900d6
JH
137 },
138 },
139 },
140 args: args{
141 ctx: func() context.Context {
142 ctx := context.Background()
143 ctx = context.WithValue(ctx, contextKeyReq, requestCtx{})
144 return ctx
145 }(),
146 rwc: func() io.ReadWriter {
147 mrw := mockReadWriter{}
148 mrw.WBuf = &bytes.Buffer{}
149 mrw.RBuf.Write(
150 []byte{
151 0x48, 0x54, 0x58, 0x46,
152 0, 0, 0, 5,
153 0, 0, 0x01, 0,
154 0, 0, 0, 0,
155 },
156 )
157 return mrw
158 }(),
159 },
160 wantErr: assert.NoError,
161 wantDump: `00000000 46 49 4c 50 00 01 00 00 00 00 00 00 00 00 00 00 |FILP............|
16200000010 00 00 00 00 00 00 00 02 49 4e 46 4f 00 00 00 00 |........INFO....|
16300000020 00 00 00 00 00 00 00 55 41 4d 41 43 54 45 58 54 |.......UAMACTEXT|
16400000030 54 54 58 54 00 00 00 00 00 00 01 00 00 00 00 00 |TTXT............|
16500000040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
16600000050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
16700000060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0b |................|
16800000070 74 65 73 74 66 69 6c 65 2d 38 62 00 00 44 41 54 |testfile-8b..DAT|
16900000080 41 00 00 00 00 00 00 00 00 00 00 00 08 7c 39 e0 |A............|9.|
17000000090 bc 64 e2 cd de 4d 41 43 52 00 00 00 00 00 00 00 |.d...MACR.......|
171000000a0 00 00 00 00 00 |.....|
172`,
173 },
174 }
175 for _, tt := range tests {
176 t.Run(tt.name, func(t *testing.T) {
177 s := &Server{
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,
df1ade54 183 fileTransfers: tt.fields.fileTransfers,
7cd900d6
JH
184 Config: tt.fields.Config,
185 ConfigDir: tt.fields.ConfigDir,
186 Logger: tt.fields.Logger,
187 Stats: tt.fields.Stats,
188 FS: tt.fields.FS,
189 }
190 tt.wantErr(t, s.handleFileTransfer(tt.args.ctx, tt.args.rwc), fmt.Sprintf("handleFileTransfer(%v, %v)", tt.args.ctx, tt.args.rwc))
191
192 assertTransferBytesEqual(t, tt.wantDump, tt.args.rwc.(mockReadWriter).WBuf.Bytes())
193 })
194 }
195}