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.
11 "golang.org/x/crypto/openpgp/errors"
17 func TestReadFull(t *testing.T) {
20 b := bytes.NewBufferString("foo")
21 n, err := readFull(b, out[:3])
22 if n != 3 || err != nil {
23 t.Errorf("full read failed n:%d err:%s", n, err)
26 b = bytes.NewBufferString("foo")
27 n, err = readFull(b, out[:4])
28 if n != 3 || err != io.ErrUnexpectedEOF {
29 t.Errorf("partial read failed n:%d err:%s", n, err)
32 b = bytes.NewBuffer(nil)
33 n, err = readFull(b, out[:3])
34 if n != 0 || err != io.ErrUnexpectedEOF {
35 t.Errorf("empty read failed n:%d err:%s", n, err)
39 func readerFromHex(s string) io.Reader {
40 data, err := hex.DecodeString(s)
42 panic("readerFromHex: bad input")
44 return bytes.NewBuffer(data)
47 var readLengthTests = []struct {
53 {"", 0, false, io.ErrUnexpectedEOF},
54 {"1f", 31, false, nil},
55 {"c0", 0, false, io.ErrUnexpectedEOF},
56 {"c101", 256 + 1 + 192, false, nil},
60 {"ff", 0, false, io.ErrUnexpectedEOF},
61 {"ff00", 0, false, io.ErrUnexpectedEOF},
62 {"ff0000", 0, false, io.ErrUnexpectedEOF},
63 {"ff000000", 0, false, io.ErrUnexpectedEOF},
64 {"ff00000000", 0, false, nil},
65 {"ff01020304", 16909060, false, nil},
68 func TestReadLength(t *testing.T) {
69 for i, test := range readLengthTests {
70 length, isPartial, err := readLength(readerFromHex(test.hexInput))
73 t.Errorf("%d: expected different error got:%s want:%s", i, err, test.err)
78 t.Errorf("%d: unexpected error: %s", i, err)
81 if length != test.length || isPartial != test.isPartial {
82 t.Errorf("%d: bad result got:(%d,%t) want:(%d,%t)", i, length, isPartial, test.length, test.isPartial)
87 var partialLengthReaderTests = []struct {
92 {"e0", io.ErrUnexpectedEOF, ""},
93 {"e001", io.ErrUnexpectedEOF, ""},
94 {"e0010102", nil, "0102"},
95 {"ff00000000", nil, ""},
96 {"e10102e1030400", nil, "01020304"},
97 {"e101", io.ErrUnexpectedEOF, ""},
100 func TestPartialLengthReader(t *testing.T) {
101 for i, test := range partialLengthReaderTests {
102 r := &partialLengthReader{readerFromHex(test.hexInput), 0, true}
103 out, err := ioutil.ReadAll(r)
106 t.Errorf("%d: expected different error got:%s want:%s", i, err, test.err)
111 t.Errorf("%d: unexpected error: %s", i, err)
115 got := fmt.Sprintf("%x", out)
116 if got != test.hexOutput {
117 t.Errorf("%d: got:%s want:%s", i, test.hexOutput, got)
122 var readHeaderTests = []struct {
130 {"", false, false, 0, 0, ""},
131 {"7f", true, false, 0, 0, ""},
133 // Old format headers
134 {"80", false, true, 0, 0, ""},
135 {"8001", false, true, 0, 1, ""},
136 {"800102", false, false, 0, 1, "02"},
137 {"81000102", false, false, 0, 1, "02"},
138 {"820000000102", false, false, 0, 1, "02"},
139 {"860000000102", false, false, 1, 1, "02"},
140 {"83010203", false, false, 0, -1, "010203"},
142 // New format headers
143 {"c0", false, true, 0, 0, ""},
144 {"c000", false, false, 0, 0, ""},
145 {"c00102", false, false, 0, 1, "02"},
146 {"c0020203", false, false, 0, 2, "0203"},
147 {"c00202", false, true, 0, 2, ""},
148 {"c3020203", false, false, 3, 2, "0203"},
151 func TestReadHeader(t *testing.T) {
152 for i, test := range readHeaderTests {
153 tag, length, contents, err := readHeader(readerFromHex(test.hexInput))
154 if test.structuralError {
155 if _, ok := err.(errors.StructuralError); ok {
158 t.Errorf("%d: expected StructuralError, got:%s", i, err)
162 if len(test.hexInput) == 0 && err == io.EOF {
165 if !test.unexpectedEOF || err != io.ErrUnexpectedEOF {
166 t.Errorf("%d: unexpected error from readHeader: %s", i, err)
170 if int(tag) != test.tag || length != test.length {
171 t.Errorf("%d: got:(%d,%d) want:(%d,%d)", i, int(tag), length, test.tag, test.length)
175 body, err := ioutil.ReadAll(contents)
177 if !test.unexpectedEOF || err != io.ErrUnexpectedEOF {
178 t.Errorf("%d: unexpected error from contents: %s", i, err)
182 if test.unexpectedEOF {
183 t.Errorf("%d: expected ErrUnexpectedEOF from contents but got no error", i)
186 got := fmt.Sprintf("%x", body)
187 if got != test.hexOutput {
188 t.Errorf("%d: got:%s want:%s", i, got, test.hexOutput)
193 func TestSerializeHeader(t *testing.T) {
194 tag := packetTypePublicKey
195 lengths := []int{0, 1, 2, 64, 192, 193, 8000, 8384, 8385, 10000}
197 for _, length := range lengths {
198 buf := bytes.NewBuffer(nil)
199 serializeHeader(buf, tag, length)
200 tag2, length2, _, err := readHeader(buf)
202 t.Errorf("length %d, err: %s", length, err)
205 t.Errorf("length %d, tag incorrect (got %d, want %d)", length, tag2, tag)
207 if int(length2) != length {
208 t.Errorf("length %d, length incorrect (got %d)", length, length2)
213 func TestPartialLengths(t *testing.T) {
214 buf := bytes.NewBuffer(nil)
215 w := new(partialLengthWriter)
216 w.w = noOpCloser{buf}
218 const maxChunkSize = 64
220 var b [maxChunkSize]byte
222 for l := 1; l <= maxChunkSize; l++ {
223 for i := 0; i < l; i++ {
227 m, err := w.Write(b[:l])
229 t.Errorf("short write got: %d want: %d", m, l)
232 t.Errorf("error from write: %s", err)
237 want := (maxChunkSize * (maxChunkSize + 1)) / 2
238 copyBuf := bytes.NewBuffer(nil)
239 r := &partialLengthReader{buf, 0, true}
240 m, err := io.Copy(copyBuf, r)
241 if m != int64(want) {
242 t.Errorf("short copy got: %d want: %d", m, want)
245 t.Errorf("error from copy: %s", err)
248 copyBytes := copyBuf.Bytes()
249 for i := 0; i < want; i++ {
250 if copyBytes[i] != uint8(i) {
251 t.Errorf("bad pattern in copy at %d", i)