forked from viant/afsc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_test.go
81 lines (68 loc) · 1.79 KB
/
key_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
package gs
import (
"bytes"
"context"
"fmt"
"io"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/viant/afs/option"
"github.com/viant/afs/url"
)
func TestAES256Key_SetHeader(t *testing.T) {
jwtConfig, err := NewTestJwtConfig()
if err != nil {
t.Skip(err)
return
}
ctx := context.Background()
var useCases = []struct {
description string
URL string
location string
data []byte
key string
base64Key string
}{
{
description: "securing data with key",
key: strings.Repeat("xd", 16),
location: "folder/secret1.txt",
URL: fmt.Sprintf("gs://%v/", TestBucket),
data: []byte("this is test"),
},
{
description: "securing data with base64key",
location: "folder/secret2.txt",
URL: fmt.Sprintf("gs://%v/", TestBucket),
data: []byte("this is test"),
base64Key: "eGR4ZHhkeGR4ZHhkeGR4ZHhkeGR4ZHhkeGR4ZHhkeGQ=",
},
}
mgr := New(jwtConfig)
defer func() {
_ = mgr.Delete(ctx, fmt.Sprintf("gs://%v/", TestBucket))
}()
for _, useCase := range useCases {
var key *option.AES256Key
if useCase.key != "" {
key, err = option.NewAES256Key([]byte(useCase.key))
assert.Nil(t, err, useCase.description)
} else {
key, err = option.NewBase64AES256Key(useCase.base64Key)
assert.Nil(t, err, useCase.description)
}
URL := url.Join(useCase.URL, useCase.location)
err := mgr.Upload(ctx, URL, 0644, bytes.NewReader(useCase.data), key)
assert.Nil(t, err, useCase.description)
_, err = mgr.OpenURL(ctx, URL)
assert.NotNil(t, err, useCase.description)
reader, err := mgr.OpenURL(ctx, URL, key)
if !assert.Nil(t, err, useCase.description) {
continue
}
data, err := io.ReadAll(reader)
assert.EqualValues(t, useCase.data, data, useCase.description)
}
}