File: parse_test.go

package info (click to toggle)
golang-github-zmap-zcrypto 0.0~git20240512.0fef58d-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,856 kB
  • sloc: python: 567; sh: 124; makefile: 9
file content (82 lines) | stat: -rw-r--r-- 1,839 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package x509

import (
	"encoding/pem"
	"io/ioutil"
	"testing"
)

const testdataPrefix = "testdata/"

func TestDetectSelfSigned(t *testing.T) {
	tests := []struct {
		Filename string
		Expected bool
	}{
		{
			Filename: "self-signed.pem",
			Expected: true,
		},
		{
			Filename: "self-signed-invalid-sig.pem",
			Expected: false,
		},
		{
			Filename: "self-signed-invalid-name.pem",
			Expected: false,
		},
		{
			Filename: "dadrian.io.pem",
			Expected: false,
		},
		{
			Filename: "self-signed-md5-rsa.pem",
			Expected: true,
		},
	}
	for _, test := range tests {
		path := testdataPrefix + test.Filename
		b, err := ioutil.ReadFile(path)
		if err != nil {
			t.Fatalf("could not open %s: %s", test.Filename, err)
		}
		p, _ := pem.Decode(b)
		if p == nil {
			t.Fatalf("bad pem %s", test.Filename)
		}
		c, err := ParseCertificate(p.Bytes)
		if err != nil {
			t.Fatalf("could not parse %s: %s", test.Filename, err)
		}
		if c.SelfSigned != test.Expected {
			t.Errorf("expected %s to have SelfSigned = %t", test.Filename, test.Expected)
			t.Fail()
		}
	}
}

func TestParseEmailInDN(t *testing.T) {
	const expectedEmail = "ca-winshuttle@dfn.de"
	b, err := ioutil.ReadFile(testdataPrefix + "email-in-subject.pem")
	if err != nil {
		t.Fatalf("could not open file: %s", err)
	}
	p, _ := pem.Decode(b)
	if p == nil {
		t.Fatalf("bad pem")
	}
	c, err := ParseCertificate(p.Bytes)
	if err != nil {
		t.Fatalf("could not parse: %s", err)
	}
	if len(c.Subject.EmailAddress) != 1 {
		t.Error("did not parse email address")
	}
	if email := c.Subject.EmailAddress[0]; email != expectedEmail {
		t.Errorf("mismatched email address, expected %s, got %s", expectedEmail, email)
	}
}