]> Cypherpunks.ru repositories - nncp.git/blob - src/tx_test.go
Note about buildability with 1.22
[nncp.git] / src / tx_test.go
1 // NNCP -- Node to Node copy, utilities for store-and-forward data exchange
2 // Copyright (C) 2016-2024 Sergey Matveev <stargrave@stargrave.org>
3 //
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.
7 //
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.
12 //
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/>.
15
16 package nncp
17
18 import (
19         "bytes"
20         "crypto/rand"
21         "io"
22         "os"
23         "path/filepath"
24         "testing"
25         "testing/quick"
26
27         xdr "github.com/davecgh/go-xdr/xdr2"
28 )
29
30 func TestTx(t *testing.T) {
31         f := func(
32                 hops uint8,
33                 pathSrc string,
34                 dataSize uint32,
35                 nice, replyNice uint8,
36                 minSize uint32,
37         ) bool {
38                 dataSize %= 1 << 20
39                 data := make([]byte, dataSize)
40                 if _, err := io.ReadFull(rand.Reader, data); err != nil {
41                         panic(err)
42                 }
43                 minSize %= 1 << 20
44                 if len(pathSrc) > int(MaxPathSize) {
45                         pathSrc = pathSrc[:MaxPathSize]
46                 }
47                 hops = hops % 4
48                 spool, err := os.MkdirTemp("", "testtx")
49                 if err != nil {
50                         panic(err)
51                 }
52                 defer os.RemoveAll(spool)
53                 nodeOur, err := NewNodeGenerate()
54                 if err != nil {
55                         panic(err)
56                 }
57                 nodeTgtOur, err := NewNodeGenerate()
58                 if err != nil {
59                         panic(err)
60                 }
61                 nodeTgt := nodeTgtOur.Their()
62                 ctx := Ctx{
63                         Spool:   spool,
64                         LogPath: filepath.Join(spool, "log.log"),
65                         Debug:   true,
66                         Self:    nodeOur,
67                         SelfId:  nodeOur.Id,
68                         Neigh:   make(map[NodeId]*Node, hops),
69                         Alias:   make(map[string]*NodeId),
70                 }
71                 ctx.Neigh[*nodeOur.Id] = nodeOur.Their()
72                 ctx.Neigh[*nodeTgt.Id] = nodeTgt
73                 privates := make(map[NodeId]*NodeOur, int(hops)+1)
74                 privates[*nodeTgt.Id] = nodeTgtOur
75                 privates[*nodeOur.Id] = nodeOur
76                 for i := uint8(0); i < hops; i++ {
77                         node, err := NewNodeGenerate()
78                         if err != nil {
79                                 panic(err)
80                         }
81                         ctx.Neigh[*node.Id] = node.Their()
82                         privates[*node.Id] = node
83                         nodeTgt.Via = append(nodeTgt.Via, node.Id)
84                 }
85                 pkt, err := NewPkt(PktTypeExec, replyNice, []byte(pathSrc))
86                 if err != nil {
87                         panic(err)
88                 }
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.Equal(bufR.Bytes(), []byte(data)) {
152                                         return false
153                                 }
154                         } else {
155                                 if pkt.Type != PktTypeTrns {
156                                         return false
157                                 }
158                                 if !bytes.Equal(pkt.Path[:MTHSize], vias[i+1][:]) {
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 }