Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
number571 committed Oct 17, 2023
1 parent 0d22567 commit b41df8f
Show file tree
Hide file tree
Showing 15 changed files with 1,707 additions and 1,337 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,9 @@ Logs from `middle_hls` node. When sending requests and receiving responses, `mid
Send request
```bash
$ cd examples/echo_service
$ ./request.sh
$ make request # go run ./_request/main.go
# OR
$ ./_request/request.sh
```

Get response
Expand Down
7 changes: 5 additions & 2 deletions cmd/hidden_lake/service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ PUSH_FORMAT='{

Build and run nodes
```bash
$ cd examples/echo_service
$ cd examples/echo_service/default
$ make
```

Expand All @@ -183,7 +183,10 @@ Logs from `middle_hls` node. When sending requests and receiving responses, `mid

Send request
```bash
$ ./request.sh
$ cd examples/echo_service
$ make request # go run ./_request/main.go
# OR
$ ./_request/request.sh
```

Get response
Expand Down
6 changes: 1 addition & 5 deletions cmd/hidden_lake/service/internal/handler/config_connects.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,7 @@ func HandleConfigConnectsAPI(pWrapper config.IWrapper, pLogger logger.ILogger, p
return
}

if err := pNode.GetNetworkNode().DelConnection(connect); err != nil {
pLogger.PushWarn(logBuilder.WithMessage("del_connections"))
api.Response(pW, http.StatusInternalServerError, "failed: del connection")
return
}
_ = pNode.GetNetworkNode().DelConnection(connect) // connection may be refused (closed)

pLogger.PushInfo(logBuilder.WithMessage(http_logger.CLogSuccess))
api.Response(pW, http.StatusOK, "success: delete connection")
Expand Down
9 changes: 9 additions & 0 deletions pkg/filesystem/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package filesystem
import (
"os"
"testing"

"github.com/number571/go-peer/pkg/crypto/random"
)

