-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplus_test.go
110 lines (85 loc) · 3.3 KB
/
plus_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package PLUS_test
import (
. "github.com/mami-project/plus-lib"
packet "github.com/mami-project/plus-lib/packet"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Plus", func() {
Context("ConnectionManager", func() {
It("Deals with new basic packets", func() {
p := packet.NewBasicPLUSPacket(false, false, false, 1234, 11, 12, []byte{0x00})
packetConn := NewMockPacketConn()
packetConn.PutData(p.Buffer())
manager := NewConnectionManager(packetConn)
packet, addr, err := manager.ReadPacket()
Expect(err).ToNot(HaveOccurred())
Expect(packet).ToNot(Equal(nil))
Expect(addr).ToNot(Equal(nil))
conn, feedbackData, err := manager.ProcessPacket(p, addr)
Expect(err).ToNot(HaveOccurred())
Expect(conn).ToNot(Equal(nil))
Expect(conn.CAT()).To(Equal(uint64(1234)))
Expect(conn.PSE()).To(Equal(uint32(11)))
var nilbuf []byte
Expect(feedbackData).To(Equal(nilbuf))
})
It("Deals with new extended packets", func() {
p, err := packet.NewExtendedPLUSPacket(false, false, false, 1234, 11, 12, 0x01, 0x00, []byte{0xCA, 0xFE}, []byte{0xBA, 0xBE})
Expect(err).ToNot(HaveOccurred())
packetConn := NewMockPacketConn()
packetConn.PutData(p.Buffer())
manager := NewConnectionManager(packetConn)
packet, addr, err := manager.ReadPacket()
Expect(err).ToNot(HaveOccurred())
Expect(packet).ToNot(Equal(nil))
Expect(addr).ToNot(Equal(nil))
conn, feedbackData, err := manager.ProcessPacket(p, addr)
Expect(err).ToNot(HaveOccurred())
Expect(conn).ToNot(Equal(nil))
Expect(conn.CAT()).To(Equal(uint64(1234)))
Expect(conn.PSE()).To(Equal(uint32(11)))
Expect(feedbackData).To(Equal(p.Header()))
})
It("Deals with new extended packets with INTEGRITY_FULL", func() {
p, err := packet.NewExtendedPLUSPacket(false, false, false, 1234, 11, 12, 0x01, 0x03, []byte{0xCA, 0xFE}, []byte{0xBA, 0xBE})
Expect(err).ToNot(HaveOccurred())
packetConn := NewMockPacketConn()
packetConn.PutData(p.Buffer())
manager := NewConnectionManager(packetConn)
packet, addr, err := manager.ReadPacket()
Expect(err).ToNot(HaveOccurred())
Expect(packet).ToNot(Equal(nil))
Expect(addr).ToNot(Equal(nil))
conn, feedbackData, err := manager.ProcessPacket(p, addr)
Expect(err).ToNot(HaveOccurred())
Expect(conn).ToNot(Equal(nil))
Expect(conn.CAT()).To(Equal(uint64(1234)))
Expect(conn.PSE()).To(Equal(uint32(11)))
var nilbuf []byte
Expect(feedbackData).To(Equal(nilbuf))
})
It("Deals with connections and packets", func() {
p := packet.NewBasicPLUSPacket(false, false, false, 1234, 11, 12, []byte{0x12, 0x21, 0x31, 0x13})
packetConn := NewMockPacketConn()
packetConn.PutData(p.Buffer())
manager := NewConnectionManager(packetConn)
go manager.Listen()
conn := manager.Accept()
buffer := make([]byte, 4096)
n, err := conn.Read(buffer)
Expect(err).ToNot(HaveOccurred())
buffer = buffer[:n]
Expect(buffer).To(Equal([]byte{0x12, 0x21, 0x31, 0x13}))
p = packet.NewBasicPLUSPacket(false, false, false, 1234, 99, 88, []byte{0x33, 0x44, 0x55, 0x66})
packetConn.PutData(p.Buffer())
buffer = make([]byte, 4096)
n, err = conn.Read(buffer)
Expect(err).ToNot(HaveOccurred())
buffer = buffer[:n]
Expect(buffer).To(Equal([]byte{0x33, 0x44, 0x55, 0x66}))
conn.Close()
manager.Close()
})
})
})