signer_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package signer
  2. import (
  3. "bytes"
  4. "crypto/x509"
  5. "encoding/asn1"
  6. "encoding/hex"
  7. "fmt"
  8. "reflect"
  9. "testing"
  10. "github.com/cloudflare/cfssl/config"
  11. "github.com/cloudflare/cfssl/csr"
  12. )
  13. func TestAppendIf(t *testing.T) {
  14. s := ""
  15. a := make([]string, 0, 5)
  16. appendIf(s, &a)
  17. if len(a) != 0 {
  18. t.Fatal("appendIf should not append to a with an empty s")
  19. }
  20. s = "test"
  21. appendIf(s, &a)
  22. if len(a[0]) != 4 {
  23. t.Fatal("appendIf should append s to a")
  24. }
  25. }
  26. func TestSplitHosts(t *testing.T) {
  27. list := SplitHosts("")
  28. if list != nil {
  29. t.Fatal("SplitHost should return nil with empty input")
  30. }
  31. list = SplitHosts("single.domain")
  32. if len(list) != 1 {
  33. t.Fatal("SplitHost fails to split single domain")
  34. }
  35. list = SplitHosts("comma,separated,values")
  36. if len(list) != 3 {
  37. t.Fatal("SplitHost fails to split multiple domains")
  38. }
  39. if list[0] != "comma" || list[1] != "separated" || list[2] != "values" {
  40. t.Fatal("SplitHost fails to split multiple domains")
  41. }
  42. }
  43. func TestAddPolicies(t *testing.T) {
  44. var cert x509.Certificate
  45. addPolicies(&cert, []config.CertificatePolicy{
  46. {
  47. ID: config.OID([]int{1, 2, 3, 4}),
  48. },
  49. })
  50. if len(cert.ExtraExtensions) != 1 {
  51. t.Fatal("No extension added")
  52. }
  53. ext := cert.ExtraExtensions[0]
  54. if !reflect.DeepEqual(ext.Id, asn1.ObjectIdentifier{2, 5, 29, 32}) {
  55. t.Fatal(fmt.Sprintf("Wrong OID for policy qualifier %v", ext.Id))
  56. }
  57. if ext.Critical {
  58. t.Fatal("Policy qualifier marked critical")
  59. }
  60. expectedBytes, _ := hex.DecodeString("3007300506032a0304")
  61. if !bytes.Equal(ext.Value, expectedBytes) {
  62. t.Fatal(fmt.Sprintf("Value didn't match expected bytes: got %s, expected %s",
  63. hex.EncodeToString(ext.Value), hex.EncodeToString(expectedBytes)))
  64. }
  65. }
  66. func TestAddPoliciesWithQualifiers(t *testing.T) {
  67. var cert x509.Certificate
  68. addPolicies(&cert, []config.CertificatePolicy{
  69. {
  70. ID: config.OID([]int{1, 2, 3, 4}),
  71. Qualifiers: []config.CertificatePolicyQualifier{
  72. {
  73. Type: "id-qt-cps",
  74. Value: "http://example.com/cps",
  75. },
  76. {
  77. Type: "id-qt-unotice",
  78. Value: "Do What Thou Wilt",
  79. },
  80. },
  81. },
  82. })
  83. if len(cert.ExtraExtensions) != 1 {
  84. t.Fatal("No extension added")
  85. }
  86. ext := cert.ExtraExtensions[0]
  87. if !reflect.DeepEqual(ext.Id, asn1.ObjectIdentifier{2, 5, 29, 32}) {
  88. t.Fatal(fmt.Sprintf("Wrong OID for policy qualifier %v", ext.Id))
  89. }
  90. if ext.Critical {
  91. t.Fatal("Policy qualifier marked critical")
  92. }
  93. expectedBytes, _ := hex.DecodeString("304e304c06032a03043045302206082b060105050702011616687474703a2f2f6578616d706c652e636f6d2f637073301f06082b0601050507020230130c11446f20576861742054686f752057696c74")
  94. if !bytes.Equal(ext.Value, expectedBytes) {
  95. t.Fatal(fmt.Sprintf("Value didn't match expected bytes: %s vs %s",
  96. hex.EncodeToString(ext.Value), hex.EncodeToString(expectedBytes)))
  97. }
  98. }
  99. func TestName(t *testing.T) {
  100. sub := &Subject{
  101. CN: "foobar",
  102. Names: []csr.Name{
  103. {
  104. C: "US",
  105. ST: "CA",
  106. L: "Cool Locality",
  107. O: "Cool Org",
  108. OU: "Really Cool Sub Org",
  109. },
  110. {
  111. L: "Another Cool Locality",
  112. },
  113. },
  114. SerialNumber: "deadbeef",
  115. }
  116. name := sub.Name()
  117. if name.CommonName != sub.CN {
  118. t.Errorf("CommonName: want %#v, got %#v", sub.CN, name.CommonName)
  119. }
  120. if name.SerialNumber != sub.SerialNumber {
  121. t.Errorf("SerialNumber: want %#v, got %#v", sub.SerialNumber, name.SerialNumber)
  122. }
  123. if !reflect.DeepEqual([]string{"US"}, name.Country) {
  124. t.Errorf("Country: want %s, got %s", []string{"US"}, name.Country)
  125. }
  126. if !reflect.DeepEqual([]string{"CA"}, name.Province) {
  127. t.Errorf("Province: want %s, got %s", []string{"CA"}, name.Province)
  128. }
  129. if !reflect.DeepEqual([]string{"Cool Org"}, name.Organization) {
  130. t.Errorf("Organization: want %s, got %s", []string{"Cool Org"}, name.Organization)
  131. }
  132. if !reflect.DeepEqual([]string{"Really Cool Sub Org"}, name.OrganizationalUnit) {
  133. t.Errorf("Organizational Unit: want %s, got %s", []string{"Really Cool Sub Org"}, name.OrganizationalUnit)
  134. }
  135. if !reflect.DeepEqual([]string{"Cool Locality", "Another Cool Locality"}, name.Locality) {
  136. t.Errorf("Locality: want %s, got %s", []string{"CA"}, name.Locality)
  137. }
  138. }