mirror of https://github.com/schollz/croc.git
add more tests
This commit is contained in:
parent
a82dd2f284
commit
22f6a13a9f
1
go.sum
1
go.sum
|
@ -77,6 +77,7 @@ golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
|||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
|
||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e h1:FDhOuMEY4JVRztM/gsbk+IKUQ8kj74bxZrgw87eMMVc=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||
|
|
|
@ -58,4 +58,8 @@ func TestComm(t *testing.T) {
|
|||
assert.Nil(t, a.Send(token))
|
||||
_ = a.Connection()
|
||||
a.Close()
|
||||
assert.NotNil(t, a.Send(token))
|
||||
_, err = a.Write(token)
|
||||
assert.NotNil(t, err)
|
||||
|
||||
}
|
||||
|
|
|
@ -68,12 +68,8 @@ func TestCroc(t *testing.T) {
|
|||
|
||||
func TestCrocLocal(t *testing.T) {
|
||||
log.SetLevel("trace")
|
||||
defer os.Remove("README.md")
|
||||
// go tcp.Run("debug", "8081", "8082,8083,8084,8085")
|
||||
// go tcp.Run("debug", "8082")
|
||||
// go tcp.Run("debug", "8083")
|
||||
// go tcp.Run("debug", "8084")
|
||||
// go tcp.Run("debug", "8085")
|
||||
defer os.Remove("LICENSE")
|
||||
defer os.Remove("touched")
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
|
||||
log.Debug("setting up sender")
|
||||
|
@ -83,7 +79,7 @@ func TestCrocLocal(t *testing.T) {
|
|||
Debug: true,
|
||||
RelayAddress: "localhost:8181",
|
||||
RelayPorts: []string{"8181", "8182"},
|
||||
Stdout: false,
|
||||
Stdout: true,
|
||||
NoPrompt: true,
|
||||
DisableLocal: false,
|
||||
})
|
||||
|
@ -98,7 +94,7 @@ func TestCrocLocal(t *testing.T) {
|
|||
SharedSecret: "test",
|
||||
Debug: true,
|
||||
RelayAddress: "localhost:8181",
|
||||
Stdout: false,
|
||||
Stdout: true,
|
||||
NoPrompt: true,
|
||||
DisableLocal: false,
|
||||
})
|
||||
|
@ -107,11 +103,12 @@ func TestCrocLocal(t *testing.T) {
|
|||
}
|
||||
|
||||
var wg sync.WaitGroup
|
||||
os.Create("touched")
|
||||
wg.Add(2)
|
||||
go func() {
|
||||
sender.Send(TransferOptions{
|
||||
PathToFiles: []string{"../../README.md"},
|
||||
KeepPathInRemote: true,
|
||||
PathToFiles: []string{"../../LICENSE", "touched"},
|
||||
KeepPathInRemote: false,
|
||||
})
|
||||
wg.Done()
|
||||
}()
|
||||
|
|
|
@ -1,10 +1,15 @@
|
|||
package message
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/schollz/croc/v6/src/comm"
|
||||
"github.com/schollz/croc/v6/src/crypt"
|
||||
log "github.com/schollz/logger"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
@ -20,4 +25,51 @@ func TestMessage(t *testing.T) {
|
|||
m2, err := Decode(e, b)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, m, m2)
|
||||
assert.Equal(t, `{"t":"message","m":"hello, world"}`, m.String())
|
||||
}
|
||||
|
||||
func TestSend(t *testing.T) {
|
||||
token := make([]byte, 40000000)
|
||||
rand.Read(token)
|
||||
|
||||
port := "8801"
|
||||
go func() {
|
||||
log.Debugf("starting TCP server on " + port)
|
||||
server, err := net.Listen("tcp", "0.0.0.0:"+port)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
defer server.Close()
|
||||
// spawn a new goroutine whenever a client connects
|
||||
for {
|
||||
connection, err := server.Accept()
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
log.Debugf("client %s connected", connection.RemoteAddr().String())
|
||||
go func(port string, connection net.Conn) {
|
||||
c := comm.New(connection)
|
||||
err = c.Send([]byte("hello, world"))
|
||||
assert.Nil(t, err)
|
||||
data, err := c.Receive()
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, []byte("hello, computer"), data)
|
||||
data, err = c.Receive()
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, []byte{'\x00'}, data)
|
||||
data, err = c.Receive()
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, token, data)
|
||||
}(port, connection)
|
||||
}
|
||||
}()
|
||||
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
a, err := comm.NewConnection("localhost:"+port, 10*time.Minute)
|
||||
assert.Nil(t, err)
|
||||
m := Message{Type: "message", Message: "hello, world"}
|
||||
e, err := crypt.New(nil, nil)
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Nil(t, Send(a, e, m))
|
||||
}
|
||||
|
|
|
@ -3,8 +3,10 @@ package utils
|
|||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"math/rand"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -112,3 +114,41 @@ func TestMissingChunks(t *testing.T) {
|
|||
// fmt.Println(ChunkRangesToChunks((chunkRanges)))
|
||||
// assert.Nil(t, nil)
|
||||
// }
|
||||
|
||||
func TestHashFile(t *testing.T) {
|
||||
content := []byte("temporary file's content")
|
||||
tmpfile, err := ioutil.TempFile("", "example")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
defer os.Remove(tmpfile.Name()) // clean up
|
||||
|
||||
if _, err := tmpfile.Write(content); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := tmpfile.Close(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
hashed, err := HashFile(tmpfile.Name())
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "18c9673a4bb8325d323e7f24fda9ae1e", fmt.Sprintf("%x", hashed))
|
||||
}
|
||||
|
||||
func TestPublicIP(t *testing.T) {
|
||||
ip, err := PublicIP()
|
||||
fmt.Println(ip)
|
||||
assert.True(t, strings.Contains(ip, "."))
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestLocalIP(t *testing.T) {
|
||||
ip := LocalIP()
|
||||
fmt.Println(ip)
|
||||
assert.True(t, strings.Contains(ip, "."))
|
||||
}
|
||||
|
||||
func TestGetRandomName(t *testing.T) {
|
||||
name := GetRandomName()
|
||||
assert.NotEmpty(t, name)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue