2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2021 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"
33 func TestTx(t *testing.T) {
34 f := func(hops uint8, pathSrc, data string, nice, replyNice uint8, padSize int16) bool {
35 if len(pathSrc) > int(MaxPathSize) {
36 pathSrc = pathSrc[:MaxPathSize]
40 spool, err := ioutil.TempDir("", "testtx")
44 defer os.RemoveAll(spool)
45 nodeOur, err := NewNodeGenerate()
49 nodeTgtOur, err := NewNodeGenerate()
53 nodeTgt := nodeTgtOur.Their()
56 LogPath: path.Join(spool, "log.log"),
60 Neigh: make(map[NodeId]*Node, hops),
61 Alias: make(map[string]*NodeId),
63 ctx.Neigh[*nodeOur.Id] = nodeOur.Their()
64 ctx.Neigh[*nodeTgt.Id] = nodeTgt
65 privates := make(map[NodeId]*NodeOur, int(hops)+1)
66 privates[*nodeTgt.Id] = nodeTgtOur
67 privates[*nodeOur.Id] = nodeOur
68 for i := uint8(0); i < hops; i++ {
69 node, err := NewNodeGenerate()
73 ctx.Neigh[*node.Id] = node.Their()
74 privates[*node.Id] = node
75 nodeTgt.Via = append(nodeTgt.Via, node.Id)
77 pkt, err := NewPkt(PktTypeExec, replyNice, []byte(pathSrc))
78 src := strings.NewReader(data)
79 dstNode, err := ctx.Tx(
92 sentJobs := make([]Job, 0, 1)
93 for txJob := range ctx.Jobs(dstNode.Id, TTx) {
94 sentJobs = append(sentJobs, txJob)
96 if len(sentJobs) != 1 {
100 fd, err := os.Open(txJob.Path)
105 var bufR bytes.Buffer
106 if _, err = io.Copy(&bufR, fd); err != nil {
109 var bufW bytes.Buffer
110 vias := append(nodeTgt.Via, nodeTgt.Id)
111 for i, hopId := range vias {
112 hopOur := privates[*hopId]
113 foundNode, _, err := PktEncRead(hopOur, ctx.Neigh, &bufR, &bufW)
117 if *foundNode.Id != *nodeOur.Id {
120 bufR, bufW = bufW, bufR
123 if _, err = xdr.Unmarshal(&bufR, &pkt); err != nil {
126 if *hopId == *nodeTgt.Id {
127 if pkt.Type != PktTypeExec {
130 if pkt.Nice != replyNice {
133 if !bytes.HasPrefix(pkt.Path[:], []byte(pathSrc)) {
136 if bytes.Compare(bufR.Bytes(), []byte(data)) != 0 {
140 if pkt.Type != PktTypeTrns {
143 if bytes.Compare(pkt.Path[:MTHSize], vias[i+1][:]) != 0 {
150 if err := quick.Check(f, nil); err != nil {