summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@anytun.org>2017-09-07 12:34:42 +0200
committerChristian Pointner <equinox@anytun.org>2017-09-07 12:34:42 +0200
commit36174e701fd6f5c30976771d1f74923b9d6e75e6 (patch)
tree60fdf5f922b9cbf5f59a35226f3fb95ed39995cc
parentmore test data for plain packet (diff)
more test data
-rw-r--r--satp/packet.go4
-rw-r--r--satp/packet_test.go106
2 files changed, 87 insertions, 23 deletions
diff --git a/satp/packet.go b/satp/packet.go
index a24b69a..7e638fa 100644
--- a/satp/packet.go
+++ b/satp/packet.go
@@ -96,7 +96,9 @@ type EncryptedPacket struct {
func NewEncryptedPacket(AuthTagLength uint) (ep *EncryptedPacket) {
ep = &EncryptedPacket{}
- ep.AuthTag = make([]byte, AuthTagLength)
+ if AuthTagLength > 0 {
+ ep.AuthTag = make([]byte, AuthTagLength)
+ }
return
}
diff --git a/satp/packet_test.go b/satp/packet_test.go
index f1e25b7..2c54d32 100644
--- a/satp/packet_test.go
+++ b/satp/packet_test.go
@@ -32,6 +32,7 @@ package satp
import (
"bytes"
+ "math/rand"
"reflect"
"testing"
)
@@ -130,21 +131,46 @@ func TestPlainPacketUnmarshal(t *testing.T) {
}
}
+func generateRandomTestDataPlainPacket() (pp *PlainPacket) {
+ payloadlen := uint(rand.Int31n(2000))
+
+ pp = NewPlainPacket(uint16(rand.Uint32()))
+ pp.Payload = make([]byte, payloadlen)
+ rand.Read(pp.Payload)
+ return
+}
+
+func TestPlainPacketMarshalUnmarshal(t *testing.T) {
+ for i := 0; i < 100; i++ {
+ in := generateRandomTestDataPlainPacket()
+ marshalled, err := in.MarshalBinary()
+ if err != nil {
+ t.Fatal("unexpected error:", err)
+ }
+ out := NewPlainPacket(in.Type)
+ if err = out.UnmarshalBinary(marshalled); err != nil {
+ t.Fatal("unexpected error:", err)
+ }
+ if !reflect.DeepEqual(in, out) {
+ t.Fatalf("unmarshalled packet is wrong: is '%+v', should be '%+v'", out, in)
+ }
+ }
+}
+
func TestEncryptedPacketMarshal(t *testing.T) {
testvectors := []struct {
packet EncryptedPacket
valid bool
expected []byte
}{
- {
- EncryptedPacket{}, false,
- []byte{},
- },
- {
- EncryptedPacket{Payload: []byte{0x0, 0x0}}, true,
- []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
- },
- // TODO: much more test vectors
+ {EncryptedPacket{}, false, []byte{}},
+ {EncryptedPacket{Payload: []byte{0x0}}, false, []byte{}},
+ {EncryptedPacket{Payload: []byte{0x8}}, false, []byte{}},
+ {EncryptedPacket{Payload: []byte{0x0, 0x0}}, true, []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}},
+ {EncryptedPacket{Payload: []byte{0x42, 0x23}}, true, []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x42, 0x23}},
+ {EncryptedPacket{Payload: []byte{0x17, 0x4, 0x0}}, true, []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x17, 0x4, 0x0}},
+ {*NewEncryptedPacket(3), false, []byte{}},
+ {EncryptedPacket{Payload: []byte{0x00, 0x56, 0xa5}, AuthTag: make([]byte, 3)}, true, []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x56, 0xa5, 0x0, 0x0, 0x0}},
}
for _, vector := range testvectors {
@@ -166,23 +192,29 @@ func TestEncryptedPacketMarshal(t *testing.T) {
func TestEncryptedPacketUnmarshal(t *testing.T) {
testvectors := []struct {
- data []byte
- valid bool
- expected EncryptedPacket
+ data []byte
+ valid bool
+ authTagLen uint
+ expected EncryptedPacket
}{
- {
- []byte{},
- false, EncryptedPacket{},
- },
- {
- []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
- true, EncryptedPacket{Payload: []byte{0x0, 0x0}},
- },
- // TODO: much more test vectors
+ {[]byte{}, false, 0, EncryptedPacket{}},
+ {[]byte{0x0, 0x0}, false, 0, EncryptedPacket{}},
+ {[]byte{0x0, 0x0, 0x0}, false, 0, EncryptedPacket{}},
+ {[]byte{0x0, 0x0, 0x0, 0x0}, false, 0, EncryptedPacket{}},
+ {[]byte{0x0, 0x0, 0x0, 0x0, 0x0}, false, 0, EncryptedPacket{}},
+ {[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, false, 0, EncryptedPacket{}},
+ {[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, false, 0, EncryptedPacket{}},
+ {[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, false, 0, EncryptedPacket{}},
+ {[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, false, 0, EncryptedPacket{}},
+ {[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, true, 0, EncryptedPacket{Payload: []byte{0x0, 0x0}}},
+ {[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, false, 1, EncryptedPacket{Payload: []byte{0x0, 0x0}}},
+ {[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, true, 1, EncryptedPacket{Payload: []byte{0x0, 0x0}, AuthTag: []byte{0x0}}},
+ {[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, true, 0, EncryptedPacket{Payload: []byte{0x0, 0x0, 0x0}}},
+ {[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, true, 3, EncryptedPacket{Payload: []byte{0x0, 0x0}, AuthTag: []byte{0x0, 0x0, 0x0}}},
}
for _, vector := range testvectors {
- var result EncryptedPacket
+ result := *NewEncryptedPacket(vector.authTagLen)
err := result.UnmarshalBinary(vector.data)
if vector.valid {
if err != nil {
@@ -198,3 +230,33 @@ func TestEncryptedPacketUnmarshal(t *testing.T) {
}
}
}
+
+func generateRandomTestDataEncryptedPacket() (ep *EncryptedPacket) {
+ authtaglen := uint(rand.Int31n(32))
+ payloadlen := uint(rand.Int31n(2000))
+
+ ep = NewEncryptedPacket(authtaglen)
+ ep.SequenceNumber = rand.Uint32()
+ ep.SenderID = uint16(rand.Uint32())
+ ep.Mux = uint16(rand.Uint32())
+ ep.Payload = make([]byte, payloadlen)
+ rand.Read(ep.Payload)
+ return
+}
+
+func TestEncryptedPacketMarshalUnmarshal(t *testing.T) {
+ for i := 0; i < 100; i++ {
+ in := generateRandomTestDataEncryptedPacket()
+ marshalled, err := in.MarshalBinary()
+ if err != nil {
+ t.Fatal("unexpected error:", err)
+ }
+ out := NewEncryptedPacket(uint(len(in.AuthTag)))
+ if err = out.UnmarshalBinary(marshalled); err != nil {
+ t.Fatal("unexpected error:", err)
+ }
+ if !reflect.DeepEqual(in, out) {
+ t.Fatalf("unmarshalled packet is wrong: is '%+v', should be '%+v'", out, in)
+ }
+ }
+}