accesscontrol_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (c) 2018, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package accesscontrol
  20. import (
  21. "encoding/base64"
  22. "encoding/json"
  23. "fmt"
  24. "testing"
  25. "time"
  26. )
  27. func TestAuthorization(t *testing.T) {
  28. correctAccess := "access1"
  29. otherAccess := "access2"
  30. correctSigningKey, correctVerificationKey, err := NewKeyPair(correctAccess)
  31. if err != nil {
  32. t.Fatalf("NewKeyPair failed: %s", err)
  33. }
  34. otherSigningKey, otherVerificationKey, err := NewKeyPair(otherAccess)
  35. if err != nil {
  36. t.Fatalf("NewKeyPair failed: %s", err)
  37. }
  38. invalidSigningKey, _, err := NewKeyPair(correctAccess)
  39. if err != nil {
  40. t.Fatalf("NewKeyPair failed: %s", err)
  41. }
  42. keyRing := &VerificationKeyRing{
  43. Keys: []*VerificationKey{correctVerificationKey, otherVerificationKey},
  44. }
  45. // Test: valid key
  46. err = ValidateSigningKey(correctSigningKey)
  47. if err != nil {
  48. t.Fatalf("ValidateSigningKey failed: %s", err)
  49. }
  50. // Test: invalid key
  51. err = ValidateSigningKey(&SigningKey{})
  52. if err == nil {
  53. t.Fatalf("ValidateSigningKey unexpected success")
  54. }
  55. // Test: valid key ring
  56. err = ValidateVerificationKeyRing(keyRing)
  57. if err != nil {
  58. t.Fatalf("ValidateVerificationKeyRing failed: %s", err)
  59. }
  60. // Test: invalid key ring
  61. invalidKeyRing := &VerificationKeyRing{
  62. Keys: []*VerificationKey{&VerificationKey{}},
  63. }
  64. err = ValidateVerificationKeyRing(invalidKeyRing)
  65. if err == nil {
  66. t.Fatalf("ValidateVerificationKeyRing unexpected success")
  67. }
  68. // Test: valid authorization
  69. id := []byte("0000000000000001")
  70. expires := time.Now().Add(10 * time.Second)
  71. auth, err := IssueAuthorization(correctSigningKey, id, expires)
  72. if err != nil {
  73. t.Fatalf("IssueAuthorization failed: %s", err)
  74. }
  75. fmt.Printf("encoded authorization length: %d\n", len(auth))
  76. verifiedAuth, err := VerifyAuthorization(keyRing, auth)
  77. if err != nil {
  78. t.Fatalf("VerifyAuthorization failed: %s", err)
  79. }
  80. if verifiedAuth.AccessType != correctAccess {
  81. t.Fatalf("unexpected access type: %s", verifiedAuth.AccessType)
  82. }
  83. // Test: expired authorization
  84. expires = time.Now().Add(-10 * time.Second)
  85. auth, err = IssueAuthorization(correctSigningKey, id, expires)
  86. if err != nil {
  87. t.Fatalf("IssueAuthorization failed: %s", err)
  88. }
  89. verifiedAuth, err = VerifyAuthorization(keyRing, auth)
  90. // TODO: check error message?
  91. if err == nil {
  92. t.Fatalf("VerifyAuthorization unexpected success")
  93. }
  94. // Test: authorization signed with key not in key ring
  95. expires = time.Now().Add(10 * time.Second)
  96. auth, err = IssueAuthorization(invalidSigningKey, id, expires)
  97. if err != nil {
  98. t.Fatalf("IssueAuthorization failed: %s", err)
  99. }
  100. verifiedAuth, err = VerifyAuthorization(keyRing, auth)
  101. // TODO: check error message?
  102. if err == nil {
  103. t.Fatalf("VerifyAuthorization unexpected success")
  104. }
  105. // Test: authorization signed with valid key, but hacked access type
  106. expires = time.Now().Add(10 * time.Second)
  107. auth, err = IssueAuthorization(otherSigningKey, id, expires)
  108. if err != nil {
  109. t.Fatalf("IssueAuthorization failed: %s", err)
  110. }
  111. decodedAuth, err := base64.StdEncoding.DecodeString(auth)
  112. if err != nil {
  113. t.Fatalf("DecodeString failed: %s", err)
  114. }
  115. var hackSignedAuth signedAuthorization
  116. err = json.Unmarshal(decodedAuth, &hackSignedAuth)
  117. if err != nil {
  118. t.Fatalf("Unmarshal failed: %s", err)
  119. }
  120. var hackAuth Authorization
  121. err = json.Unmarshal(hackSignedAuth.Authorization, &hackAuth)
  122. if err != nil {
  123. t.Fatalf("Unmarshal failed: %s", err)
  124. }
  125. hackAuth.AccessType = correctAccess
  126. marshaledAuth, err := json.Marshal(hackAuth)
  127. if err != nil {
  128. t.Fatalf("Marshall failed: %s", err)
  129. }
  130. hackSignedAuth.Authorization = marshaledAuth
  131. marshaledSignedAuth, err := json.Marshal(hackSignedAuth)
  132. if err != nil {
  133. t.Fatalf("Marshall failed: %s", err)
  134. }
  135. encodedSignedAuth := base64.StdEncoding.EncodeToString(marshaledSignedAuth)
  136. verifiedAuth, err = VerifyAuthorization(keyRing, encodedSignedAuth)
  137. // TODO: check error message?
  138. if err == nil {
  139. t.Fatalf("VerifyAuthorization unexpected success")
  140. }
  141. }