| 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 packet |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"encoding/hex" |
| 10 |
"fmt" |
| 11 |
"io" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"github.com/ProtonMail/go-crypto/openpgp/errors" |
| 15 |
) |
| 16 |
|
| 17 |
func TestReadFull(t *testing.T) { |
| 18 |
var out [4]byte |
| 19 |
|
| 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) |
| 24 |
} |
| 25 |
|
| 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) |
| 30 |
} |
| 31 |
|
| 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) |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
func readerFromHex(s string) io.Reader { |
| 40 |
data, err := hex.DecodeString(s) |
| 41 |
if err != nil { |
| 42 |
panic("readerFromHex: bad input") |
| 43 |
} |
| 44 |
return bytes.NewBuffer(data) |
| 45 |
} |
| 46 |
|
| 47 |
var readLengthTests = []struct { |
| 48 |
hexInput string |
| 49 |
length int64 |
| 50 |
isPartial bool |
| 51 |
err error |
| 52 |
}{ |
| 53 |
{"", 0, false, io.ErrUnexpectedEOF}, |
| 54 |
{"1f", 31, false, nil}, |
| 55 |
{"c0", 0, false, io.ErrUnexpectedEOF}, |
| 56 |
{"c101", 256 + 1 + 192, false, nil}, |
| 57 |
{"e0", 1, true, nil}, |
| 58 |
{"e1", 2, true, nil}, |
| 59 |
{"e2", 4, true, 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}, |
| 66 |
} |
| 67 |
|
| 68 |
func TestReadLength(t *testing.T) { |
| 69 |
for i, test := range readLengthTests { |
| 70 |
length, isPartial, err := readLength(readerFromHex(test.hexInput)) |
| 71 |
if test.err != nil { |
| 72 |
if err != test.err { |
| 73 |
t.Errorf("%d: expected different error got:%s want:%s", i, err, test.err) |
| 74 |
} |
| 75 |
continue |
| 76 |
} |
| 77 |
if err != nil { |
| 78 |
t.Errorf("%d: unexpected error: %s", i, err) |
| 79 |
continue |
| 80 |
} |
| 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) |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
var partialLengthReaderTests = []struct { |
| 88 |
hexInput string |
| 89 |
err error |
| 90 |
hexOutput string |
| 91 |
}{ |
| 92 |
{"e0", io.ErrUnexpectedEOF, ""}, |
| 93 |
{"e001", io.ErrUnexpectedEOF, ""}, |
| 94 |
{"e0010102", nil, "0102"}, |
| 95 |
{"ff00000000", nil, ""}, |
| 96 |
{"e10102e1030400", nil, "01020304"}, |
| 97 |
{"e101", io.ErrUnexpectedEOF, ""}, |
| 98 |
} |
| 99 |
|
| 100 |
func TestPartialLengthReader(t *testing.T) { |
| 101 |
for i, test := range partialLengthReaderTests { |
| 102 |
r := &partialLengthReader{readerFromHex(test.hexInput), 0, true} |
| 103 |
out, err := io.ReadAll(r) |
| 104 |
if test.err != nil { |
| 105 |
if err != test.err { |
| 106 |
t.Errorf("%d: expected different error got:%s want:%s", i, err, test.err) |
| 107 |
} |
| 108 |
continue |
| 109 |
} |
| 110 |
if err != nil { |
| 111 |
t.Errorf("%d: unexpected error: %s", i, err) |
| 112 |
continue |
| 113 |
} |
| 114 |
|
| 115 |
got := fmt.Sprintf("%x", out) |
| 116 |
if got != test.hexOutput { |
| 117 |
t.Errorf("%d: got:%s want:%s", i, test.hexOutput, got) |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
var readHeaderTests = []struct { |
| 123 |
hexInput string |
| 124 |
structuralError bool |
| 125 |
unexpectedEOF bool |
| 126 |
tag int |
| 127 |
length int64 |
| 128 |
hexOutput string |
| 129 |
}{ |
| 130 |
{"", false, false, 0, 0, ""}, |
| 131 |
{"7f", true, false, 0, 0, ""}, |
| 132 |
|
| 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"}, |
| 141 |
|
| 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"}, |
| 149 |
} |
| 150 |
|
| 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 { |
| 156 |
continue |
| 157 |
} |
| 158 |
t.Errorf("%d: expected StructuralError, got:%s", i, err) |
| 159 |
continue |
| 160 |
} |
| 161 |
if err != nil { |
| 162 |
if len(test.hexInput) == 0 && err == io.EOF { |
| 163 |
continue |
| 164 |
} |
| 165 |
if !test.unexpectedEOF || err != io.ErrUnexpectedEOF { |
| 166 |
t.Errorf("%d: unexpected error from readHeader: %s", i, err) |
| 167 |
} |
| 168 |
continue |
| 169 |
} |
| 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) |
| 172 |
continue |
| 173 |
} |
| 174 |
|
| 175 |
body, err := io.ReadAll(contents) |
| 176 |
if err != nil { |
| 177 |
if !test.unexpectedEOF || err != io.ErrUnexpectedEOF { |
| 178 |
t.Errorf("%d: unexpected error from contents: %s", i, err) |
| 179 |
} |
| 180 |
continue |
| 181 |
} |
| 182 |
if test.unexpectedEOF { |
| 183 |
t.Errorf("%d: expected ErrUnexpectedEOF from contents but got no error", i) |
| 184 |
continue |
| 185 |
} |
| 186 |
got := fmt.Sprintf("%x", body) |
| 187 |
if got != test.hexOutput { |
| 188 |
t.Errorf("%d: got:%s want:%s", i, got, test.hexOutput) |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
func TestSerializeHeader(t *testing.T) { |
| 194 |
tag := packetTypePublicKey |
| 195 |
lengths := []int{0, 1, 2, 64, 192, 193, 8000, 8384, 8385, 10000} |
| 196 |
|
| 197 |
for _, length := range lengths { |
| 198 |
buf := bytes.NewBuffer(nil) |
| 199 |
serializeHeader(buf, tag, length) |
| 200 |
tag2, length2, _, err := readHeader(buf) |
| 201 |
if err != nil { |
| 202 |
t.Errorf("length %d, err: %s", length, err) |
| 203 |
} |
| 204 |
if tag2 != tag { |
| 205 |
t.Errorf("length %d, tag incorrect (got %d, want %d)", length, tag2, tag) |
| 206 |
} |
| 207 |
if int(length2) != length { |
| 208 |
t.Errorf("length %d, length incorrect (got %d)", length, length2) |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
func TestPartialLengths(t *testing.T) { |
| 214 |
buf := bytes.NewBuffer(nil) |
| 215 |
w := new(partialLengthWriter) |
| 216 |
w.w = noOpCloser{buf} |
| 217 |
|
| 218 |
const maxChunkSize = 64 |
| 219 |
|
| 220 |
var b [maxChunkSize]byte |
| 221 |
var n uint8 |
| 222 |
for l := 1; l <= maxChunkSize; l++ { |
| 223 |
for i := 0; i < l; i++ { |
| 224 |
b[i] = n |
| 225 |
n++ |
| 226 |
} |
| 227 |
m, err := w.Write(b[:l]) |
| 228 |
if m != l { |
| 229 |
t.Errorf("short write got: %d want: %d", m, l) |
| 230 |
} |
| 231 |
if err != nil { |
| 232 |
t.Errorf("error from write: %s", err) |
| 233 |
} |
| 234 |
} |
| 235 |
if err := w.Close(); err != nil { |
| 236 |
t.Fatal(err) |
| 237 |
} |
| 238 |
|
| 239 |
// The first packet should be at least 512 bytes. |
| 240 |
first, err := buf.ReadByte() |
| 241 |
if err != nil { |
| 242 |
t.Fatal(err) |
| 243 |
} |
| 244 |
if plen := 1 << (first & 0x1f); plen < 512 { |
| 245 |
t.Errorf("first packet too short: got %d want at least %d", plen, 512) |
| 246 |
} |
| 247 |
if err := buf.UnreadByte(); err != nil { |
| 248 |
t.Fatal(err) |
| 249 |
} |
| 250 |
|
| 251 |
want := (maxChunkSize * (maxChunkSize + 1)) / 2 |
| 252 |
copyBuf := bytes.NewBuffer(nil) |
| 253 |
r := &partialLengthReader{buf, 0, true} |
| 254 |
m, err := io.Copy(copyBuf, r) |
| 255 |
if m != int64(want) { |
| 256 |
t.Errorf("short copy got: %d want: %d", m, want) |
| 257 |
} |
| 258 |
if err != nil { |
| 259 |
t.Errorf("error from copy: %s", err) |
| 260 |
} |
| 261 |
|
| 262 |
copyBytes := copyBuf.Bytes() |
| 263 |
for i := 0; i < want; i++ { |
| 264 |
if copyBytes[i] != uint8(i) { |
| 265 |
t.Errorf("bad pattern in copy at %d", i) |
| 266 |
break |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
func TestPartialLengthsShortWrite(t *testing.T) { |
| 272 |
buf := bytes.NewBuffer(nil) |
| 273 |
w := &partialLengthWriter{ |
| 274 |
w: noOpCloser{buf}, |
| 275 |
} |
| 276 |
data := bytes.Repeat([]byte("a"), 510) |
| 277 |
if _, err := w.Write(data); err != nil { |
| 278 |
t.Fatal(err) |
| 279 |
} |
| 280 |
if err := w.Close(); err != nil { |
| 281 |
t.Fatal(err) |
| 282 |
} |
| 283 |
copyBuf := bytes.NewBuffer(nil) |
| 284 |
r := &partialLengthReader{buf, 0, true} |
| 285 |
if _, err := io.Copy(copyBuf, r); err != nil { |
| 286 |
t.Fatal(err) |
| 287 |
} |
| 288 |
if !bytes.Equal(copyBuf.Bytes(), data) { |
| 289 |
t.Errorf("got %q want %q", buf.Bytes(), data) |
| 290 |
} |
| 291 |
} |