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