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/>.
33 xdr "github.com/davecgh/go-xdr/xdr2"
40 func dirFiles(path string) []string {
41 dir, err := os.Open(path)
46 names, err := dir.Readdirnames(0)
53 func TestTossExec(t *testing.T) {
54 f := func(replyNice uint8, handleRaw uint32, recipients [16]uint8) bool {
55 handle := strconv.Itoa(int(handleRaw))
56 for i, recipient := range recipients {
57 recipients[i] = recipient % 8
59 spool, err := ioutil.TempDir("", "testtoss")
63 defer os.RemoveAll(spool)
64 nodeOur, err := NewNodeGenerate()
73 Neigh: make(map[NodeId]*Node),
74 Alias: make(map[string]*NodeId),
75 LogPath: filepath.Join(spool, "log.log"),
78 ctx.Neigh[*nodeOur.Id] = nodeOur.Their()
79 privates := make(map[uint8]*NodeOur)
80 for _, recipient := range recipients {
81 if _, exists := privates[recipient]; exists {
84 our, err := NewNodeGenerate()
89 privates[recipient] = our
90 ctx.Neigh[*our.Id] = our.Their()
92 for _, recipient := range recipients {
94 ctx.Neigh[*privates[recipient].Id],
98 []string{"arg0", "arg1"},
99 strings.NewReader("BODY\n"),
108 for _, recipient := range recipients {
109 ctx.Self = privates[recipient]
110 rxPath := filepath.Join(spool, ctx.Self.Id.String(), string(TRx))
111 os.Rename(filepath.Join(spool, ctx.Self.Id.String(), string(TTx)), rxPath)
112 if len(dirFiles(rxPath)) == 0 {
115 ctx.Toss(ctx.Self.Id, TRx, DefaultNiceExec-1,
116 false, false, false, false, false, false, false)
117 if len(dirFiles(rxPath)) == 0 {
120 ctx.Neigh[*nodeOur.Id].Exec = make(map[string][]string)
121 ctx.Neigh[*nodeOur.Id].Exec[handle] = []string{"/bin/sh", "-c", "false"}
122 ctx.Toss(ctx.Self.Id, TRx, DefaultNiceExec,
123 false, false, false, false, false, false, false)
124 if len(dirFiles(rxPath)) == 0 {
127 ctx.Neigh[*nodeOur.Id].Exec[handle] = []string{
130 "echo $NNCP_NICE $0 $1 >> %s ; cat >> %s",
131 filepath.Join(spool, "mbox"),
132 filepath.Join(spool, "mbox"),
135 ctx.Toss(ctx.Self.Id, TRx, DefaultNiceExec,
136 false, false, false, false, false, false, false)
137 if len(dirFiles(rxPath)) != 0 {
141 mbox, err := ioutil.ReadFile(filepath.Join(spool, "mbox"))
145 expected := make([]byte, 0, 16)
146 for i := 0; i < 16; i++ {
149 []byte(fmt.Sprintf("%d arg0 arg1\n", replyNice))...,
151 expected = append(expected, []byte("BODY\n")...)
153 return bytes.Compare(mbox, expected) == 0
155 if err := quick.Check(f, nil); err != nil {
160 func TestTossFile(t *testing.T) {
161 f := func(fileSizes []uint8) bool {
162 if len(fileSizes) == 0 {
165 files := make(map[string][]byte)
166 for i, fileSize := range fileSizes {
168 // to prevent chunked send
171 data := make([]byte, fileSize)
172 if _, err := io.ReadFull(rand.Reader, data); err != nil {
175 files[strconv.Itoa(i)] = data
177 spool, err := ioutil.TempDir("", "testtoss")
181 defer os.RemoveAll(spool)
182 nodeOur, err := NewNodeGenerate()
191 Neigh: make(map[NodeId]*Node),
192 Alias: make(map[string]*NodeId),
193 LogPath: filepath.Join(spool, "log.log"),
196 ctx.Neigh[*nodeOur.Id] = nodeOur.Their()
197 incomingPath := filepath.Join(spool, "incoming")
198 for _, fileData := range files {
199 hasher := MTHNew(0, 0)
200 hasher.Write(fileData)
201 fileName := Base32Codec.EncodeToString(hasher.Sum(nil))
202 src := filepath.Join(spool, fileName)
203 if err := ioutil.WriteFile(src, fileData, os.FileMode(0600)); err != nil {
206 if err := ctx.TxFile(
207 ctx.Neigh[*nodeOur.Id],
220 rxPath := filepath.Join(spool, ctx.Self.Id.String(), string(TRx))
221 os.Rename(filepath.Join(spool, ctx.Self.Id.String(), string(TTx)), rxPath)
222 ctx.Toss(ctx.Self.Id, TRx, DefaultNiceFile,
223 false, false, false, false, false, false, false)
224 if len(dirFiles(rxPath)) == 0 {
227 ctx.Neigh[*nodeOur.Id].Incoming = &incomingPath
228 if ctx.Toss(ctx.Self.Id, TRx, DefaultNiceFile,
229 false, false, false, false, false, false, false) {
232 if len(dirFiles(rxPath)) != 0 {
235 for _, fileData := range files {
236 hasher := MTHNew(0, 0)
237 hasher.Write(fileData)
238 fileName := Base32Codec.EncodeToString(hasher.Sum(nil))
239 data, err := ioutil.ReadFile(filepath.Join(incomingPath, fileName))
243 if bytes.Compare(data, fileData) != 0 {
249 if err := quick.Check(f, nil); err != nil {
254 func TestTossFileSameName(t *testing.T) {
255 f := func(filesRaw uint8) bool {
256 files := int(filesRaw)%8 + 1
257 spool, err := ioutil.TempDir("", "testtoss")
261 defer os.RemoveAll(spool)
262 nodeOur, err := NewNodeGenerate()
271 Neigh: make(map[NodeId]*Node),
272 Alias: make(map[string]*NodeId),
273 LogPath: filepath.Join(spool, "log.log"),
276 ctx.Neigh[*nodeOur.Id] = nodeOur.Their()
277 srcPath := filepath.Join(spool, "junk")
278 if err = ioutil.WriteFile(
280 []byte("doesnotmatter"),
286 incomingPath := filepath.Join(spool, "incoming")
287 for i := 0; i < files; i++ {
288 if err := ctx.TxFile(
289 ctx.Neigh[*nodeOur.Id],
302 rxPath := filepath.Join(spool, ctx.Self.Id.String(), string(TRx))
303 os.Rename(filepath.Join(spool, ctx.Self.Id.String(), string(TTx)), rxPath)
304 ctx.Neigh[*nodeOur.Id].Incoming = &incomingPath
305 ctx.Toss(ctx.Self.Id, TRx, DefaultNiceFile,
306 false, false, false, false, false, false, false)
307 expected := make(map[string]struct{})
308 expected["samefile"] = struct{}{}
309 for i := 0; i < files-1; i++ {
310 expected["samefile."+strconv.Itoa(i)] = struct{}{}
312 for _, filename := range dirFiles(incomingPath) {
313 if _, exists := expected[filename]; !exists {
316 delete(expected, filename)
318 if len(expected) != 0 {
323 if err := quick.Check(f, nil); err != nil {
328 func TestTossFreq(t *testing.T) {
329 f := func(fileSizes []uint8, replyNice uint8) bool {
330 if len(fileSizes) == 0 {
333 spool, err := ioutil.TempDir("", "testtoss")
337 defer os.RemoveAll(spool)
338 nodeOur, err := NewNodeGenerate()
347 Neigh: make(map[NodeId]*Node),
348 Alias: make(map[string]*NodeId),
349 LogPath: filepath.Join(spool, "log.log"),
352 ctx.Neigh[*nodeOur.Id] = nodeOur.Their()
353 files := make(map[string][]byte)
354 for i, fileSize := range fileSizes {
356 // to prevent chunked send
359 fileData := make([]byte, fileSize)
360 if _, err := io.ReadFull(rand.Reader, fileData); err != nil {
363 fileName := strconv.Itoa(i)
364 files[fileName] = fileData
365 if err := ctx.TxFreq(
366 ctx.Neigh[*nodeOur.Id],
377 rxPath := filepath.Join(spool, ctx.Self.Id.String(), string(TRx))
378 txPath := filepath.Join(spool, ctx.Self.Id.String(), string(TTx))
379 os.Rename(txPath, rxPath)
380 os.MkdirAll(txPath, os.FileMode(0700))
381 ctx.Toss(ctx.Self.Id, TRx, DefaultNiceFreq,
382 false, false, false, false, false, false, false)
383 if len(dirFiles(txPath)) != 0 || len(dirFiles(rxPath)) == 0 {
386 ctx.Neigh[*nodeOur.Id].FreqPath = &spool
387 ctx.Toss(ctx.Self.Id, TRx, DefaultNiceFreq,
388 false, false, false, false, false, false, false)
389 if len(dirFiles(txPath)) != 0 || len(dirFiles(rxPath)) == 0 {
392 for fileName, fileData := range files {
393 if err := ioutil.WriteFile(
394 filepath.Join(spool, fileName),
401 ctx.Toss(ctx.Self.Id, TRx, DefaultNiceFreq,
402 false, false, false, false, false, false, false)
403 if len(dirFiles(txPath)) == 0 || len(dirFiles(rxPath)) != 0 {
406 for job := range ctx.Jobs(ctx.Self.Id, TTx) {
408 fd, err := os.Open(job.Path)
413 _, _, _, err = PktEncRead(ctx.Self, ctx.Neigh, fd, &buf, true, nil)
419 if _, err = xdr.Unmarshal(&buf, &pkt); err != nil {
423 if pkt.Nice != replyNice {
426 dst := string(pkt.Path[:int(pkt.PathLen)])
427 if bytes.Compare(buf.Bytes(), files[dst]) != 0 {
433 if err := quick.Check(f, nil); err != nil {
438 func TestTossTrns(t *testing.T) {
439 f := func(datumLens []uint8) bool {
440 if len(datumLens) == 0 {
443 datum := make(map[int][]byte)
444 for i, datumLen := range datumLens {
446 data := make([]byte, datumLen)
447 if _, err := io.ReadFull(rand.Reader, data); err != nil {
452 spool, err := ioutil.TempDir("", "testtoss")
456 defer os.RemoveAll(spool)
457 nodeOur, err := NewNodeGenerate()
466 Neigh: make(map[NodeId]*Node),
467 Alias: make(map[string]*NodeId),
468 LogPath: filepath.Join(spool, "log.log"),
471 ctx.Neigh[*nodeOur.Id] = nodeOur.Their()
472 rxPath := filepath.Join(spool, ctx.Self.Id.String(), string(TRx))
473 os.MkdirAll(rxPath, os.FileMode(0700))
474 txPath := filepath.Join(spool, ctx.Self.Id.String(), string(TTx))
475 os.MkdirAll(txPath, os.FileMode(0700))
476 for _, data := range datum {
478 Magic: MagicNNCPPv3.B,
482 copy(pktTrans.Path[:], nodeOur.Id[:])
484 if _, _, err := PktEncWrite(
486 ctx.Neigh[*nodeOur.Id],
490 bytes.NewReader(data),
496 hasher := MTHNew(0, 0)
497 hasher.Write(dst.Bytes())
498 if err := ioutil.WriteFile(
499 filepath.Join(rxPath, Base32Codec.EncodeToString(hasher.Sum(nil))),
506 ctx.Toss(ctx.Self.Id, TRx, 123,
507 false, false, false, false, false, false, false)
508 if len(dirFiles(rxPath)) != 0 {
511 for _, filename := range dirFiles(txPath) {
512 dataRead, err := ioutil.ReadFile(filepath.Join(txPath, filename))
516 for k, data := range datum {
517 if bytes.Compare(dataRead, data) == 0 {
527 if err := quick.Check(f, nil); err != nil {