-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpayload_test.go
62 lines (51 loc) · 1.24 KB
/
payload_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* Copyright (c) 2017 by Farsight Security, Inc.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package nmsg_test
import (
"testing"
"github.com/farsightsec/go-nmsg"
)
func testMessage(length int) nmsg.Message {
return &TestMessage{Bytes: make([]byte, length)}
}
func testPayload(length int) *nmsg.NmsgPayload {
p, err := nmsg.Payload(testMessage(length))
if err != nil {
return nil
}
return p
}
func (t *TestMessage) GetVid() uint32 { return 10 }
func (t *TestMessage) GetMsgtype() uint32 { return 20 }
func init() {
nmsg.Register(&TestMessage{})
}
func TestRegister(t *testing.T) {
msg, err := nmsg.NewMessage(10, 20)
if err != nil {
t.Error(err)
}
if _, ok := msg.(*TestMessage); !ok {
t.Errorf("NewMessage returned wrong type")
}
}
func TestPayload(t *testing.T) {
p, err := nmsg.Payload(testMessage(1000))
if err != nil {
t.Errorf("nmsg.Payload(): %s", err)
}
m, err := p.Message()
if err != nil {
t.Error(err)
}
if tp, ok := m.(*TestMessage); !ok {
t.Errorf("Wrong type from payload")
} else if len(tp.Bytes) != 1000 {
t.Error("decode failed")
}
}