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
|
package s3crypto
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/awstesting"
"github.com/aws/aws-sdk-go/awstesting/unit"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/aws/aws-sdk-go/service/s3"
)
func TestDefaultConfigValues(t *testing.T) {
sess := unit.Session.Copy(&aws.Config{
MaxRetries: aws.Int(0),
S3ForcePathStyle: aws.Bool(true),
Region: aws.String("us-west-2"),
})
svc := kms.New(sess)
handler := NewKMSKeyGenerator(svc, "testid")
c := NewEncryptionClient(sess, AESGCMContentCipherBuilder(handler))
if c == nil {
t.Error("expected non-vil client value")
}
if c.ContentCipherBuilder == nil {
t.Error("expected non-vil content cipher builder value")
}
if c.SaveStrategy == nil {
t.Error("expected non-vil save strategy value")
}
}
func TestPutObject(t *testing.T) {
size := 1024 * 1024
data := make([]byte, size)
expected := bytes.Repeat([]byte{1}, size)
generator := mockGenerator{}
cb := mockCipherBuilder{generator}
sess := unit.Session.Copy(&aws.Config{
MaxRetries: aws.Int(0),
S3ForcePathStyle: aws.Bool(true),
Region: aws.String("us-west-2"),
})
c := NewEncryptionClient(sess, cb)
if c == nil {
t.Error("expected non-vil client value")
}
input := &s3.PutObjectInput{
Key: aws.String("test"),
Bucket: aws.String("test"),
Body: bytes.NewReader(data),
}
req, _ := c.PutObjectRequest(input)
req.Handlers.Send.Clear()
req.Handlers.Send.PushBack(func(r *request.Request) {
r.Error = errors.New("stop")
r.HTTPResponse = &http.Response{
StatusCode: 200,
}
})
err := req.Send()
if e, a := "stop", err.Error(); e != a {
t.Errorf("expected %s error, but received %s", e, a)
}
b, err := ioutil.ReadAll(req.HTTPRequest.Body)
if err != nil {
t.Errorf("expected no error, but received %v", err)
}
if !bytes.Equal(expected, b) {
t.Error("expected bytes to be equivalent, but received otherwise")
}
}
func TestPutObjectWithContext(t *testing.T) {
generator := mockGenerator{}
cb := mockCipherBuilder{generator}
c := NewEncryptionClient(unit.Session, cb)
ctx := &awstesting.FakeContext{DoneCh: make(chan struct{})}
ctx.Error = fmt.Errorf("context canceled")
close(ctx.DoneCh)
input := s3.PutObjectInput{
Bucket: aws.String("test"),
Key: aws.String("test"),
Body: bytes.NewReader([]byte{}),
}
_, err := c.PutObjectWithContext(ctx, &input)
if err == nil {
t.Fatalf("expected error, did not get one")
}
aerr := err.(awserr.Error)
if e, a := request.CanceledErrorCode, aerr.Code(); e != a {
t.Errorf("expected error code %q, got %q", e, a)
}
if e, a := "canceled", aerr.Message(); !strings.Contains(a, e) {
t.Errorf("expected error message to contain %q, but did not %q", e, a)
}
}
func TestEncryptionClient_PutObject_InvalidClientConstruction(t *testing.T) {
generator := mockGeneratorV2{}
cb := mockCipherBuilderV2{generator: generator}
c := NewEncryptionClient(unit.Session, cb)
if c == nil {
t.Errorf("expected client not to be nil")
}
input := s3.PutObjectInput{
Bucket: aws.String("test"),
Key: aws.String("test"),
Body: bytes.NewReader([]byte{}),
}
_, err := c.PutObject(&input)
if err == nil {
t.Fatalf("expected error, did not get one")
}
if e, a := "invalid client configuration", err.Error(); !strings.Contains(a, e) {
t.Errorf("expected %v, got %v", e, a)
}
_, err = c.PutObjectWithContext(aws.BackgroundContext(), &input)
if err == nil {
t.Fatalf("expected error, did not get one")
}
if e, a := "invalid client configuration", err.Error(); !strings.Contains(a, e) {
t.Errorf("expected %v, got %v", e, a)
}
_, err = c.PutObjectWithContext(aws.BackgroundContext(), &input)
if err == nil {
t.Fatalf("expected error, did not get one")
}
if e, a := "invalid client configuration", err.Error(); !strings.Contains(a, e) {
t.Errorf("expected %v, got %v", e, a)
}
req, _ := c.PutObjectRequest(&input)
err = req.Send()
if err == nil {
t.Fatalf("expected error, did not get one")
}
if e, a := "invalid client configuration", err.Error(); !strings.Contains(a, e) {
t.Errorf("expected %v, got %v", e, a)
}
}
|