print_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package main
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "os"
  6. "testing"
  7. "time"
  8. "github.com/slackhq/nebula/cert"
  9. "github.com/stretchr/testify/assert"
  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. " -out-qr string\n"+
  23. " \tOptional: output a qr code image (png) of the certificate\n"+
  24. " -path string\n"+
  25. " \tRequired: path to the certificate\n",
  26. ob.String(),
  27. )
  28. }
  29. func Test_printCert(t *testing.T) {
  30. // Orient our local time and avoid headaches
  31. time.Local = time.UTC
  32. ob := &bytes.Buffer{}
  33. eb := &bytes.Buffer{}
  34. // no path
  35. err := printCert([]string{}, ob, eb)
  36. assert.Equal(t, "", ob.String())
  37. assert.Equal(t, "", eb.String())
  38. assertHelpError(t, err, "-path is required")
  39. // no cert at path
  40. ob.Reset()
  41. eb.Reset()
  42. err = printCert([]string{"-path", "does_not_exist"}, ob, eb)
  43. assert.Equal(t, "", ob.String())
  44. assert.Equal(t, "", eb.String())
  45. assert.EqualError(t, err, "unable to read cert; open does_not_exist: "+NoSuchFileError)
  46. // invalid cert at path
  47. ob.Reset()
  48. eb.Reset()
  49. tf, err := ioutil.TempFile("", "print-cert")
  50. assert.Nil(t, err)
  51. defer os.Remove(tf.Name())
  52. tf.WriteString("-----BEGIN NOPE-----")
  53. err = printCert([]string{"-path", tf.Name()}, ob, eb)
  54. assert.Equal(t, "", ob.String())
  55. assert.Equal(t, "", eb.String())
  56. assert.EqualError(t, err, "error while unmarshaling cert: input did not contain a valid PEM encoded block")
  57. // test multiple certs
  58. ob.Reset()
  59. eb.Reset()
  60. tf.Truncate(0)
  61. tf.Seek(0, 0)
  62. c := cert.NebulaCertificate{
  63. Details: cert.NebulaCertificateDetails{
  64. Name: "test",
  65. Groups: []string{"hi"},
  66. 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},
  67. },
  68. 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},
  69. }
  70. p, _ := c.MarshalToPEM()
  71. tf.Write(p)
  72. tf.Write(p)
  73. tf.Write(p)
  74. err = printCert([]string{"-path", tf.Name()}, ob, eb)
  75. assert.Nil(t, err)
  76. assert.Equal(
  77. t,
  78. "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\tCurve: CURVE25519\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\tCurve: CURVE25519\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\tCurve: CURVE25519\n\t}\n\tFingerprint: cc3492c0e9c48f17547f5987ea807462ebb3451e622590a10bb3763c344c82bd\n\tSignature: 0102030405060708090001020304050607080900010203040506070809000102\n}\n",
  79. ob.String(),
  80. )
  81. assert.Equal(t, "", eb.String())
  82. // test json
  83. ob.Reset()
  84. eb.Reset()
  85. tf.Truncate(0)
  86. tf.Seek(0, 0)
  87. c = cert.NebulaCertificate{
  88. Details: cert.NebulaCertificateDetails{
  89. Name: "test",
  90. Groups: []string{"hi"},
  91. 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},
  92. },
  93. 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},
  94. }
  95. p, _ = c.MarshalToPEM()
  96. tf.Write(p)
  97. tf.Write(p)
  98. tf.Write(p)
  99. err = printCert([]string{"-json", "-path", tf.Name()}, ob, eb)
  100. assert.Nil(t, err)
  101. assert.Equal(
  102. t,
  103. "{\"details\":{\"curve\":\"CURVE25519\",\"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\":{\"curve\":\"CURVE25519\",\"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\":{\"curve\":\"CURVE25519\",\"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",
  104. ob.String(),
  105. )
  106. assert.Equal(t, "", eb.String())
  107. }