Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cmd/dmsg-discovery/commands/dmsg-discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var (
testEnvironment bool
pk cipher.PubKey
sk cipher.SecKey
keyFile string
dmsgPort uint16
authPassphrase string
officialServers string
Expand All @@ -68,6 +69,7 @@ func init() {
RootCmd.Flags().BoolVar(&enableLoadTesting, "enable-load-testing", false, "enable load testing")
RootCmd.Flags().BoolVar(&testEnvironment, "test-environment", false, "distinguished between prod and test environment")
RootCmd.Flags().Var(&sk, "sk", "dmsg secret key\n\r")
RootCmd.Flags().StringVar(&keyFile, "keyfile", "", "path to file containing secret key (auto-generated if missing)\n\r")
RootCmd.Flags().Uint16Var(&dmsgPort, "dmsgPort", dmsg.DefaultDmsgHTTPPort, "dmsg port value\n\r")
RootCmd.Flags().StringVar(&dmsgServerType, "dmsg-server-type", "", "type of dmsg server on dmsghttp handler")
}
Expand Down Expand Up @@ -115,6 +117,11 @@ Example:
log := sf.Logger()

var err error
if keyFile != "" {
if err = dmsgcmdutil.LoadOrGenerateKey(keyFile, &sk); err != nil {
log.Fatal("Failed to load keyfile: ", err)
}
}
if pk, err = sk.PubKey(); err != nil {
log.WithError(err).Warn("No SecKey found. Skipping serving on dmsghttp.")
}
Expand Down
123 changes: 123 additions & 0 deletions cmd/dmsgweb/commands/conf_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package commands

import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
)

// TestDmsgwebConfFile verifies that dmsgweb reads defaults from a conf file
// specified via the DMSGWEB env var and reflects them in --help output.
func TestDmsgwebConfFile(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("bash-based conf file not applicable on Windows")
}

dir := t.TempDir()
confPath := filepath.Join(dir, "dmsgweb-test.conf")
confContent := `WEBPORT=(9090 9091)
PROXYPORT=5555
ADDPROXY='127.0.0.1:1080'
`
if err := os.WriteFile(confPath, []byte(confContent), 0600); err != nil {
t.Fatal(err)
}

// Run dmsgweb --help with the conf file
cmd := exec.Command("go", "run", "./cmd/dmsgweb", "--help")
cmd.Dir = repoRoot(t)
cmd.Env = append(os.Environ(), "DMSGWEB="+confPath)
out, err := cmd.CombinedOutput()
if err != nil {
// --help exits with code 0 normally, but some cobra versions exit non-zero
if !strings.Contains(string(out), "Usage:") {
t.Fatalf("dmsgweb --help failed: %v\noutput: %s", err, out)
}
}

helpText := string(out)

// Check that conf file values appear as defaults in help output
checks := []struct {
desc string
substr string
}{
{"WEBPORT should be 9090,9091", "[9090,9091]"},
{"PROXYPORT should be 5555", "5555"},
{"ADDPROXY should be 127.0.0.1:1080", "127.0.0.1:1080"},
}

for _, c := range checks {
if !strings.Contains(helpText, c.substr) {
t.Errorf("%s: expected %q in help output, not found.\nHelp output:\n%s", c.desc, c.substr, helpText)
}
}
}

// TestDmsgwebSrvConfFile verifies that dmsgweb srv reads defaults from a conf file
// specified via the DMSGWEBSRV env var and reflects them in --help output.
func TestDmsgwebSrvConfFile(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("bash-based conf file not applicable on Windows")
}

dir := t.TempDir()
confPath := filepath.Join(dir, "dmsgwebsrv-test.conf")
confContent := `DMSGPORT=(8888 8889)
LOCALPORT=(7070 7071)
WHITELISTPKS=('02a49bc0aa1b5b78f638e9189be4c5d699e6d1358472d8a47f4c20daacd672d7e5')
`
if err := os.WriteFile(confPath, []byte(confContent), 0600); err != nil {
t.Fatal(err)
}

// Run dmsgweb srv --help with the conf file
cmd := exec.Command("go", "run", "./cmd/dmsgweb", "srv", "--help")
cmd.Dir = repoRoot(t)
cmd.Env = append(os.Environ(), "DMSGWEBSRV="+confPath)
out, err := cmd.CombinedOutput()
if err != nil {
if !strings.Contains(string(out), "Usage:") {
t.Fatalf("dmsgweb srv --help failed: %v\noutput: %s", err, out)
}
}

