2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
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 {
61 if err := quick.Check(f, nil); err != nil {
66 func TestPktEncRead(t *testing.T) {
67 node1, err := NewNodeGenerate()
71 node2, err := NewNodeGenerate()
75 f := func(path string, pathSize uint8, data [1 << 16]byte, size uint16, junk []byte) bool {
76 dataR := bytes.NewReader(data[:])
78 if len(path) > int(pathSize) {
79 path = path[:int(pathSize)]
81 pkt, err := NewPkt(PktTypeFile, path)
85 err = PktEncWrite(node1, node2.Their(), pkt, 123, int64(size), dataR, &ct)
91 nodes := make(map[NodeId]*Node)
92 nodes[*node1.Id] = node1.Their()
93 node, err := PktEncRead(node2, nodes, &ct, &pt)
97 if *node.Id != *node1.Id {
100 var pktBuf bytes.Buffer
101 xdr.Marshal(&pktBuf, &pkt)
102 return bytes.Compare(pt.Bytes(), append(pktBuf.Bytes(), data[:int(size)]...)) == 0
104 if err := quick.Check(f, nil); err != nil {