]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/toss_test.go
Merge branch 'develop'
[nncp.git] / src / toss_test.go
index 8a16dd94512f184c87e60aef65e88f30c0f629be..eb83309e19b2c77ec3ae87e80c4ba4d7e7a12ee5 100644 (file)
@@ -1,6 +1,6 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2021 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2023 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
@@ -22,7 +22,6 @@ import (
        "crypto/rand"
        "fmt"
        "io"
-       "io/ioutil"
        "os"
        "path/filepath"
        "strconv"
@@ -31,7 +30,6 @@ import (
        "testing/quick"
 
        xdr "github.com/davecgh/go-xdr/xdr2"
-       "golang.org/x/crypto/blake2b"
 )
 
 var (
@@ -57,7 +55,7 @@ func TestTossExec(t *testing.T) {
                for i, recipient := range recipients {
                        recipients[i] = recipient % 8
                }
-               spool, err := ioutil.TempDir("", "testtoss")
+               spool, err := os.MkdirTemp("", "testtoss")
                if err != nil {
                        panic(err)
                }
@@ -98,9 +96,9 @@ func TestTossExec(t *testing.T) {
                                handle,
                                []string{"arg0", "arg1"},
                                strings.NewReader("BODY\n"),
-                               1<<15,
-                               false,
+                               1<<15, MaxFileSize,
                                false,
+                               nil,
                        ); err != nil {
                                t.Error(err)
                                return false
@@ -113,13 +111,13 @@ 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, &TossOpts{Nice: DefaultNiceExec - 1})
                        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, &TossOpts{Nice: DefaultNiceExec})
                        if len(dirFiles(rxPath)) == 0 {
                                return false
                        }
@@ -131,12 +129,12 @@ 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, &TossOpts{Nice: DefaultNiceExec})
                        if len(dirFiles(rxPath)) != 0 {
                                return false
                        }
                }
-               mbox, err := ioutil.ReadFile(filepath.Join(spool, "mbox"))
+               mbox, err := os.ReadFile(filepath.Join(spool, "mbox"))
                if err != nil {
                        return false
                }
@@ -148,7 +146,7 @@ func TestTossExec(t *testing.T) {
                        )
                        expected = append(expected, []byte("BODY\n")...)
                }
-               return bytes.Compare(mbox, expected) == 0
+               return bytes.Equal(mbox, expected)
        }
        if err := quick.Check(f, nil); err != nil {
                t.Error(err)
@@ -162,13 +160,17 @@ 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)
                        }
                        files[strconv.Itoa(i)] = data
                }
-               spool, err := ioutil.TempDir("", "testtoss")
+               spool, err := os.MkdirTemp("", "testtoss")
                if err != nil {
                        panic(err)
                }
@@ -190,10 +192,11 @@ 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 := Base32Codec.EncodeToString(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 {
+                       if err := os.WriteFile(src, fileData, os.FileMode(0600)); err != nil {
                                panic(err)
                        }
                        if err := ctx.TxFile(
@@ -204,6 +207,7 @@ func TestTossFile(t *testing.T) {
                                MaxFileSize,
                                1<<15,
                                MaxFileSize,
+                               nil,
                        ); err != nil {
                                t.Error(err)
                                return false
@@ -211,23 +215,26 @@ 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, &TossOpts{Nice: DefaultNiceFile})
                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, &TossOpts{Nice: DefaultNiceFile}) {
+                       return false
+               }
                if len(dirFiles(rxPath)) != 0 {
                        return false
                }
                for _, fileData := range files {
-                       checksum := blake2b.Sum256(fileData)
-                       fileName := Base32Codec.EncodeToString(checksum[:])
-                       data, err := ioutil.ReadFile(filepath.Join(incomingPath, fileName))
+                       hasher := MTHNew(0, 0)
+                       hasher.Write(fileData)
+                       fileName := Base32Codec.EncodeToString(hasher.Sum(nil))
+                       data, err := os.ReadFile(filepath.Join(incomingPath, fileName))
                        if err != nil {
                                panic(err)
                        }
-                       if bytes.Compare(data, fileData) != 0 {
+                       if !bytes.Equal(data, fileData) {
                                return false
                        }
                }
@@ -241,7 +248,7 @@ func TestTossFile(t *testing.T) {
 func TestTossFileSameName(t *testing.T) {
        f := func(filesRaw uint8) bool {
                files := int(filesRaw)%8 + 1
-               spool, err := ioutil.TempDir("", "testtoss")
+               spool, err := os.MkdirTemp("", "testtoss")
                if err != nil {
                        panic(err)
                }
@@ -262,7 +269,7 @@ func TestTossFileSameName(t *testing.T) {
                }
                ctx.Neigh[*nodeOur.Id] = nodeOur.Their()
                srcPath := filepath.Join(spool, "junk")
-               if err = ioutil.WriteFile(
+               if err = os.WriteFile(
                        srcPath,
                        []byte("doesnotmatter"),
                        os.FileMode(0600),
@@ -280,6 +287,7 @@ func TestTossFileSameName(t *testing.T) {
                                MaxFileSize,
                                1<<15,
                                MaxFileSize,
+                               nil,
                        ); err != nil {
                                t.Error(err)
                                return false
@@ -288,7 +296,7 @@ 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, &TossOpts{Nice: DefaultNiceFile})
                expected := make(map[string]struct{})
                expected["samefile"] = struct{}{}
                for i := 0; i < files-1; i++ {
@@ -300,10 +308,7 @@ func TestTossFileSameName(t *testing.T) {
                        }
                        delete(expected, filename)
                }
-               if len(expected) != 0 {
-                       return false
-               }
-               return true
+               return len(expected) == 0
        }
        if err := quick.Check(f, nil); err != nil {
                t.Error(err)
@@ -315,7 +320,7 @@ func TestTossFreq(t *testing.T) {
                if len(fileSizes) == 0 {
                        return true
                }
-               spool, err := ioutil.TempDir("", "testtoss")
+               spool, err := os.MkdirTemp("", "testtoss")
                if err != nil {
                        panic(err)
                }
@@ -337,6 +342,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)
@@ -359,17 +368,17 @@ 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, &TossOpts{Nice: DefaultNiceFreq})
                if len(dirFiles(txPath)) != 0 || len(dirFiles(rxPath)) == 0 {
                        return false
                }
                ctx.Neigh[*nodeOur.Id].FreqPath = &spool
-               ctx.Toss(ctx.Self.Id, DefaultNiceFreq, false, false, false, false, false, false)
+               ctx.Toss(ctx.Self.Id, TRx, &TossOpts{Nice: DefaultNiceFreq})
                if len(dirFiles(txPath)) != 0 || len(dirFiles(rxPath)) == 0 {
                        return false
                }
                for fileName, fileData := range files {
-                       if err := ioutil.WriteFile(
+                       if err := os.WriteFile(
                                filepath.Join(spool, fileName),
                                fileData,
                                os.FileMode(0600),
@@ -377,13 +386,18 @@ 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, &TossOpts{Nice: DefaultNiceFreq})
                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
@@ -396,8 +410,7 @@ func TestTossFreq(t *testing.T) {
                        if pkt.Nice != replyNice {
                                return false
                        }
-                       dst := string(pkt.Path[:int(pkt.PathLen)])
-                       if bytes.Compare(buf.Bytes(), files[dst]) != 0 {
+                       if !bytes.Equal(buf.Bytes(), files[string(pkt.Path[:int(pkt.PathLen)])]) {
                                return false
                        }
                }
@@ -422,7 +435,7 @@ func TestTossTrns(t *testing.T) {
                        }
                        datum[i] = data
                }
-               spool, err := ioutil.TempDir("", "testtoss")
+               spool, err := os.MkdirTemp("", "testtoss")
                if err != nil {
                        panic(err)
                }
@@ -448,53 +461,50 @@ func TestTossTrns(t *testing.T) {
                os.MkdirAll(txPath, os.FileMode(0700))
                for _, data := range datum {
                        pktTrans := Pkt{
-                               Magic:   MagicNNCPPv3,
+                               Magic:   MagicNNCPPv3.B,
                                Type:    PktTypeTrns,
-                               PathLen: blake2b.Size256,
+                               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())
-                       if err := ioutil.WriteFile(
-                               filepath.Join(rxPath, Base32Codec.EncodeToString(checksum[:])),
+                       hasher := MTHNew(0, 0)
+                       hasher.Write(dst.Bytes())
+                       if err := os.WriteFile(
+                               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, &TossOpts{Nice: 123})
                if len(dirFiles(rxPath)) != 0 {
                        return false
                }
                for _, filename := range dirFiles(txPath) {
-                       dataRead, err := ioutil.ReadFile(filepath.Join(txPath, filename))
+                       dataRead, err := os.ReadFile(filepath.Join(txPath, filename))
                        if err != nil {
                                panic(err)
                        }
                        for k, data := range datum {
-                               if bytes.Compare(dataRead, data) == 0 {
+                               if bytes.Equal(dataRead, data) {
                                        delete(datum, k)
                                }
                        }
                }
-               if len(datum) > 0 {
-                       return false
-               }
-               return true
+               return len(datum) == 0
        }
        if err := quick.Check(f, nil); err != nil {
                t.Error(err)