1 // NNCP -- Node to Node copy, utilities for store-and-forward data exchange
2 // Copyright (C) 2016-2024 Sergey Matveev <stargrave@stargrave.org>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
27 xdr "github.com/davecgh/go-xdr/xdr2"
30 func TestTx(t *testing.T) {
35 nice, replyNice uint8,
39 data := make([]byte, dataSize)
40 if _, err := io.ReadFull(rand.Reader, data); err != nil {
44 if len(pathSrc) > int(MaxPathSize) {
45 pathSrc = pathSrc[:MaxPathSize]
48 spool, err := os.MkdirTemp("", "testtx")
52 defer os.RemoveAll(spool)
53 nodeOur, err := NewNodeGenerate()
57 nodeTgtOur, err := NewNodeGenerate()
61 nodeTgt := nodeTgtOur.Their()
64 LogPath: filepath.Join(spool, "log.log"),
68 Neigh: make(map[NodeId]*Node, hops),
69 Alias: make(map[string]*NodeId),
71 ctx.Neigh[*nodeOur.Id] = nodeOur.Their()
72 ctx.Neigh[*nodeTgt.Id] = nodeTgt
73 privates := make(map[NodeId]*NodeOur, int(hops)+1)
74 privates[*nodeTgt.Id] = nodeTgtOur
75 privates[*nodeOur.Id] = nodeOur
76 for i := uint8(0); i < hops; i++ {
77 node, err := NewNodeGenerate()
81 ctx.Neigh[*node.Id] = node.Their()
82 privates[*node.Id] = node
83 nodeTgt.Via = append(nodeTgt.Via, node.Id)
85 pkt, err := NewPkt(PktTypeExec, replyNice, []byte(pathSrc))
89 src := bytes.NewReader(data)
90 dstNode, _, _, err := ctx.Tx(
105 sentJobs := make([]Job, 0, 1)
106 for txJob := range ctx.Jobs(dstNode.Id, TTx) {
107 sentJobs = append(sentJobs, txJob)
109 if len(sentJobs) != 1 {
113 fd, err := os.Open(txJob.Path)
118 var bufR bytes.Buffer
119 if _, err = io.Copy(&bufR, fd); err != nil {
122 var bufW bytes.Buffer
123 vias := append(nodeTgt.Via, nodeTgt.Id)
124 for i, hopId := range vias {
125 hopOur := privates[*hopId]
126 _, foundNode, _, err := PktEncRead(
127 hopOur, ctx.Neigh, &bufR, &bufW, true, nil,
132 if *foundNode.Id != *nodeOur.Id {
135 bufR, bufW = bufW, bufR
138 if _, err = xdr.Unmarshal(&bufR, &pkt); err != nil {
141 if *hopId == *nodeTgt.Id {
142 if pkt.Type != PktTypeExec {
145 if pkt.Nice != replyNice {
148 if !bytes.HasPrefix(pkt.Path[:], []byte(pathSrc)) {
151 if !bytes.Equal(bufR.Bytes(), []byte(data)) {
155 if pkt.Type != PktTypeTrns {
158 if !bytes.Equal(pkt.Path[:MTHSize], vias[i+1][:]) {
165 if err := quick.Check(f, nil); err != nil {