2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2023 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/>.
29 xdr "github.com/davecgh/go-xdr/xdr2"
32 func TestTx(t *testing.T) {
37 nice, replyNice uint8,
41 data := make([]byte, dataSize)
42 if _, err := io.ReadFull(rand.Reader, data); err != nil {
46 if len(pathSrc) > int(MaxPathSize) {
47 pathSrc = pathSrc[:MaxPathSize]
50 spool, err := os.MkdirTemp("", "testtx")
54 defer os.RemoveAll(spool)
55 nodeOur, err := NewNodeGenerate()
59 nodeTgtOur, err := NewNodeGenerate()
63 nodeTgt := nodeTgtOur.Their()
66 LogPath: filepath.Join(spool, "log.log"),
70 Neigh: make(map[NodeId]*Node, hops),
71 Alias: make(map[string]*NodeId),
73 ctx.Neigh[*nodeOur.Id] = nodeOur.Their()
74 ctx.Neigh[*nodeTgt.Id] = nodeTgt
75 privates := make(map[NodeId]*NodeOur, int(hops)+1)
76 privates[*nodeTgt.Id] = nodeTgtOur
77 privates[*nodeOur.Id] = nodeOur
78 for i := uint8(0); i < hops; i++ {
79 node, err := NewNodeGenerate()
83 ctx.Neigh[*node.Id] = node.Their()
84 privates[*node.Id] = node
85 nodeTgt.Via = append(nodeTgt.Via, node.Id)
87 pkt, err := NewPkt(PktTypeExec, replyNice, []byte(pathSrc))
91 src := bytes.NewReader(data)
92 dstNode, _, _, err := ctx.Tx(
107 sentJobs := make([]Job, 0, 1)
108 for txJob := range ctx.Jobs(dstNode.Id, TTx) {
109 sentJobs = append(sentJobs, txJob)
111 if len(sentJobs) != 1 {
115 fd, err := os.Open(txJob.Path)
120 var bufR bytes.Buffer
121 if _, err = io.Copy(&bufR, fd); err != nil {
124 var bufW bytes.Buffer
125 vias := append(nodeTgt.Via, nodeTgt.Id)
126 for i, hopId := range vias {
127 hopOur := privates[*hopId]
128 _, foundNode, _, err := PktEncRead(
129 hopOur, ctx.Neigh, &bufR, &bufW, true, nil,
134 if *foundNode.Id != *nodeOur.Id {
137 bufR, bufW = bufW, bufR
140 if _, err = xdr.Unmarshal(&bufR, &pkt); err != nil {
143 if *hopId == *nodeTgt.Id {
144 if pkt.Type != PktTypeExec {
147 if pkt.Nice != replyNice {
150 if !bytes.HasPrefix(pkt.Path[:], []byte(pathSrc)) {
153 if !bytes.Equal(bufR.Bytes(), []byte(data)) {
157 if pkt.Type != PktTypeTrns {
160 if !bytes.Equal(pkt.Path[:MTHSize], vias[i+1][:]) {
167 if err := quick.Check(f, nil); err != nil {