db_gin_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. // Writes the filters to a file and returns the directory that contains the
  33. // filter file (to be set as setting.CustomPath)
  34. func writeFilterFile(t *testing.T, filters []string) string {
  35. path := t.TempDir()
  36. fname := filepath.Join(path, "addressfilters")
  37. if err := ioutil.WriteFile(fname, []byte(strings.Join(filters, "\n")), 0777); err != nil {
  38. t.Fatalf("Failed to write line filters to file %q: %v", fname, err.Error())
  39. }
  40. return path
  41. }
  42. // randAlnum returns a random alphanumeric (lowercase, latin) string of length 'n'.
  43. func randAlnum(n int) string {
  44. N := len(ALNUM)
  45. chrs := make([]byte, n)
  46. for idx := range chrs {
  47. chrs[idx] = ALNUM[rand.Intn(N)]
  48. }
  49. return string(chrs)
  50. }
  51. func randAddress() string {
  52. user := randAlnum(rand.Intn(20))
  53. domain := randAlnum(rand.Intn(20)) + "." + randAlnum(rand.Intn(3))
  54. return string(user) + "@" + string(domain)
  55. }
  56. func TestAllowGNodeFilter(t *testing.T) {
  57. setting.CustomPath = writeFilterFile(t, allowGNodeFilter)
  58. for _, address := range emails {
  59. if isAddressAllowed(address) {
  60. t.Fatalf("Address %q should be blocked but was allowed", address)
  61. }
  62. }
  63. if !isAddressAllowed("me@g-node.org") {
  64. t.Fatalf("G-Node address blocked but should be allowed")
  65. }
  66. if isAddressAllowed("malicious@g-node.org@piracy.tk") {
  67. t.Fatalf("Malicious address should be blocked but was allowed")
  68. }
  69. }
  70. func TestEverythingFilters(t *testing.T) {
  71. setting.CustomPath = writeFilterFile(t, allowEverythingFilter)
  72. rand.Seed(time.Now().UnixNano())
  73. for idx := 0; idx < 100; idx++ {
  74. randress := randAddress()
  75. if !isAddressAllowed(randAddress()) {
  76. t.Fatalf("Address %q should be allowed but was blocked", randress)
  77. }
  78. }
  79. setting.CustomPath = writeFilterFile(t, blockEverythingFilter)
  80. for idx := 0; idx < 100; idx++ {
  81. randress := randAddress()
  82. if isAddressAllowed(randAddress()) {
  83. t.Fatalf("Address %q should be blocked but was allowed", randress)
  84. }
  85. }
  86. }
  87. func TestBlockDomainFilter(t *testing.T) {
  88. setting.CustomPath = writeFilterFile(t, blockMalicious)
  89. // 0, 3 should be allowed; 1, 2 should be blocked
  90. if address := emails[0]; !isAddressAllowed(address) {
  91. t.Fatalf("Address %q should be allowed but was blocked", address)
  92. }
  93. if address := emails[1]; isAddressAllowed(address) {
  94. t.Fatalf("Address %q should be blocked but was allowed", address)
  95. }
  96. if address := emails[2]; isAddressAllowed(address) {
  97. t.Fatalf("Address %q should be blocked but was allowed", address)
  98. }
  99. if address := emails[3]; !isAddressAllowed(address) {
  100. t.Fatalf("Address %q should be allowed but was blocked", address)
  101. }
  102. }
  103. func TestFiltersNone(t *testing.T) {
  104. setting.CustomPath = filepath.Join(t.TempDir(), "does", "not", "exist")
  105. if address := emails[3]; !isAddressAllowed(address) {
  106. t.Fatalf("Address %q should be allowed but was blocked", address)
  107. }
  108. }