]> Cypherpunks.ru repositories - nncp.git/blob - src/tx_test.go
Raise copyright years
[nncp.git] / src / tx_test.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2022 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, version 3 of the License.
8
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.
13
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/>.
16 */
17
18 package nncp
19
20 import (
21         "bytes"
22         "crypto/rand"
23         "io"
24         "io/ioutil"
25         "os"
26         "path"
27         "testing"
28         "testing/quick"
29
30         xdr "github.com/davecgh/go-xdr/xdr2"
31 )
32
33 func TestTx(t *testing.T) {
34         f := func(
35                 hops uint8,
36                 pathSrc string,
37                 dataSize uint32,
38                 nice, replyNice uint8,
39                 minSize uint32,
40         ) bool {
41                 dataSize %= 1 << 20
42                 data := make([]byte, dataSize)
43                 if _, err := io.ReadFull(rand.Reader, data); err != nil {
44                         panic(err)
45                 }
46                 minSize %= 1 << 20
47                 if len(pathSrc) > int(MaxPathSize) {
48                         pathSrc = pathSrc[:MaxPathSize]
49                 }
50                 hops = hops % 4
51                 spool, err := ioutil.TempDir("", "testtx")
52                 if err != nil {
53                         panic(err)
54                 }
55                 defer os.RemoveAll(spool)
56                 nodeOur, err := NewNodeGenerate()
57                 if err != nil {
58                         panic(err)
59                 }
60                 nodeTgtOur, err := NewNodeGenerate()
61                 if err != nil {
62                         panic(err)
63                 }
64                 nodeTgt := nodeTgtOur.Their()
65                 ctx := Ctx{
66                         Spool:   spool,
67                         LogPath: path.Join(spool, "log.log"),
68                         Debug:   true,
69                         Self:    nodeOur,
70                         SelfId:  nodeOur.Id,
71                         Neigh:   make(map[NodeId]*Node, hops),
72                         Alias:   make(map[string]*NodeId),
73                 }
74                 ctx.Neigh[*nodeOur.Id] = nodeOur.Their()
75                 ctx.Neigh[*nodeTgt.Id] = nodeTgt
76                 privates := make(map[NodeId]*NodeOur, int(hops)+1)
77                 privates[*nodeTgt.Id] = nodeTgtOur
78                 privates[*nodeOur.Id] = nodeOur
79                 for i := uint8(0); i < hops; i++ {
80                         node, err := NewNodeGenerate()
81                         if err != nil {
82                                 panic(err)
83                         }
84                         ctx.Neigh[*node.Id] = node.Their()
85                         privates[*node.Id] = node
86                         nodeTgt.Via = append(nodeTgt.Via, node.Id)
87                 }
88                 pkt, err := NewPkt(PktTypeExec, replyNice, []byte(pathSrc))
89                 src := bytes.NewReader(data)
90                 dstNode, _, err := ctx.Tx(
91                         nodeTgt,
92                         pkt,
93                         123,
94                         int64(src.Len()),
95                         int64(minSize),
96                         MaxFileSize,
97                         src,
98                         "pktName",
99                         nil,
100                 )
101                 if err != nil {
102                         return false
103                 }
104
105                 sentJobs := make([]Job, 0, 1)
106                 for txJob := range ctx.Jobs(dstNode.Id, TTx) {
107                         sentJobs = append(sentJobs, txJob)
108                 }
109                 if len(sentJobs) != 1 {
110                         return false
111                 }
112                 txJob := sentJobs[0]
113                 fd, err := os.Open(txJob.Path)
114                 if err != nil {
115                         panic(err)
116                 }
117                 defer fd.Close()
118                 var bufR bytes.Buffer
119                 if _, err = io.Copy(&bufR, fd); err != nil {
120                         panic(err)
121                 }
122                 var bufW bytes.Buffer
123                 vias := append(nodeTgt.Via, nodeTgt.Id)
124                 for i, hopId := range vias {
125                         hopOur := privates[*hopId]
126                         _, foundNode, _, err := PktEncRead(
127                                 hopOur, ctx.Neigh, &bufR, &bufW, true, nil,
128                         )
129                         if err != nil {
130                                 return false
131                         }
132                         if *foundNode.Id != *nodeOur.Id {
133                                 return false
134                         }
135                         bufR, bufW = bufW, bufR
136                         bufW.Reset()
137                         var pkt Pkt
138                         if _, err = xdr.Unmarshal(&bufR, &pkt); err != nil {
139                                 return false
140                         }
141                         if *hopId == *nodeTgt.Id {
142                                 if pkt.Type != PktTypeExec {
143                                         return false
144                                 }
145                                 if pkt.Nice != replyNice {
146                                         return false
147                                 }
148                                 if !bytes.HasPrefix(pkt.Path[:], []byte(pathSrc)) {
149                                         return false
150                                 }
151                                 if bytes.Compare(bufR.Bytes(), []byte(data)) != 0 {
152                                         return false
153                                 }
154                         } else {
155                                 if pkt.Type != PktTypeTrns {
156                                         return false
157                                 }
158                                 if bytes.Compare(pkt.Path[:MTHSize], vias[i+1][:]) != 0 {
159                                         return false
160                                 }
161                         }
162                 }
163                 return true
164         }
165         if err := quick.Check(f, nil); err != nil {
166                 t.Error(err)
167         }
168 }