armor_test.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2010 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. package armor
  5. import (
  6. "bytes"
  7. "hash/adler32"
  8. "io/ioutil"
  9. "testing"
  10. )
  11. func TestDecodeEncode(t *testing.T) {
  12. buf := bytes.NewBuffer([]byte(armorExample1))
  13. result, err := Decode(buf)
  14. if err != nil {
  15. t.Error(err)
  16. }
  17. expectedType := "PGP SIGNATURE"
  18. if result.Type != expectedType {
  19. t.Errorf("result.Type: got:%s want:%s", result.Type, expectedType)
  20. }
  21. if len(result.Header) != 1 {
  22. t.Errorf("len(result.Header): got:%d want:1", len(result.Header))
  23. }
  24. v, ok := result.Header["Version"]
  25. if !ok || v != "GnuPG v1.4.10 (GNU/Linux)" {
  26. t.Errorf("result.Header: got:%#v", result.Header)
  27. }
  28. contents, err := ioutil.ReadAll(result.Body)
  29. if err != nil {
  30. t.Error(err)
  31. }
  32. if adler32.Checksum(contents) != 0x27b144be {
  33. t.Errorf("contents: got: %x", contents)
  34. }
  35. buf = bytes.NewBuffer(nil)
  36. w, err := Encode(buf, result.Type, result.Header)
  37. if err != nil {
  38. t.Error(err)
  39. }
  40. _, err = w.Write(contents)
  41. if err != nil {
  42. t.Error(err)
  43. }
  44. w.Close()
  45. if !bytes.Equal(buf.Bytes(), []byte(armorExample1)) {
  46. t.Errorf("got: %s\nwant: %s", string(buf.Bytes()), armorExample1)
  47. }
  48. }
  49. func TestLongHeader(t *testing.T) {
  50. buf := bytes.NewBuffer([]byte(armorLongLine))
  51. result, err := Decode(buf)
  52. if err != nil {
  53. t.Error(err)
  54. return
  55. }
  56. value, ok := result.Header["Version"]
  57. if !ok {
  58. t.Errorf("missing Version header")
  59. }
  60. if value != longValueExpected {
  61. t.Errorf("got: %s want: %s", value, longValueExpected)
  62. }
  63. }
  64. const armorExample1 = `-----BEGIN PGP SIGNATURE-----
  65. Version: GnuPG v1.4.10 (GNU/Linux)
  66. iJwEAAECAAYFAk1Fv/0ACgkQo01+GMIMMbsYTwQAiAw+QAaNfY6WBdplZ/uMAccm
  67. 4g+81QPmTSGHnetSb6WBiY13kVzK4HQiZH8JSkmmroMLuGeJwsRTEL4wbjRyUKEt
  68. p1xwUZDECs234F1xiG5enc5SGlRtP7foLBz9lOsjx+LEcA4sTl5/2eZR9zyFZqWW
  69. TxRjs+fJCIFuo71xb1g=
  70. =/teI
  71. -----END PGP SIGNATURE-----`
  72. const armorLongLine = `-----BEGIN PGP SIGNATURE-----
  73. Version: 0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz
  74. iQEcBAABAgAGBQJMtFESAAoJEKsQXJGvOPsVj40H/1WW6jaMXv4BW+1ueDSMDwM8
  75. kx1fLOXbVM5/Kn5LStZNt1jWWnpxdz7eq3uiqeCQjmqUoRde3YbB2EMnnwRbAhpp
  76. cacnAvy9ZQ78OTxUdNW1mhX5bS6q1MTEJnl+DcyigD70HG/yNNQD7sOPMdYQw0TA
  77. byQBwmLwmTsuZsrYqB68QyLHI+DUugn+kX6Hd2WDB62DKa2suoIUIHQQCd/ofwB3
  78. WfCYInXQKKOSxu2YOg2Eb4kLNhSMc1i9uKUWAH+sdgJh7NBgdoE4MaNtBFkHXRvv
  79. okWuf3+xA9ksp1npSY/mDvgHijmjvtpRDe6iUeqfCn8N9u9CBg8geANgaG8+QA4=
  80. =wfQG
  81. -----END PGP SIGNATURE-----`
  82. const longValueExpected = "0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz"