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 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (c) 2020 Shivaram Lingamneni
  2. // released under the MIT license
  3. package irc
  4. import (
  5. "reflect"
  6. "testing"
  7. )
  8. func TestEnvironmentOverrides(t *testing.T) {
  9. var config Config
  10. config.Server.Compatibility.SendUnprefixedSasl = true
  11. config.History.Enabled = true
  12. defaultUserModes := "+i"
  13. config.Accounts.DefaultUserModes = &defaultUserModes
  14. config.Server.WebSockets.AllowedOrigins = []string{"https://www.ircv3.net"}
  15. config.Server.MOTD = "long.motd.txt" // overwrite this
  16. env := []string{
  17. `USER=shivaram`, // unrelated var
  18. `ORAGONO_USER=oragono`, // this should be ignored as well
  19. `ERGO__NETWORK__NAME=example.com`,
  20. `ORAGONO__SERVER__COMPATIBILITY__FORCE_TRAILING=false`,
  21. `ORAGONO__SERVER__COERCE_IDENT="~user"`,
  22. `ERGO__SERVER__MOTD=short.motd.txt`,
  23. `ORAGONO__ACCOUNTS__NICK_RESERVATION__ENABLED=true`,
  24. `ERGO__ACCOUNTS__DEFAULT_USER_MODES="+iR"`,
  25. `ORAGONO__SERVER__IP_CLOAKING={"enabled": true, "enabled-for-always-on": true, "netname": "irc", "cidr-len-ipv4": 32, "cidr-len-ipv6": 64, "num-bits": 64}`,
  26. }
  27. for _, envPair := range env {
  28. _, _, err := mungeFromEnvironment(&config, envPair)
  29. if err != nil {
  30. t.Errorf("couldn't apply override `%s`: %v", envPair, err)
  31. }
  32. }
  33. if config.Network.Name != "example.com" {
  34. t.Errorf("unexpected value of network.name: %s", config.Network.Name)
  35. }
  36. if config.Server.CoerceIdent != "~user" {
  37. t.Errorf("unexpected value of coerce-ident: %s", config.Server.CoerceIdent)
  38. }
  39. if config.Server.MOTD != "short.motd.txt" {
  40. t.Errorf("unexpected value of motd: %s", config.Server.MOTD)
  41. }
  42. if !config.Accounts.NickReservation.Enabled {
  43. t.Errorf("did not set bool as expected")
  44. }
  45. if !config.Server.Compatibility.SendUnprefixedSasl {
  46. t.Errorf("overwrote unrelated field")
  47. }
  48. if !config.History.Enabled {
  49. t.Errorf("overwrote unrelated field")
  50. }
  51. if !reflect.DeepEqual(config.Server.WebSockets.AllowedOrigins, []string{"https://www.ircv3.net"}) {
  52. t.Errorf("overwrote unrelated field: %#v", config.Server.WebSockets.AllowedOrigins)
  53. }
  54. cloakConf := config.Server.Cloaks
  55. if !(cloakConf.Enabled == true && cloakConf.EnabledForAlwaysOn == true && cloakConf.Netname == "irc" && cloakConf.CidrLenIPv6 == 64) {
  56. t.Errorf("bad value of Cloaks: %#v", config.Server.Cloaks)
  57. }
  58. if *config.Server.Compatibility.ForceTrailing != false {
  59. t.Errorf("couldn't set unset ptr field to false")
  60. }
  61. if *config.Accounts.DefaultUserModes != "+iR" {
  62. t.Errorf("couldn't override pre-set ptr field")
  63. }
  64. }
  65. func TestEnvironmentOverrideErrors(t *testing.T) {
  66. var config Config
  67. config.Server.Compatibility.SendUnprefixedSasl = true
  68. config.History.Enabled = true
  69. invalidEnvs := []string{
  70. `ORAGONO__=asdf`,
  71. `ORAGONO__SERVER__=asdf`,
  72. `ORAGONO__SERVER____=asdf`,
  73. `ORAGONO__NONEXISTENT_KEY=1`,
  74. `ORAGONO__SERVER__NONEXISTENT_KEY=1`,
  75. // invalid yaml:
  76. `ORAGONO__SERVER__IP_CLOAKING__NETNAME="`,
  77. // invalid type:
  78. `ORAGONO__SERVER__IP_CLOAKING__NUM_BITS=asdf`,
  79. `ORAGONO__SERVER__STS=[]`,
  80. // index into non-struct:
  81. `ORAGONO__NETWORK__NAME__QUX=1`,
  82. // private field:
  83. `ORAGONO__SERVER__PASSWORDBYTES="asdf"`,
  84. }
  85. for _, env := range invalidEnvs {
  86. success, _, err := mungeFromEnvironment(&config, env)
  87. if err == nil || success {
  88. t.Errorf("accepted invalid env override `%s`", env)
  89. }
  90. }
  91. }