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