This is a fork of github.com/btcsuite/btcd aimed at eliminating the tangle and complexity of its structure and make it more amenable to reuse as a part of another application, as well as easier reuse of its parts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config_test.go_ 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package main
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path/filepath"
  6. "regexp"
  7. "runtime"
  8. "testing"
  9. )
  10. var (
  11. rpcuserRegexp = regexp.MustCompile("(?m)^rpcuser=.+$")
  12. rpcpassRegexp = regexp.MustCompile("(?m)^rpcpass=.+$")
  13. )
  14. func TestCreateDefaultConfigFile(t *testing.T) {
  15. // find out where the sample config lives
  16. _, path, _, ok := runtime.Caller(0)
  17. if !ok {
  18. t.Fatalf("Failed finding config file path")
  19. }
  20. sampleConfigFile := filepath.Join(filepath.Dir(path), "sample-btcd.conf")
  21. // Setup a temporary directory
  22. tmpDir, err := ioutil.TempDir("", "btcd")
  23. if err != nil {
  24. t.Fatalf("Failed creating a temporary directory: %v", err)
  25. }
  26. testpath := filepath.Join(tmpDir, "test.conf")
  27. // copy config file to location of btcd binary
  28. data, err := ioutil.ReadFile(sampleConfigFile)
  29. if err != nil {
  30. t.Fatalf("Failed reading sample config file: %v", err)
  31. }
  32. appPath, err := filepath.Abs(filepath.Dir(os.Args[0]))
  33. if err != nil {
  34. t.Fatalf("Failed obtaining app path: %v", err)
  35. }
  36. tmpConfigFile := filepath.Join(appPath, "sample-btcd.conf")
  37. err = ioutil.WriteFile(tmpConfigFile, data, 0644)
  38. if err != nil {
  39. t.Fatalf("Failed copying sample config file: %v", err)
  40. }
  41. // Clean-up
  42. defer func() {
  43. os.Remove(testpath)
  44. os.Remove(tmpConfigFile)
  45. os.Remove(tmpDir)
  46. }()
  47. err = createDefaultConfigFile(testpath)
  48. if err != nil {
  49. t.Fatalf("Failed to create a default config file: %v", err)
  50. }
  51. content, err := ioutil.ReadFile(testpath)
  52. if err != nil {
  53. t.Fatalf("Failed to read generated default config file: %v", err)
  54. }
  55. if !rpcuserRegexp.Match(content) {
  56. t.Error("Could not find rpcuser in generated default config file.")
  57. }
  58. if !rpcpassRegexp.Match(content) {
  59. t.Error("Could not find rpcpass in generated default config file.")
  60. }
  61. }