ban_test.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // Copyright (C) 2017-2021 Marcus Rohrmoser, http://purl.mro.name/ShaarliGo
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. //
  17. package main
  18. import (
  19. "gopkg.in/yaml.v2"
  20. "time"
  21. "github.com/stretchr/testify/assert"
  22. "testing"
  23. )
  24. func TestSuffix(t *testing.T) {
  25. assert.Equal(t, "1.2.3.4", remoteAddressToKey("1.2.3.4:5"), "soso")
  26. }
  27. func TestIsRemoteAddrBanned(t *testing.T) {
  28. now := mustParseRFC3339("2017-11-01T00:00:00+01:00")
  29. {
  30. data, err := yaml.Marshal(now)
  31. assert.Nil(t, err, "soso")
  32. assert.Equal(t, "2017-11-01T00:00:00+01:00\n", string(data), "Oh je")
  33. }
  34. bp := BanPenalties{
  35. Penalties: map[string]Penalty{
  36. "92.194.87.209": {Badness: -100, End: mustParseRFC3339("2017-01-01T01:02:03+02:00")},
  37. "92.194.87.210": {Badness: -100, End: now.Add(10 * time.Minute)},
  38. "1.2.3.3": {Badness: 2, End: now.Add(4 * time.Hour)},
  39. "1.2.3.4": {Badness: 10, End: now.Add(10 * time.Minute)},
  40. },
  41. }
  42. {
  43. data, err := yaml.Marshal(bp)
  44. assert.Nil(t, err, "soso")
  45. assert.Equal(t, `penalties:
  46. 1.2.3.3:
  47. badness: 2
  48. end: 2017-11-01T04:00:00+01:00
  49. 1.2.3.4:
  50. badness: 10
  51. end: 2017-11-01T00:10:00+01:00
  52. 92.194.87.209:
  53. badness: -100
  54. end: 2017-01-01T01:02:03+02:00
  55. 92.194.87.210:
  56. badness: -100
  57. end: 2017-11-01T00:10:00+01:00
  58. `, string(data), "ach!")
  59. }
  60. assert.NotNil(t, &bp, "soso")
  61. assert.Equal(t, -100, bp.Penalties["92.194.87.209"].Badness, "soso")
  62. assert.False(t, BanPenalties{}.isRemoteAddrBanned("nix", time.Time{}), "unknown shouldn't be banned from the start")
  63. assert.False(t, bp.isRemoteAddrBanned("nix", time.Time{}), "unknown shouldn't be banned from the start")
  64. assert.False(t, bp.isRemoteAddrBanned("1.2.3.3", time.Time{}), "should not be banned yet")
  65. assert.False(t, bp.isRemoteAddrBanned("1.2.3.3", now), "should not be banned yet")
  66. assert.True(t, bp.isRemoteAddrBanned("1.2.3.4", time.Time{}), "should be banned")
  67. assert.True(t, bp.isRemoteAddrBanned("1.2.3.4", now), "should be banned")
  68. assert.False(t, bp.isRemoteAddrBanned("1.2.3.4", now.Add(10*24*time.Hour)), "ban should be lifted meanwhile")
  69. }