]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/toss_test.go
ACK
[nncp.git] / src / toss_test.go
index 48149c49f2d807fec138143d5be6f325206aba30..000653d7be96cf60c8b0fd4a09961836bea880d8 100644 (file)
@@ -1,6 +1,6 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2019 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2022 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -26,11 +26,11 @@ import (
        "os"
        "path/filepath"
        "strconv"
+       "strings"
        "testing"
        "testing/quick"
 
-       "github.com/davecgh/go-xdr/xdr2"
-       "golang.org/x/crypto/blake2b"
+       xdr "github.com/davecgh/go-xdr/xdr2"
 )
 
 var (
@@ -96,8 +96,10 @@ func TestTossExec(t *testing.T) {
                                replyNice,
                                handle,
                                []string{"arg0", "arg1"},
-                               []byte("BODY\n"),
-                               1<<15,
+                               strings.NewReader("BODY\n"),
+                               1<<15, MaxFileSize,
+                               false,
+                               nil,
                        ); err != nil {
                                t.Error(err)
                                return false
@@ -110,13 +112,15 @@ func TestTossExec(t *testing.T) {
                        if len(dirFiles(rxPath)) == 0 {
                                continue
                        }
-                       ctx.Toss(ctx.Self.Id, DefaultNiceExec-1, false, false, false, false, false, false)
+                       ctx.Toss(ctx.Self.Id, TRx, DefaultNiceExec-1,
+                               false, false, false, false, false, false, false, false)
                        if len(dirFiles(rxPath)) == 0 {
                                return false
                        }
                        ctx.Neigh[*nodeOur.Id].Exec = make(map[string][]string)
                        ctx.Neigh[*nodeOur.Id].Exec[handle] = []string{"/bin/sh", "-c", "false"}
-                       ctx.Toss(ctx.Self.Id, DefaultNiceExec, false, false, false, false, false, false)
+                       ctx.Toss(ctx.Self.Id, TRx, DefaultNiceExec,
+                               false, false, false, false, false, false, false, false)
                        if len(dirFiles(rxPath)) == 0 {
                                return false
                        }
@@ -128,7 +132,8 @@ func TestTossExec(t *testing.T) {
                                        filepath.Join(spool, "mbox"),
                                ),
                        }
-                       ctx.Toss(ctx.Self.Id, DefaultNiceExec, false, false, false, false, false, false)
+                       ctx.Toss(ctx.Self.Id, TRx, DefaultNiceExec,
+                               false, false, false, false, false, false, false, false)
                        if len(dirFiles(rxPath)) != 0 {
                                return false
                        }
@@ -159,6 +164,10 @@ func TestTossFile(t *testing.T) {
                }
                files := make(map[string][]byte)
                for i, fileSize := range fileSizes {
+                       if fileSize == 0 {
+                               // to prevent chunked send
+                               fileSize++
+                       }
                        data := make([]byte, fileSize)
                        if _, err := io.ReadFull(rand.Reader, data); err != nil {
                                panic(err)
@@ -187,8 +196,9 @@ func TestTossFile(t *testing.T) {
                ctx.Neigh[*nodeOur.Id] = nodeOur.Their()
                incomingPath := filepath.Join(spool, "incoming")
                for _, fileData := range files {
-                       checksum := blake2b.Sum256(fileData)
-                       fileName := ToBase32(checksum[:])
+                       hasher := MTHNew(0, 0)
+                       hasher.Write(fileData)
+                       fileName := Base32Codec.EncodeToString(hasher.Sum(nil))
                        src := filepath.Join(spool, fileName)
                        if err := ioutil.WriteFile(src, fileData, os.FileMode(0600)); err != nil {
                                panic(err)
@@ -198,7 +208,10 @@ func TestTossFile(t *testing.T) {
                                DefaultNiceFile,
                                src,
                                fileName,
+                               MaxFileSize,
                                1<<15,
+                               MaxFileSize,
+                               nil,
                        ); err != nil {
                                t.Error(err)
                                return false
@@ -206,18 +219,23 @@ func TestTossFile(t *testing.T) {
                }
                rxPath := filepath.Join(spool, ctx.Self.Id.String(), string(TRx))
                os.Rename(filepath.Join(spool, ctx.Self.Id.String(), string(TTx)), rxPath)
-               ctx.Toss(ctx.Self.Id, DefaultNiceFile, false, false, false, false, false, false)
+               ctx.Toss(ctx.Self.Id, TRx, DefaultNiceFile,
+                       false, false, false, false, false, false, false, false)
                if len(dirFiles(rxPath)) == 0 {
                        return false
                }
                ctx.Neigh[*nodeOur.Id].Incoming = &incomingPath
-               ctx.Toss(ctx.Self.Id, DefaultNiceFile, false, false, false, false, false, false)
+               if ctx.Toss(ctx.Self.Id, TRx, DefaultNiceFile,
+                       false, false, false, false, false, false, false, false) {
+                       return false
+               }
                if len(dirFiles(rxPath)) != 0 {
                        return false
                }
                for _, fileData := range files {
-                       checksum := blake2b.Sum256(fileData)
-                       fileName := ToBase32(checksum[:])
+                       hasher := MTHNew(0, 0)
+                       hasher.Write(fileData)
+                       fileName := Base32Codec.EncodeToString(hasher.Sum(nil))
                        data, err := ioutil.ReadFile(filepath.Join(incomingPath, fileName))
                        if err != nil {
                                panic(err)
@@ -272,7 +290,10 @@ func TestTossFileSameName(t *testing.T) {
                                DefaultNiceFile,
                                srcPath,
                                "samefile",
+                               MaxFileSize,
                                1<<15,
+                               MaxFileSize,
+                               nil,
                        ); err != nil {
                                t.Error(err)
                                return false
@@ -281,11 +302,12 @@ func TestTossFileSameName(t *testing.T) {
                rxPath := filepath.Join(spool, ctx.Self.Id.String(), string(TRx))
                os.Rename(filepath.Join(spool, ctx.Self.Id.String(), string(TTx)), rxPath)
                ctx.Neigh[*nodeOur.Id].Incoming = &incomingPath
-               ctx.Toss(ctx.Self.Id, DefaultNiceFile, false, false, false, false, false, false)
+               ctx.Toss(ctx.Self.Id, TRx, DefaultNiceFile,
+                       false, false, false, false, false, false, false, false)
                expected := make(map[string]struct{})
                expected["samefile"] = struct{}{}
                for i := 0; i < files-1; i++ {
-                       expected["samefile"+strconv.Itoa(i)] = struct{}{}
+                       expected["samefile."+strconv.Itoa(i)] = struct{}{}
                }
                for _, filename := range dirFiles(incomingPath) {
                        if _, exists := expected[filename]; !exists {
@@ -330,6 +352,10 @@ func TestTossFreq(t *testing.T) {
                ctx.Neigh[*nodeOur.Id] = nodeOur.Their()
                files := make(map[string][]byte)
                for i, fileSize := range fileSizes {
+                       if fileSize == 0 {
+                               // to prevent chunked send
+                               fileSize++
+                       }
                        fileData := make([]byte, fileSize)
                        if _, err := io.ReadFull(rand.Reader, fileData); err != nil {
                                panic(err)
@@ -352,12 +378,14 @@ func TestTossFreq(t *testing.T) {
                txPath := filepath.Join(spool, ctx.Self.Id.String(), string(TTx))
                os.Rename(txPath, rxPath)
                os.MkdirAll(txPath, os.FileMode(0700))
-               ctx.Toss(ctx.Self.Id, DefaultNiceFreq, false, false, false, false, false, false)
+               ctx.Toss(ctx.Self.Id, TRx, DefaultNiceFreq,
+                       false, false, false, false, false, false, false, false)
                if len(dirFiles(txPath)) != 0 || len(dirFiles(rxPath)) == 0 {
                        return false
                }
-               ctx.Neigh[*nodeOur.Id].Freq = &spool
-               ctx.Toss(ctx.Self.Id, DefaultNiceFreq, false, false, false, false, false, false)
+               ctx.Neigh[*nodeOur.Id].FreqPath = &spool
+               ctx.Toss(ctx.Self.Id, TRx, DefaultNiceFreq,
+                       false, false, false, false, false, false, false, false)
                if len(dirFiles(txPath)) != 0 || len(dirFiles(rxPath)) == 0 {
                        return false
                }
@@ -370,13 +398,19 @@ func TestTossFreq(t *testing.T) {
                                panic(err)
                        }
                }
-               ctx.Toss(ctx.Self.Id, DefaultNiceFreq, false, false, false, false, false, false)
+               ctx.Toss(ctx.Self.Id, TRx, DefaultNiceFreq,
+                       false, false, false, false, false, false, false, false)
                if len(dirFiles(txPath)) == 0 || len(dirFiles(rxPath)) != 0 {
                        return false
                }
                for job := range ctx.Jobs(ctx.Self.Id, TTx) {
                        var buf bytes.Buffer
-                       _, _, err := PktEncRead(ctx.Self, ctx.Neigh, job.Fd, &buf)
+                       fd, err := os.Open(job.Path)
+                       if err != nil {
+                               t.Error(err)
+                               return false
+                       }
+                       _, _, _, err = PktEncRead(ctx.Self, ctx.Neigh, fd, &buf, true, nil)
                        if err != nil {
                                t.Error(err)
                                return false
@@ -441,36 +475,36 @@ func TestTossTrns(t *testing.T) {
                os.MkdirAll(txPath, os.FileMode(0700))
                for _, data := range datum {
                        pktTrans := Pkt{
-                               Magic:   MagicNNCPPv2,
+                               Magic:   MagicNNCPPv3.B,
                                Type:    PktTypeTrns,
-                               PathLen: blake2b.Size256,
-                               Path:    new([MaxPathSize]byte),
+                               PathLen: MTHSize,
                        }
                        copy(pktTrans.Path[:], nodeOur.Id[:])
                        var dst bytes.Buffer
-                       if err := PktEncWrite(
+                       if _, _, err := PktEncWrite(
                                ctx.Self,
                                ctx.Neigh[*nodeOur.Id],
                                &pktTrans,
                                123,
-                               int64(len(data)),
-                               0,
+                               0, MaxFileSize, 1,
                                bytes.NewReader(data),
                                &dst,
                        ); err != nil {
                                t.Error(err)
                                return false
                        }
-                       checksum := blake2b.Sum256(dst.Bytes())
+                       hasher := MTHNew(0, 0)
+                       hasher.Write(dst.Bytes())
                        if err := ioutil.WriteFile(
-                               filepath.Join(rxPath, ToBase32(checksum[:])),
+                               filepath.Join(rxPath, Base32Codec.EncodeToString(hasher.Sum(nil))),
                                dst.Bytes(),
                                os.FileMode(0600),
                        ); err != nil {
                                panic(err)
                        }
                }
-               ctx.Toss(ctx.Self.Id, 123, false, false, false, false, false, false)
+               ctx.Toss(ctx.Self.Id, TRx, 123,
+                       false, false, false, false, false, false, false, false)
                if len(dirFiles(rxPath)) != 0 {
                        return false
                }