| 1 |
package fakeca |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/ecdsa" |
| 6 |
"crypto/elliptic" |
| 7 |
"crypto/rand" |
| 8 |
"crypto/x509" |
| 9 |
"crypto/x509/pkix" |
| 10 |
"reflect" |
| 11 |
"testing" |
| 12 |
) |
| 13 |
|
| 14 |
func TestDefaults(t *testing.T) { |
| 15 |
assertNoPanic(t, func() { |
| 16 |
root := New(IsCA) |
| 17 |
|
| 18 |
if err := root.Certificate.CheckSignatureFrom(root.Certificate); err != nil { |
| 19 |
t.Fatal(err) |
| 20 |
} |
| 21 |
}) |
| 22 |
} |
| 23 |
|
| 24 |
func TestIntermediate(t *testing.T) { |
| 25 |
assertNoPanic(t, func() { |
| 26 |
New().Issue() |
| 27 |
}) |
| 28 |
} |
| 29 |
|
| 30 |
func TestSubject(t *testing.T) { |
| 31 |
assertNoPanic(t, func() { |
| 32 |
var ( |
| 33 |
expected = "foobar" |
| 34 |
root = New(Subject(pkix.Name{CommonName: expected})) |
| 35 |
actual = root.Certificate.Subject.CommonName |
| 36 |
) |
| 37 |
|
| 38 |
if actual != expected { |
| 39 |
t.Fatalf("bad subject. expected '%s', got '%s'", expected, actual) |
| 40 |
} |
| 41 |
}) |
| 42 |
} |
| 43 |
|
| 44 |
func TestNextSerialNumber(t *testing.T) { |
| 45 |
assertNoPanic(t, func() { |
| 46 |
var ( |
| 47 |
expected = int64(123) |
| 48 |
ca = New(NextSerialNumber(expected)).Issue() |
| 49 |
actual = ca.Certificate.SerialNumber.Int64() |
| 50 |
) |
| 51 |
|
| 52 |
if actual != expected { |
| 53 |
t.Fatalf("bad sn. expected '%d', got '%d'", expected, actual) |
| 54 |
} |
| 55 |
}) |
| 56 |
} |
| 57 |
|
| 58 |
func TestPrivateKey(t *testing.T) { |
| 59 |
assertNoPanic(t, func() { |
| 60 |
var ( |
| 61 |
expected, _ = ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 62 |
ca = New(PrivateKey(expected)) |
| 63 |
actual = ca.PrivateKey.(*ecdsa.PrivateKey) |
| 64 |
) |
| 65 |
|
| 66 |
if actual.D.Cmp(expected.D) != 0 { |
| 67 |
t.Fatalf("bad D. expected '%s', got '%s'", expected.D.String(), actual.D.String()) |
| 68 |
} |
| 69 |
|
| 70 |
if actual.X.Cmp(expected.X) != 0 { |
| 71 |
t.Fatalf("bad X. expected '%s', got '%s'", expected.X.String(), actual.X.String()) |
| 72 |
} |
| 73 |
|
| 74 |
if actual.Y.Cmp(expected.Y) != 0 { |
| 75 |
t.Fatalf("bad Y. expected '%s', got '%s'", expected.Y.String(), actual.Y.String()) |
| 76 |
} |
| 77 |
}) |
| 78 |
} |
| 79 |
|
| 80 |
func TestIssuer(t *testing.T) { |
| 81 |
assertNoPanic(t, func() { |
| 82 |
var ( |
| 83 |
root = New(IsCA) |
| 84 |
inter = New(Issuer(root)) |
| 85 |
|
| 86 |
expected = root.Certificate.RawSubject |
| 87 |
actual = inter.Certificate.RawIssuer |
| 88 |
) |
| 89 |
|
| 90 |
if !bytes.Equal(actual, expected) { |
| 91 |
t.Fatalf("bad issuer. expected '%s', got '%s'", string(expected), string(actual)) |
| 92 |
} |
| 93 |
|
| 94 |
if err := inter.Certificate.CheckSignatureFrom(root.Certificate); err != nil { |
| 95 |
t.Fatal(err) |
| 96 |
} |
| 97 |
}) |
| 98 |
} |
| 99 |
|
| 100 |
func TestIsCA(t *testing.T) { |
| 101 |
var ( |
| 102 |
normal = New() |
| 103 |
ca = New(IsCA) |
| 104 |
) |
| 105 |
|
| 106 |
if normal.Certificate.IsCA { |
| 107 |
t.Fatal("expected normal cert not to be CA") |
| 108 |
} |
| 109 |
|
| 110 |
if !ca.Certificate.IsCA { |
| 111 |
t.Fatal("expected CA cert to be CA") |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
func TestChain(t *testing.T) { |
| 116 |
var ( |
| 117 |
ca = New(IsCA) |
| 118 |
inter = ca.Issue(IsCA) |
| 119 |
leaf = inter.Issue() |
| 120 |
) |
| 121 |
|
| 122 |
if !leaf.Chain()[0].Equal(leaf.Certificate) { |
| 123 |
t.Fatal() |
| 124 |
} |
| 125 |
|
| 126 |
if !leaf.Chain()[1].Equal(inter.Certificate) { |
| 127 |
t.Fatal() |
| 128 |
} |
| 129 |
|
| 130 |
if !leaf.Chain()[2].Equal(ca.Certificate) { |
| 131 |
t.Fatal() |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
func TestChainPool(t *testing.T) { |
| 136 |
var ( |
| 137 |
ca = New(IsCA) |
| 138 |
inter = ca.Issue(IsCA) |
| 139 |
leaf = inter.Issue() |
| 140 |
) |
| 141 |
|
| 142 |
_, err := leaf.Certificate.Verify(x509.VerifyOptions{ |
| 143 |
Roots: ca.ChainPool(), |
| 144 |
Intermediates: leaf.ChainPool(), |
| 145 |
}) |
| 146 |
|
| 147 |
if err != nil { |
| 148 |
t.Fatal(err) |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
func TestPFX(t *testing.T) { |
| 153 |
assertNoPanic(t, func() { |
| 154 |
New().PFX("asdf") |
| 155 |
}) |
| 156 |
} |
| 157 |
|
| 158 |
func TestAIA(t *testing.T) { |
| 159 |
i := New(IssuingCertificateURL("a", "b"), OCSPServer("c", "d")) |
| 160 |
|
| 161 |
if !reflect.DeepEqual(i.Certificate.IssuingCertificateURL, []string{"a", "b"}) { |
| 162 |
t.Error("bad IssuingCertificateURL: ", i.Certificate.IssuingCertificateURL) |
| 163 |
} |
| 164 |
|
| 165 |
if !reflect.DeepEqual(i.Certificate.OCSPServer, []string{"c", "d"}) { |
| 166 |
t.Error("bad OCSPServer: ", i.Certificate.OCSPServer) |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
func assertNoPanic(t *testing.T, cb func()) { |
| 171 |
|
| 172 |
if h, ok := interface{}(t).(interface{ Helper() }); ok { |
| 173 |
h.Helper() |
| 174 |
} |
| 175 |
|
| 176 |
defer func() { |
| 177 |
if r := recover(); r != nil { |
| 178 |
t.Fatal(r) |
| 179 |
} |
| 180 |
}() |
| 181 |
|
| 182 |
cb() |
| 183 |
} |
| 184 |
|
| 185 |
func TestKeyUsage(t *testing.T) { |
| 186 |
root := New(IsCA, KeyUsage(x509.KeyUsageCertSign)) |
| 187 |
if root.Certificate.KeyUsage != x509.KeyUsageCertSign { |
| 188 |
t.Fatalf("expected %x, got %d", x509.KeyUsageCertSign, root.Certificate.KeyUsage) |
| 189 |
} |
| 190 |
|
| 191 |
leaf := root.Issue(KeyUsage(x509.KeyUsageDataEncipherment | x509.KeyUsageDigitalSignature)) |
| 192 |
if leaf.Certificate.KeyUsage != x509.KeyUsageDataEncipherment|x509.KeyUsageDigitalSignature { |
| 193 |
t.Fatalf("expected %x, got %d", x509.KeyUsageDataEncipherment|x509.KeyUsageDigitalSignature, leaf.Certificate.KeyUsage) |
| 194 |
} |
| 195 |
} |