print_test.go 5.8 KB

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