(t *testing.T)
| 90 | } |
| 91 | |
| 92 | func TestPrivateKeyTextMarshaling(t *testing.T) { |
| 93 | t.Run("Zero", func(t *testing.T) { |
| 94 | // Zero cannot marshal. |
| 95 | _, err := PrivateKey{}.MarshalText() |
| 96 | assert.ErrorContains(t, err, "unknown") |
| 97 | |
| 98 | // Empty cannot unmarshal. |
| 99 | var sink PrivateKey |
| 100 | assert.ErrorContains(t, sink.UnmarshalText(nil), "PEM-encoded") |
| 101 | assert.ErrorContains(t, sink.UnmarshalText([]byte{}), "PEM-encoded") |
| 102 | }) |
| 103 | |
| 104 | root, err := NewRootCertificateAuthority() |
| 105 | assert.NilError(t, err) |
| 106 | |
| 107 | key := root.PrivateKey |
| 108 | txt, err := key.MarshalText() |
| 109 | assert.NilError(t, err) |
| 110 | assert.Assert(t, bytes.HasPrefix(txt, []byte("-----BEGIN EC PRIVATE KEY-----\n")), "got %q", txt) |
| 111 | assert.Assert(t, bytes.HasSuffix(txt, []byte("\n-----END EC PRIVATE KEY-----\n")), "got %q", txt) |
| 112 | |
| 113 | t.Run("RoundTrip", func(t *testing.T) { |
| 114 | var sink PrivateKey |
| 115 | assert.NilError(t, sink.UnmarshalText(txt)) |
| 116 | assert.DeepEqual(t, key, sink) |
| 117 | }) |
| 118 | |
| 119 | t.Run("Bundle", func(t *testing.T) { |
| 120 | other, _ := NewRootCertificateAuthority() |
| 121 | otherText, err := other.PrivateKey.MarshalText() |
| 122 | assert.NilError(t, err) |
| 123 | |
| 124 | bundle := bytes.Join([][]byte{txt, otherText}, nil) |
| 125 | |
| 126 | // Only the first key of a bundle is parsed. |
| 127 | var sink PrivateKey |
| 128 | assert.NilError(t, sink.UnmarshalText(bundle)) |
| 129 | assert.DeepEqual(t, key, sink) |
| 130 | }) |
| 131 | |
| 132 | t.Run("EncodedEmpty", func(t *testing.T) { |
| 133 | txt := []byte("-----BEGIN EC PRIVATE KEY-----\n\n-----END EC PRIVATE KEY-----\n") |
| 134 | |
| 135 | var sink PrivateKey |
| 136 | assert.ErrorContains(t, sink.UnmarshalText(txt), "asn1") |
| 137 | }) |
| 138 | |
| 139 | t.Run("EncodedGarbage", func(t *testing.T) { |
| 140 | txt := []byte("-----BEGIN EC PRIVATE KEY-----\nasdfasdf\n-----END EC PRIVATE KEY-----\n") |
| 141 | |
| 142 | var sink PrivateKey |
| 143 | assert.ErrorContains(t, sink.UnmarshalText(txt), "asn1") |
| 144 | }) |
| 145 | |
| 146 | t.Run("ReadByOpenSSL", func(t *testing.T) { |
| 147 | openssl := require.OpenSSL(t) |
| 148 | dir := t.TempDir() |
| 149 |
nothing calls this directly
no test coverage detected