print_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package main
  2. import (
  3. "bytes"
  4. "github.com/stretchr/testify/assert"
  5. "io/ioutil"
  6. "os"
  7. "github.com/slackhq/nebula/cert"
  8. "testing"
  9. "time"
  10. )
  11. func Test_printSummary(t *testing.T) {
  12. assert.Equal(t, "print <flags>: prints details about a certificate", printSummary())
  13. }
  14. func Test_printHelp(t *testing.T) {
  15. ob := &bytes.Buffer{}
  16. printHelp(ob)
  17. assert.Equal(
  18. t,
  19. "Usage of "+os.Args[0]+" print <flags>: prints details about a certificate\n"+
  20. " -json\n"+
  21. " \tOptional: outputs certificates in json format\n"+
  22. " -path string\n"+
  23. " \tRequired: path to the certificate\n",
  24. ob.String(),
  25. )
  26. }
  27. func Test_printCert(t *testing.T) {
  28. // Orient our local time and avoid headaches
  29. time.Local = time.UTC
  30. ob := &bytes.Buffer{}
  31. eb := &bytes.Buffer{}
  32. // no path
  33. err := printCert([]string{}, ob, eb)
  34. assert.Equal(t, "", ob.String())
  35. assert.Equal(t, "", eb.String())
  36. assertHelpError(t, err, "-path is required")
  37. // no cert at path
  38. ob.Reset()
  39. eb.Reset()
  40. err = printCert([]string{"-path", "does_not_exist"}, ob, eb)
  41. assert.Equal(t, "", ob.String())
  42. assert.Equal(t, "", eb.String())
  43. assert.EqualError(t, err, "unable to read cert; open does_not_exist: "+NoSuchFileError)
  44. // invalid cert at path
  45. ob.Reset()
  46. eb.Reset()
  47. tf, err := ioutil.TempFile("", "print-cert")
  48. assert.Nil(t, err)
  49. defer os.Remove(tf.Name())
  50. tf.WriteString("-----BEGIN NOPE-----")
  51. err = printCert([]string{"-path", tf.Name()}, ob, eb)
  52. assert.Equal(t, "", ob.String())
  53. assert.Equal(t, "", eb.String())
  54. assert.EqualError(t, err, "error while unmarshaling cert: input did not contain a valid PEM encoded block")
  55. // test multiple certs
  56. ob.Reset()
  57. eb.Reset()
  58. tf.Truncate(0)
  59. tf.Seek(0, 0)
  60. c := cert.NebulaCertificate{
  61. Details: cert.NebulaCertificateDetails{
  62. Name: "test",
  63. Groups: []string{"hi"},
  64. PublicKey: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2},
  65. },
  66. Signature: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2},
  67. }
  68. p, _ := c.MarshalToPEM()
  69. tf.Write(p)
  70. tf.Write(p)
  71. tf.Write(p)
  72. err = printCert([]string{"-path", tf.Name()}, ob, eb)
  73. assert.Nil(t, err)
  74. assert.Equal(
  75. t,
  76. "NebulaCertificate {\n\tDetails {\n\t\tName: test\n\t\tIps: []\n\t\tSubnets: []\n\t\tGroups: [\n\t\t\t\"hi\"\n\t\t]\n\t\tNot before: 0001-01-01 00:00:00 +0000 UTC\n\t\tNot After: 0001-01-01 00:00:00 +0000 UTC\n\t\tIs CA: false\n\t\tIssuer: \n\t\tPublic key: 0102030405060708090001020304050607080900010203040506070809000102\n\t}\n\tFingerprint: cc3492c0e9c48f17547f5987ea807462ebb3451e622590a10bb3763c344c82bd\n\tSignature: 0102030405060708090001020304050607080900010203040506070809000102\n}\nNebulaCertificate {\n\tDetails {\n\t\tName: test\n\t\tIps: []\n\t\tSubnets: []\n\t\tGroups: [\n\t\t\t\"hi\"\n\t\t]\n\t\tNot before: 0001-01-01 00:00:00 +0000 UTC\n\t\tNot After: 0001-01-01 00:00:00 +0000 UTC\n\t\tIs CA: false\n\t\tIssuer: \n\t\tPublic key: 0102030405060708090001020304050607080900010203040506070809000102\n\t}\n\tFingerprint: cc3492c0e9c48f17547f5987ea807462ebb3451e622590a10bb3763c344c82bd\n\tSignature: 0102030405060708090001020304050607080900010203040506070809000102\n}\nNebulaCertificate {\n\tDetails {\n\t\tName: test\n\t\tIps: []\n\t\tSubnets: []\n\t\tGroups: [\n\t\t\t\"hi\"\n\t\t]\n\t\tNot before: 0001-01-01 00:00:00 +0000 UTC\n\t\tNot After: 0001-01-01 00:00:00 +0000 UTC\n\t\tIs CA: false\n\t\tIssuer: \n\t\tPublic key: 0102030405060708090001020304050607080900010203040506070809000102\n\t}\n\tFingerprint: cc3492c0e9c48f17547f5987ea807462ebb3451e622590a10bb3763c344c82bd\n\tSignature: 0102030405060708090001020304050607080900010203040506070809000102\n}\n",
  77. ob.String(),
  78. )
  79. assert.Equal(t, "", eb.String())
  80. // test json
  81. ob.Reset()
  82. eb.Reset()
  83. tf.Truncate(0)
  84. tf.Seek(0, 0)
  85. c = cert.NebulaCertificate{
  86. Details: cert.NebulaCertificateDetails{
  87. Name: "test",
  88. Groups: []string{"hi"},
  89. PublicKey: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2},
  90. },
  91. Signature: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2},
  92. }
  93. p, _ = c.MarshalToPEM()
  94. tf.Write(p)
  95. tf.Write(p)
  96. tf.Write(p)
  97. err = printCert([]string{"-json", "-path", tf.Name()}, ob, eb)
  98. assert.Nil(t, err)
  99. assert.Equal(
  100. t,
  101. "{\"details\":{\"groups\":[\"hi\"],\"ips\":[],\"isCa\":false,\"issuer\":\"\",\"name\":\"test\",\"notAfter\":\"0001-01-01T00:00:00Z\",\"notBefore\":\"0001-01-01T00:00:00Z\",\"publicKey\":\"0102030405060708090001020304050607080900010203040506070809000102\",\"subnets\":[]},\"fingerprint\":\"cc3492c0e9c48f17547f5987ea807462ebb3451e622590a10bb3763c344c82bd\",\"signature\":\"0102030405060708090001020304050607080900010203040506070809000102\"}\n{\"details\":{\"groups\":[\"hi\"],\"ips\":[],\"isCa\":false,\"issuer\":\"\",\"name\":\"test\",\"notAfter\":\"0001-01-01T00:00:00Z\",\"notBefore\":\"0001-01-01T00:00:00Z\",\"publicKey\":\"0102030405060708090001020304050607080900010203040506070809000102\",\"subnets\":[]},\"fingerprint\":\"cc3492c0e9c48f17547f5987ea807462ebb3451e622590a10bb3763c344c82bd\",\"signature\":\"0102030405060708090001020304050607080900010203040506070809000102\"}\n{\"details\":{\"groups\":[\"hi\"],\"ips\":[],\"isCa\":false,\"issuer\":\"\",\"name\":\"test\",\"notAfter\":\"0001-01-01T00:00:00Z\",\"notBefore\":\"0001-01-01T00:00:00Z\",\"publicKey\":\"0102030405060708090001020304050607080900010203040506070809000102\",\"subnets\":[]},\"fingerprint\":\"cc3492c0e9c48f17547f5987ea807462ebb3451e622590a10bb3763c344c82bd\",\"signature\":\"0102030405060708090001020304050607080900010203040506070809000102\"}\n",
  102. ob.String(),
  103. )
  104. assert.Equal(t, "", eb.String())
  105. }