helpText := string(out)

checks := []struct {
desc string
substr string
}{
{"DMSGPORT should be 8888,8889", "[8888,8889]"},
{"LOCALPORT should be 7070,7071", "[7070,7071]"},
{"WHITELISTPKS should contain the PK", "02a49bc0aa1b5b78"},
}

for _, c := range checks {
if !strings.Contains(helpText, c.substr) {
t.Errorf("%s: expected %q in help output, not found.\nHelp output:\n%s", c.desc, c.substr, helpText)
}
}
}

// repoRoot walks up from cwd to find go.mod
func repoRoot(t *testing.T) string {
t.Helper()
dir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
for {
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
return dir
}
parent := filepath.Dir(dir)
if parent == dir {
t.Fatal("could not find repo root (go.mod)")
}
dir = parent
}
}
14 changes: 7 additions & 7 deletions cmd/dmsgweb/commands/dmsgweb.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ const dwenv = "DMSGWEB"
var dwcfg = os.Getenv(dwenv)

func init() {
webPort = scriptExecUintSlice("${WEBPORT[@]:-8080}", dwcfg)
proxyPort = scriptExecUint("${PROXYPORT:-4445}", dwcfg)
addProxy = scriptExecString("${ADDPROXY}", dwcfg)
resolveDmsgAddr = scriptExecStringSlice("${RESOLVEPK[@]}", dwcfg)
rawTCP = scriptExecBoolSlice("${RAWTCP[@]:-false}", dwcfg)
webPort = cmdutil.SkyenvUintSlice("${WEBPORT[@]:-8080}", dwcfg)
proxyPort = cmdutil.SkyenvUint("${PROXYPORT:-4445}", dwcfg)
addProxy = cmdutil.SkyenvString("${ADDPROXY}", dwcfg)
resolveDmsgAddr = cmdutil.SkyenvStringSlice("${RESOLVEPK[@]}", dwcfg)
rawTCP = cmdutil.SkyenvBoolSlice("${RAWTCP[@]:-false}", dwcfg)
if os.Getenv("DMSGWEBSK") != "" {
sk.Set(os.Getenv("DMSGWEBSK")) //nolint
}
if scriptExecString("${DMSGWEBSK}", dwcfg) != "" {
sk.Set(scriptExecString("${DMSGWEBSK}", dwcfg)) //nolint
if cmdutil.SkyenvString("${DMSGWEBSK}", dwcfg) != "" {
sk.Set(cmdutil.SkyenvString("${DMSGWEBSK}", dwcfg)) //nolint
}
pk, _ = sk.PubKey() //nolint

Expand Down
12 changes: 6 additions & 6 deletions cmd/dmsgweb/commands/dmsgwebsrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ const dwsenv = "DMSGWEBSRV"
var dwscfg = os.Getenv(dwsenv)

func init() {
dmsgPort = scriptExecUintSlice("${DMSGPORT[@]:-80}", dwscfg)
wl = scriptExecStringSlice("${WHITELISTPKS[@]}", dwscfg)
localPort = scriptExecUintSlice("${LOCALPORT[@]:-8086}", dwscfg)
rawTCP = scriptExecBoolSlice("${RAWTCP[@]:-false}", dwscfg)
dmsgPort = cmdutil.SkyenvUintSlice("${DMSGPORT[@]:-80}", dwscfg)
wl = cmdutil.SkyenvStringSlice("${WHITELISTPKS[@]}", dwscfg)
localPort = cmdutil.SkyenvUintSlice("${LOCALPORT[@]:-8086}", dwscfg)
rawTCP = cmdutil.SkyenvBoolSlice("${RAWTCP[@]:-false}", dwscfg)
if os.Getenv("DMSGWEBSRVSK") != "" {
sk.Set(os.Getenv("DMSGWEBSRVSK")) //nolint
}
if scriptExecString("${DMSGWEBSRVSK}", dwscfg) != "" {
sk.Set(scriptExecString("${DMSGWEBSRVSK}", dwscfg)) //nolint
if cmdutil.SkyenvString("${DMSGWEBSRVSK}", dwscfg) != "" {
sk.Set(cmdutil.SkyenvString("${DMSGWEBSRVSK}", dwscfg)) //nolint
}
pk, _ = sk.PubKey() //nolint

Expand Down
Loading
Loading