blob: 8f3ba2a8814ea9c48b0d34fa6018c5aa5468d283 [file] [log] [blame]
Russ Cox470549d2012-01-25 15:31:12 -05001// 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
5package openpgp
6
7import (
8 "bytes"
9 "testing"
10)
11
12type recordingHash struct {
13 buf *bytes.Buffer
14}
15
16func (r recordingHash) Write(b []byte) (n int, err error) {
17 return r.buf.Write(b)
18}
19
20func (r recordingHash) Sum(in []byte) []byte {
21 return append(in, r.buf.Bytes()...)
22}
23
24func (r recordingHash) Reset() {
25 panic("shouldn't be called")
26}
27
28func (r recordingHash) Size() int {
29 panic("shouldn't be called")
30}
31
32func (r recordingHash) BlockSize() int {
33 panic("shouldn't be called")
34}
35
36func 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
46func 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}