forked from OffchainLabs/nitro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle_cloud_storage_service_test.go
87 lines (70 loc) · 2.26 KB
/
google_cloud_storage_service_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
package das
import (
"bytes"
"context"
"errors"
"testing"
"time"
googlestorage "cloud.google.com/go/storage"
"github.com/ethereum/go-ethereum/common"
"github.com/offchainlabs/nitro/das/dastree"
)
type mockGCSClient struct {
storage map[string][]byte
}
func (c *mockGCSClient) Bucket(name string) *googlestorage.BucketHandle {
return nil
}
func (c *mockGCSClient) Download(ctx context.Context, bucket, objectPrefix string, key common.Hash) ([]byte, error) {
value, ok := c.storage[objectPrefix+EncodeStorageServiceKey(key)]
if !ok {
return nil, ErrNotFound
}
return value, nil
}
func (c *mockGCSClient) Close(ctx context.Context) error {
return nil
}
func (c *mockGCSClient) Upload(ctx context.Context, bucket, objectPrefix string, value []byte) error {
key := objectPrefix + EncodeStorageServiceKey(dastree.Hash(value))
c.storage[key] = value
return nil
}
func NewTestGoogleCloudStorageService(ctx context.Context, googleCloudStorageConfig GoogleCloudStorageServiceConfig) (StorageService, error) {
return &GoogleCloudStorageService{
bucket: googleCloudStorageConfig.Bucket,
objectPrefix: googleCloudStorageConfig.ObjectPrefix,
operator: &mockGCSClient{
storage: make(map[string][]byte),
},
maxRetention: googleCloudStorageConfig.MaxRetention,
}, nil
}
func TestNewGoogleCloudStorageService(t *testing.T) {
ctx := context.Background()
// #nosec G115
expiry := uint64(time.Now().Add(time.Hour).Unix())
googleCloudStorageServiceConfig := DefaultGoogleCloudStorageServiceConfig
googleCloudStorageServiceConfig.Enable = true
googleCloudStorageServiceConfig.MaxRetention = time.Hour * 24
googleCloudService, err := NewTestGoogleCloudStorageService(ctx, googleCloudStorageServiceConfig)
Require(t, err)
val1 := []byte("The first value")
val1CorrectKey := dastree.Hash(val1)
val2IncorrectKey := dastree.Hash(append(val1, 0))
_, err = googleCloudService.GetByHash(ctx, val1CorrectKey)
if !errors.Is(err, ErrNotFound) {
t.Fatal(err)
}
err = googleCloudService.Put(ctx, val1, expiry)
Require(t, err)
_, err = googleCloudService.GetByHash(ctx, val2IncorrectKey)
if !errors.Is(err, ErrNotFound) {
t.Fatal(err)
}
val, err := googleCloudService.GetByHash(ctx, val1CorrectKey)
Require(t, err)
if !bytes.Equal(val, val1) {
t.Fatal(val, val1)
}
}