2015-02-11 02:25:40 +01:00
|
|
|
package handlers
|
2015-01-05 23:25:28 +01:00
|
|
|
|
2015-01-08 23:59:15 +01:00
|
|
|
import "testing"
|
2015-01-05 23:25:28 +01:00
|
|
|
|
2015-01-08 23:59:15 +01:00
|
|
|
var layerUploadStates = []layerUploadState{
|
2015-01-05 23:25:28 +01:00
|
|
|
{
|
|
|
|
Name: "hello",
|
|
|
|
UUID: "abcd-1234-qwer-0987",
|
|
|
|
Offset: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "hello-world",
|
|
|
|
UUID: "abcd-1234-qwer-0987",
|
|
|
|
Offset: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "h3ll0_w0rld",
|
|
|
|
UUID: "abcd-1234-qwer-0987",
|
|
|
|
Offset: 1337,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "ABCDEFG",
|
|
|
|
UUID: "ABCD-1234-QWER-0987",
|
|
|
|
Offset: 1234567890,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "this-is-A-sort-of-Long-name-for-Testing",
|
|
|
|
UUID: "dead-1234-beef-0987",
|
|
|
|
Offset: 8675309,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var secrets = []string{
|
|
|
|
"supersecret",
|
|
|
|
"12345",
|
|
|
|
"a",
|
|
|
|
"SuperSecret",
|
|
|
|
"Sup3r... S3cr3t!",
|
|
|
|
"This is a reasonably long secret key that is used for the purpose of testing.",
|
|
|
|
"\u2603+\u2744", // snowman+snowflake
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestLayerUploadTokens constructs stateTokens from LayerUploadStates and
|
|
|
|
// validates that the tokens can be used to reconstruct the proper upload state.
|
|
|
|
func TestLayerUploadTokens(t *testing.T) {
|
2015-01-08 23:59:15 +01:00
|
|
|
secret := hmacKey("supersecret")
|
2015-01-05 23:25:28 +01:00
|
|
|
|
|
|
|
for _, testcase := range layerUploadStates {
|
2015-01-08 23:59:15 +01:00
|
|
|
token, err := secret.packUploadState(testcase)
|
2015-01-05 23:25:28 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-01-08 23:59:15 +01:00
|
|
|
lus, err := secret.unpackUploadState(token)
|
2015-01-05 23:25:28 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertLayerUploadStateEquals(t, testcase, lus)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestHMACValidate ensures that any HMAC token providers are compatible if and
|
|
|
|
// only if they share the same secret.
|
|
|
|
func TestHMACValidation(t *testing.T) {
|
|
|
|
for _, secret := range secrets {
|
2015-01-08 23:59:15 +01:00
|
|
|
secret1 := hmacKey(secret)
|
|
|
|
secret2 := hmacKey(secret)
|
|
|
|
badSecret := hmacKey("DifferentSecret")
|
2015-01-05 23:25:28 +01:00
|
|
|
|
|
|
|
for _, testcase := range layerUploadStates {
|
2015-01-08 23:59:15 +01:00
|
|
|
token, err := secret1.packUploadState(testcase)
|
2015-01-05 23:25:28 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-01-08 23:59:15 +01:00
|
|
|
lus, err := secret2.unpackUploadState(token)
|
2015-01-05 23:25:28 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertLayerUploadStateEquals(t, testcase, lus)
|
|
|
|
|
2015-01-08 23:59:15 +01:00
|
|
|
_, err = badSecret.unpackUploadState(token)
|
2015-01-05 23:25:28 +01:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected token provider to fail at retrieving state from token: %s", token)
|
|
|
|
}
|
|
|
|
|
2015-01-08 23:59:15 +01:00
|
|
|
badToken, err := badSecret.packUploadState(lus)
|
2015-01-05 23:25:28 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-01-08 23:59:15 +01:00
|
|
|
_, err = secret1.unpackUploadState(badToken)
|
2015-01-05 23:25:28 +01:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected token provider to fail at retrieving state from token: %s", badToken)
|
|
|
|
}
|
|
|
|
|
2015-01-08 23:59:15 +01:00
|
|
|
_, err = secret2.unpackUploadState(badToken)
|
2015-01-05 23:25:28 +01:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected token provider to fail at retrieving state from token: %s", badToken)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-08 23:59:15 +01:00
|
|
|
func assertLayerUploadStateEquals(t *testing.T, expected layerUploadState, received layerUploadState) {
|
2015-01-05 23:25:28 +01:00
|
|
|
if expected.Name != received.Name {
|
|
|
|
t.Fatalf("Expected Name=%q, Received Name=%q", expected.Name, received.Name)
|
|
|
|
}
|
|
|
|
if expected.UUID != received.UUID {
|
|
|
|
t.Fatalf("Expected UUID=%q, Received UUID=%q", expected.UUID, received.UUID)
|
|
|
|
}
|
|
|
|
if expected.Offset != received.Offset {
|
|
|
|
t.Fatalf("Expected Offset=%d, Received Offset=%d", expected.Offset, received.Offset)
|
|
|
|
}
|
|
|
|
}
|