const (
Expand Down Expand Up @@ -60,4 +62,11 @@ func TestWriteFile(t *testing.T) {
if string(res) != tcFileData {
t.Errorf("invalid read text from '%s'", tcRandFile3)
}

prng := random.NewStdPRNG()
randInvalidPath := prng.GetString(32) + "/" + prng.GetString(32) + "/" + prng.GetString(32)
if err := OpenFile(randInvalidPath).Write([]byte("hello, world!")); err == nil {
t.Error("success write bytes to invalid path")
return
}
}
12 changes: 12 additions & 0 deletions pkg/network/anonymity/anonymity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,18 @@ func testNewNodes(t *testing.T, timeWait time.Duration, typeDB int) [5]INode {
return nodes
}

func TestWrapper(t *testing.T) {
wrapper := NewWrapperDB()
if db := wrapper.Get(); db != nil {
t.Error("db is not null")
return
}
if err := wrapper.Close(); err != nil {
t.Error(err)
return
}
}

func testNewNode(i int, timeWait time.Duration, addr string, typeDB int) INode {
db, err := database.NewKeyValueDB(
storage.NewSettings(&storage.SSettings{
Expand Down
157 changes: 152 additions & 5 deletions pkg/network/conn/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package conn
import (
"bytes"
"net"
"strings"
"testing"
"time"

Expand All @@ -15,8 +16,66 @@ const (
tcBody = "hello, world!"
)

func TestConn(t *testing.T) {
listener := testNewService(t)
func TestSettingsNetworkKey(t *testing.T) {
for i := 0; i < 4; i++ {
testSettings(t, i)
}
}

func TestSettings(t *testing.T) {
sett := NewSettings(&SSettings{
FMessageSizeBytes: testutils.TCMessageSize,
FWaitReadDeadline: time.Hour,
FReadDeadline: time.Minute,
FWriteDeadline: time.Minute,
})

networkKey := "hello, world!"
sett.SetNetworkKey(networkKey)

if sett.GetNetworkKey() != networkKey {
t.Error("got invalid network key")
return
}
}

func testSettings(t *testing.T, n int) {
defer func() {
if r := recover(); r == nil {
t.Error("nothing panics")
return
}
}()
switch n {
case 0:
_ = NewSettings(&SSettings{
FWaitReadDeadline: time.Hour,
FReadDeadline: time.Minute,
FWriteDeadline: time.Minute,
})
case 1:
_ = NewSettings(&SSettings{
FMessageSizeBytes: testutils.TCMessageSize,
FReadDeadline: time.Minute,
FWriteDeadline: time.Minute,
})
case 2:
_ = NewSettings(&SSettings{
FMessageSizeBytes: testutils.TCMessageSize,
FWaitReadDeadline: time.Hour,
FWriteDeadline: time.Minute,
})
case 3:
_ = NewSettings(&SSettings{
FMessageSizeBytes: testutils.TCMessageSize,
FWaitReadDeadline: time.Hour,
FReadDeadline: time.Minute,
})
}
}

func TestClosedConn(t *testing.T) {
listener := testNewService(t, testutils.TgAddrs[30], "")
defer testFreeService(listener)

conn, err := NewConn(
Expand All @@ -26,13 +85,99 @@ func TestConn(t *testing.T) {
FReadDeadline: time.Minute,
FWriteDeadline: time.Minute,
}),
testutils.TgAddrs[17],
testutils.TgAddrs[30],
)
if err != nil {
t.Error(err)
return
}

if err := conn.Close(); err != nil {
t.Error(err)
return
}

if err := conn.WritePayload(payload.NewPayload(1, []byte("aaa"))); err == nil {
t.Error("success write payload to closed connection")
return
}

readCh := make(chan struct{})
go func() { <-readCh }()

if _, err := conn.ReadPayload(readCh); err == nil {
t.Error("success read payload from closed connection")
return
}

sconn := conn.(*sConn)
if err := sconn.sendBytes([]byte("hello, world!")); err == nil {
t.Error("success send bytes to closed connection")
return
}

if _, err := sconn.recvDataBytes(128); err == nil {
t.Error("success recv data bytes from closed connection")
return
}

readCh2 := make(chan struct{})
go func() { <-readCh2 }()

if _, _, _, err := sconn.recvHeadBytes(readCh2, time.Minute); err == nil {
t.Error("success recv head bytes from closed connection")
return
}
}

func TestInvalidConn(t *testing.T) {
_, err := NewConn(
NewSettings(&SSettings{
FMessageSizeBytes: testutils.TCMessageSize,
FWaitReadDeadline: time.Hour,
FReadDeadline: time.Minute,
FWriteDeadline: time.Minute,
}),
"INVALID_ADDRESS",
)
if err == nil {
t.Error("success connect to invalid address")
return
}
}

func TestConnWithNetworkKey(t *testing.T) {
testConn(t, testutils.TgAddrs[17], "")
testConn(t, testutils.TgAddrs[29], "hello, world!")
}

func testConn(t *testing.T, pAddr, pNetworkKey string) {
listener := testNewService(t, pAddr, pNetworkKey)
defer testFreeService(listener)

conn, err := NewConn(
NewSettings(&SSettings{
FMessageSizeBytes: testutils.TCMessageSize,
FWaitReadDeadline: time.Hour,
FReadDeadline: time.Minute,
FWriteDeadline: time.Minute,
}),
pAddr,
)
if err != nil {
t.Error(err)
return
}

socket := conn.GetSocket()
remoteAddr := strings.ReplaceAll(pAddr, "localhost", "127.0.0.1")
if socket.RemoteAddr().String() != remoteAddr {
t.Error("got incorrect remote address")
return
}

conn.GetSettings().SetNetworkKey(pNetworkKey)

if err := conn.WritePayload(payload.NewPayload(tcHead, []byte(tcBody))); err != nil {
t.Error(err)
return
Expand All @@ -53,8 +198,8 @@ func TestConn(t *testing.T) {
}
}

func testNewService(t *testing.T) net.Listener {
listener, err := net.Listen("tcp", testutils.TgAddrs[17])
func testNewService(t *testing.T, pAddr, pNetworkKey string) net.Listener {
listener, err := net.Listen("tcp", pAddr)
if err != nil {
t.Error(err)
return nil
Expand All @@ -77,6 +222,8 @@ func testNewService(t *testing.T) net.Listener {
aconn,
)

conn.GetSettings().SetNetworkKey(pNetworkKey)

readCh := make(chan struct{})
go func() { <-readCh }()

Expand Down
53 changes: 33 additions & 20 deletions pkg/network/conn_keeper/conn_keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,21 @@ func testSettings(t *testing.T, n int) {
}
}

func TestConnKeeperSettings(t *testing.T) {
duration := time.Second / 2
connKeeper := newTestConnKeeper(duration)

if connKeeper.GetSettings().GetDuration() != duration {
t.Error("got invalid settings param")
return
}
}

func TestConnKeeper(t *testing.T) {
listener := testNewService(t)
defer testFreeService(listener)

node := network.NewNode(network.NewSettings(&network.SSettings{
FCapacity: testutils.TCCapacity,
FMaxConnects: testutils.TCMaxConnects,
FReadTimeout: time.Minute,
FWriteTimeout: time.Minute,
FConnSettings: conn.NewSettings(&conn.SSettings{
FMessageSizeBytes: testutils.TCMessageSize,
FWaitReadDeadline: time.Hour,
FReadDeadline: time.Minute,
FWriteDeadline: time.Minute,
}),
}))
connKeeper := NewConnKeeper(
NewSettings(&SSettings{
FConnections: func() []string { return []string{testutils.TgAddrs[18]} },
FDuration: 500 * time.Millisecond,
}),
node,
)
connKeeper := newTestConnKeeper(time.Second / 2)

if node := connKeeper.GetNetworkNode(); node == nil {
t.Error("network node is nil")
Expand All @@ -75,7 +67,7 @@ func TestConnKeeper(t *testing.T) {
}

time.Sleep(time.Second)
if len(node.GetConnections()) != 1 {
if len(connKeeper.GetNetworkNode().GetConnections()) != 1 {
t.Error("length of connections != 1")
return
}
Expand All @@ -91,6 +83,27 @@ func TestConnKeeper(t *testing.T) {
}
}

func newTestConnKeeper(pDuration time.Duration) IConnKeeper {
return NewConnKeeper(
NewSettings(&SSettings{
FConnections: func() []string { return []string{testutils.TgAddrs[18]} },
FDuration: pDuration,
}),
network.NewNode(network.NewSettings(&network.SSettings{
FCapacity: testutils.TCCapacity,
FMaxConnects: testutils.TCMaxConnects,
FReadTimeout: time.Minute,
FWriteTimeout: time.Minute,
FConnSettings: conn.NewSettings(&conn.SSettings{
FMessageSizeBytes: testutils.TCMessageSize,
FWaitReadDeadline: time.Hour,
FReadDeadline: time.Minute,
FWriteDeadline: time.Minute,
}),
})),
)
}

func testNewService(t *testing.T) net.Listener {
listener, err := net.Listen("tcp", testutils.TgAddrs[18])
if err != nil {
Expand Down
24 changes: 24 additions & 0 deletions pkg/network/message/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"testing"

"github.com/number571/go-peer/pkg/crypto/random"
"github.com/number571/go-peer/pkg/payload"
)

Expand Down Expand Up @@ -36,4 +37,27 @@ func TestMessage(t *testing.T) {
t.Error("load message not equal new message")
return
}

if msg := LoadMessage([]byte{1}); msg != nil {
t.Error("success load incorrect message")
return
}

prng := random.NewStdPRNG()
if msg := LoadMessage(prng.GetBytes(64)); msg != nil {
t.Error("success load incorrect message")
return
}

msgBytes := bytes.Join(
[][]byte{
{}, // pass payload
getHash([]byte{}),
},
[]byte{},
)
if msg := LoadMessage(msgBytes); msg != nil {
t.Error("success load incorrect payload")
return
}
}
Loading

0 comments on commit b41df8f

Please sign in to comment.