]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/tx_test.go
Merge branch 'develop'
[nncp.git] / src / tx_test.go
index 85a00db69a416bbd6ae19cb6e9e5b569751f026f..2dccbc3d32eb341f1f657ad0d8a38b778c61f5f2 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
@@ -19,11 +19,10 @@ package nncp
 
 import (
        "bytes"
+       "crypto/rand"
        "io"
-       "io/ioutil"
        "os"
-       "path"
-       "strings"
+       "path/filepath"
        "testing"
        "testing/quick"
 
@@ -31,13 +30,24 @@ import (
 )
 
 func TestTx(t *testing.T) {
-       f := func(hops uint8, pathSrc, data string, nice, replyNice uint8, padSize int16) bool {
+       f := func(
+               hops uint8,
+               pathSrc string,
+               dataSize uint32,
+               nice, replyNice uint8,
+               minSize uint32,
+       ) bool {
+               dataSize %= 1 << 20
+               data := make([]byte, dataSize)
+               if _, err := io.ReadFull(rand.Reader, data); err != nil {
+                       panic(err)
+               }
+               minSize %= 1 << 20
                if len(pathSrc) > int(MaxPathSize) {
                        pathSrc = pathSrc[:MaxPathSize]
                }
                hops = hops % 4
-               hops = 1
-               spool, err := ioutil.TempDir("", "testtx")
+               spool, err := os.MkdirTemp("", "testtx")
                if err != nil {
                        panic(err)
                }
@@ -53,7 +63,7 @@ func TestTx(t *testing.T) {
                nodeTgt := nodeTgtOur.Their()
                ctx := Ctx{
                        Spool:   spool,
-                       LogPath: path.Join(spool, "log.log"),
+                       LogPath: filepath.Join(spool, "log.log"),
                        Debug:   true,
                        Self:    nodeOur,
                        SelfId:  nodeOur.Id,
@@ -75,13 +85,17 @@ func TestTx(t *testing.T) {
                        nodeTgt.Via = append(nodeTgt.Via, node.Id)
                }
                pkt, err := NewPkt(PktTypeExec, replyNice, []byte(pathSrc))
-               src := strings.NewReader(data)
-               dstNode, err := ctx.Tx(
+               if err != nil {
+                       panic(err)
+               }
+               src := bytes.NewReader(data)
+               dstNode, _, _, err := ctx.Tx(
                        nodeTgt,
                        pkt,
                        123,
                        int64(src.Len()),
-                       int64(padSize),
+                       int64(minSize),
+                       MaxFileSize,
                        src,
                        "pktName",
                        nil,
@@ -136,14 +150,14 @@ func TestTx(t *testing.T) {
                                if !bytes.HasPrefix(pkt.Path[:], []byte(pathSrc)) {
                                        return false
                                }
-                               if bytes.Compare(bufR.Bytes(), []byte(data)) != 0 {
+                               if !bytes.Equal(bufR.Bytes(), []byte(data)) {
                                        return false
                                }
                        } else {
                                if pkt.Type != PktTypeTrns {
                                        return false
                                }
-                               if bytes.Compare(pkt.Path[:MTHSize], vias[i+1][:]) != 0 {
+                               if !bytes.Equal(pkt.Path[:MTHSize], vias[i+1][:]) {
                                        return false
                                }
                        }