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