dissertation-2-code/proxy/packet_test.go

51 lines
1.2 KiB
Go
Raw Normal View History

2020-10-25 15:36:34 +00:00
package proxy
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)
type AlmostUselessMac bool
func (AlmostUselessMac) CodeLength() int {
return 4
}
func (AlmostUselessMac) Generate([]byte) []byte {
return []byte{'a', 'b', 'c', 'd'}
}
func (u AlmostUselessMac) Verify(_, sum []byte) error {
if !(sum[0] == 'a' && sum[1] == 'b' && sum[2] == 'c' && sum[3] == 'd') {
return ErrBadChecksum
}
return nil
}
func TestPacket_Marshal(t *testing.T) {
testContent := []byte("A test string is the content of this packet.")
testPacket := NewPacket(testContent)
testMac := AlmostUselessMac(false)
t.Run("Length", func(t *testing.T) {
marshalled := testPacket.Marshal(testMac)
assert.Len(t, marshalled, len(testContent)+8+4)
})
}
func TestUnmarshalPacket(t *testing.T) {
testContent := []byte("A test string is the content of this packet.")
testPacket := NewPacket(testContent)
testMac := AlmostUselessMac(false)
testMarshalled := testPacket.Marshal(testMac)
t.Run("Success", func(t *testing.T) {
content, err := UnmarshalPacket(testMarshalled, testMac)
require.Nil(t, err)
assert.Equal(t, testContent, content.Raw())
})
}