| 1 |
package v2 |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/binary" |
| 6 |
"io" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
func packetLength(n int) []byte { |
| 11 |
switch { |
| 12 |
case n < 192: |
| 13 |
return []byte{byte(n)} |
| 14 |
case n < 8384: |
| 15 |
n -= 192 |
| 16 |
return []byte{byte(n>>8) + 192, byte(n)} |
| 17 |
default: |
| 18 |
return []byte{0xff, byte(n >> 24), byte(n >> 16), byte(n >> 8), byte(n)} |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
func wrapPacket(tag byte, body []byte) []byte { |
| 23 |
out := []byte{0xc0 | tag} |
| 24 |
out = append(out, packetLength(len(body))...) |
| 25 |
return append(out, body...) |
| 26 |
} |
| 27 |
|
| 28 |
func sigNoIssuer() []byte { |
| 29 |
creation := []byte{0x05, 0x02, 0x00, 0x00, 0x00, 0x01} |
| 30 |
var body bytes.Buffer |
| 31 |
body.WriteByte(0x04) |
| 32 |
body.WriteByte(0x00) |
| 33 |
body.WriteByte(0x01) |
| 34 |
body.WriteByte(0x08) |
| 35 |
binary.Write(&body, binary.BigEndian, uint16(len(creation))) |
| 36 |
body.Write(creation) |
| 37 |
binary.Write(&body, binary.BigEndian, uint16(0)) |
| 38 |
body.Write([]byte{0x00, 0x00}) |
| 39 |
body.Write([]byte{0x00, 0x00}) |
| 40 |
return wrapPacket(2, body.Bytes()) |
| 41 |
} |
| 42 |
|
| 43 |
func literalPacket(data []byte) []byte { |
| 44 |
var body bytes.Buffer |
| 45 |
body.WriteByte('b') |
| 46 |
body.WriteByte(0x00) |
| 47 |
body.Write([]byte{0x00, 0x00, 0x00, 0x00}) |
| 48 |
body.Write(data) |
| 49 |
return wrapPacket(11, body.Bytes()) |
| 50 |
} |
| 51 |
|
| 52 |
func TestReadMessageNilIssuerNoPanic(t *testing.T) { |
| 53 |
msg := append(sigNoIssuer(), literalPacket([]byte("hello"))...) |
| 54 |
md, err := ReadMessage(bytes.NewReader(msg), nil, nil, nil) |
| 55 |
if err == nil { |
| 56 |
if md != nil && md.UnverifiedBody != nil { |
| 57 |
io.Copy(io.Discard, md.UnverifiedBody) |
| 58 |
} |
| 59 |
t.Fatal("expected an error for signature without issuer, got nil") |
| 60 |
} |
| 61 |
} |