-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathintegration_test.go
177 lines (151 loc) · 5.3 KB
/
integration_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
package cachecash_test
import (
"context"
"encoding/json"
"fmt"
"testing"
cachecash "github.com/cachecashproject/go-cachecash"
"github.com/cachecashproject/go-cachecash/ccmsg"
"github.com/cachecashproject/go-cachecash/colocationpuzzle"
"github.com/cachecashproject/go-cachecash/common"
"github.com/cachecashproject/go-cachecash/testdatagen"
"github.com/cachecashproject/go-cachecash/util"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"golang.org/x/crypto/ed25519"
)
type IntegrationTestSuite struct {
suite.Suite
}
func TestIntegrationTestSuite(t *testing.T) {
suite.Run(t, new(IntegrationTestSuite))
}
// func (suite *IntegrationTestSuite) SetupTest() {
// t := suite.T()
// }
func (suite *IntegrationTestSuite) TestTransfer() {
t := suite.T()
println("Integration test disabled due to L2Response cast failure bug #122")
if true {
return
}
l := logrus.New()
ctx := context.Background()
scen, err := testdatagen.GenerateTestScenario(l, &testdatagen.TestScenarioParams{
ChunkSize: 128 * 1024,
ObjectSize: 128 * 1024 * 16,
MockUpstream: true,
GenerateObject: true,
})
assert.Nil(t, err)
prov := scen.Publisher
caches := scen.Caches
// Pull information about the object into the publisher's catalog so that no upstream fetches are necessary.
// l.Infof("pulling metadata into publisher catalog: start")
// // scen.Catalog.
// l.Infof("pulling metadata into publisher catalog: done")
l.Infof("pulling data into publisher catalog: start")
for i := 0; i < int(scen.ChunkCount()); i++ {
_, err = scen.Catalog.GetData(ctx, &ccmsg.ContentRequest{
Path: "/foo/bar",
RangeBegin: uint64(i) * scen.Params.ChunkSize,
RangeEnd: uint64(i+1) * scen.Params.ChunkSize,
})
assert.Nil(t, err)
}
l.Infof("pulling data into publisher catalog: done")
// Create a client keypair.
clientPublicKey, _ /* clientPrivateKey */, err := ed25519.GenerateKey(nil)
assert.Nil(t, err)
// Create a client request.
bundleReq := &ccmsg.ContentRequest{
ClientPublicKey: cachecash.PublicKeyMessage(clientPublicKey),
Path: "/foo/bar",
RangeBegin: 0,
RangeEnd: 4,
SequenceNo: 1,
}
// Get a ticket bundle from the publisher.
bundles, err := prov.HandleContentRequest(context.Background(), bundleReq)
assert.Nil(t, err)
for _, bundle := range bundles {
bundleJSON, err := json.Marshal(bundle)
assert.Nil(t, err)
fmt.Printf("ticket bundle:\n%s\n", bundleJSON)
// Exchange request tickets with each cache.
var doubleEncryptedChunks [][]byte
for i, cache := range caches {
msg, err := bundle.BuildClientCacheRequest(bundle.TicketRequest[i])
assert.Nil(t, err)
resp, err := cache.HandleRequest(ctx, msg)
assert.Nil(t, err)
subMsg, ok := resp.Msg.(*ccmsg.ClientCacheResponse_DataResponse)
assert.True(t, ok)
doubleEncryptedChunks = append(doubleEncryptedChunks, subMsg.DataResponse.Data)
}
// Give each cache its L1 ticket; receive the outer session key for that cache's chunk in exchange.
var outerSessionKeys []*ccmsg.BlockKey
for i, cache := range caches {
msg, err := bundle.BuildClientCacheRequest(bundle.TicketL1[i])
assert.Nil(t, err)
resp, err := cache.HandleRequest(ctx, msg)
assert.Nil(t, err)
subMsg, ok := resp.Msg.(*ccmsg.ClientCacheResponse_L1Response)
assert.True(t, ok)
outerSessionKeys = append(outerSessionKeys, subMsg.L1Response.OuterKey)
}
// Decrypt once to reveal singly-encrypted chunks.
var singleEncryptedChunks [][]byte
for i, ciphertext := range doubleEncryptedChunks {
plaintext, err := util.EncryptChunk(
bundle.TicketRequest[i].ChunkIdx,
bundle.Remainder.RequestSequenceNo,
outerSessionKeys[i].Key,
ciphertext)
assert.Nil(t, err)
singleEncryptedChunks = append(singleEncryptedChunks, plaintext)
}
// Solve colocation puzzle.
pi := bundle.Remainder.PuzzleInfo
secret, _, err := colocationpuzzle.Solve(colocationpuzzle.Parameters{
Rounds: pi.Rounds,
StartOffset: uint32(pi.StartOffset),
StartRange: uint32(pi.StartRange),
}, singleEncryptedChunks, pi.Goal)
assert.Nil(t, err)
// Decrypt L2 ticket.
ticketL2, err := common.DecryptTicketL2(ctx, secret, bundle.EncryptedTicketL2)
assert.Nil(t, err)
// Give L2 tickets to caches.
for _, cache := range caches {
msg, err := bundle.BuildClientCacheRequest(&ccmsg.TicketL2Info{
EncryptedTicketL2: bundle.EncryptedTicketL2,
PuzzleSecret: secret,
})
assert.Nil(t, err)
resp, err := cache.HandleRequest(ctx, msg)
assert.Nil(t, err)
subMsg, ok := resp.Msg.(*ccmsg.ClientCacheResponse_L2Response)
assert.True(t, ok)
// TODO: Check that response is successful, at least?
_ = subMsg
}
// Decrypt singly-encrypted chunks to reveal plaintext data.
var plaintextChunks [][]byte
for i, ciphertext := range doubleEncryptedChunks {
plaintext, err := util.EncryptChunk(
bundle.TicketRequest[i].ChunkIdx,
bundle.Remainder.RequestSequenceNo,
ticketL2.InnerSessionKey[i].Key,
ciphertext)
assert.Nil(t, err)
plaintextChunks = append(plaintextChunks, plaintext)
}
// Verify that the plaintext data the client has received matches what the publisher and caches have.
for i, b := range plaintextChunks {
expected := scen.Chunks[bundle.TicketRequest[i].ChunkIdx]
assert.Equal(t, expected, b)
}
}
}