config_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. package config_test
  2. import (
  3. "errors"
  4. "io/fs"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "github.com/TecharoHQ/anubis/data"
  9. . "github.com/TecharoHQ/anubis/lib/config"
  10. )
  11. func p[V any](v V) *V { return &v }
  12. func TestBotValid(t *testing.T) {
  13. var tests = []struct {
  14. bot BotConfig
  15. err error
  16. name string
  17. }{
  18. {
  19. name: "simple user agent",
  20. bot: BotConfig{
  21. Name: "mozilla-ua",
  22. Action: RuleChallenge,
  23. UserAgentRegex: p("Mozilla"),
  24. },
  25. err: nil,
  26. },
  27. {
  28. name: "simple path",
  29. bot: BotConfig{
  30. Name: "well-known-path",
  31. Action: RuleAllow,
  32. PathRegex: p("^/.well-known/.*$"),
  33. },
  34. err: nil,
  35. },
  36. {
  37. name: "no rule name",
  38. bot: BotConfig{
  39. Action: RuleChallenge,
  40. UserAgentRegex: p("Mozilla"),
  41. },
  42. err: ErrBotMustHaveName,
  43. },
  44. {
  45. name: "no rule matcher",
  46. bot: BotConfig{
  47. Name: "broken-rule",
  48. Action: RuleAllow,
  49. },
  50. err: ErrBotMustHaveUserAgentOrPath,
  51. },
  52. {
  53. name: "both user-agent and path",
  54. bot: BotConfig{
  55. Name: "path-and-user-agent",
  56. Action: RuleDeny,
  57. UserAgentRegex: p("Mozilla"),
  58. PathRegex: p("^/.secret-place/.*$"),
  59. },
  60. err: ErrBotMustHaveUserAgentOrPathNotBoth,
  61. },
  62. {
  63. name: "unknown action",
  64. bot: BotConfig{
  65. Name: "Unknown action",
  66. Action: RuleUnknown,
  67. UserAgentRegex: p("Mozilla"),
  68. },
  69. err: ErrUnknownAction,
  70. },
  71. {
  72. name: "invalid user agent regex",
  73. bot: BotConfig{
  74. Name: "mozilla-ua",
  75. Action: RuleChallenge,
  76. UserAgentRegex: p("a(b"),
  77. },
  78. err: ErrInvalidUserAgentRegex,
  79. },
  80. {
  81. name: "invalid path regex",
  82. bot: BotConfig{
  83. Name: "mozilla-ua",
  84. Action: RuleChallenge,
  85. PathRegex: p("a(b"),
  86. },
  87. err: ErrInvalidPathRegex,
  88. },
  89. {
  90. name: "invalid headers regex",
  91. bot: BotConfig{
  92. Name: "mozilla-ua",
  93. Action: RuleChallenge,
  94. HeadersRegex: map[string]string{
  95. "Content-Type": "a(b",
  96. },
  97. PathRegex: p("a(b"),
  98. },
  99. err: ErrInvalidHeadersRegex,
  100. },
  101. {
  102. name: "challenge difficulty too low",
  103. bot: BotConfig{
  104. Name: "mozilla-ua",
  105. Action: RuleChallenge,
  106. PathRegex: p("Mozilla"),
  107. Challenge: &ChallengeRules{
  108. Difficulty: -1,
  109. Algorithm: "fast",
  110. },
  111. },
  112. err: ErrChallengeDifficultyTooLow,
  113. },
  114. {
  115. name: "challenge difficulty too high",
  116. bot: BotConfig{
  117. Name: "mozilla-ua",
  118. Action: RuleChallenge,
  119. PathRegex: p("Mozilla"),
  120. Challenge: &ChallengeRules{
  121. Difficulty: 420,
  122. Algorithm: "fast",
  123. },
  124. },
  125. err: ErrChallengeDifficultyTooHigh,
  126. },
  127. {
  128. name: "invalid cidr range",
  129. bot: BotConfig{
  130. Name: "mozilla-ua",
  131. Action: RuleAllow,
  132. RemoteAddr: []string{"0.0.0.0/33"},
  133. },
  134. err: ErrInvalidCIDR,
  135. },
  136. {
  137. name: "only filter by IP range",
  138. bot: BotConfig{
  139. Name: "mozilla-ua",
  140. Action: RuleAllow,
  141. RemoteAddr: []string{"0.0.0.0/0"},
  142. },
  143. err: nil,
  144. },
  145. {
  146. name: "filter by user agent and IP range",
  147. bot: BotConfig{
  148. Name: "mozilla-ua",
  149. Action: RuleAllow,
  150. UserAgentRegex: p("Mozilla"),
  151. RemoteAddr: []string{"0.0.0.0/0"},
  152. },
  153. err: nil,
  154. },
  155. {
  156. name: "filter by path and IP range",
  157. bot: BotConfig{
  158. Name: "mozilla-ua",
  159. Action: RuleAllow,
  160. PathRegex: p("^.*$"),
  161. RemoteAddr: []string{"0.0.0.0/0"},
  162. },
  163. err: nil,
  164. },
  165. {
  166. name: "weight rule without weight",
  167. bot: BotConfig{
  168. Name: "weight-adjust-if-mozilla",
  169. Action: RuleWeigh,
  170. UserAgentRegex: p("Mozilla"),
  171. },
  172. },
  173. {
  174. name: "weight rule with weight adjust",
  175. bot: BotConfig{
  176. Name: "weight-adjust-if-mozilla",
  177. Action: RuleWeigh,
  178. UserAgentRegex: p("Mozilla"),
  179. Weight: &Weight{
  180. Adjust: 5,
  181. },
  182. },
  183. },
  184. }
  185. for _, cs := range tests {
  186. cs := cs
  187. t.Run(cs.name, func(t *testing.T) {
  188. err := cs.bot.Valid()
  189. if err == nil && cs.err == nil {
  190. return
  191. }
  192. if err == nil && cs.err != nil {
  193. t.Errorf("didn't get an error, but wanted: %v", cs.err)
  194. }
  195. if !errors.Is(err, cs.err) {
  196. t.Logf("got wrong error from Valid()")
  197. t.Logf("wanted: %v", cs.err)
  198. t.Logf("got: %v", err)
  199. t.Errorf("got invalid error from check")
  200. }
  201. })
  202. }
  203. }
  204. func TestConfigValidKnownGood(t *testing.T) {
  205. finfos, err := os.ReadDir("testdata/good")
  206. if err != nil {
  207. t.Fatal(err)
  208. }
  209. for _, st := range finfos {
  210. st := st
  211. t.Run(st.Name(), func(t *testing.T) {
  212. fin, err := os.Open(filepath.Join("testdata", "good", st.Name()))
  213. if err != nil {
  214. t.Fatal(err)
  215. }
  216. defer fin.Close()
  217. c, err := Load(fin, st.Name())
  218. if err != nil {
  219. t.Fatal(err)
  220. }
  221. if err := c.Valid(); err != nil {
  222. t.Error(err)
  223. }
  224. if len(c.Bots) == 0 {
  225. t.Error("wanted more than 0 bots, got zero")
  226. }
  227. })
  228. }
  229. }
  230. func TestImportStatement(t *testing.T) {
  231. type testCase struct {
  232. err error
  233. name string
  234. importPath string
  235. }
  236. var tests []testCase
  237. for _, folderName := range []string{
  238. "apps",
  239. "bots",
  240. "common",
  241. "crawlers",
  242. "meta",
  243. } {
  244. if err := fs.WalkDir(data.BotPolicies, folderName, func(path string, d fs.DirEntry, err error) error {
  245. if err != nil {
  246. return err
  247. }
  248. if d.IsDir() {
  249. return nil
  250. }
  251. if d.Name() == "README.md" {
  252. return nil
  253. }
  254. tests = append(tests, testCase{
  255. name: "(data)/" + path,
  256. importPath: "(data)/" + path,
  257. err: nil,
  258. })
  259. return nil
  260. }); err != nil {
  261. t.Fatal(err)
  262. }
  263. }
  264. for _, tt := range tests {
  265. t.Run(tt.name, func(t *testing.T) {
  266. is := &ImportStatement{
  267. Import: tt.importPath,
  268. }
  269. if err := is.Valid(); err != nil {
  270. t.Errorf("validation error: %v", err)
  271. }
  272. if len(is.Bots) == 0 {
  273. t.Error("wanted bot definitions, but got none")
  274. }
  275. })
  276. }
  277. }
  278. func TestConfigValidBad(t *testing.T) {
  279. finfos, err := os.ReadDir("testdata/bad")
  280. if err != nil {
  281. t.Fatal(err)
  282. }
  283. for _, st := range finfos {
  284. st := st
  285. t.Run(st.Name(), func(t *testing.T) {
  286. fin, err := os.Open(filepath.Join("testdata", "bad", st.Name()))
  287. if err != nil {
  288. t.Fatal(err)
  289. }
  290. defer fin.Close()
  291. _, err = Load(fin, filepath.Join("testdata", "bad", st.Name()))
  292. if err == nil {
  293. t.Fatal("validation should have failed but didn't somehow")
  294. } else {
  295. t.Log(err)
  296. }
  297. })
  298. }
  299. }
  300. func TestBotConfigZero(t *testing.T) {
  301. var b BotConfig
  302. if !b.Zero() {
  303. t.Error("zero value config.BotConfig is not zero value")
  304. }
  305. b.Name = "hi"
  306. if b.Zero() {
  307. t.Error("config.BotConfig with name is zero value")
  308. }
  309. b.UserAgentRegex = p(".*")
  310. if b.Zero() {
  311. t.Error("config.BotConfig with user agent regex is zero value")
  312. }
  313. b.PathRegex = p(".*")
  314. if b.Zero() {
  315. t.Error("config.BotConfig with path regex is zero value")
  316. }
  317. b.HeadersRegex = map[string]string{"hi": "there"}
  318. if b.Zero() {
  319. t.Error("config.BotConfig with headers regex is zero value")
  320. }
  321. b.Action = RuleAllow
  322. if b.Zero() {
  323. t.Error("config.BotConfig with action is zero value")
  324. }
  325. b.RemoteAddr = []string{"::/0"}
  326. if b.Zero() {
  327. t.Error("config.BotConfig with remote addresses is zero value")
  328. }
  329. b.Challenge = &ChallengeRules{
  330. Difficulty: 4,
  331. Algorithm: DefaultAlgorithm,
  332. }
  333. if b.Zero() {
  334. t.Error("config.BotConfig with challenge rules is zero value")
  335. }
  336. }