LEFT | RIGHT |
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package armor | 5 package armor |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "hash/adler32" | 9 "hash/adler32" |
10 "io/ioutil" | 10 "io/ioutil" |
11 "testing" | 11 "testing" |
12 ) | 12 ) |
13 | 13 |
14 func TestDecodeEncode(t *testing.T) { | 14 func TestDecodeEncode(t *testing.T) { |
15 buf := bytes.NewBuffer([]byte(armorExample1)) | 15 buf := bytes.NewBuffer([]byte(armorExample1)) |
16 result, err := Decode(buf) | 16 result, err := Decode(buf) |
17 if err != nil { | 17 if err != nil { |
18 t.Error(err) | 18 t.Error(err) |
19 } | 19 } |
20 expectedType := "PGP SIGNATURE" | 20 expectedType := "PGP SIGNATURE" |
21 if result.Type != expectedType { | 21 if result.Type != expectedType { |
22 t.Errorf("result.Type: got:%s want:%s", result.Type, expectedTyp
e) | 22 t.Errorf("result.Type: got:%s want:%s", result.Type, expectedTyp
e) |
23 } | 23 } |
24 » if len(result.Headers) != 1 { | 24 » if len(result.Header) != 1 { |
25 » » t.Errorf("len(result.Headers): got:%d want:1", len(result.Header
s)) | 25 » » t.Errorf("len(result.Header): got:%d want:1", len(result.Header)
) |
26 } | 26 } |
27 » v, ok := result.Headers["Version"] | 27 » v, ok := result.Header["Version"] |
28 if !ok || v != "GnuPG v1.4.10 (GNU/Linux)" { | 28 if !ok || v != "GnuPG v1.4.10 (GNU/Linux)" { |
29 » » t.Errorf("result.Headers: got:%#v", result.Headers) | 29 » » t.Errorf("result.Header: got:%#v", result.Header) |
30 } | 30 } |
31 | 31 |
32 » contents, err := ioutil.ReadAll(result.Contents) | 32 » contents, err := ioutil.ReadAll(result.Body) |
33 if err != nil { | 33 if err != nil { |
34 t.Error(err) | 34 t.Error(err) |
35 } | 35 } |
36 | 36 |
37 if adler32.Checksum(contents) != 0x789d7f00 { | 37 if adler32.Checksum(contents) != 0x789d7f00 { |
38 t.Errorf("contents: got: %x", contents) | 38 t.Errorf("contents: got: %x", contents) |
39 } | 39 } |
40 | 40 |
41 buf = bytes.NewBuffer(nil) | 41 buf = bytes.NewBuffer(nil) |
42 » w, err := Encode(buf, result.Type, result.Headers) | 42 » w, err := Encode(buf, result.Type, result.Header) |
43 if err != nil { | 43 if err != nil { |
44 t.Error(err) | 44 t.Error(err) |
45 } | 45 } |
46 _, err = w.Write(contents) | 46 _, err = w.Write(contents) |
47 if err != nil { | 47 if err != nil { |
48 t.Error(err) | 48 t.Error(err) |
49 } | 49 } |
50 w.Close() | 50 w.Close() |
51 | 51 |
52 if !bytes.Equal(buf.Bytes(), []byte(armorExample1)) { | 52 if !bytes.Equal(buf.Bytes(), []byte(armorExample1)) { |
53 t.Errorf("got: %s\nwant: %s", string(buf.Bytes()), armorExample1
) | 53 t.Errorf("got: %s\nwant: %s", string(buf.Bytes()), armorExample1
) |
54 } | 54 } |
55 } | 55 } |
56 | 56 |
57 func TestLongHeader(t *testing.T) { | 57 func TestLongHeader(t *testing.T) { |
58 buf := bytes.NewBuffer([]byte(armorLongLine)) | 58 buf := bytes.NewBuffer([]byte(armorLongLine)) |
59 result, err := Decode(buf) | 59 result, err := Decode(buf) |
60 if err != nil { | 60 if err != nil { |
61 t.Error(err) | 61 t.Error(err) |
62 return | 62 return |
63 } | 63 } |
64 » value, ok := result.Headers["Version"] | 64 » value, ok := result.Header["Version"] |
65 if !ok { | 65 if !ok { |
66 t.Errorf("missing Version header") | 66 t.Errorf("missing Version header") |
67 } | 67 } |
68 if value != longValueExpected { | 68 if value != longValueExpected { |
69 t.Errorf("got: %s want: %s", value, longValueExpected) | 69 t.Errorf("got: %s want: %s", value, longValueExpected) |
70 } | 70 } |
71 } | 71 } |
72 | 72 |
73 const armorExample1 = `-----BEGIN PGP SIGNATURE----- | 73 const armorExample1 = `-----BEGIN PGP SIGNATURE----- |
74 Version: GnuPG v1.4.10 (GNU/Linux) | 74 Version: GnuPG v1.4.10 (GNU/Linux) |
(...skipping 13 matching lines...) Expand all Loading... |
88 iQEcBAABAgAGBQJMtFESAAoJEKsQXJGvOPsVj40H/1WW6jaMXv4BW+1ueDSMDwM8 | 88 iQEcBAABAgAGBQJMtFESAAoJEKsQXJGvOPsVj40H/1WW6jaMXv4BW+1ueDSMDwM8 |
89 kx1fLOXbVM5/Kn5LStZNt1jWWnpxdz7eq3uiqeCQjmqUoRde3YbB2EMnnwRbAhpp | 89 kx1fLOXbVM5/Kn5LStZNt1jWWnpxdz7eq3uiqeCQjmqUoRde3YbB2EMnnwRbAhpp |
90 cacnAvy9ZQ78OTxUdNW1mhX5bS6q1MTEJnl+DcyigD70HG/yNNQD7sOPMdYQw0TA | 90 cacnAvy9ZQ78OTxUdNW1mhX5bS6q1MTEJnl+DcyigD70HG/yNNQD7sOPMdYQw0TA |
91 byQBwmLwmTsuZsrYqB68QyLHI+DUugn+kX6Hd2WDB62DKa2suoIUIHQQCd/ofwB3 | 91 byQBwmLwmTsuZsrYqB68QyLHI+DUugn+kX6Hd2WDB62DKa2suoIUIHQQCd/ofwB3 |
92 WfCYInXQKKOSxu2YOg2Eb4kLNhSMc1i9uKUWAH+sdgJh7NBgdoE4MaNtBFkHXRvv | 92 WfCYInXQKKOSxu2YOg2Eb4kLNhSMc1i9uKUWAH+sdgJh7NBgdoE4MaNtBFkHXRvv |
93 okWuf3+xA9ksp1npSY/mDvgHijmjvtpRDe6iUeqfCn8N9u9CBg8geANgaG8+QA4= | 93 okWuf3+xA9ksp1npSY/mDvgHijmjvtpRDe6iUeqfCn8N9u9CBg8geANgaG8+QA4= |
94 =wfQG | 94 =wfQG |
95 -----END PGP SIGNATURE-----` | 95 -----END PGP SIGNATURE-----` |
96 | 96 |
97 const longValueExpected = "0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefg
hijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmno
pqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvw
xyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz01234
56789abcdefghijklmnopqrstuvwxyz" | 97 const longValueExpected = "0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefg
hijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmno
pqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvw
xyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz01234
56789abcdefghijklmnopqrstuvwxyz" |
LEFT | RIGHT |