1 // NNCP -- Node to Node copy, utilities for store-and-forward data exchange
2 // Copyright (C) 2016-2024 Sergey Matveev <stargrave@stargrave.org>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
25 xdr "github.com/davecgh/go-xdr/xdr2"
28 func TestPktEncWrite(t *testing.T) {
29 nodeOur, err := NewNodeGenerate()
33 nodeTheir, err := NewNodeGenerate()
45 data := make([]byte, dataSize)
46 if _, err = io.ReadFull(rand.Reader, data); err != nil {
50 if len(path) > int(pathSize) {
51 path = path[:int(pathSize)]
54 pkt, err := NewPkt(PktTypeFile, nice, []byte(path))
59 _, _, err = PktEncWrite(
67 bytes.NewReader(data),
74 if _, err = xdr.Unmarshal(&ct, &pktEnc); err != nil {
77 if *pktEnc.Sender != *nodeOur.Id {
82 if err := quick.Check(f, nil); err != nil {
87 func TestPktEncRead(t *testing.T) {
88 node1, err := NewNodeGenerate()
92 node2, err := NewNodeGenerate()
104 data := make([]byte, dataSize)
105 if _, err = io.ReadFull(rand.Reader, data); err != nil {
109 if len(path) > int(pathSize) {
110 path = path[:int(pathSize)]
113 pkt, err := NewPkt(PktTypeFile, nice, []byte(path))
118 _, _, err = PktEncWrite(
126 bytes.NewReader(data),
133 nodes := make(map[NodeId]*Node)
134 nodes[*node1.Id] = node1.Their()
135 _, node, sizeGot, err := PktEncRead(node2, nodes, &ct, &pt, true, nil)
139 if *node.Id != *node1.Id {
142 if sizeGot != int64(len(data)+int(PktOverhead)) {
145 var pktBuf bytes.Buffer
146 xdr.Marshal(&pktBuf, &pkt)
147 return bytes.Equal(pt.Bytes(), append(pktBuf.Bytes(), data...))
149 if err := quick.Check(f, nil); err != nil {