kaspad/config/config_test.go
Svarog ade33a94f3 [DEV-331] Disallowed node to run in reserved sub-networks (#148)
* [DEV-331] Disallowed node to run in reserved sub-networks

* [DEV-331] Added test to make sure that constant are not modified without modifying help text
2019-01-06 16:15:37 +02:00

90 lines
2.4 KiB
Go

package config
import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"runtime"
"testing"
"github.com/daglabs/btcd/wire"
)
var (
rpcuserRegexp = regexp.MustCompile("(?m)^rpcuser=.+$")
rpcpassRegexp = regexp.MustCompile("(?m)^rpcpass=.+$")
)
func TestCreateDefaultConfigFile(t *testing.T) {
// find out where the sample config lives
_, path, _, ok := runtime.Caller(0)
if !ok {
t.Fatalf("Failed finding config file path")
}
sampleConfigFile := filepath.Join(filepath.Dir(path), "..", "sample-btcd.conf")
// Setup a temporary directory
tmpDir, err := ioutil.TempDir("", "btcd")
if err != nil {
t.Fatalf("Failed creating a temporary directory: %v", err)
}
testpath := filepath.Join(tmpDir, "test.conf")
// copy config file to location of btcd binary
data, err := ioutil.ReadFile(sampleConfigFile)
if err != nil {
t.Fatalf("Failed reading sample config file: %v", err)
}
appPath, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
t.Fatalf("Failed obtaining app path: %v", err)
}
tmpConfigFile := filepath.Join(appPath, "sample-btcd.conf")
err = ioutil.WriteFile(tmpConfigFile, data, 0644)
if err != nil {
t.Fatalf("Failed copying sample config file: %v", err)
}
// Clean-up
defer func() {
os.Remove(testpath)
os.Remove(tmpConfigFile)
os.Remove(tmpDir)
}()
err = createDefaultConfigFile(testpath)
if err != nil {
t.Fatalf("Failed to create a default config file: %v", err)
}
content, err := ioutil.ReadFile(testpath)
if err != nil {
t.Fatalf("Failed to read generated default config file: %v", err)
}
if !rpcuserRegexp.Match(content) {
t.Error("Could not find rpcuser in generated default config file.")
}
if !rpcpassRegexp.Match(content) {
t.Error("Could not find rpcpass in generated default config file.")
}
}
// TestConstants makes sure that all constants hard-coded into the help text were not modified.
func TestConstants(t *testing.T) {
if wire.SubNetworkSupportsAll != 0 {
t.Errorf("wire.SubNetworkSupportsAll value was changed from 0, therefore you probably need to update the help text for SubNetwork")
}
if wire.SubNetworkReservedFirst != 3 {
t.Errorf("wire.SubNetworkReservedFirst value was changed from 0, therefore you probably need to update the help text for SubNetwork")
}
if wire.SubNetworkReservedLast != 255 {
t.Errorf("wire.SubNetworkReservedLast value was changed from 0, therefore you probably need to update the help text for SubNetwork")
}
}