OSDN Git Service

new repo
[bytom/vapor.git] / vendor / golang.org / x / crypto / openpgp / clearsign / clearsign_test.go
1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package clearsign
6
7 import (
8         "bytes"
9         "golang.org/x/crypto/openpgp"
10         "testing"
11 )
12
13 func testParse(t *testing.T, input []byte, expected, expectedPlaintext string) {
14         b, rest := Decode(input)
15         if b == nil {
16                 t.Fatal("failed to decode clearsign message")
17         }
18         if !bytes.Equal(rest, []byte("trailing")) {
19                 t.Errorf("unexpected remaining bytes returned: %s", string(rest))
20         }
21         if b.ArmoredSignature.Type != "PGP SIGNATURE" {
22                 t.Errorf("bad armor type, got:%s, want:PGP SIGNATURE", b.ArmoredSignature.Type)
23         }
24         if !bytes.Equal(b.Bytes, []byte(expected)) {
25                 t.Errorf("bad body, got:%x want:%x", b.Bytes, expected)
26         }
27
28         if !bytes.Equal(b.Plaintext, []byte(expectedPlaintext)) {
29                 t.Errorf("bad plaintext, got:%x want:%x", b.Plaintext, expectedPlaintext)
30         }
31
32         keyring, err := openpgp.ReadArmoredKeyRing(bytes.NewBufferString(signingKey))
33         if err != nil {
34                 t.Errorf("failed to parse public key: %s", err)
35         }
36
37         if _, err := openpgp.CheckDetachedSignature(keyring, bytes.NewBuffer(b.Bytes), b.ArmoredSignature.Body); err != nil {
38                 t.Errorf("failed to check signature: %s", err)
39         }
40 }
41
42 func TestParse(t *testing.T) {
43         testParse(t, clearsignInput, "Hello world\r\nline 2", "Hello world\nline 2\n")
44         testParse(t, clearsignInput2, "\r\n\r\n(This message has a couple of blank lines at the start and end.)\r\n\r\n", "\n\n(This message has a couple of blank lines at the start and end.)\n\n\n")
45 }
46
47 func TestParseInvalid(t *testing.T) {
48         if b, _ := Decode(clearsignInput3); b != nil {
49                 t.Fatal("decoded a bad clearsigned message without any error")
50         }
51 }
52
53 func TestParseWithNoNewlineAtEnd(t *testing.T) {
54         input := clearsignInput
55         input = input[:len(input)-len("trailing")-1]
56         b, rest := Decode(input)
57         if b == nil {
58                 t.Fatal("failed to decode clearsign message")
59         }
60         if len(rest) > 0 {
61                 t.Errorf("unexpected remaining bytes returned: %s", string(rest))
62         }
63 }
64
65 var signingTests = []struct {
66         in, signed, plaintext string
67 }{
68         {"", "", ""},
69         {"a", "a", "a\n"},
70         {"a\n", "a", "a\n"},
71         {"-a\n", "-a", "-a\n"},
72         {"--a\nb", "--a\r\nb", "--a\nb\n"},
73         // leading whitespace
74         {" a\n", " a", " a\n"},
75         {"  a\n", "  a", "  a\n"},
76         // trailing whitespace (should be stripped)
77         {"a \n", "a", "a\n"},
78         {"a ", "a", "a\n"},
79         // whitespace-only lines (should be stripped)
80         {"  \n", "", "\n"},
81         {"  ", "", "\n"},
82         {"a\n  \n  \nb\n", "a\r\n\r\n\r\nb", "a\n\n\nb\n"},
83 }
84
85 func TestSigning(t *testing.T) {
86         keyring, err := openpgp.ReadArmoredKeyRing(bytes.NewBufferString(signingKey))
87         if err != nil {
88                 t.Errorf("failed to parse public key: %s", err)
89         }
90
91         for i, test := range signingTests {
92                 var buf bytes.Buffer
93
94                 plaintext, err := Encode(&buf, keyring[0].PrivateKey, nil)
95                 if err != nil {
96                         t.Errorf("#%d: error from Encode: %s", i, err)
97                         continue
98                 }
99                 if _, err := plaintext.Write([]byte(test.in)); err != nil {
100                         t.Errorf("#%d: error from Write: %s", i, err)
101                         continue
102                 }
103                 if err := plaintext.Close(); err != nil {
104                         t.Fatalf("#%d: error from Close: %s", i, err)
105                         continue
106                 }
107
108                 b, _ := Decode(buf.Bytes())
109                 if b == nil {
110                         t.Errorf("#%d: failed to decode clearsign message", i)
111                         continue
112                 }
113                 if !bytes.Equal(b.Bytes, []byte(test.signed)) {
114                         t.Errorf("#%d: bad result, got:%x, want:%x", i, b.Bytes, test.signed)
115                         continue
116                 }
117                 if !bytes.Equal(b.Plaintext, []byte(test.plaintext)) {
118                         t.Errorf("#%d: bad result, got:%x, want:%x", i, b.Plaintext, test.plaintext)
119                         continue
120                 }
121
122                 if _, err := openpgp.CheckDetachedSignature(keyring, bytes.NewBuffer(b.Bytes), b.ArmoredSignature.Body); err != nil {
123                         t.Errorf("#%d: failed to check signature: %s", i, err)
124                 }
125         }
126 }
127
128 var clearsignInput = []byte(`
129 ;lasjlkfdsa
130
131 -----BEGIN PGP SIGNED MESSAGE-----
132 Hash: SHA1
133
134 Hello world
135 line 2
136 -----BEGIN PGP SIGNATURE-----
137 Version: GnuPG v1.4.10 (GNU/Linux)
138
139 iJwEAQECAAYFAk8kMuEACgkQO9o98PRieSpMsAQAhmY/vwmNpflrPgmfWsYhk5O8
140 pjnBUzZwqTDoDeINjZEoPDSpQAHGhjFjgaDx/Gj4fAl0dM4D0wuUEBb6QOrwflog
141 2A2k9kfSOMOtk0IH/H5VuFN1Mie9L/erYXjTQIptv9t9J7NoRBMU0QOOaFU0JaO9
142 MyTpno24AjIAGb+mH1U=
143 =hIJ6
144 -----END PGP SIGNATURE-----
145 trailing`)
146
147 var clearsignInput2 = []byte(`
148 asdlfkjasdlkfjsadf
149
150 -----BEGIN PGP SIGNED MESSAGE-----
151 Hash: SHA256
152
153
154
155 (This message has a couple of blank lines at the start and end.)
156
157
158 -----BEGIN PGP SIGNATURE-----
159 Version: GnuPG v1.4.11 (GNU/Linux)
160
161 iJwEAQEIAAYFAlPpSREACgkQO9o98PRieSpZTAP+M8QUoCt/7Rf3YbXPcdzIL32v
162 pt1I+cMNeopzfLy0u4ioEFi8s5VkwpL1AFmirvgViCwlf82inoRxzZRiW05JQ5LI
163 ESEzeCoy2LIdRCQ2hcrG8pIUPzUO4TqO5D/dMbdHwNH4h5nNmGJUAEG6FpURlPm+
164 qZg6BaTvOxepqOxnhVU=
165 =e+C6
166 -----END PGP SIGNATURE-----
167
168 trailing`)
169
170 var clearsignInput3 = []byte(`
171 -----BEGIN PGP SIGNED MESSAGE-----
172 Hash: SHA256
173
174 (This message was truncated.)
175 `)
176
177 var signingKey = `-----BEGIN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nb3X71GfVXlc9hyv7CD6tcowL59hg1E/DC5ydI8K8iEpUmK
196 /UnHdIY5h8/kqgGxkY/T/hgp5fRQgW1ZoZxLajVlMRZ8W4tFtT0DeA+JABEBAAEA
197 A/0bE1jaaZKj6ndqcw86jd+QtD1SF+Cf21CWRNeLKnUds4FRRvclzTyUMuWPkUeX
198 TaNNsUOFqBsf6QQ2oHUBBK4VCHffHCW4ZEX2cd6umz7mpHW6XzN4DECEzOVksXtc
199 lUC1j4UB91DC/RNQqwX1IV2QLSwssVotPMPqhOi0ZLNY7wIA3n7DWKInxYZZ4K+6
200 rQ+POsz6brEoRHwr8x6XlHenq1Oki855pSa1yXIARoTrSJkBtn5oI+f8AzrnN0BN
201 oyeQAwIA/7E++3HDi5aweWrViiul9cd3rcsS0dEnksPhvS0ozCJiHsq/6GFmy7J8
202 QSHZPteedBnZyNp5jR+H7cIfVN3KgwH/Skq4PsuPhDq5TKK6i8Pc1WW8MA6DXTdU
203 nLkX7RGmMwjC0DBf7KWAlPjFaONAX3a8ndnz//fy1q7u2l9AZwrj1qa1iJ8EGAEC
204 AAkFAk2rFNoCGwwACgkQO9o98PRieSo2/QP/WTzr4ioINVsvN1akKuekmEMI3LAp
205 BfHwatufxxP1U+3Si/6YIk7kuPB9Hs+pRqCXzbvPRrI8NHZBmc8qIGthishdCYad
206 AHcVnXjtxrULkQFGbGvhKURLvS9WnzD/m1K2zzwxzkPTzT9/Yf06O6Mal5AdugPL
207 VrM0m72/jnpKo04=
208 =zNCn
209 -----END PGP PRIVATE KEY BLOCK-----
210 `