db_gin_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package db
  2. import (
  3. "io/ioutil"
  4. "math/rand"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "testing"
  9. "time"
  10. "github.com/G-Node/gogs/internal/conf"
  11. )
  12. const ALNUM = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  13. var emails = []string{
  14. "foo@example.org",
  15. "spammer@example.com",
  16. "user@malicious-domain.net",
  17. "someone@example.mal",
  18. }
  19. var blockEverythingFilter = []string{
  20. "- .*",
  21. }
  22. var allowEverythingFilter = []string{
  23. "+ .*",
  24. }
  25. var allowGNodeFilter = []string{
  26. "+ @g-node.org$",
  27. "- .*",
  28. }
  29. var blockMalicious = []string{
  30. "- .*malicious-domain.net$",
  31. "- spammer@",
  32. }
  33. func filterExpect(t *testing.T, address string, expect bool) {
  34. if isAddressAllowed(address) != expect {
  35. t.Fatalf("Address %q block: expected %t got %t", address, expect, !expect)
  36. }
  37. }
  38. // Writes the filters to a file in the specified custom directory. This file needs to
  39. // be cleaned up afterwards. Returns the full path to the written file as string.
  40. func writeCustomDirFilterFile(t *testing.T, filters []string) string {
  41. fname := filepath.Join(conf.CustomDir(), "addressfilters")
  42. if err := ioutil.WriteFile(fname, []byte(strings.Join(filters, "\n")), 0777); err != nil {
  43. t.Fatalf("Failed to write line filters to file %q: %v", fname, err.Error())
  44. }
  45. return fname
  46. }
  47. // randAlnum returns a random alphanumeric (lowercase, latin) string of length 'n'.
  48. func randAlnum(n int) string {
  49. N := len(ALNUM)
  50. chrs := make([]byte, n)
  51. for idx := range chrs {
  52. chrs[idx] = ALNUM[rand.Intn(N)]
  53. }
  54. return string(chrs)
  55. }
  56. func randAddress() string {
  57. user := randAlnum(rand.Intn(20))
  58. domain := randAlnum(rand.Intn(20)) + "." + randAlnum(rand.Intn(3))
  59. return string(user) + "@" + string(domain)
  60. }
  61. func TestAllowGNodeFilter(t *testing.T) {
  62. cdir := filepath.Join(conf.CustomDir())
  63. if _, err := os.Stat(cdir); os.IsNotExist(err) {
  64. _ = os.Mkdir(cdir, 0777)
  65. }
  66. ffile := writeCustomDirFilterFile(t, allowGNodeFilter)
  67. defer os.Remove(ffile)
  68. for _, address := range emails {
  69. filterExpect(t, address, false)
  70. }
  71. filterExpect(t, "me@g-node.org", true)
  72. filterExpect(t, "malicious@g-node.org@piracy.tk", false)
  73. }
  74. func TestEverythingFilters(t *testing.T) {
  75. cdir := filepath.Join(conf.CustomDir())
  76. if _, err := os.Stat(cdir); os.IsNotExist(err) {
  77. _ = os.Mkdir(cdir, 0777)
  78. }
  79. ffile := writeCustomDirFilterFile(t, allowEverythingFilter)
  80. defer os.Remove(ffile)
  81. rand.Seed(time.Now().UnixNano())
  82. for idx := 0; idx < 100; idx++ {
  83. filterExpect(t, randAddress(), true)
  84. }
  85. ffile = writeCustomDirFilterFile(t, blockEverythingFilter)
  86. defer os.Remove(ffile)
  87. for idx := 0; idx < 100; idx++ {
  88. filterExpect(t, randAddress(), false)
  89. }
  90. }
  91. func TestBlockDomainFilter(t *testing.T) {
  92. cdir := filepath.Join(conf.CustomDir())
  93. if _, err := os.Stat(cdir); os.IsNotExist(err) {
  94. _ = os.Mkdir(cdir, 0777)
  95. }
  96. ffile := writeCustomDirFilterFile(t, blockMalicious)
  97. defer os.Remove(ffile)
  98. // 0, 3 should be allowed; 1, 2 should be blocked
  99. filterExpect(t, emails[0], true)
  100. filterExpect(t, emails[1], false)
  101. filterExpect(t, emails[2], false)
  102. filterExpect(t, emails[3], true)
  103. }
  104. func TestFiltersNone(t *testing.T) {
  105. filterExpect(t, emails[3], true)
  106. }