-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
number571
committed
Nov 4, 2024
1 parent
d16a49f
commit 3ff73ec
Showing
11 changed files
with
222 additions
and
20 deletions.
There are no files selected for viewing
128 changes: 126 additions & 2 deletions
128
internal/applications/remoter/internal/handler/handler_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,131 @@ | ||
package handler | ||
|
||
import ( | ||
"testing" | ||
"context" | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
"github.com/number571/go-peer/pkg/crypto/asymmetric" | ||
"github.com/number571/go-peer/pkg/logger" | ||
"github.com/number571/hidden-lake/internal/applications/remoter/pkg/app/config" | ||
"github.com/number571/hidden-lake/internal/applications/remoter/pkg/settings" | ||
"github.com/number571/hidden-lake/internal/service/pkg/app" | ||
hls_config "github.com/number571/hidden-lake/internal/service/pkg/app/config" | ||
) | ||
|
||
var ( | ||
tgPrivKey1 = asymmetric.NewPrivKey() | ||
tgPrivKey2 = asymmetric.NewPrivKey() | ||
) | ||
|
||
const ( | ||
tcPassword = "test-password" | ||
tcPathConfigTemplate = "config_test_%d.yml" | ||
tcPathDatabaseTemplate = "database_test_%d.yml" | ||
) | ||
|
||
func TestNothing(_ *testing.T) {} | ||
func testRunService(addr string) (config.IConfig, *http.Server) { | ||
mux := http.NewServeMux() | ||
|
||
cfg := &config.SConfig{ | ||
FSettings: &config.SConfigSettings{ | ||
FPassword: tcPassword, | ||
FExecTimeoutMS: 5_000, | ||
}, | ||
FAddress: &config.SAddress{ | ||
FIncoming: addr, | ||
}, | ||
} | ||
|
||
logger := logger.NewLogger( | ||
logger.NewSettings(&logger.SSettings{}), | ||
func(_ logger.ILogArg) string { return "" }, | ||
) | ||
|
||
ctx := context.Background() | ||
|
||
mux.HandleFunc(settings.CExecPath, HandleIncomingExecHTTP(ctx, cfg, logger)) | ||
|
||
srv := &http.Server{ | ||
Addr: addr, | ||
Handler: http.TimeoutHandler(mux, time.Minute/2, "timeout"), | ||
ReadTimeout: time.Second, | ||
} | ||
|
||
go func() { _ = srv.ListenAndServe() }() | ||
return cfg, srv | ||
} | ||
|
||
func testRunNewNodes(ctx context.Context, httpAddrNode1, tcpAddrNode2, addrService string) { | ||
runner1 := app.NewApp( | ||
initConfig( | ||
"./testdata/node1/hls.yml", | ||
"", | ||
httpAddrNode1, | ||
"", | ||
tcpAddrNode2, | ||
tgPrivKey2.GetPubKey(), | ||
), | ||
tgPrivKey1, | ||
"./testdata/node1", | ||
1, | ||
) | ||
|
||
runner2 := app.NewApp( | ||
initConfig( | ||
"./testdata/node2/hls.yml", | ||
tcpAddrNode2, | ||
"", | ||
addrService, | ||
"", | ||
tgPrivKey1.GetPubKey(), | ||
), | ||
tgPrivKey2, | ||
"./testdata/node2", | ||
1, | ||
) | ||
|
||
go func() { _ = runner1.Run(ctx) }() | ||
go func() { _ = runner2.Run(ctx) }() | ||
} | ||
|
||
func initConfig( | ||
cfgPath, tcpAddrNode, httpAddrNode, addrService, conn string, | ||
pubKey asymmetric.IPubKey, | ||
) hls_config.IConfig { | ||
os.Remove(cfgPath) | ||
|
||
services := map[string]string{} | ||
if addrService != "" { | ||
services[settings.CServiceFullName] = addrService | ||
} | ||
|
||
connections := []string{} | ||
if conn != "" { | ||
connections = []string{conn} | ||
} | ||
|
||
cfg, err := hls_config.BuildConfig(cfgPath, &hls_config.SConfig{ | ||
FSettings: &hls_config.SConfigSettings{ | ||
FMessageSizeBytes: (8 << 10), | ||
FWorkSizeBits: 1, | ||
FFetchTimeoutMS: 10_000, | ||
FQueuePeriodMS: 500, | ||
}, | ||
FLogging: []string{}, | ||
FAddress: &hls_config.SAddress{ | ||
FTCP: tcpAddrNode, | ||
FHTTP: httpAddrNode, | ||
}, | ||
FServices: services, | ||
FConnections: connections, | ||
FFriends: map[string]string{ | ||
"test_recv": pubKey.ToString(), | ||
}, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return cfg | ||
} |
58 changes: 58 additions & 0 deletions
58
internal/applications/remoter/internal/handler/incoming_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package handler | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/number571/hidden-lake/internal/applications/remoter/pkg/client" | ||
"github.com/number571/hidden-lake/internal/applications/remoter/pkg/settings" | ||
hls_client "github.com/number571/hidden-lake/internal/service/pkg/client" | ||
testutils "github.com/number571/hidden-lake/test/utils" | ||
) | ||
|
||
func TestIncomingExecHTTP(t *testing.T) { | ||
t.Parallel() | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
_, service := testRunService(testutils.TgAddrs[40]) | ||
defer service.Close() | ||
|
||
testRunNewNodes( | ||
ctx, | ||
testutils.TgAddrs[41], | ||
testutils.TgAddrs[42], | ||
testutils.TgAddrs[40], | ||
) | ||
|
||
time.Sleep(100 * time.Millisecond) | ||
|
||
hlsClient := hls_client.NewClient( | ||
hls_client.NewBuilder(), | ||
hls_client.NewRequester( | ||
"http://"+testutils.TgAddrs[41], | ||
&http.Client{Timeout: time.Minute}, | ||
), | ||
) | ||
|
||
hlrClient := client.NewClient( | ||
client.NewBuilder(tcPassword), | ||
client.NewRequester(hlsClient), | ||
) | ||
|
||
msg := "hello, world!" | ||
rsp, err := hlrClient.Exec(ctx, "test_recv", "echo"+settings.CExecSeparator+msg) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
if strings.TrimSpace(string(rsp)) != msg { | ||
t.Error("get invalid response") | ||
return | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
internal/applications/remoter/internal/handler/testdata/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.yml | ||
*.db |
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters