canonical_text_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2011 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 openpgp
  5. import (
  6. "bytes"
  7. "testing"
  8. )
  9. type recordingHash struct {
  10. buf *bytes.Buffer
  11. }
  12. func (r recordingHash) Write(b []byte) (n int, err error) {
  13. return r.buf.Write(b)
  14. }
  15. func (r recordingHash) Sum(in []byte) []byte {
  16. return append(in, r.buf.Bytes()...)
  17. }
  18. func (r recordingHash) Reset() {
  19. panic("shouldn't be called")
  20. }
  21. func (r recordingHash) Size() int {
  22. panic("shouldn't be called")
  23. }
  24. func (r recordingHash) BlockSize() int {
  25. panic("shouldn't be called")
  26. }
  27. func testCanonicalText(t *testing.T, input, expected string) {
  28. r := recordingHash{bytes.NewBuffer(nil)}
  29. c := NewCanonicalTextHash(r)
  30. c.Write([]byte(input))
  31. result := c.Sum(nil)
  32. if expected != string(result) {
  33. t.Errorf("input: %x got: %x want: %x", input, result, expected)
  34. }
  35. }
  36. func TestCanonicalText(t *testing.T) {
  37. testCanonicalText(t, "foo\n", "foo\r\n")
  38. testCanonicalText(t, "foo", "foo")
  39. testCanonicalText(t, "foo\r\n", "foo\r\n")
  40. testCanonicalText(t, "foo\r\nbar", "foo\r\nbar")
  41. testCanonicalText(t, "foo\r\nbar\n\n", "foo\r\nbar\r\n\r\n")
  42. }