forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attachment_httpsrv_test.go
298 lines (260 loc) · 9.28 KB
/
attachment_httpsrv_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package chat
import (
"bytes"
"context"
"errors"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"testing"
"time"
"github.com/keybase/client/go/kbhttp/manager"
"github.com/keybase/client/go/chat/attachments"
"github.com/keybase/client/go/chat/utils"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/chat/s3"
"github.com/keybase/client/go/chat/types"
"github.com/keybase/client/go/protocol/chat1"
"github.com/keybase/client/go/protocol/gregor1"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/stretchr/testify/require"
)
type nopCloser struct {
io.Reader
}
func (nopCloser) Close() error { return nil }
type mockAttachmentRemoteStore struct {
decryptCh chan struct{}
assetReaderCh chan struct{}
}
func (m mockAttachmentRemoteStore) DecryptAsset(ctx context.Context, w io.Writer, body io.Reader,
asset chat1.Asset, progress types.ProgressReporter) error {
if m.decryptCh != nil {
m.decryptCh <- struct{}{}
}
_, err := io.Copy(w, body)
return err
}
func (m mockAttachmentRemoteStore) DeleteAssets(ctx context.Context, params chat1.S3Params, signer s3.Signer,
assets []chat1.Asset) error {
return nil
}
func (m mockAttachmentRemoteStore) DeleteAsset(ctx context.Context, params chat1.S3Params, signer s3.Signer,
asset chat1.Asset) error {
return nil
}
func (m mockAttachmentRemoteStore) DownloadAsset(ctx context.Context, params chat1.S3Params,
asset chat1.Asset, w io.Writer, signer s3.Signer, progress types.ProgressReporter) error {
return errors.New("not implemented")
}
func (m mockAttachmentRemoteStore) UploadAsset(ctx context.Context, task *attachments.UploadTask,
encryptedOut io.Writer) (chat1.Asset, error) {
return chat1.Asset{}, errors.New("not implemented")
}
func (m mockAttachmentRemoteStore) StreamAsset(ctx context.Context, params chat1.S3Params, asset chat1.Asset,
signer s3.Signer) (io.ReadSeeker, error) {
return nil, errors.New("not implemented")
}
func (m mockAttachmentRemoteStore) GetAssetReader(ctx context.Context, params chat1.S3Params, asset chat1.Asset,
signer s3.Signer) (io.ReadCloser, error) {
if m.assetReaderCh != nil {
m.assetReaderCh <- struct{}{}
}
return nopCloser{Reader: bytes.NewBufferString("HI")}, nil
}
type mockSigningRemote struct {
chat1.RemoteInterface
}
func (m mockSigningRemote) Sign(payload []byte) ([]byte, error) {
return nil, nil
}
func (m mockSigningRemote) GetS3Params(ctx context.Context, convID chat1.ConversationID) (res chat1.S3Params, err error) {
return res, nil
}
func TestChatSrvAttachmentHTTPSrv(t *testing.T) {
ctc := makeChatTestContext(t, "TestChatSrvAttachmentHTTPSrv", 1)
defer ctc.cleanup()
users := ctc.users()
defer func() {
useRemoteMock = true
}()
useRemoteMock = false
tc := ctc.world.Tcs[users[0].Username]
decryptCh := make(chan struct{}, 10)
assetReaderCh := make(chan struct{}, 10)
store := mockAttachmentRemoteStore{
decryptCh: decryptCh,
assetReaderCh: assetReaderCh,
}
fetcher := NewCachingAttachmentFetcher(tc.Context(), store, 1)
d, err := libkb.RandHexString("", 8)
require.NoError(t, err)
fetcher.tempDir = filepath.Join(os.TempDir(), d)
uid := gregor1.UID(users[0].GetUID().ToBytes())
conv := mustCreateConversationForTest(t, ctc, users[0], chat1.TopicType_CHAT,
chat1.ConversationMembersType_IMPTEAMNATIVE)
tc.ChatG.AttachmentURLSrv = NewAttachmentHTTPSrv(tc.Context(),
manager.NewSrv(tc.Context().ExternalG()), fetcher,
func() chat1.RemoteInterface { return mockSigningRemote{} })
_, err = postLocalForTest(t, ctc, users[0], conv, chat1.NewMessageBodyWithAttachment(chat1.MessageAttachment{
Object: chat1.Asset{
Path: "m0",
},
}))
require.NoError(t, err)
_, err = postLocalForTest(t, ctc, users[0], conv, chat1.NewMessageBodyWithAttachment(chat1.MessageAttachment{
Object: chat1.Asset{
Path: "m1",
},
}))
require.NoError(t, err)
tv, err := tc.Context().ConvSource.Pull(context.TODO(), conv.Id, uid,
chat1.GetThreadReason_GENERAL, nil,
&chat1.GetThreadQuery{
MessageTypes: []chat1.MessageType{chat1.MessageType_ATTACHMENT},
}, nil)
require.NoError(t, err)
require.Equal(t, 2, len(tv.Messages))
uiMsg := utils.PresentMessageUnboxed(context.TODO(), tc.Context(), tv.Messages[0], uid, conv.Id)
require.NotNil(t, uiMsg.Valid().AssetUrlInfo)
uiMsg2 := utils.PresentMessageUnboxed(context.TODO(), tc.Context(), tv.Messages[1], uid, conv.Id)
require.NotNil(t, uiMsg.Valid().AssetUrlInfo)
waitTime := 20 * time.Second
readAsset := func(msg chat1.UIMessage, cacheHit bool) {
httpRes, err := http.Get(msg.Valid().AssetUrlInfo.FullUrl)
require.NoError(t, err)
body, err := ioutil.ReadAll(httpRes.Body)
require.NoError(t, err)
require.Equal(t, "HI", string(body))
if cacheHit {
select {
case <-assetReaderCh:
require.Fail(t, "should have hit cache")
default:
}
} else {
select {
case <-assetReaderCh:
case <-time.After(waitTime):
require.Fail(t, "should have read")
}
}
select {
case <-decryptCh:
case <-time.After(waitTime):
require.Fail(t, "should have decrypted")
}
}
readAsset(uiMsg, false)
readAsset(uiMsg, true)
readAsset(uiMsg2, false)
readAsset(uiMsg2, true)
readAsset(uiMsg, false) // make sure it got evicted
readAsset(uiMsg, true)
require.NoError(t, os.RemoveAll(fetcher.tempDir))
readAsset(uiMsg, false)
readAsset(uiMsg, true)
assets := utils.AssetsForMessage(tc.Context(), tv.Messages[0].Valid().MessageBody)
require.Len(t, assets, 1)
found, localPath, err := fetcher.localAssetPath(context.TODO(), assets[0])
require.NoError(t, err)
require.True(t, found)
_, err = os.Stat(localPath)
require.False(t, os.IsNotExist(err))
err = fetcher.DeleteAssets(context.TODO(), conv.Id, assets,
func() chat1.RemoteInterface { return mockSigningRemote{} }, mockSigningRemote{})
require.NoError(t, err)
// make sure we have purged the attachment from disk as well
_, err = os.Stat(localPath)
require.True(t, os.IsNotExist(err))
}
func TestChatSrvAttachmentUploadPreviewCached(t *testing.T) {
ctc := makeChatTestContext(t, "TestChatSrvAttachmentUploadPreviewCached", 1)
defer ctc.cleanup()
users := ctc.users()
defer func() {
useRemoteMock = true
}()
useRemoteMock = false
tc := ctc.world.Tcs[users[0].Username]
store := attachments.NewStoreTesting(tc.Context(), nil)
fetcher := NewCachingAttachmentFetcher(tc.Context(), store, 5)
ri := ctc.as(t, users[0]).ri
d, err := libkb.RandHexString("", 8)
require.NoError(t, err)
fetcher.tempDir = filepath.Join(os.TempDir(), d)
conv := mustCreateConversationForTest(t, ctc, users[0], chat1.TopicType_CHAT,
chat1.ConversationMembersType_IMPTEAMNATIVE)
tc.ChatG.AttachmentURLSrv = NewAttachmentHTTPSrv(tc.Context(),
manager.NewSrv(tc.Context().ExternalG()),
fetcher, func() chat1.RemoteInterface { return mockSigningRemote{} })
uploader := attachments.NewUploader(tc.Context(), store, mockSigningRemote{},
func() chat1.RemoteInterface { return ri }, 1)
uploader.SetPreviewTempDir(fetcher.tempDir)
tc.ChatG.AttachmentUploader = uploader
res, err := ctc.as(t, users[0]).chatLocalHandler().PostFileAttachmentLocal(context.TODO(),
chat1.PostFileAttachmentLocalArg{
Arg: chat1.PostFileAttachmentArg{
ConversationID: conv.Id,
TlfName: conv.TlfName,
Visibility: keybase1.TLFVisibility_PRIVATE,
Filename: "testdata/ship.jpg",
Title: "SHIP",
},
})
require.NoError(t, err)
msgRes, err := ctc.as(t, users[0]).chatLocalHandler().GetMessagesLocal(context.TODO(),
chat1.GetMessagesLocalArg{
ConversationID: conv.Id,
MessageIDs: []chat1.MessageID{res.MessageID},
})
require.NoError(t, err)
require.Equal(t, 1, len(msgRes.Messages))
require.True(t, msgRes.Messages[0].IsValid())
body := msgRes.Messages[0].Valid().MessageBody
require.NotNil(t, body.Attachment().Preview)
t.Logf("remote preview path: %s", body.Attachment().Preview.Path)
t.Logf("remote object path: %s", body.Attachment().Object.Path)
found, path, err := fetcher.localAssetPath(context.TODO(), *body.Attachment().Preview)
require.NoError(t, err)
require.True(t, found)
_, err = os.Stat(path)
require.NoError(t, err)
t.Logf("found path: %s", path)
found, path, err = fetcher.localAssetPath(context.TODO(), body.Attachment().Object)
require.NoError(t, err)
require.True(t, found)
_, err = os.Stat(path)
require.NoError(t, err)
t.Logf("found path: %s", path)
// Try with an attachment with no preview
res, err = ctc.as(t, users[0]).chatLocalHandler().PostFileAttachmentLocal(context.TODO(),
chat1.PostFileAttachmentLocalArg{
Arg: chat1.PostFileAttachmentArg{
ConversationID: conv.Id,
TlfName: conv.TlfName,
Visibility: keybase1.TLFVisibility_PRIVATE,
Filename: "testdata/weather.pdf",
Title: "WEATHER",
},
})
require.NoError(t, err)
msgRes, err = ctc.as(t, users[0]).chatLocalHandler().GetMessagesLocal(context.TODO(),
chat1.GetMessagesLocalArg{
ConversationID: conv.Id,
MessageIDs: []chat1.MessageID{res.MessageID},
})
require.NoError(t, err)
require.Equal(t, 1, len(msgRes.Messages))
require.True(t, msgRes.Messages[0].IsValid())
body = msgRes.Messages[0].Valid().MessageBody
require.Nil(t, body.Attachment().Preview)
found, _, err = fetcher.localAssetPath(context.TODO(), body.Attachment().Object)
require.NoError(t, err)
require.False(t, found)
// No preview is available, but the file is still on disk.
_, err = os.Stat(path)
require.NoError(t, err)
}