]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cypherpunks.ru/nncp/toss_test.go
Remote command execution
[nncp.git] / src / cypherpunks.ru / nncp / toss_test.go
index c40c9d8628779ffe5b5855f0922b05575d930c3c..7ff216408c1103142dde1ad7356518ec3cab0639 100644 (file)
@@ -1,6 +1,6 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2017 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2018 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
@@ -51,8 +51,8 @@ func dirFiles(path string) []string {
        return names
 }
 
-func TestTossEmail(t *testing.T) {
-       f := func(recipients [16]uint8) bool {
+func TestTossExec(t *testing.T) {
+       f := func(replyNice uint8, handle string, recipients [16]uint8) bool {
                for i, recipient := range recipients {
                        recipients[i] = recipient % 8
                }
@@ -63,7 +63,8 @@ func TestTossEmail(t *testing.T) {
                defer os.RemoveAll(spool)
                nodeOur, err := NewNodeGenerate()
                if err != nil {
-                       panic(err)
+                       t.Error(err)
+                       return false
                }
                ctx := Ctx{
                        Spool:   spool,
@@ -82,20 +83,24 @@ func TestTossEmail(t *testing.T) {
                        }
                        our, err := NewNodeGenerate()
                        if err != nil {
-                               panic(err)
+                               t.Error(err)
+                               return false
                        }
                        privates[recipient] = our
                        ctx.Neigh[*our.Id] = our.Their()
                }
                for _, recipient := range recipients {
-                       if err := ctx.TxMail(
+                       if err := ctx.TxExec(
                                ctx.Neigh[*privates[recipient].Id],
-                               DefaultNiceMail,
-                               "recipient",
+                               DefaultNiceExec,
+                               replyNice,
+                               handle,
+                               []string{"arg0", "arg1"},
                                []byte{123},
                                1<<15,
                        ); err != nil {
-                               panic(err)
+                               t.Error(err)
+                               return false
                        }
                }
                for _, recipient := range recipients {
@@ -105,20 +110,25 @@ func TestTossEmail(t *testing.T) {
                        if len(dirFiles(rxPath)) == 0 {
                                continue
                        }
-                       ctx.Toss(ctx.Self.Id, DefaultNiceMail-1, false)
+                       ctx.Toss(ctx.Self.Id, DefaultNiceExec-1, false, false, false, false, false, false)
                        if len(dirFiles(rxPath)) == 0 {
                                return false
                        }
-                       ctx.Neigh[*nodeOur.Id].Sendmail = []string{"/bin/sh", "-c", "false"}
-                       ctx.Toss(ctx.Self.Id, DefaultNiceMail, 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)
                        if len(dirFiles(rxPath)) == 0 {
                                return false
                        }
-                       ctx.Neigh[*nodeOur.Id].Sendmail = []string{
+                       ctx.Neigh[*nodeOur.Id].Exec[handle] = []string{
                                "/bin/sh", "-c",
-                               fmt.Sprintf("cat >> %s", filepath.Join(spool, "mbox")),
+                               fmt.Sprintf(
+                                       "echo $NNCP_NICE $@ >> %s ; cat >> %s",
+                                       filepath.Join(spool, "mbox"),
+                                       filepath.Join(spool, "mbox"),
+                               ),
                        }
-                       ctx.Toss(ctx.Self.Id, DefaultNiceMail, false)
+                       ctx.Toss(ctx.Self.Id, DefaultNiceExec, false, false, false, false, false, false)
                        if len(dirFiles(rxPath)) != 0 {
                                return false
                        }
@@ -129,6 +139,10 @@ func TestTossEmail(t *testing.T) {
                }
                expected := make([]byte, 0, 16)
                for i := 0; i < 16; i++ {
+                       expected = append(
+                               expected,
+                               []byte(fmt.Sprintf("%d arg0 arg1\n", replyNice))...,
+                       )
                        expected = append(expected, 123)
                }
                return bytes.Compare(mbox, expected) == 0
@@ -158,7 +172,8 @@ func TestTossFile(t *testing.T) {
                defer os.RemoveAll(spool)
                nodeOur, err := NewNodeGenerate()
                if err != nil {
-                       panic(err)
+                       t.Error(err)
+                       return false
                }
                ctx := Ctx{
                        Spool:   spool,
@@ -185,17 +200,18 @@ func TestTossFile(t *testing.T) {
                                fileName,
                                1<<15,
                        ); err != nil {
-                               panic(err)
+                               t.Error(err)
+                               return false
                        }
                }
                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)
+               ctx.Toss(ctx.Self.Id, DefaultNiceFile, 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)
+               ctx.Toss(ctx.Self.Id, DefaultNiceFile, false, false, false, false, false, false)
                if len(dirFiles(rxPath)) != 0 {
                        return false
                }
@@ -227,7 +243,8 @@ func TestTossFileSameName(t *testing.T) {
                defer os.RemoveAll(spool)
                nodeOur, err := NewNodeGenerate()
                if err != nil {
-                       panic(err)
+                       t.Error(err)
+                       return false
                }
                ctx := Ctx{
                        Spool:   spool,
@@ -245,7 +262,8 @@ func TestTossFileSameName(t *testing.T) {
                        []byte("doesnotmatter"),
                        os.FileMode(0600),
                ); err != nil {
-                       panic(err)
+                       t.Error(err)
+                       return false
                }
                incomingPath := filepath.Join(spool, "incoming")
                for i := 0; i < files; i++ {
@@ -256,13 +274,14 @@ func TestTossFileSameName(t *testing.T) {
                                "samefile",
                                1<<15,
                        ); err != nil {
-                               panic(err)
+                               t.Error(err)
+                               return false
                        }
                }
                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)
+               ctx.Toss(ctx.Self.Id, DefaultNiceFile, false, false, false, false, false, false)
                expected := make(map[string]struct{})
                expected["samefile"] = struct{}{}
                for i := 0; i < files-1; i++ {
@@ -285,7 +304,7 @@ func TestTossFileSameName(t *testing.T) {
 }
 
 func TestTossFreq(t *testing.T) {
-       f := func(fileSizes []uint8) bool {
+       f := func(fileSizes []uint8, replyNice uint8) bool {
                if len(fileSizes) == 0 {
                        return true
                }
@@ -296,7 +315,8 @@ func TestTossFreq(t *testing.T) {
                defer os.RemoveAll(spool)
                nodeOur, err := NewNodeGenerate()
                if err != nil {
-                       panic(err)
+                       t.Error(err)
+                       return false
                }
                ctx := Ctx{
                        Spool:   spool,
@@ -319,23 +339,25 @@ func TestTossFreq(t *testing.T) {
                        if err := ctx.TxFreq(
                                ctx.Neigh[*nodeOur.Id],
                                DefaultNiceFreq,
+                               replyNice,
                                fileName,
                                fileName,
                                1<<15,
                        ); err != nil {
-                               panic(err)
+                               t.Error(err)
+                               return false
                        }
                }
                rxPath := filepath.Join(spool, ctx.Self.Id.String(), string(TRx))
                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)
+               ctx.Toss(ctx.Self.Id, DefaultNiceFreq, 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)
+               ctx.Toss(ctx.Self.Id, DefaultNiceFreq, false, false, false, false, false, false)
                if len(dirFiles(txPath)) != 0 || len(dirFiles(rxPath)) == 0 {
                        return false
                }
@@ -348,7 +370,7 @@ func TestTossFreq(t *testing.T) {
                                panic(err)
                        }
                }
-               ctx.Toss(ctx.Self.Id, DefaultNiceFreq, false)
+               ctx.Toss(ctx.Self.Id, DefaultNiceFreq, false, false, false, false, false, false)
                if len(dirFiles(txPath)) == 0 || len(dirFiles(rxPath)) != 0 {
                        return false
                }
@@ -356,11 +378,16 @@ func TestTossFreq(t *testing.T) {
                        var buf bytes.Buffer
                        _, _, err := PktEncRead(ctx.Self, ctx.Neigh, job.Fd, &buf)
                        if err != nil {
-                               panic(err)
+                               t.Error(err)
+                               return false
                        }
                        var pkt Pkt
                        if _, err = xdr.Unmarshal(&buf, &pkt); err != nil {
-                               panic(err)
+                               t.Error(err)
+                               return false
+                       }
+                       if pkt.Nice != replyNice {
+                               return false
                        }
                        dst := string(pkt.Path[:int(pkt.PathLen)])
                        if bytes.Compare(buf.Bytes(), files[dst]) != 0 {
@@ -395,7 +422,8 @@ func TestTossTrns(t *testing.T) {
                defer os.RemoveAll(spool)
                nodeOur, err := NewNodeGenerate()
                if err != nil {
-                       panic(err)
+                       t.Error(err)
+                       return false
                }
                ctx := Ctx{
                        Spool:   spool,
@@ -413,7 +441,7 @@ func TestTossTrns(t *testing.T) {
                os.MkdirAll(txPath, os.FileMode(0700))
                for _, data := range datum {
                        pktTrans := Pkt{
-                               Magic:   MagicNNCPPv1,
+                               Magic:   MagicNNCPPv2,
                                Type:    PktTypeTrns,
                                PathLen: blake2b.Size256,
                                Path:    new([MaxPathSize]byte),
@@ -430,7 +458,8 @@ func TestTossTrns(t *testing.T) {
                                bytes.NewReader(data),
                                &dst,
                        ); err != nil {
-                               panic(err)
+                               t.Error(err)
+                               return false
                        }
                        checksum := blake2b.Sum256(dst.Bytes())
                        if err := ioutil.WriteFile(
@@ -441,7 +470,7 @@ func TestTossTrns(t *testing.T) {
                                panic(err)
                        }
                }
-               ctx.Toss(ctx.Self.Id, 123, false)
+               ctx.Toss(ctx.Self.Id, 123, false, false, false, false, false, false)
                if len(dirFiles(rxPath)) != 0 {
                        return false
                }
@@ -453,7 +482,6 @@ func TestTossTrns(t *testing.T) {
                        for k, data := range datum {
                                if bytes.Compare(dataRead, data) == 0 {
                                        delete(datum, k)
-                                       break
                                }
                        }
                }