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/>.
32 xdr "github.com/davecgh/go-xdr/xdr2"
39 func dirFiles(path string) []string {
40 dir, err := os.Open(path)
45 names, err := dir.Readdirnames(0)
52 func TestTossExec(t *testing.T) {
53 f := func(replyNice uint8, handleRaw uint32, recipients [16]uint8) bool {
54 handle := strconv.Itoa(int(handleRaw))
55 for i, recipient := range recipients {
56 recipients[i] = recipient % 8
58 spool, err := os.MkdirTemp("", "testtoss")
62 defer os.RemoveAll(spool)
63 nodeOur, err := NewNodeGenerate()
72 Neigh: make(map[NodeId]*Node),
73 Alias: make(map[string]*NodeId),
74 LogPath: filepath.Join(spool, "log.log"),
77 ctx.Neigh[*nodeOur.Id] = nodeOur.Their()
78 privates := make(map[uint8]*NodeOur)
79 for _, recipient := range recipients {
80 if _, exists := privates[recipient]; exists {
83 our, err := NewNodeGenerate()
88 privates[recipient] = our
89 ctx.Neigh[*our.Id] = our.Their()
91 for _, recipient := range recipients {
93 ctx.Neigh[*privates[recipient].Id],
97 []string{"arg0", "arg1"},
98 strings.NewReader("BODY\n"),
107 for _, recipient := range recipients {
108 ctx.Self = privates[recipient]
109 rxPath := filepath.Join(spool, ctx.Self.Id.String(), string(TRx))
110 os.Rename(filepath.Join(spool, ctx.Self.Id.String(), string(TTx)), rxPath)
111 if len(dirFiles(rxPath)) == 0 {
114 ctx.Toss(ctx.Self.Id, TRx, &TossOpts{Nice: DefaultNiceExec - 1})
115 if len(dirFiles(rxPath)) == 0 {
118 ctx.Neigh[*nodeOur.Id].Exec = make(map[string][]string)
119 ctx.Neigh[*nodeOur.Id].Exec[handle] = []string{"/bin/sh", "-c", "false"}
120 ctx.Toss(ctx.Self.Id, TRx, &TossOpts{Nice: DefaultNiceExec})
121 if len(dirFiles(rxPath)) == 0 {
124 ctx.Neigh[*nodeOur.Id].Exec[handle] = []string{
127 "echo $NNCP_NICE $0 $1 >> %s ; cat >> %s",
128 filepath.Join(spool, "mbox"),
129 filepath.Join(spool, "mbox"),
132 ctx.Toss(ctx.Self.Id, TRx, &TossOpts{Nice: DefaultNiceExec})
133 if len(dirFiles(rxPath)) != 0 {
137 mbox, err := os.ReadFile(filepath.Join(spool, "mbox"))
141 expected := make([]byte, 0, 16)
142 for i := 0; i < 16; i++ {
145 []byte(fmt.Sprintf("%d arg0 arg1\n", replyNice))...,
147 expected = append(expected, []byte("BODY\n")...)
149 return bytes.Equal(mbox, expected)
151 if err := quick.Check(f, nil); err != nil {
156 func TestTossFile(t *testing.T) {
157 f := func(fileSizes []uint8) bool {
158 if len(fileSizes) == 0 {
161 files := make(map[string][]byte)
162 for i, fileSize := range fileSizes {
164 // to prevent chunked send
167 data := make([]byte, fileSize)
168 if _, err := io.ReadFull(rand.Reader, data); err != nil {
171 files[strconv.Itoa(i)] = data
173 spool, err := os.MkdirTemp("", "testtoss")
177 defer os.RemoveAll(spool)
178 nodeOur, err := NewNodeGenerate()
187 Neigh: make(map[NodeId]*Node),
188 Alias: make(map[string]*NodeId),
189 LogPath: filepath.Join(spool, "log.log"),
192 ctx.Neigh[*nodeOur.Id] = nodeOur.Their()
193 incomingPath := filepath.Join(spool, "incoming")
194 for _, fileData := range files {
195 hasher := MTHNew(0, 0)
196 hasher.Write(fileData)
197 fileName := Base32Codec.EncodeToString(hasher.Sum(nil))
198 src := filepath.Join(spool, fileName)
199 if err := os.WriteFile(src, fileData, os.FileMode(0600)); err != nil {
202 if err := ctx.TxFile(
203 ctx.Neigh[*nodeOur.Id],
216 rxPath := filepath.Join(spool, ctx.Self.Id.String(), string(TRx))
217 os.Rename(filepath.Join(spool, ctx.Self.Id.String(), string(TTx)), rxPath)
218 ctx.Toss(ctx.Self.Id, TRx, &TossOpts{Nice: DefaultNiceFile})
219 if len(dirFiles(rxPath)) == 0 {
222 ctx.Neigh[*nodeOur.Id].Incoming = &incomingPath
223 if ctx.Toss(ctx.Self.Id, TRx, &TossOpts{Nice: DefaultNiceFile}) {
226 if len(dirFiles(rxPath)) != 0 {
229 for _, fileData := range files {
230 hasher := MTHNew(0, 0)
231 hasher.Write(fileData)
232 fileName := Base32Codec.EncodeToString(hasher.Sum(nil))
233 data, err := os.ReadFile(filepath.Join(incomingPath, fileName))
237 if !bytes.Equal(data, fileData) {
243 if err := quick.Check(f, nil); err != nil {
248 func TestTossFileSameName(t *testing.T) {
249 f := func(filesRaw uint8) bool {
250 files := int(filesRaw)%8 + 1
251 spool, err := os.MkdirTemp("", "testtoss")
255 defer os.RemoveAll(spool)
256 nodeOur, err := NewNodeGenerate()
265 Neigh: make(map[NodeId]*Node),
266 Alias: make(map[string]*NodeId),
267 LogPath: filepath.Join(spool, "log.log"),
270 ctx.Neigh[*nodeOur.Id] = nodeOur.Their()
271 srcPath := filepath.Join(spool, "junk")
272 if err = os.WriteFile(
274 []byte("doesnotmatter"),
280 incomingPath := filepath.Join(spool, "incoming")
281 for i := 0; i < files; i++ {
282 if err := ctx.TxFile(
283 ctx.Neigh[*nodeOur.Id],
296 rxPath := filepath.Join(spool, ctx.Self.Id.String(), string(TRx))
297 os.Rename(filepath.Join(spool, ctx.Self.Id.String(), string(TTx)), rxPath)
298 ctx.Neigh[*nodeOur.Id].Incoming = &incomingPath
299 ctx.Toss(ctx.Self.Id, TRx, &TossOpts{Nice: DefaultNiceFile})
300 expected := make(map[string]struct{})
301 expected["samefile"] = struct{}{}
302 for i := 0; i < files-1; i++ {
303 expected["samefile."+strconv.Itoa(i)] = struct{}{}
305 for _, filename := range dirFiles(incomingPath) {
306 if _, exists := expected[filename]; !exists {
309 delete(expected, filename)
311 return len(expected) == 0
313 if err := quick.Check(f, nil); err != nil {
318 func TestTossFreq(t *testing.T) {
319 f := func(fileSizes []uint8, replyNice uint8) bool {
320 if len(fileSizes) == 0 {
323 spool, err := os.MkdirTemp("", "testtoss")
327 defer os.RemoveAll(spool)
328 nodeOur, err := NewNodeGenerate()
337 Neigh: make(map[NodeId]*Node),
338 Alias: make(map[string]*NodeId),
339 LogPath: filepath.Join(spool, "log.log"),
342 ctx.Neigh[*nodeOur.Id] = nodeOur.Their()
343 files := make(map[string][]byte)
344 for i, fileSize := range fileSizes {
346 // to prevent chunked send
349 fileData := make([]byte, fileSize)
350 if _, err := io.ReadFull(rand.Reader, fileData); err != nil {
353 fileName := strconv.Itoa(i)
354 files[fileName] = fileData
355 if err := ctx.TxFreq(
356 ctx.Neigh[*nodeOur.Id],
367 rxPath := filepath.Join(spool, ctx.Self.Id.String(), string(TRx))
368 txPath := filepath.Join(spool, ctx.Self.Id.String(), string(TTx))
369 os.Rename(txPath, rxPath)
370 os.MkdirAll(txPath, os.FileMode(0700))
371 ctx.Toss(ctx.Self.Id, TRx, &TossOpts{Nice: DefaultNiceFreq})
372 if len(dirFiles(txPath)) != 0 || len(dirFiles(rxPath)) == 0 {
375 ctx.Neigh[*nodeOur.Id].FreqPath = &spool
376 ctx.Toss(ctx.Self.Id, TRx, &TossOpts{Nice: DefaultNiceFreq})
377 if len(dirFiles(txPath)) != 0 || len(dirFiles(rxPath)) == 0 {
380 for fileName, fileData := range files {
381 if err := os.WriteFile(
382 filepath.Join(spool, fileName),
389 ctx.Toss(ctx.Self.Id, TRx, &TossOpts{Nice: DefaultNiceFreq})
390 if len(dirFiles(txPath)) == 0 || len(dirFiles(rxPath)) != 0 {
393 for job := range ctx.Jobs(ctx.Self.Id, TTx) {
395 fd, err := os.Open(job.Path)
400 _, _, _, err = PktEncRead(ctx.Self, ctx.Neigh, fd, &buf, true, nil)
406 if _, err = xdr.Unmarshal(&buf, &pkt); err != nil {
410 if pkt.Nice != replyNice {
413 if !bytes.Equal(buf.Bytes(), files[string(pkt.Path[:int(pkt.PathLen)])]) {
419 if err := quick.Check(f, nil); err != nil {
424 func TestTossTrns(t *testing.T) {
425 f := func(datumLens []uint8) bool {
426 if len(datumLens) == 0 {
429 datum := make(map[int][]byte)
430 for i, datumLen := range datumLens {
432 data := make([]byte, datumLen)
433 if _, err := io.ReadFull(rand.Reader, data); err != nil {
438 spool, err := os.MkdirTemp("", "testtoss")
442 defer os.RemoveAll(spool)
443 nodeOur, err := NewNodeGenerate()
452 Neigh: make(map[NodeId]*Node),
453 Alias: make(map[string]*NodeId),
454 LogPath: filepath.Join(spool, "log.log"),
457 ctx.Neigh[*nodeOur.Id] = nodeOur.Their()
458 rxPath := filepath.Join(spool, ctx.Self.Id.String(), string(TRx))
459 os.MkdirAll(rxPath, os.FileMode(0700))
460 txPath := filepath.Join(spool, ctx.Self.Id.String(), string(TTx))
461 os.MkdirAll(txPath, os.FileMode(0700))
462 for _, data := range datum {
464 Magic: MagicNNCPPv3.B,
468 copy(pktTrans.Path[:], nodeOur.Id[:])
470 if _, _, err := PktEncWrite(
472 ctx.Neigh[*nodeOur.Id],
476 bytes.NewReader(data),
482 hasher := MTHNew(0, 0)
483 hasher.Write(dst.Bytes())
484 if err := os.WriteFile(
485 filepath.Join(rxPath, Base32Codec.EncodeToString(hasher.Sum(nil))),
492 ctx.Toss(ctx.Self.Id, TRx, &TossOpts{Nice: 123})
493 if len(dirFiles(rxPath)) != 0 {
496 for _, filename := range dirFiles(txPath) {
497 dataRead, err := os.ReadFile(filepath.Join(txPath, filename))
501 for k, data := range datum {
502 if bytes.Equal(dataRead, data) {
507 return len(datum) == 0
509 if err := quick.Check(f, nil); err != nil {