policies_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. Copyright 2019 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 authority
  14. import (
  15. "crypto/x509"
  16. "fmt"
  17. "reflect"
  18. "testing"
  19. capi "k8s.io/api/certificates/v1beta1"
  20. )
  21. func TestKeyUsagesFromStrings(t *testing.T) {
  22. testcases := []struct {
  23. usages []capi.KeyUsage
  24. expectedKeyUsage x509.KeyUsage
  25. expectedExtKeyUsage []x509.ExtKeyUsage
  26. expectErr bool
  27. }{
  28. {
  29. usages: []capi.KeyUsage{"signing"},
  30. expectedKeyUsage: x509.KeyUsageDigitalSignature,
  31. expectedExtKeyUsage: nil,
  32. expectErr: false,
  33. },
  34. {
  35. usages: []capi.KeyUsage{"client auth"},
  36. expectedKeyUsage: 0,
  37. expectedExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
  38. expectErr: false,
  39. },
  40. {
  41. usages: []capi.KeyUsage{"client auth", "client auth"},
  42. expectedKeyUsage: 0,
  43. expectedExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
  44. expectErr: false,
  45. },
  46. {
  47. usages: []capi.KeyUsage{"cert sign", "encipher only"},
  48. expectedKeyUsage: x509.KeyUsageCertSign | x509.KeyUsageEncipherOnly,
  49. expectedExtKeyUsage: nil,
  50. expectErr: false,
  51. },
  52. {
  53. usages: []capi.KeyUsage{"ocsp signing", "crl sign", "s/mime", "content commitment"},
  54. expectedKeyUsage: x509.KeyUsageCRLSign | x509.KeyUsageContentCommitment,
  55. expectedExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageEmailProtection, x509.ExtKeyUsageOCSPSigning},
  56. expectErr: false,
  57. },
  58. {
  59. usages: []capi.KeyUsage{"unsupported string"},
  60. expectedKeyUsage: 0,
  61. expectedExtKeyUsage: nil,
  62. expectErr: true,
  63. },
  64. }
  65. for _, tc := range testcases {
  66. t.Run(fmt.Sprint(tc.usages), func(t *testing.T) {
  67. ku, eku, err := keyUsagesFromStrings(tc.usages)
  68. if tc.expectErr {
  69. if err == nil {
  70. t.Errorf("did not return an error, but expected one")
  71. }
  72. return
  73. }
  74. if err != nil {
  75. t.Errorf("unexpected error: %v", err)
  76. }
  77. if ku != tc.expectedKeyUsage || !reflect.DeepEqual(eku, tc.expectedExtKeyUsage) {
  78. t.Errorf("got=(%v, %v), want=(%v, %v)", ku, eku, tc.expectedKeyUsage, tc.expectedExtKeyUsage)
  79. }
  80. })
  81. }
  82. }