jws_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package bootstrap
  14. import (
  15. "testing"
  16. "github.com/stretchr/testify/assert"
  17. )
  18. const (
  19. content = "Hello from the other side. I must have called a thousand times."
  20. secret = "my voice is my passcode"
  21. id = "joshua"
  22. )
  23. func TestComputeDetachedSig(t *testing.T) {
  24. sig, err := computeDetachedSig(content, id, secret)
  25. assert.NoError(t, err, "Error when computing signature: %v", err)
  26. assert.Equal(
  27. t,
  28. "eyJhbGciOiJIUzI1NiIsImtpZCI6Impvc2h1YSJ9..VShe2taLd-YTrmWuRkcL_8QTNDHYxQIEBsAYYiIj1_8",
  29. sig,
  30. "Wrong signature. Got: %v", sig)
  31. // Try with null content
  32. sig, err = computeDetachedSig("", id, secret)
  33. assert.NoError(t, err, "Error when computing signature: %v", err)
  34. assert.Equal(
  35. t,
  36. "eyJhbGciOiJIUzI1NiIsImtpZCI6Impvc2h1YSJ9..7Ui1ALizW4jXphVUB7xUqC9vLYLL9RZeOFfVLoB7Tgk",
  37. sig,
  38. "Wrong signature. Got: %v", sig)
  39. // Try with no secret
  40. sig, err = computeDetachedSig(content, id, "")
  41. assert.NoError(t, err, "Error when computing signature: %v", err)
  42. assert.Equal(
  43. t,
  44. "eyJhbGciOiJIUzI1NiIsImtpZCI6Impvc2h1YSJ9..UfkqvDGiIFxrMnFseDj9LYJOLNrvjW8aHhF71mvvAs8",
  45. sig,
  46. "Wrong signature. Got: %v", sig)
  47. }
  48. func TestDetachedTokenIsValid(t *testing.T) {
  49. // Valid detached JWS token and valid inputs should succeed
  50. sig := "eyJhbGciOiJIUzI1NiIsImtpZCI6Impvc2h1YSJ9..VShe2taLd-YTrmWuRkcL_8QTNDHYxQIEBsAYYiIj1_8"
  51. assert.True(t, DetachedTokenIsValid(sig, content, id, secret),
  52. "Content %q and token \"%s:%s\" should equal signature: %q", content, id, secret, sig)
  53. // Invalid detached JWS token and valid inputs should fail
  54. sig2 := sig + "foo"
  55. assert.False(t, DetachedTokenIsValid(sig2, content, id, secret),
  56. "Content %q and token \"%s:%s\" should not equal signature: %q", content, id, secret, sig)
  57. }