-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcertificate_test.go
114 lines (110 loc) · 2.86 KB
/
certificate_test.go
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package apns
import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
)
func TestCertificate(t *testing.T) {
jsonEnc := json.NewEncoder(os.Stdout)
jsonEnc.SetIndent("", "\t")
for certfile, password := range map[string]string{
"cert.p12": "xopen123",
"cert2.p12": "xopen123",
"cert3.p12": "open321",
"cert4.p12": "xopen123",
"TiTPushDev.p12": "xopen123",
"TiTUniversal.p12": "xopen123",
} {
fmt.Println("File:", certfile)
certificate, err := LoadCertificate(certfile, password)
if certificate == nil && err != nil {
t.Error("Load certificate error:", err)
continue
} else if err != nil {
fmt.Println("Certificate error:", err)
}
certificate.Leaf = nil
info := GetCertificateInfo(certificate)
if info == nil {
t.Error("Bad certificate info")
continue
}
fmt.Println(info)
if err := jsonEnc.Encode(info); err != nil {
t.Error("JSON encode error:", err)
}
if !info.Support(info.BundleID) {
t.Error("Unsupported bundle ID")
}
if info.Support("xxx") {
t.Error("Bad support function")
}
fmt.Println(strings.Repeat("-", 60))
}
}
func TestCertificateNotFound(t *testing.T) {
if _, err := LoadCertificate("notexists.p12", "xopen"); err == nil {
t.Error("Load not existing certificate error")
}
if _, err := LoadCertificate("cert.p12", "xopen"); err == nil {
t.Error("Load not existing certificate error")
}
}
func TestCertificateWithErrors(t *testing.T) {
jsonEnc := json.NewEncoder(os.Stdout)
jsonEnc.SetIndent("", "\t")
for certfile, password := range map[string]string{
"Certificates.p12": "xopen123",
"MessageTrack2Dev.p12": "xopen123",
"MessageTrack2Prod.p12": "xopen123",
"MessageTrackDev.p12": "xopen123",
"MessageTrackProd.p12": "xopen123",
"MyMessagesDev.p12": "xopen123",
"MyMessagesProd.p12": "xopen123",
} {
fmt.Println("File:", certfile)
certificate, err := LoadCertificate(certfile, password)
if certificate == nil && err != nil {
fmt.Println("Load certificate error:", err)
fmt.Println(strings.Repeat("-", 60))
data, err := ioutil.ReadFile(certfile)
if err != nil {
t.Error(err)
continue
}
certificate = &tls.Certificate{
Certificate: [][]byte{data},
PrivateKey: nil,
Leaf: nil,
}
info := GetCertificateInfo(certificate)
if info != nil {
t.Error("Bad info")
}
continue
} else if err != nil {
fmt.Println("Certificate error:", err)
}
certificate.Leaf = nil
info := GetCertificateInfo(certificate)
if info == nil {
t.Error("Bad certificate info")
continue
}
fmt.Println(info)
if err := jsonEnc.Encode(info); err != nil {
t.Error("JSON encode error:", err)
}
if !info.Support(info.BundleID) {
t.Error("Unsupported bundle ID")
}
if info.Support("xxx") {
t.Error("Bad support function")
}
fmt.Println(strings.Repeat("-", 60))
}
}