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