(t *testing.T)
| 10 | ) |
| 11 | |
| 12 | func TestCertAuthority(t *testing.T) { |
| 13 | ca, err := newCertAuthority() |
| 14 | require.NoError(t, err) |
| 15 | |
| 16 | block, _ := pem.Decode(ca.certPEM) |
| 17 | require.NotNil(t, block, "the authority is written as PEM for SSL_CERT_FILE") |
| 18 | root, err := x509.ParseCertificate(block.Bytes) |
| 19 | require.NoError(t, err) |
| 20 | assert.True(t, root.IsCA) |
| 21 | roots := x509.NewCertPool() |
| 22 | roots.AddCert(root) |
| 23 | |
| 24 | tests := []struct { |
| 25 | name string |
| 26 | host string |
| 27 | verify string |
| 28 | wantErr bool |
| 29 | }{ |
| 30 | {name: "the API host", host: "api.github.com", verify: "api.github.com"}, |
| 31 | {name: "another host", host: "uploads.github.com", verify: "uploads.github.com"}, |
| 32 | {name: "host names are case-insensitive", host: "API.GitHub.com", verify: "api.github.com"}, |
| 33 | {name: "an IP address", host: "127.0.0.1", verify: "127.0.0.1"}, |
| 34 | {name: "a certificate is only for its host", host: "api.github.com", verify: "github.com", wantErr: true}, |
| 35 | } |
| 36 | for _, tt := range tests { |
| 37 | t.Run(tt.name, func(t *testing.T) { |
| 38 | c, err := ca.leaf(tt.host) |
| 39 | require.NoError(t, err) |
| 40 | leaf, err := x509.ParseCertificate(c.Certificate[0]) |
| 41 | require.NoError(t, err) |
| 42 | |
| 43 | _, err = leaf.Verify(x509.VerifyOptions{DNSName: tt.verify, Roots: roots}) |
| 44 | if tt.wantErr { |
| 45 | assert.Error(t, err) |
| 46 | return |
| 47 | } |
| 48 | assert.NoError(t, err) |
| 49 | |
| 50 | again, err := ca.leaf(tt.host) |
| 51 | require.NoError(t, err) |
| 52 | assert.Same(t, c, again, "each host's certificate is made once") |
| 53 | }) |
| 54 | } |
| 55 | } |
nothing calls this directly
no test coverage detected