@ -19,7 +19,7 @@ import (
"testing"
)
var i sRequestedTests = [ ] struct {
var s3I sRequestedTests = [ ] struct {
Header http . Header
Expected bool
} {
@ -30,14 +30,14 @@ var isRequestedTests = []struct {
}
func TestS3IsRequested ( t * testing . T ) {
for i , test := range i sRequestedTests {
for i , test := range s3I sRequestedTests {
if got := S3 . IsRequested ( test . Header ) ; got != test . Expected {
t . Errorf ( "Test %d: Wanted %v but got %v" , i , test . Expected , got )
}
}
}
var p arseTests = [ ] struct {
var s3P arseTests = [ ] struct {
Header http . Header
ExpectedErr error
} {
@ -48,9 +48,257 @@ var parseTests = []struct {
}
func TestS3Parse ( t * testing . T ) {
for i , test := range p arseTests {
for i , test := range s3P arseTests {
if err := S3 . Parse ( test . Header ) ; err != test . ExpectedErr {
t . Errorf ( "Test %d: Wanted '%v' but got '%v'" , i , test . ExpectedErr , err )
}
}
}
var ssecIsRequestedTests = [ ] struct {
Header http . Header
Expected bool
} {
{ Header : http . Header { } , Expected : false } , // 0
{ Header : http . Header { "X-Amz-Server-Side-Encryption-Customer-Algorithm" : [ ] string { "AES256" } } , Expected : true } , // 1
{ Header : http . Header { "X-Amz-Server-Side-Encryption-Customer-Key" : [ ] string { "MzJieXRlc2xvbmdzZWNyZXRrZXltdXN0cHJvdmlkZWQ=" } } , Expected : true } , // 2
{ Header : http . Header { "X-Amz-Server-Side-Encryption-Customer-Key-Md5" : [ ] string { "7PpPLAK26ONlVUGOWlusfg==" } } , Expected : true } , // 3
{
Header : http . Header {
"X-Amz-Server-Side-Encryption-Customer-Algorithm" : [ ] string { "" } ,
"X-Amz-Server-Side-Encryption-Customer-Key" : [ ] string { "" } ,
"X-Amz-Server-Side-Encryption-Customer-Key-Md5" : [ ] string { "" } ,
} ,
Expected : true ,
} , // 4
{
Header : http . Header {
"X-Amz-Server-Side-Encryption-Customer-Algorithm" : [ ] string { "AES256" } ,
"X-Amz-Server-Side-Encryption-Customer-Key" : [ ] string { "MzJieXRlc2xvbmdzZWNyZXRrZXltdXN0cHJvdmlkZWQ=" } ,
"X-Amz-Server-Side-Encryption-Customer-Key-Md5" : [ ] string { "7PpPLAK26ONlVUGOWlusfg==" } ,
} ,
Expected : true ,
} , // 5
{
Header : http . Header {
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Algorithm" : [ ] string { "AES256" } ,
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Key" : [ ] string { "MzJieXRlc2xvbmdzZWNyZXRrZXltdXN0cHJvdmlkZWQ=" } ,
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Key-Md5" : [ ] string { "7PpPLAK26ONlVUGOWlusfg==" } ,
} ,
Expected : false ,
} , // 6
}
func TestSSECIsRequested ( t * testing . T ) {
for i , test := range ssecIsRequestedTests {
if got := SSEC . IsRequested ( test . Header ) ; got != test . Expected {
t . Errorf ( "Test %d: Wanted %v but got %v" , i , test . Expected , got )
}
}
}
var ssecCopyIsRequestedTests = [ ] struct {
Header http . Header
Expected bool
} {
{ Header : http . Header { } , Expected : false } , // 0
{ Header : http . Header { "X-Amz-Copy-Source-Server-Side-Encryption-Customer-Algorithm" : [ ] string { "AES256" } } , Expected : true } , // 1
{ Header : http . Header { "X-Amz-Copy-Source-Server-Side-Encryption-Customer-Key" : [ ] string { "MzJieXRlc2xvbmdzZWNyZXRrZXltdXN0cHJvdmlkZWQ=" } } , Expected : true } , // 2
{ Header : http . Header { "X-Amz-Copy-Source-Server-Side-Encryption-Customer-Key-Md5" : [ ] string { "7PpPLAK26ONlVUGOWlusfg==" } } , Expected : true } , // 3
{
Header : http . Header {
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Algorithm" : [ ] string { "" } ,
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Key" : [ ] string { "" } ,
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Key-Md5" : [ ] string { "" } ,
} ,
Expected : true ,
} , // 4
{
Header : http . Header {
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Algorithm" : [ ] string { "AES256" } ,
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Key" : [ ] string { "MzJieXRlc2xvbmdzZWNyZXRrZXltdXN0cHJvdmlkZWQ=" } ,
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Key-Md5" : [ ] string { "7PpPLAK26ONlVUGOWlusfg==" } ,
} ,
Expected : true ,
} , // 5
{
Header : http . Header {
"X-Amz-Server-Side-Encryption-Customer-Algorithm" : [ ] string { "AES256" } ,
"X-Amz-Server-Side-Encryption-Customer-Key" : [ ] string { "MzJieXRlc2xvbmdzZWNyZXRrZXltdXN0cHJvdmlkZWQ=" } ,
"X-Amz-Server-Side-Encryption-Customer-Key-Md5" : [ ] string { "7PpPLAK26ONlVUGOWlusfg==" } ,
} ,
Expected : false ,
} , // 6
}
func TestSSECopyIsRequested ( t * testing . T ) {
for i , test := range ssecCopyIsRequestedTests {
if got := SSECopy . IsRequested ( test . Header ) ; got != test . Expected {
t . Errorf ( "Test %d: Wanted %v but got %v" , i , test . Expected , got )
}
}
}
var ssecParseTests = [ ] struct {
Header http . Header
ExpectedErr error
} {
{
Header : http . Header {
"X-Amz-Server-Side-Encryption-Customer-Algorithm" : [ ] string { "AES256" } ,
"X-Amz-Server-Side-Encryption-Customer-Key" : [ ] string { "MzJieXRlc2xvbmdzZWNyZXRrZXltdXN0cHJvdmlkZWQ=" } ,
"X-Amz-Server-Side-Encryption-Customer-Key-Md5" : [ ] string { "7PpPLAK26ONlVUGOWlusfg==" } ,
} ,
ExpectedErr : nil , // 0
} ,
{
Header : http . Header {
"X-Amz-Server-Side-Encryption-Customer-Algorithm" : [ ] string { "AES-256" } , // invalid algorithm
"X-Amz-Server-Side-Encryption-Customer-Key" : [ ] string { "MzJieXRlc2xvbmdzZWNyZXRrZXltdXN0cHJvdmlkZWQ=" } ,
"X-Amz-Server-Side-Encryption-Customer-Key-Md5" : [ ] string { "7PpPLAK26ONlVUGOWlusfg==" } ,
} ,
ExpectedErr : ErrInvalidCustomerAlgorithm , // 1
} ,
{
Header : http . Header {
"X-Amz-Server-Side-Encryption-Customer-Algorithm" : [ ] string { "AES256" } ,
"X-Amz-Server-Side-Encryption-Customer-Key" : [ ] string { "" } , // no client key
"X-Amz-Server-Side-Encryption-Customer-Key-Md5" : [ ] string { "7PpPLAK26ONlVUGOWlusfg==" } ,
} ,
ExpectedErr : ErrMissingCustomerKey , // 2
} ,
{
Header : http . Header {
"X-Amz-Server-Side-Encryption-Customer-Algorithm" : [ ] string { "AES256" } ,
"X-Amz-Server-Side-Encryption-Customer-Key" : [ ] string { "MzJieXRlc2xvbmdzZWNyZXRr.ZXltdXN0cHJvdmlkZWQ=" } , // invalid key
"X-Amz-Server-Side-Encryption-Customer-Key-Md5" : [ ] string { "7PpPLAK26ONlVUGOWlusfg==" } ,
} ,
ExpectedErr : ErrInvalidCustomerKey , // 3
} ,
{
Header : http . Header {
"X-Amz-Server-Side-Encryption-Customer-Algorithm" : [ ] string { "AES256" } ,
"X-Amz-Server-Side-Encryption-Customer-Key" : [ ] string { "MzJieXRlc2xvbmdzZWNyZXRrZXltdXN0cHJvdmlkZWQ=" } ,
"X-Amz-Server-Side-Encryption-Customer-Key-Md5" : [ ] string { "" } , // no key MD5
} ,
ExpectedErr : ErrMissingCustomerKeyMD5 , // 4
} ,
{
Header : http . Header {
"X-Amz-Server-Side-Encryption-Customer-Algorithm" : [ ] string { "AES256" } ,
"X-Amz-Server-Side-Encryption-Customer-Key" : [ ] string { "DzJieXRlc2xvbmdzZWNyZXRrZXltdXN0cHJvdmlkZWQ=" } , // wrong client key
"X-Amz-Server-Side-Encryption-Customer-Key-Md5" : [ ] string { "7PpPLAK26ONlVUGOWlusfg==" } ,
} ,
ExpectedErr : ErrCustomerKeyMD5Mismatch , // 5
} ,
{
Header : http . Header {
"X-Amz-Server-Side-Encryption-Customer-Algorithm" : [ ] string { "AES256" } ,
"X-Amz-Server-Side-Encryption-Customer-Key" : [ ] string { "MzJieXRlc2xvbmdzZWNyZXRrZXltdXN0cHJvdmlkZWQ=" } ,
"X-Amz-Server-Side-Encryption-Customer-Key-Md5" : [ ] string { ".7PpPLAK26ONlVUGOWlusfg==" } , // wrong key MD5
} ,
ExpectedErr : ErrCustomerKeyMD5Mismatch , // 6
} ,
}
func TestSSECParse ( t * testing . T ) {
var zeroKey [ 32 ] byte
for i , test := range ssecParseTests {
key , err := SSEC . Parse ( test . Header )
if err != test . ExpectedErr {
t . Errorf ( "Test %d: want error '%v' but got '%v'" , i , test . ExpectedErr , err )
}
if err != nil && key != zeroKey {
t . Errorf ( "Test %d: parsing failed and client key is not zero key" , i )
}
if err == nil && key == zeroKey {
t . Errorf ( "Test %d: parsed client key is zero key" , i )
}
if _ , ok := test . Header [ SSECKey ] ; ok {
t . Errorf ( "Test %d: client key is not removed from HTTP headers after parsing" , i )
}
}
}
var ssecCopyParseTests = [ ] struct {
Header http . Header
ExpectedErr error
} {
{
Header : http . Header {
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Algorithm" : [ ] string { "AES256" } ,
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Key" : [ ] string { "MzJieXRlc2xvbmdzZWNyZXRrZXltdXN0cHJvdmlkZWQ=" } ,
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Key-Md5" : [ ] string { "7PpPLAK26ONlVUGOWlusfg==" } ,
} ,
ExpectedErr : nil , // 0
} ,
{
Header : http . Header {
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Algorithm" : [ ] string { "AES-256" } , // invalid algorithm
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Key" : [ ] string { "MzJieXRlc2xvbmdzZWNyZXRrZXltdXN0cHJvdmlkZWQ=" } ,
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Key-Md5" : [ ] string { "7PpPLAK26ONlVUGOWlusfg==" } ,
} ,
ExpectedErr : ErrInvalidCustomerAlgorithm , // 1
} ,
{
Header : http . Header {
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Algorithm" : [ ] string { "AES256" } ,
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Key" : [ ] string { "" } , // no client key
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Key-Md5" : [ ] string { "7PpPLAK26ONlVUGOWlusfg==" } ,
} ,
ExpectedErr : ErrMissingCustomerKey , // 2
} ,
{
Header : http . Header {
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Algorithm" : [ ] string { "AES256" } ,
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Key" : [ ] string { "MzJieXRlc2xvbmdzZWNyZXRr.ZXltdXN0cHJvdmlkZWQ=" } , // invalid key
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Key-Md5" : [ ] string { "7PpPLAK26ONlVUGOWlusfg==" } ,
} ,
ExpectedErr : ErrInvalidCustomerKey , // 3
} ,
{
Header : http . Header {
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Algorithm" : [ ] string { "AES256" } ,
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Key" : [ ] string { "MzJieXRlc2xvbmdzZWNyZXRrZXltdXN0cHJvdmlkZWQ=" } ,
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Key-Md5" : [ ] string { "" } , // no key MD5
} ,
ExpectedErr : ErrMissingCustomerKeyMD5 , // 4
} ,
{
Header : http . Header {
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Algorithm" : [ ] string { "AES256" } ,
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Key" : [ ] string { "DzJieXRlc2xvbmdzZWNyZXRrZXltdXN0cHJvdmlkZWQ=" } , // wrong client key
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Key-Md5" : [ ] string { "7PpPLAK26ONlVUGOWlusfg==" } ,
} ,
ExpectedErr : ErrCustomerKeyMD5Mismatch , // 5
} ,
{
Header : http . Header {
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Algorithm" : [ ] string { "AES256" } ,
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Key" : [ ] string { "MzJieXRlc2xvbmdzZWNyZXRrZXltdXN0cHJvdmlkZWQ=" } ,
"X-Amz-Copy-Source-Server-Side-Encryption-Customer-Key-Md5" : [ ] string { ".7PpPLAK26ONlVUGOWlusfg==" } , // wrong key MD5
} ,
ExpectedErr : ErrCustomerKeyMD5Mismatch , // 6
} ,
}
func TestSSECopyParse ( t * testing . T ) {
var zeroKey [ 32 ] byte
for i , test := range ssecCopyParseTests {
key , err := SSECopy . Parse ( test . Header )
if err != test . ExpectedErr {
t . Errorf ( "Test %d: want error '%v' but got '%v'" , i , test . ExpectedErr , err )
}
if err != nil && key != zeroKey {
t . Errorf ( "Test %d: parsing failed and client key is not zero key" , i )
}
if err == nil && key == zeroKey {
t . Errorf ( "Test %d: parsed client key is zero key" , i )
}
if _ , ok := test . Header [ SSECKey ] ; ok {
t . Errorf ( "Test %d: client key is not removed from HTTP headers after parsing" , i )
}
}
}