]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/pkt_test.go
Initial
[nncp.git] / src / cypherpunks.ru / nncp / pkt_test.go
1 /*
2 NNCP -- Node-to-Node CoPy
3 Copyright (C) 2016-2017 Sergey Matveev <stargrave@stargrave.org>
4
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.
9
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.
14
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/>.
17 */
18
19 package nncp
20
21 import (
22         "bytes"
23         "testing"
24         "testing/quick"
25
26         "github.com/davecgh/go-xdr/xdr2"
27 )
28
29 func TestPktEncWrite(t *testing.T) {
30         nodeOur, err := NewNodeGenerate()
31         if err != nil {
32                 panic(err)
33         }
34         nodeTheir, err := NewNodeGenerate()
35         if err != nil {
36                 panic(err)
37         }
38         f := func(path string, pathSize uint8, data [1 << 16]byte, size uint16) bool {
39                 dataR := bytes.NewReader(data[:])
40                 var ct bytes.Buffer
41                 if len(path) > int(pathSize) {
42                         path = path[:int(pathSize)]
43                 }
44                 pkt, err := NewPkt(PktTypeFile, path)
45                 if err != nil {
46                         panic(err)
47                 }
48                 err = PktEncWrite(nodeOur, nodeTheir.Their(), pkt, 123, int64(size), dataR, &ct)
49                 if err != nil {
50                         return false
51                 }
52                 var pktEnc PktEnc
53                 if _, err = xdr.Unmarshal(&ct, &pktEnc); err != nil {
54                         return false
55                 }
56                 if *pktEnc.Sender != *nodeOur.Id {
57                         return false
58                 }
59                 if pktEnc.Size != uint64(ct.Len()) {
60                         return false
61                 }
62                 return true
63         }
64         if err := quick.Check(f, nil); err != nil {
65                 t.Error(err)
66         }
67 }
68
69 func TestPktEncRead(t *testing.T) {
70         node1, err := NewNodeGenerate()
71         if err != nil {
72                 panic(err)
73         }
74         node2, err := NewNodeGenerate()
75         if err != nil {
76                 panic(err)
77         }
78         f := func(path string, pathSize uint8, data [1 << 16]byte, size uint16) bool {
79                 dataR := bytes.NewReader(data[:])
80                 var ct bytes.Buffer
81                 if len(path) > int(pathSize) {
82                         path = path[:int(pathSize)]
83                 }
84                 pkt, err := NewPkt(PktTypeFile, path)
85                 if err != nil {
86                         panic(err)
87                 }
88                 err = PktEncWrite(node1, node2.Their(), pkt, 123, int64(size), dataR, &ct)
89                 if err != nil {
90                         return false
91                 }
92                 var pt bytes.Buffer
93                 nodes := make(map[NodeId]*Node)
94                 nodes[*node1.Id] = node1.Their()
95                 node, err := PktEncRead(node2, nodes, &ct, &pt)
96                 if err != nil {
97                         return false
98                 }
99                 if *node.Id != *node1.Id {
100                         return false
101                 }
102                 var pktBuf bytes.Buffer
103                 xdr.Marshal(&pktBuf, &pkt)
104                 return bytes.Compare(pt.Bytes(), append(pktBuf.Bytes(), data[:int(size)]...)) == 0
105         }
106         if err := quick.Check(f, nil); err != nil {
107                 t.Error(err)
108         }
109 }