2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2020 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
30 xdr "github.com/davecgh/go-xdr/xdr2"
31 "golang.org/x/crypto/blake2b"
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]
41 spool, err := ioutil.TempDir("", "testtx")
45 defer os.RemoveAll(spool)
46 nodeOur, err := NewNodeGenerate()
50 nodeTgtOur, err := NewNodeGenerate()
54 nodeTgt := nodeTgtOur.Their()
57 LogPath: path.Join(spool, "log.log"),
61 Neigh: make(map[NodeId]*Node, hops),
62 Alias: make(map[string]*NodeId),
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()
74 ctx.Neigh[*node.Id] = node.Their()
75 privates[*node.Id] = node
76 nodeTgt.Via = append(nodeTgt.Via, node.Id)
78 pkt, err := NewPkt(PktTypeExec, replyNice, []byte(pathSrc))
79 src := strings.NewReader(data)
80 dstNode, err := ctx.Tx(
93 sentJobs := make([]Job, 0, 1)
94 for txJob := range ctx.Jobs(dstNode.Id, TTx) {
95 sentJobs = append(sentJobs, txJob)
97 if len(sentJobs) != 1 {
101 defer txJob.Fd.Close()
102 var bufR bytes.Buffer
103 if _, err = io.Copy(&bufR, txJob.Fd); err != nil {
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)
114 if *foundNode.Id != *nodeOur.Id {
117 bufR, bufW = bufW, bufR
120 if _, err = xdr.Unmarshal(&bufR, &pkt); err != nil {
123 if *hopId == *nodeTgt.Id {
124 if pkt.Type != PktTypeExec {
127 if pkt.Nice != replyNice {
130 if !bytes.HasPrefix(pkt.Path[:], []byte(pathSrc)) {
133 if bytes.Compare(bufR.Bytes(), []byte(data)) != 0 {
137 if pkt.Type != PktTypeTrns {
140 if bytes.Compare(pkt.Path[:blake2b.Size256], vias[i+1][:]) != 0 {
147 if err := quick.Check(f, nil); err != nil {