policy_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package sign
  2. import (
  3. "bytes"
  4. "crypto"
  5. "crypto/rsa"
  6. "crypto/sha1"
  7. "encoding/base64"
  8. "fmt"
  9. "math/rand"
  10. "strings"
  11. "testing"
  12. "time"
  13. )
  14. func TestEpochTimeMarshal(t *testing.T) {
  15. v := AWSEpochTime{time.Now()}
  16. b, err := v.MarshalJSON()
  17. if err != nil {
  18. t.Fatalf("Unexpected error, %#v", err)
  19. }
  20. expected := fmt.Sprintf(`{"AWS:EpochTime":%d}`, v.UTC().Unix())
  21. if string(b) != expected {
  22. t.Errorf("Expected marshaled time to match, expect: %s, actual: %s",
  23. expected, string(b))
  24. }
  25. }
  26. var testCreateResource = []struct {
  27. scheme, u string
  28. expect string
  29. errPrefix string
  30. }{
  31. {
  32. "https", "https://example.com/a?b=1",
  33. "https://example.com/a?b=1", "",
  34. },
  35. {
  36. "http", "http*://example.com/a?b=1",
  37. "http*://example.com/a?b=1", "",
  38. },
  39. {
  40. "rtmp", "https://example.com/a?b=1",
  41. "a?b=1", "",
  42. },
  43. {
  44. "ftp", "ftp://example.com/a?b=1",
  45. "", "invalid URL scheme",
  46. },
  47. }
  48. func TestCreateResource(t *testing.T) {
  49. for i, v := range testCreateResource {
  50. r, err := CreateResource(v.scheme, v.u)
  51. if err != nil {
  52. if v.errPrefix == "" {
  53. t.Errorf("%d, Unexpected error %s", i, err.Error())
  54. continue
  55. }
  56. if !strings.HasPrefix(err.Error(), v.errPrefix) {
  57. t.Errorf("%d, Expected to find prefix\nexpect: %s\nactual: %s", i, v.errPrefix, err.Error())
  58. continue
  59. }
  60. } else if v.errPrefix != "" {
  61. t.Errorf("%d, Expected error %s", i, v.errPrefix)
  62. continue
  63. }
  64. if v.expect != r {
  65. t.Errorf("%d, Expected to find prefix\nexpect: %s\nactual: %s", i, v.expect, r)
  66. }
  67. }
  68. }
  69. var testTime = time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
  70. const expectedJSONPolicy = `{"Statement":[{"Resource":"https://example.com/a","Condition":{"DateLessThan":{"AWS:EpochTime":1257894000}}}]}`
  71. const expectedB64Policy = `eyJTdGF0ZW1lbnQiOlt7IlJlc291cmNlIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9hIiwiQ29uZGl0aW9uIjp7IkRhdGVMZXNzVGhhbiI6eyJBV1M6RXBvY2hUaW1lIjoxMjU3ODk0MDAwfX19XX0=`
  72. func TestEncodePolicy(t *testing.T) {
  73. p := NewCannedPolicy("https://example.com/a", testTime)
  74. b64Policy, jsonPolicy, err := encodePolicy(p)
  75. if err != nil {
  76. t.Fatalf("Unexpected error, %#v", err)
  77. }
  78. if string(jsonPolicy) != expectedJSONPolicy {
  79. t.Errorf("Expected json encoding to match, \nexpect: %s\nactual: %s\n", expectedJSONPolicy, jsonPolicy)
  80. }
  81. if string(b64Policy) != expectedB64Policy {
  82. t.Errorf("Expected b64 encoding to match, \nexpect: %s\nactual: %s\n", expectedB64Policy, b64Policy)
  83. }
  84. }
  85. func TestSignEncodedPolicy(t *testing.T) {
  86. p := NewCannedPolicy("https://example.com/a", testTime)
  87. _, jsonPolicy, err := encodePolicy(p)
  88. if err != nil {
  89. t.Fatalf("Unexpected policy encode error, %#v", err)
  90. }
  91. r := newRandomReader(rand.New(rand.NewSource(1)))
  92. privKey, err := rsa.GenerateKey(r, 1024)
  93. if err != nil {
  94. t.Fatalf("Unexpected priv key error, %#v", err)
  95. }
  96. b64Signature, err := signEncodedPolicy(r, jsonPolicy, privKey)
  97. if err != nil {
  98. t.Fatalf("Unexpected policy sign error, %#v", err)
  99. }
  100. hash := sha1.New()
  101. if _, err := bytes.NewReader(jsonPolicy).WriteTo(hash); err != nil {
  102. t.Fatalf("Unexpected hash error, %#v", err)
  103. }
  104. decodedSig, err := base64.StdEncoding.DecodeString(string(b64Signature))
  105. if err != nil {
  106. t.Fatalf("Unexpected base64 decode signature, %#v", err)
  107. }
  108. if err := rsa.VerifyPKCS1v15(&privKey.PublicKey, crypto.SHA1, hash.Sum(nil), decodedSig); err != nil {
  109. t.Fatalf("Unable to verify signature, %#v", err)
  110. }
  111. }
  112. func TestAWSEscape(t *testing.T) {
  113. expect := "a-b_c~"
  114. actual := []byte("a+b=c/")
  115. awsEscapeEncoded(actual)
  116. if string(actual) != expect {
  117. t.Errorf("expect: %s, actual: %s", expect, string(actual))
  118. }
  119. }