-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathblip_api_attachment_test.go
672 lines (545 loc) · 27.3 KB
/
blip_api_attachment_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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/*
Copyright 2022-Present Couchbase, Inc.
Use of this software is governed by the Business Source License included in
the file licenses/BSL-Couchbase.txt. As of the Change Date specified in that
file, in accordance with the Business Source License, use of this software will
be governed by the Apache License, Version 2.0, included in the file
licenses/APL2.txt.
*/
package rest
import (
"encoding/base64"
"errors"
"fmt"
"net/http"
"testing"
"github.com/couchbase/gocb/v2"
sgbucket "github.com/couchbase/sg-bucket"
"github.com/couchbase/sync_gateway/base"
"github.com/couchbase/sync_gateway/db"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Test pushing and pulling v2 attachments with v2 client
// 1. Create test client.
// 2. Start continuous push and pull replication in client
// 3. Create doc with attachment in SGW
// 4. Update doc in the test client and keep the same attachment stub.
// 5. Have that update pushed via the continuous replication started in step 2
func TestBlipPushPullV2AttachmentV2Client(t *testing.T) {
base.SetUpTestLogging(t, base.LevelInfo, base.KeyAll)
rtConfig := RestTesterConfig{
DatabaseConfig: &DatabaseConfig{
DbConfig: DbConfig{
DeltaSync: &DeltaSyncConfig{
Enabled: base.Ptr(true),
},
},
},
GuestEnabled: true,
}
btcRunner := NewBlipTesterClientRunner(t)
// given this test is for v2 protocol, skip version vector test
btcRunner.SkipVersionVectorInitialization = true
const docID = "doc1"
btcRunner.Run(func(t *testing.T, SupportedBLIPProtocols []string) {
rt := NewRestTester(t, &rtConfig)
defer rt.Close()
opts := &BlipTesterClientOpts{}
opts.SupportedBLIPProtocols = []string{db.CBMobileReplicationV2.SubprotocolString()}
btc := btcRunner.NewBlipTesterClientOptsWithRT(rt, opts)
defer btc.Close()
btcRunner.StartPull(btc.id)
btcRunner.StartPush(btc.id)
// Create doc revision with attachment on SG.
bodyText := `{"greetings":[{"hi": "alice"}],"_attachments":{"hello.txt":{"data":"aGVsbG8gd29ybGQ="}}}`
version1 := btc.rt.PutDoc(docID, bodyText)
data := btcRunner.WaitForVersion(btc.id, docID, version1)
bodyTextExpected := `{"greetings":[{"hi":"alice"}],"_attachments":{"hello.txt":{"revpos":1,"length":11,"stub":true,"digest":"sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0="}}}`
require.JSONEq(t, bodyTextExpected, string(data))
// Update the replicated doc at client along with keeping the same attachment stub.
bodyText = `{"greetings":[{"hi":"bob"}],"_attachments":{"hello.txt":{"revpos":1,"length":11,"stub":true,"digest":"sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0="}}}`
version2 := btcRunner.AddRev(btc.id, docID, &version1, []byte(bodyText))
rt.WaitForVersion(docID, version2)
respBody := btc.rt.GetDocVersion(docID, version2)
assert.Equal(t, docID, respBody[db.BodyId])
greetings := respBody["greetings"].([]interface{})
assert.Len(t, greetings, 1)
assert.Equal(t, map[string]interface{}{"hi": "bob"}, greetings[0])
attachments, ok := respBody[db.BodyAttachments].(map[string]interface{})
require.True(t, ok)
assert.Len(t, attachments, 1)
hello, ok := attachments["hello.txt"].(map[string]interface{})
require.True(t, ok)
assert.Equal(t, "sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0=", hello["digest"])
assert.Equal(t, float64(11), hello["length"])
assert.Equal(t, float64(1), hello["revpos"])
assert.True(t, hello["stub"].(bool))
assert.Equal(t, int64(1), btc.rt.GetDatabase().DbStats.CBLReplicationPush().AttachmentPushCount.Value())
assert.Equal(t, int64(11), btc.rt.GetDatabase().DbStats.CBLReplicationPush().AttachmentPushBytes.Value())
})
}
// Test pushing and pulling v2 attachments with v3 client
// 1. Create test client.
// 2. Start continuous push and pull replication in client
// 3. Create doc with attachment in SGW
// 4. Update doc in the test client and keep the same attachment stub.
// 5. Have that update pushed via the continuous replication started in step 2
func TestBlipPushPullV2AttachmentV3Client(t *testing.T) {
base.SetUpTestLogging(t, base.LevelInfo, base.KeyAll)
rtConfig := RestTesterConfig{
DatabaseConfig: &DatabaseConfig{
DbConfig: DbConfig{
DeltaSync: &DeltaSyncConfig{
Enabled: base.Ptr(true),
},
},
},
GuestEnabled: true,
}
btcRunner := NewBlipTesterClientRunner(t)
const docID = "doc1"
btcRunner.Run(func(t *testing.T, SupportedBLIPProtocols []string) {
rt := NewRestTester(t, &rtConfig)
defer rt.Close()
opts := &BlipTesterClientOpts{SupportedBLIPProtocols: SupportedBLIPProtocols}
btc := btcRunner.NewBlipTesterClientOptsWithRT(rt, opts)
defer btc.Close()
btcRunner.StartPull(btc.id)
btcRunner.StartPush(btc.id)
// Create doc revision with attachment on SG.
bodyText := `{"greetings":[{"hi": "alice"}],"_attachments":{"hello.txt":{"data":"aGVsbG8gd29ybGQ="}}}`
version1 := btc.rt.PutDoc(docID, bodyText)
data := btcRunner.WaitForVersion(btc.id, docID, version1)
bodyTextExpected := `{"greetings":[{"hi":"alice"}],"_attachments":{"hello.txt":{"revpos":1,"length":11,"stub":true,"digest":"sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0="}}}`
require.JSONEq(t, bodyTextExpected, string(data))
// Update the replicated doc at client along with keeping the same attachment stub.
bodyText = `{"greetings":[{"hi":"bob"}],"_attachments":{"hello.txt":{"revpos":1,"length":11,"stub":true,"digest":"sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0="}}}`
version2 := btcRunner.AddRev(btc.id, docID, &version1, []byte(bodyText))
rt.WaitForVersion(docID, version2)
respBody := btc.rt.GetDocVersion(docID, version2)
assert.Equal(t, docID, respBody[db.BodyId])
greetings := respBody["greetings"].([]interface{})
assert.Len(t, greetings, 1)
assert.Equal(t, map[string]interface{}{"hi": "bob"}, greetings[0])
attachments, ok := respBody[db.BodyAttachments].(map[string]interface{})
require.True(t, ok)
assert.Len(t, attachments, 1)
hello, ok := attachments["hello.txt"].(map[string]interface{})
require.True(t, ok)
assert.Equal(t, "sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0=", hello["digest"])
assert.Equal(t, float64(11), hello["length"])
assert.Equal(t, float64(1), hello["revpos"])
assert.True(t, hello["stub"].(bool))
assert.Equal(t, int64(1), btc.rt.GetDatabase().DbStats.CBLReplicationPush().AttachmentPushCount.Value())
assert.Equal(t, int64(11), btc.rt.GetDatabase().DbStats.CBLReplicationPush().AttachmentPushBytes.Value())
})
}
// TestBlipProveAttachmentV2 ensures that CBL's proveAttachment for deduplication is working correctly even for v2 attachments which aren't de-duped on the server side.
func TestBlipProveAttachmentV2(t *testing.T) {
base.SetUpTestLogging(t, base.LevelInfo, base.KeyAll)
rtConfig := RestTesterConfig{
GuestEnabled: true,
}
const (
doc1ID = "doc1"
doc2ID = "doc2"
)
const (
attachmentName = "hello.txt"
attachmentData = "hello world"
)
var (
attachmentDataB64 = base64.StdEncoding.EncodeToString([]byte(attachmentData))
attachmentDigest = "sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0="
)
btcRunner := NewBlipTesterClientRunner(t)
btcRunner.SkipVersionVectorInitialization = true // v2 protocol test
btcRunner.Run(func(t *testing.T, SupportedBLIPProtocols []string) {
rt := NewRestTester(t, &rtConfig)
defer rt.Close()
btc := btcRunner.NewBlipTesterClientOptsWithRT(rt, &BlipTesterClientOpts{
SupportedBLIPProtocols: []string{db.CBMobileReplicationV2.SubprotocolString()},
})
defer btc.Close()
btcRunner.StartPull(btc.id)
// Create two docs with the same attachment data on SG - v2 attachments intentionally result in two copies,
// CBL will still de-dupe attachments based on digest, so will still try proveAttachmnet for the 2nd.
doc1Body := fmt.Sprintf(`{"greetings":[{"hi": "alice"}],"_attachments":{"%s":{"data":"%s"}}}`, attachmentName, attachmentDataB64)
doc1Version := btc.rt.PutDoc(doc1ID, doc1Body)
data := btcRunner.WaitForVersion(btc.id, doc1ID, doc1Version)
bodyTextExpected := fmt.Sprintf(`{"greetings":[{"hi":"alice"}],"_attachments":{"%s":{"revpos":1,"length":%d,"stub":true,"digest":"%s"}}}`, attachmentName, len(attachmentData), attachmentDigest)
require.JSONEq(t, bodyTextExpected, string(data))
// create doc2 now that we know the client has the attachment
doc2Body := fmt.Sprintf(`{"greetings":[{"howdy": "bob"}],"_attachments":{"%s":{"data":"%s"}}}`, attachmentName, attachmentDataB64)
doc2Version := btc.rt.PutDoc(doc2ID, doc2Body)
data = btcRunner.WaitForVersion(btc.id, doc2ID, doc2Version)
bodyTextExpected = fmt.Sprintf(`{"greetings":[{"howdy":"bob"}],"_attachments":{"%s":{"revpos":1,"length":%d,"stub":true,"digest":"%s"}}}`, attachmentName, len(attachmentData), attachmentDigest)
require.JSONEq(t, bodyTextExpected, string(data))
// use RequireWaitForStat since rev is sent slightly before the stats are incremented
base.RequireWaitForStat(t, btc.rt.GetDatabase().DbStats.CBLReplicationPull().RevSendCount.Value, 2)
assert.Equal(t, int64(0), btc.rt.GetDatabase().DbStats.CBLReplicationPull().RevErrorCount.Value())
assert.Equal(t, int64(1), btc.rt.GetDatabase().DbStats.CBLReplicationPull().AttachmentPullCount.Value())
assert.Equal(t, int64(len(attachmentData)), btc.rt.GetDatabase().DbStats.CBLReplicationPull().AttachmentPullBytes.Value())
})
}
// TestBlipProveAttachmentV2Push ensures that CBL's attachment deduplication is ignored for push replications - resulting in new server-side digests and duplicated attachment data (v2 attachment format).
func TestBlipProveAttachmentV2Push(t *testing.T) {
base.SetUpTestLogging(t, base.LevelInfo, base.KeyAll)
rtConfig := RestTesterConfig{
GuestEnabled: true,
}
const (
doc1ID = "doc1"
doc2ID = "doc2"
)
const (
attachmentName = "hello.txt"
attachmentData = "hello world"
)
var (
attachmentDataB64 = base64.StdEncoding.EncodeToString([]byte(attachmentData))
// attachmentDigest = "sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0="
)
btcRunner := NewBlipTesterClientRunner(t)
btcRunner.SkipVersionVectorInitialization = true // v2 protocol test
btcRunner.Run(func(t *testing.T, SupportedBLIPProtocols []string) {
rt := NewRestTester(t, &rtConfig)
defer rt.Close()
btc := btcRunner.NewBlipTesterClientOptsWithRT(rt, &BlipTesterClientOpts{
SupportedBLIPProtocols: []string{db.CBMobileReplicationV2.SubprotocolString()},
})
defer btc.Close()
btcRunner.StartPush(btc.id)
// Create two docs with the same attachment data on the client - v2 attachments intentionally result in two copies stored on the server, despite the client being able to share the data for both.
doc1Body := fmt.Sprintf(`{"greetings":[{"hi": "alice"}],"_attachments":{"%s":{"data":"%s"}}}`, attachmentName, attachmentDataB64)
doc1Version := btcRunner.AddRev(btc.id, doc1ID, nil, []byte(doc1Body))
btc.rt.WaitForVersion(doc1ID, doc1Version)
// create doc2 now that we know the server has the attachment - SG should still request the attachment data from the client.
doc2Body := fmt.Sprintf(`{"greetings":[{"howdy": "bob"}],"_attachments":{"%s":{"data":"%s"}}}`, attachmentName, attachmentDataB64)
doc2Version := btcRunner.AddRev(btc.id, doc2ID, nil, []byte(doc2Body))
btc.rt.WaitForVersion(doc2ID, doc2Version)
// use RequireWaitForStat since document exists on Server very slightly before the stat is updated
base.RequireWaitForStat(t, btc.rt.GetDatabase().DbStats.CBLReplicationPush().DocPushCount.Value, 2)
assert.Equal(t, int64(0), btc.rt.GetDatabase().DbStats.CBLReplicationPush().DocPushErrorCount.Value())
assert.Equal(t, int64(2), btc.rt.GetDatabase().DbStats.CBLReplicationPush().AttachmentPushCount.Value())
assert.Equal(t, int64(2*len(attachmentData)), btc.rt.GetDatabase().DbStats.CBLReplicationPush().AttachmentPushBytes.Value())
})
}
func TestBlipPushPullNewAttachmentCommonAncestor(t *testing.T) {
base.SetUpTestLogging(t, base.LevelInfo, base.KeyAll)
rtConfig := RestTesterConfig{
GuestEnabled: true,
}
btcRunner := NewBlipTesterClientRunner(t)
const docID = "doc1"
btcRunner.Run(func(t *testing.T, SupportedBLIPProtocols []string) {
rt := NewRestTester(t, &rtConfig)
defer rt.Close()
opts := &BlipTesterClientOpts{SupportedBLIPProtocols: SupportedBLIPProtocols}
btc := btcRunner.NewBlipTesterClientOptsWithRT(rt, opts)
defer btc.Close()
btcRunner.StartPush(btc.id)
docVersion := btcRunner.AddRev(btc.id, docID, nil, []byte(`{"greetings":[{"hi": "alice"}]}`))
docVersion = btcRunner.AddRev(btc.id, docID, &docVersion, []byte(`{"greetings":[{"hi": "bob"}],"_attachments":{"hello.txt":{"data":"aGVsbG8gd29ybGQ="}}}`))
// Wait for the documents to be replicated at SG
rt.WaitForVersion(docID, docVersion)
resp := btc.rt.SendAdminRequest(http.MethodGet, "/{{.keyspace}}/"+docID+"?rev="+docVersion.RevID, "")
assert.Equal(t, http.StatusOK, resp.Code)
// CBL updates the doc w/ two more revisions, 3-abc, 4-abc,
// sent to SG as 4-abc, history:[4-abc,3-abc,2-abc], the attachment has revpos=2
docVersion = btcRunner.AddRev(btc.id, docID, &docVersion, []byte(`{"greetings":[{"hi": "charlie"}],"_attachments":{"hello.txt":{"revpos":2,"length":11,"stub":true,"digest":"sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0="}}}`))
docVersion = btcRunner.AddRev(btc.id, docID, &docVersion, []byte(`{"greetings":[{"hi": "dave"}],"_attachments":{"hello.txt":{"revpos":2,"length":11,"stub":true,"digest":"sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0="}}}`))
// Wait for the document to be replicated at SG
rt.WaitForVersion(docID, docVersion)
resp = btc.rt.SendAdminRequest(http.MethodGet, "/{{.keyspace}}/"+docID+"?rev="+docVersion.RevID, "")
assert.Equal(t, http.StatusOK, resp.Code)
var respBody db.Body
assert.NoError(t, base.JSONUnmarshal(resp.Body.Bytes(), &respBody))
assert.Equal(t, docID, respBody[db.BodyId])
assert.Equal(t, "4-abc", respBody[db.BodyRev])
greetings := respBody["greetings"].([]interface{})
assert.Len(t, greetings, 1)
assert.Equal(t, map[string]interface{}{"hi": "dave"}, greetings[0])
attachments, ok := respBody[db.BodyAttachments].(map[string]interface{})
require.True(t, ok)
assert.Len(t, attachments, 1)
hello, ok := attachments["hello.txt"].(map[string]interface{})
require.True(t, ok)
assert.Equal(t, "sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0=", hello["digest"])
assert.Equal(t, float64(11), hello["length"])
assert.Equal(t, float64(2), hello["revpos"])
assert.True(t, hello["stub"].(bool))
// Check the number of sendProveAttachment/sendGetAttachment calls.
require.NotNil(t, btc.pushReplication.replicationStats)
assert.Equal(t, int64(1), btc.pushReplication.replicationStats.GetAttachment.Value())
assert.Equal(t, int64(0), btc.pushReplication.replicationStats.ProveAttachment.Value())
})
}
func TestBlipPushPullNewAttachmentNoCommonAncestor(t *testing.T) {
rtConfig := RestTesterConfig{
GuestEnabled: true,
}
const docID = "doc1"
btcRunner := NewBlipTesterClientRunner(t)
btcRunner.Run(func(t *testing.T, SupportedBLIPProtocols []string) {
rt := NewRestTester(t, &rtConfig)
defer rt.Close()
opts := &BlipTesterClientOpts{SupportedBLIPProtocols: SupportedBLIPProtocols}
btc := btcRunner.NewBlipTesterClientOptsWithRT(rt, opts)
defer btc.Close()
btcRunner.StartPull(btc.id)
// CBL creates revisions 1-abc, 2-abc, 3-abc, 4-abc on the client, with an attachment associated with rev 2.
// rev tree pruning on the CBL side, so 1-abc no longer exists.
// CBL replicates, sends to client as 4-abc history:[4-abc, 3-abc, 2-abc], attachment has revpos=2
var latestVersion *DocVersion
var firstVersion DocVersion
for i := range 3 {
version := btcRunner.AddRev(btc.id, docID, latestVersion, []byte(fmt.Sprintf(`{"rev": %d, "greetings":[{"hi":"alice"}],"_attachments":{"hello.txt":{"data":"aGVsbG8gd29ybGQ="}}}`, i)))
if latestVersion == nil {
firstVersion = version
}
latestVersion = &version
}
btcRunner.SingleCollection(btc.id).pruneVersion(docID, firstVersion)
bodyText := `{"greetings":[{"hi":"alice"}],"_attachments":{"hello.txt":{"revpos":2,"length":11,"stub":true,"digest":"sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0="}}}`
version4 := btcRunner.AddRev(btc.id, docID, latestVersion, []byte(bodyText))
require.Equal(t, "4-abc", version4.RevID)
btcRunner.StartPushWithOpts(btc.id, BlipTesterPushOptions{Continuous: false})
// Wait for the document to be replicated at SG
rt.WaitForVersion(docID, version4)
resp := btc.rt.SendAdminRequest(http.MethodGet, "/{{.keyspace}}/"+docID+"?rev="+version4.RevID, "")
assert.Equal(t, http.StatusOK, resp.Code)
var respBody db.Body
assert.NoError(t, base.JSONUnmarshal(resp.Body.Bytes(), &respBody))
assert.Equal(t, docID, respBody[db.BodyId])
assert.Equal(t, "4-abc", respBody[db.BodyRev])
greetings := respBody["greetings"].([]interface{})
assert.Len(t, greetings, 1)
assert.Equal(t, map[string]interface{}{"hi": "alice"}, greetings[0])
attachments, ok := respBody[db.BodyAttachments].(map[string]interface{})
require.True(t, ok)
assert.Len(t, attachments, 1)
hello, ok := attachments["hello.txt"].(map[string]interface{})
require.True(t, ok)
assert.Equal(t, "sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0=", hello["digest"])
assert.Equal(t, float64(11), hello["length"])
assert.Equal(t, float64(4), hello["revpos"])
assert.True(t, hello["stub"].(bool))
// Check the number of sendProveAttachment/sendGetAttachment calls.
require.NotNil(t, btc.pushReplication.replicationStats)
assert.Equal(t, int64(1), btc.pushReplication.replicationStats.GetAttachment.Value())
assert.Equal(t, int64(0), btc.pushReplication.replicationStats.ProveAttachment.Value())
})
}
// Test Attachment replication behavior described here: https://github.com/couchbase/couchbase-lite-core/wiki/Replication-Protocol
// - Put attachment via blip
// - Verifies that getAttachment won't return attachment "out of context" of a rev request
// - Get attachment via REST and verifies it returns the correct content
func TestPutAttachmentViaBlipGetViaRest(t *testing.T) {
base.SetUpTestLogging(t, base.LevelInfo, base.KeyHTTP, base.KeySync, base.KeySyncMsg)
// Create blip tester
bt, err := NewBlipTesterFromSpec(t,
BlipTesterSpec{
connectingUsername: "user1",
connectingPassword: "1234",
})
require.NoError(t, err)
defer bt.Close()
attachmentBody := "attach"
digest := db.Sha1DigestKey([]byte(attachmentBody))
input := SendRevWithAttachmentInput{
docId: "doc",
revId: "1-rev1",
attachmentName: "myAttachment",
attachmentLength: len(attachmentBody),
attachmentBody: attachmentBody,
attachmentDigest: digest,
}
bt.SendRevWithAttachment(input)
// Try to fetch the attachment directly via getAttachment, expected to fail w/ 403 error for security reasons
// since it's not in the context of responding to a "rev" request from the peer.
getAttachmentRequest := bt.newRequest()
getAttachmentRequest.SetProfile(db.MessageGetAttachment)
getAttachmentRequest.Properties[db.GetAttachmentDigest] = input.attachmentDigest
getAttachmentRequest.Properties[db.GetAttachmentID] = input.docId
sent := bt.sender.Send(getAttachmentRequest)
if !sent {
panic(fmt.Sprintf("Failed to send request for doc: %v", input.docId))
}
getAttachmentResponse := getAttachmentRequest.Response()
errorCode, hasErrorCode := getAttachmentResponse.Properties["Error-Code"]
assert.Equal(t, "403", errorCode) // "Attachment's doc not being synced"
assert.True(t, hasErrorCode)
// Get the attachment via REST api and make sure it matches the attachment pushed earlier
response := bt.restTester.SendAdminRequest("GET", fmt.Sprintf("/{{.keyspace}}/%s/%s", input.docId, input.attachmentName), ``)
assert.Equal(t, input.attachmentBody, response.Body.String())
}
func TestPutAttachmentViaBlipGetViaBlip(t *testing.T) {
base.SetUpTestLogging(t, base.LevelInfo, base.KeyHTTP, base.KeySync, base.KeySyncMsg)
// Create blip tester
bt, err := NewBlipTesterFromSpec(t, BlipTesterSpec{
connectingUsername: "user1",
connectingPassword: "1234",
connectingUserChannelGrants: []string{"*"}, // All channels
})
require.NoError(t, err)
defer bt.Close()
attachmentBody := "attach"
digest := db.Sha1DigestKey([]byte(attachmentBody))
// Send revision with attachment
input := SendRevWithAttachmentInput{
docId: "doc",
revId: "1-rev1",
attachmentName: "myAttachment",
attachmentLength: len(attachmentBody),
attachmentBody: attachmentBody,
attachmentDigest: digest,
}
sent, _, _ := bt.SendRevWithAttachment(input)
assert.True(t, sent)
// Get all docs and attachment via subChanges request
allDocs, ok := bt.WaitForNumDocsViaChanges(1)
require.True(t, ok)
// make assertions on allDocs -- make sure attachment is present w/ expected body
require.Len(t, allDocs, 1)
retrievedDoc := allDocs[input.docId]
// doc assertions
assert.Equal(t, input.docId, retrievedDoc.ID())
assert.Equal(t, input.revId, retrievedDoc.RevID())
// attachment assertions
attachments, err := retrievedDoc.GetAttachments()
assert.True(t, err == nil)
assert.Len(t, attachments, 1)
retrievedAttachment := attachments[input.attachmentName]
require.NotNil(t, retrievedAttachment)
assert.Equal(t, input.attachmentBody, string(retrievedAttachment.Data))
assert.Equal(t, len(attachmentBody), retrievedAttachment.Length)
assert.Equal(t, retrievedAttachment.Digest, input.attachmentDigest)
}
// TestBlipAttachNameChange tests CBL handling - attachments with changed names are sent as stubs, and not new attachments
func TestBlipAttachNameChange(t *testing.T) {
base.SetUpTestLogging(t, base.LevelInfo, base.KeySync, base.KeySyncMsg, base.KeyWebSocket, base.KeyWebSocketFrame, base.KeyHTTP, base.KeyCRUD)
rtConfig := &RestTesterConfig{
GuestEnabled: true,
}
btcRunner := NewBlipTesterClientRunner(t)
btcRunner.Run(func(t *testing.T, SupportedBLIPProtocols []string) {
rt := NewRestTester(t, rtConfig)
defer rt.Close()
opts := &BlipTesterClientOpts{SupportedBLIPProtocols: SupportedBLIPProtocols}
client1 := btcRunner.NewBlipTesterClientOptsWithRT(rt, opts)
defer client1.Close()
btcRunner.StartPull(client1.id)
btcRunner.StartPush(client1.id)
attachmentA := []byte("attachmentA")
attachmentAData := base64.StdEncoding.EncodeToString(attachmentA)
digest := db.Sha1DigestKey(attachmentA)
// Push initial attachment data
version := btcRunner.AddRev(client1.id, "doc", EmptyDocVersion(), []byte(`{"key":"val","_attachments":{"attachment": {"data":"`+attachmentAData+`"}}}`))
rt.WaitForVersion("doc", version)
// Confirm attachment is in the bucket
attachmentAKey := db.MakeAttachmentKey(2, "doc", digest)
bucketAttachmentA, _, err := client1.rt.GetSingleDataStore().GetRaw(attachmentAKey)
require.NoError(t, err)
require.EqualValues(t, bucketAttachmentA, attachmentA)
// Simulate changing only the attachment name over CBL
// Use revpos 2 to simulate revpos bug in CBL 2.8 - 3.0.0
version = btcRunner.AddRev(client1.id, "doc", &version, []byte(`{"key":"val","_attachments":{"attach":{"revpos":2,"content_type":"","length":11,"stub":true,"digest":"`+digest+`"}}}`))
client1.rt.WaitForVersion("doc", version)
// Check if attachment is still in bucket
bucketAttachmentA, _, err = client1.rt.GetSingleDataStore().GetRaw(attachmentAKey)
assert.NoError(t, err)
assert.Equal(t, bucketAttachmentA, attachmentA)
resp := client1.rt.SendAdminRequest("GET", "/{{.keyspace}}/doc/attach", "")
RequireStatus(t, resp, http.StatusOK)
assert.Equal(t, attachmentA, resp.BodyBytes())
})
}
// TestBlipLegacyAttachNameChange ensures that CBL name changes for legacy attachments are handled correctly
func TestBlipLegacyAttachNameChange(t *testing.T) {
rtConfig := &RestTesterConfig{
GuestEnabled: true,
}
btcRunner := NewBlipTesterClientRunner(t)
btcRunner.Run(func(t *testing.T, SupportedBLIPProtocols []string) {
rt := NewRestTester(t, rtConfig)
defer rt.Close()
opts := &BlipTesterClientOpts{SupportedBLIPProtocols: SupportedBLIPProtocols}
client1 := btcRunner.NewBlipTesterClientOptsWithRT(rt, opts)
defer client1.Close()
// Create document in the bucket with a legacy attachment
docID := "doc"
attBody := []byte(`hi`)
digest := db.Sha1DigestKey(attBody)
attKey := db.MakeAttachmentKey(db.AttVersion1, docID, digest)
// Create a document with legacy attachment.
version1 := CreateDocWithLegacyAttachment(t, client1.rt, docID, rawDocWithAttachmentAndSyncMeta(rt), attKey, attBody)
// Confirm attachment is in the bucket
attachmentAKey := db.MakeAttachmentKey(1, "doc", digest)
bucketAttachmentA, _, err := client1.rt.GetSingleDataStore().GetRaw(attachmentAKey)
require.NoError(t, err)
require.EqualValues(t, bucketAttachmentA, attBody)
btcRunner.StartPull(client1.id)
btcRunner.WaitForVersion(client1.id, docID, version1)
// Simulate changing only the attachment name over CBL
// Use revpos 2 to simulate revpos bug in CBL 2.8 - 3.0.0
version2 := btcRunner.AddRev(client1.id, "doc", &version1, []byte(`{"key":"val","_attachments":{"attach":{"revpos":2,"content_type":"test/plain","length":2,"stub":true,"digest":"`+digest+`"}}}`))
btcRunner.StartPushWithOpts(client1.id, BlipTesterPushOptions{Continuous: false})
client1.rt.WaitForVersion("doc", version2)
resp := client1.rt.SendAdminRequest("GET", "/{{.keyspace}}/doc/attach", "")
RequireStatus(t, resp, http.StatusOK)
assert.Equal(t, attBody, resp.BodyBytes())
})
}
// TestBlipLegacyAttachDocUpdate ensures that CBL updates for documents associated with legacy attachments are handled correctly
func TestBlipLegacyAttachDocUpdate(t *testing.T) {
rtConfig := &RestTesterConfig{
GuestEnabled: true,
}
btcRunner := NewBlipTesterClientRunner(t)
btcRunner.Run(func(t *testing.T, SupportedBLIPProtocols []string) {
rt := NewRestTester(t, rtConfig)
defer rt.Close()
opts := &BlipTesterClientOpts{SupportedBLIPProtocols: SupportedBLIPProtocols}
client1 := btcRunner.NewBlipTesterClientOptsWithRT(rt, opts)
defer client1.Close()
btcRunner.StartPush(client1.id)
// Create document in the bucket with a legacy attachment. Properties here align with rawDocWithAttachmentAndSyncMeta
docID := "doc"
attBody := []byte(`hi`)
digest := db.Sha1DigestKey(attBody)
attKey := db.MakeAttachmentKey(db.AttVersion1, docID, digest)
attName := "hi.txt"
// Create a document with legacy attachment.
version1 := CreateDocWithLegacyAttachment(t, client1.rt, docID, rawDocWithAttachmentAndSyncMeta(rt), attKey, attBody)
attachmentAKey := db.MakeAttachmentKey(1, "doc", digest)
dataStore := client1.rt.GetSingleDataStore()
bucketAttachmentA, _, err := dataStore.GetRaw(attachmentAKey)
require.NoError(t, err)
require.EqualValues(t, bucketAttachmentA, attBody)
btcRunner.StartOneshotPull(client1.id)
btcRunner.WaitForVersion(client1.id, docID, version1)
btcRunner.StartPush(client1.id)
// Update the document, leaving body intact
version2 := btcRunner.AddRev(client1.id, "doc", &version1, []byte(`{"key":"val1","_attachments":{"`+attName+`":{"revpos":2,"content_type":"text/plain","length":2,"stub":true,"digest":"`+digest+`"}}}`))
client1.rt.WaitForVersion("doc", version2)
resp := client1.rt.SendAdminRequest("GET", fmt.Sprintf("/{{.keyspace}}/doc/%s", attName), "")
RequireStatus(t, resp, http.StatusOK)
assert.Equal(t, attBody, resp.BodyBytes())
// Validate that the attachment hasn't been migrated to V2
v1Key := db.MakeAttachmentKey(1, "doc", digest)
v1Body, _, err := dataStore.GetRaw(v1Key)
require.NoError(t, err)
require.EqualValues(t, attBody, v1Body)
v2Key := db.MakeAttachmentKey(2, "doc", digest)
_, _, err = dataStore.GetRaw(v2Key)
require.Error(t, err)
// Confirm correct type of error for both integration test and Walrus
if !errors.Is(err, sgbucket.MissingError{Key: v2Key}) {
var keyValueErr *gocb.KeyValueError
require.True(t, errors.As(err, &keyValueErr))
require.Equal(t, keyValueErr.DocumentID, v2Key)
}
})
}