2 NNCP -- Node-to-Node CoPy
3 Copyright (C) 2016-2017 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 "github.com/davecgh/go-xdr/xdr2"
29 func TestPktEncWrite(t *testing.T) {
30 nodeOur, err := NewNodeGenerate()
34 nodeTheir, err := NewNodeGenerate()
38 f := func(path string, pathSize uint8, data [1 << 16]byte, size uint16) bool {
39 dataR := bytes.NewReader(data[:])
41 if len(path) > int(pathSize) {
42 path = path[:int(pathSize)]
44 pkt, err := NewPkt(PktTypeFile, path)
48 err = PktEncWrite(nodeOur, nodeTheir.Their(), pkt, 123, int64(size), dataR, &ct)
53 if _, err = xdr.Unmarshal(&ct, &pktEnc); err != nil {
56 if *pktEnc.Sender != *nodeOur.Id {
59 if pktEnc.Size != uint64(ct.Len()) {
64 if err := quick.Check(f, nil); err != nil {
69 func TestPktEncRead(t *testing.T) {
70 node1, err := NewNodeGenerate()
74 node2, err := NewNodeGenerate()
78 f := func(path string, pathSize uint8, data [1 << 16]byte, size uint16) bool {
79 dataR := bytes.NewReader(data[:])
81 if len(path) > int(pathSize) {
82 path = path[:int(pathSize)]
84 pkt, err := NewPkt(PktTypeFile, path)
88 err = PktEncWrite(node1, node2.Their(), pkt, 123, int64(size), dataR, &ct)
93 nodes := make(map[NodeId]*Node)
94 nodes[*node1.Id] = node1.Their()
95 node, err := PktEncRead(node2, nodes, &ct, &pt)
99 if *node.Id != *node1.Id {
102 var pktBuf bytes.Buffer
103 xdr.Marshal(&pktBuf, &pkt)
104 return bytes.Compare(pt.Bytes(), append(pktBuf.Bytes(), data[:int(size)]...)) == 0
106 if err := quick.Check(f, nil); err != nil {