forked from WICG/webpackage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle_test.go
161 lines (148 loc) · 4.3 KB
/
bundle_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package bundle_test
import (
"bytes"
"net/http"
"net/url"
"reflect"
"testing"
. "github.com/WICG/webpackage/go/bundle"
"github.com/WICG/webpackage/go/bundle/version"
"github.com/WICG/webpackage/go/internal/signingalgorithm"
"github.com/WICG/webpackage/go/signedexchange/certurl"
)
const pemCerts = `-----BEGIN CERTIFICATE-----
MIIBhjCCAS2gAwIBAgIJAOhR3xtYd5QsMAoGCCqGSM49BAMCMDIxFDASBgNVBAMM
C2V4YW1wbGUub3JnMQ0wCwYDVQQKDARUZXN0MQswCQYDVQQGEwJVUzAeFw0xODEx
MDUwOTA5MjJaFw0xOTEwMzEwOTA5MjJaMDIxFDASBgNVBAMMC2V4YW1wbGUub3Jn
MQ0wCwYDVQQKDARUZXN0MQswCQYDVQQGEwJVUzBZMBMGByqGSM49AgEGCCqGSM49
AwEHA0IABH1E6odXRm3+r7dMYmkJRmftx5IYHAsqgA7zjsFfCvPqL/fM4Uvi8EFu
JVQM/oKEZw3foCZ1KBjo/6Tenkoj/wCjLDAqMBAGCisGAQQB1nkCARYEAgUAMBYG
A1UdEQQPMA2CC2V4YW1wbGUub3JnMAoGCCqGSM49BAMCA0cAMEQCIEbxRKhlQYlw
Ja+O9h7misjLil82Q82nhOtl4j96awZgAiB6xrvRZIlMtWYKdi41BTb5fX22gL9M
L/twWg8eWpYeJA==
-----END CERTIFICATE-----
`
func urlMustParse(rawurl string) *url.URL {
u, err := url.Parse(rawurl)
if err != nil {
panic(err)
}
return u
}
func createTestBundle(t *testing.T, ver version.Version) *Bundle {
bundle := &Bundle{
Version: ver,
Exchanges: []*Exchange{
&Exchange{
Request{
URL: urlMustParse("https://bundle.example.com/"),
},
Response{
Status: 200,
Header: http.Header{"Content-Type": []string{"text/html"}},
Body: []byte("hello, world!"),
},
},
},
Signatures: &Signatures{
Authorities: createTestCerts(t),
VouchedSubsets: []*VouchedSubset{
&VouchedSubset{Authority: 0, Sig: []byte("sig"), Signed: []byte("sig")},
},
},
}
bundle.PrimaryURL = urlMustParse("https://bundle.example.com/")
return bundle
}
func createTestBundleWithVariants(ver version.Version) *Bundle {
primaryURL := urlMustParse("https://variants.example.com/")
return &Bundle{
Version: ver,
PrimaryURL: primaryURL,
Exchanges: []*Exchange{
&Exchange{
Request{URL: primaryURL},
Response{
Status: 200,
Header: http.Header{
"Content-Type": []string{"text/plain"},
"Variants": []string{"Accept-Language;en;ja"},
"Variant-Key": []string{"en"},
},
Body: []byte("Hello, world!"),
},
},
&Exchange{
Request{URL: primaryURL},
Response{
Status: 200,
Header: http.Header{
"Content-Type": []string{"text/plain"},
"Variants": []string{"Accept-Language;en;ja"},
"Variant-Key": []string{"ja"},
},
Body: []byte("こんにちは世界"),
},
},
},
}
}
func createTestCerts(t *testing.T) []*certurl.AugmentedCertificate {
certs, err := signingalgorithm.ParseCertificates([]byte(pemCerts))
if err != nil {
t.Fatal(err)
}
var acs []*certurl.AugmentedCertificate
for _, c := range certs {
acs = append(acs, &certurl.AugmentedCertificate{Cert: c})
}
return acs
}
func TestWriteAndRead(t *testing.T) {
for _, ver := range version.AllVersions {
bundle := createTestBundle(t, ver)
var buf bytes.Buffer
n, err := bundle.WriteTo(&buf)
if err != nil {
t.Errorf("Bundle.WriteTo unexpectedly failed: %v", err)
}
if n != int64(buf.Len()) {
t.Errorf("Bundle.WriteTo returned %d, but wrote %d bytes", n, buf.Len())
}
deserialized, err := Read(&buf)
if err != nil {
t.Errorf("Bundle.Read unexpectedly failed: %v", err)
}
if !reflect.DeepEqual(deserialized, bundle) {
t.Errorf("got: %v\nwant: %v", deserialized, bundle)
}
}
}
func TestWriteAndReadWithVariants(t *testing.T) {
for _, ver := range version.AllVersions {
if !ver.SupportsVariants() {
continue
}
bundle := createTestBundleWithVariants(ver)
var buf bytes.Buffer
if _, err := bundle.WriteTo(&buf); err != nil {
t.Errorf("Bundle.WriteTo unexpectedly failed: %v", err)
}
deserialized, err := Read(&buf)
if err != nil {
t.Errorf("Bundle.Read unexpectedly failed: %v", err)
}
if !reflect.DeepEqual(deserialized, bundle) {
t.Errorf("got: %v\nwant: %v", deserialized, bundle)
}
}
}
func TestB2BundleWithSpecifiedManifestURLShouldFail(t *testing.T) {
bundle := createTestBundle(t, version.VersionB2)
bundle.ManifestURL = urlMustParse("https://bundle.example.com/manifest")
var buf bytes.Buffer
_, err := bundle.WriteTo(&buf)
if err == nil || err.Error() != "This version of the WebBundle does not support storing manifest URL." {
t.Errorf("Bundle write should fail as version B2 does not support manifest URL.")
}
}