db_gin_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package db
  2. import (
  3. "io/ioutil"
  4. "math/rand"
  5. "path/filepath"
  6. "strings"
  7. "testing"
  8. "time"
  9. "github.com/G-Node/gogs/internal/setting"
  10. )
  11. const ALNUM = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  12. var emails = []string{
  13. "foo@example.org",
  14. "spammer@example.com",
  15. "user@malicious-domain.net",
  16. "someone@example.mal",
  17. }
  18. var blockEverythingFilter = []string{
  19. "- .*",
  20. }
  21. var allowEverythingFilter = []string{
  22. "+ .*",
  23. }
  24. var allowGNodeFilter = []string{
  25. "+ @g-node.org$",
  26. "- .*",
  27. }
  28. var blockMalicious = []string{
  29. "- .*malicious-domain.net$",
  30. "- spammer@",
  31. }
  32. func filterExpect(t *testing.T, address string, expect bool) {
  33. if isAddressAllowed(address) != expect {
  34. t.Fatalf("Address %q block: expected %t got %t", address, expect, !expect)
  35. }
  36. }
  37. // Writes the filters to a file and returns the directory that contains the
  38. // filter file (to be set as setting.CustomPath)
  39. func writeFilterFile(t *testing.T, filters []string) string {
  40. path := t.TempDir()
  41. fname := filepath.Join(path, "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 path
  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. setting.CustomPath = writeFilterFile(t, allowGNodeFilter)
  63. for _, address := range emails {
  64. filterExpect(t, address, false)
  65. }
  66. filterExpect(t, "me@g-node.org", true)
  67. filterExpect(t, "malicious@g-node.org@piracy.tk", false)
  68. }
  69. func TestEverythingFilters(t *testing.T) {
  70. setting.CustomPath = writeFilterFile(t, allowEverythingFilter)
  71. rand.Seed(time.Now().UnixNano())
  72. for idx := 0; idx < 100; idx++ {
  73. filterExpect(t, randAddress(), true)
  74. }
  75. setting.CustomPath = writeFilterFile(t, blockEverythingFilter)
  76. for idx := 0; idx < 100; idx++ {
  77. filterExpect(t, randAddress(), false)
  78. }
  79. }
  80. func TestBlockDomainFilter(t *testing.T) {
  81. setting.CustomPath = writeFilterFile(t, blockMalicious)
  82. // 0, 3 should be allowed; 1, 2 should be blocked
  83. filterExpect(t, emails[0], true)
  84. filterExpect(t, emails[1], false)
  85. filterExpect(t, emails[2], false)
  86. filterExpect(t, emails[3], true)
  87. }
  88. func TestFiltersNone(t *testing.T) {
  89. setting.CustomPath = filepath.Join(t.TempDir(), "does", "not", "exist")
  90. filterExpect(t, emails[3], true)
  91. }