2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2022 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, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 xdr "github.com/davecgh/go-xdr/xdr2"
30 func TestPktEncWrite(t *testing.T) {
31 nodeOur, err := NewNodeGenerate()
35 nodeTheir, err := NewNodeGenerate()
47 data := make([]byte, dataSize)
48 if _, err = io.ReadFull(rand.Reader, data); err != nil {
52 if len(path) > int(pathSize) {
53 path = path[:int(pathSize)]
56 pkt, err := NewPkt(PktTypeFile, nice, []byte(path))
61 _, _, err = PktEncWrite(
69 bytes.NewReader(data),
76 if _, err = xdr.Unmarshal(&ct, &pktEnc); err != nil {
79 if *pktEnc.Sender != *nodeOur.Id {
84 if err := quick.Check(f, nil); err != nil {
89 func TestPktEncRead(t *testing.T) {
90 node1, err := NewNodeGenerate()
94 node2, err := NewNodeGenerate()
106 data := make([]byte, dataSize)
107 if _, err = io.ReadFull(rand.Reader, data); err != nil {
111 if len(path) > int(pathSize) {
112 path = path[:int(pathSize)]
115 pkt, err := NewPkt(PktTypeFile, nice, []byte(path))
120 _, _, err = PktEncWrite(
128 bytes.NewReader(data),
135 nodes := make(map[NodeId]*Node)
136 nodes[*node1.Id] = node1.Their()
137 _, node, sizeGot, err := PktEncRead(node2, nodes, &ct, &pt, true, nil)
141 if *node.Id != *node1.Id {
144 if sizeGot != int64(len(data)+int(PktOverhead)) {
147 var pktBuf bytes.Buffer
148 xdr.Marshal(&pktBuf, &pkt)
149 return bytes.Compare(pt.Bytes(), append(pktBuf.Bytes(), data...)) == 0
151 if err := quick.Check(f, nil); err != nil {