]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/tx_test.go
Initial
[nncp.git] / src / cypherpunks.ru / nncp / tx_test.go
1 /*
2 NNCP -- Node-to-Node CoPy
3 Copyright (C) 2016-2017 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 uint8) 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                         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(PktTypeMail, pathSrc)
79                 src := strings.NewReader(data)
80                 dstNode, err := ctx.Tx(nodeTgt, pkt, 123, int64(src.Len()), src)
81                 if err != nil {
82                         return false
83                 }
84
85                 sentJobs := make([]Job, 0, 1)
86                 for txJob := range ctx.Jobs(dstNode.Id, TTx) {
87                         sentJobs = append(sentJobs, txJob)
88                 }
89                 if len(sentJobs) != 1 {
90                         return false
91                 }
92                 txJob := sentJobs[0]
93                 defer txJob.Fd.Close()
94                 var bufR bytes.Buffer
95                 if _, err = io.Copy(&bufR, txJob.Fd); err != nil {
96                         panic(err)
97                 }
98                 var bufW bytes.Buffer
99                 vias := append(nodeTgt.Via, nodeTgt.Id)
100                 for i, hopId := range vias {
101                         hopOur := privates[*hopId]
102                         foundNode, err := PktEncRead(hopOur, ctx.Neigh, &bufR, &bufW)
103                         if err != nil {
104                                 return false
105                         }
106                         if *foundNode.Id != *nodeOur.Id {
107                                 return false
108                         }
109                         bufR, bufW = bufW, bufR
110                         bufW.Reset()
111                         var pkt Pkt
112                         if _, err = xdr.Unmarshal(&bufR, &pkt); err != nil {
113                                 return false
114                         }
115                         if *hopId == *nodeTgt.Id {
116                                 if pkt.Type != PktTypeMail {
117                                         return false
118                                 }
119                                 if !bytes.HasPrefix(pkt.Path[:], []byte(pathSrc)) {
120                                         return false
121                                 }
122                                 if bytes.Compare(bufR.Bytes(), []byte(data)) != 0 {
123                                         return false
124                                 }
125                         } else {
126                                 if pkt.Type != PktTypeTrns {
127                                         return false
128                                 }
129                                 if bytes.Compare(pkt.Path[:blake2b.Size256], vias[i+1][:]) != 0 {
130                                         return false
131                                 }
132                         }
133                 }
134                 return true
135         }
136         if err := quick.Check(f, nil); err != nil {
137                 t.Error(err)
138         }
139 }