2015-04-08 00:25:46 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
Reload authenticated-emails-file upon update
This change extracts the UserMap class from NewValidator() so that its
LoadAuthenticatedEmailsFile() method can be called concurrently. This method
is called by a goroutine containing a fsnotify.Watcher watching the
authenticated emails file.
Watching isn't forever aborted when the authenticated emails file disappears.
The goroutine will call os.Stat() up to twenty times a second if the file is
persistently missing, but that's the pathological case, not the common one.
The common case is that some editors (including Vim) will perform a
rename-and-replace when updating a file, triggering fsnotify.Rename events,
and the file will temporarily disappear. This watcher goroutine handles that
case.
Also, on some platforms (notably Arch Linux), a remove will be preceded by a
fsnotify.Chmod, causing a race between the upcoming fsnotify.Remove and the
call to UserMap.LoadAuthenticatedEmailsFile(). Hence, we treat fsnotify.Chmod
the same as fsnotify.Remove and fsnotify.Rename. There's no significant
penalty to re-adding a file to the watcher.
Also contains the following small changes from the summary of commits below:
- Minor optimization of email domain search
- Fixed api_test.go on Windows
- Add deferred File.Close() calls where needed
- Log error and return if emails file doesn't parse
These are the original commits from #89 squashed into this one:
0c6f2b6 Refactor validator_test to prepare for more tests
e0c792b Add more test cases to validator_test
a9a9d93 Minor optimization of email domain search
b763ea5 Extract LoadAuthenticatedEmailsFile()
8cdaf7f Introduce synchronized UserMap type
1b84eef Add UserMap methods, locking
af15dcf Reload authenticated-emails-file upon update
6d95548 Make UserMap operations lock-free
Per:
- http://stackoverflow.com/questions/21447463/is-assigning-a-pointer-atomic-in-golang
- https://groups.google.com/forum/#!msg/golang-nuts/ueSvaEKgyLY/ZW_74IC4PekJ
75755d5 Fix tests on Windows
d0eab2e Ignore email file watcher Chmod events
0b9798b Fix watcher on Ubuntu 12.04
3a8251a WaitForReplacement() to retry emails file watch
a57fd29 Add deferred File.Close() calls where needed
Because correctness: Don't leak file handles anywhere, and prepare for
future panics and early returns.
52ed3fd Log error and return if emails file doesn't parse
40100d4 Add gopkg.in/fsnotify.v1 dependency to Godeps file
17dfbbc Avoid a race when Remove is preceded by Chmod
2015-05-10 02:31:38 +03:00
|
|
|
type ValidatorTest struct {
|
|
|
|
auth_email_file *os.File
|
2015-05-14 00:30:19 +03:00
|
|
|
done chan bool
|
2015-05-14 01:30:22 +03:00
|
|
|
update_seen bool
|
Reload authenticated-emails-file upon update
This change extracts the UserMap class from NewValidator() so that its
LoadAuthenticatedEmailsFile() method can be called concurrently. This method
is called by a goroutine containing a fsnotify.Watcher watching the
authenticated emails file.
Watching isn't forever aborted when the authenticated emails file disappears.
The goroutine will call os.Stat() up to twenty times a second if the file is
persistently missing, but that's the pathological case, not the common one.
The common case is that some editors (including Vim) will perform a
rename-and-replace when updating a file, triggering fsnotify.Rename events,
and the file will temporarily disappear. This watcher goroutine handles that
case.
Also, on some platforms (notably Arch Linux), a remove will be preceded by a
fsnotify.Chmod, causing a race between the upcoming fsnotify.Remove and the
call to UserMap.LoadAuthenticatedEmailsFile(). Hence, we treat fsnotify.Chmod
the same as fsnotify.Remove and fsnotify.Rename. There's no significant
penalty to re-adding a file to the watcher.
Also contains the following small changes from the summary of commits below:
- Minor optimization of email domain search
- Fixed api_test.go on Windows
- Add deferred File.Close() calls where needed
- Log error and return if emails file doesn't parse
These are the original commits from #89 squashed into this one:
0c6f2b6 Refactor validator_test to prepare for more tests
e0c792b Add more test cases to validator_test
a9a9d93 Minor optimization of email domain search
b763ea5 Extract LoadAuthenticatedEmailsFile()
8cdaf7f Introduce synchronized UserMap type
1b84eef Add UserMap methods, locking
af15dcf Reload authenticated-emails-file upon update
6d95548 Make UserMap operations lock-free
Per:
- http://stackoverflow.com/questions/21447463/is-assigning-a-pointer-atomic-in-golang
- https://groups.google.com/forum/#!msg/golang-nuts/ueSvaEKgyLY/ZW_74IC4PekJ
75755d5 Fix tests on Windows
d0eab2e Ignore email file watcher Chmod events
0b9798b Fix watcher on Ubuntu 12.04
3a8251a WaitForReplacement() to retry emails file watch
a57fd29 Add deferred File.Close() calls where needed
Because correctness: Don't leak file handles anywhere, and prepare for
future panics and early returns.
52ed3fd Log error and return if emails file doesn't parse
40100d4 Add gopkg.in/fsnotify.v1 dependency to Godeps file
17dfbbc Avoid a race when Remove is preceded by Chmod
2015-05-10 02:31:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewValidatorTest(t *testing.T) *ValidatorTest {
|
|
|
|
vt := &ValidatorTest{}
|
|
|
|
var err error
|
|
|
|
vt.auth_email_file, err = ioutil.TempFile("", "test_auth_emails_")
|
2015-04-08 00:25:46 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("failed to create temp file: " + err.Error())
|
|
|
|
}
|
2017-10-31 16:12:15 +03:00
|
|
|
vt.done = make(chan bool, 1)
|
Reload authenticated-emails-file upon update
This change extracts the UserMap class from NewValidator() so that its
LoadAuthenticatedEmailsFile() method can be called concurrently. This method
is called by a goroutine containing a fsnotify.Watcher watching the
authenticated emails file.
Watching isn't forever aborted when the authenticated emails file disappears.
The goroutine will call os.Stat() up to twenty times a second if the file is
persistently missing, but that's the pathological case, not the common one.
The common case is that some editors (including Vim) will perform a
rename-and-replace when updating a file, triggering fsnotify.Rename events,
and the file will temporarily disappear. This watcher goroutine handles that
case.
Also, on some platforms (notably Arch Linux), a remove will be preceded by a
fsnotify.Chmod, causing a race between the upcoming fsnotify.Remove and the
call to UserMap.LoadAuthenticatedEmailsFile(). Hence, we treat fsnotify.Chmod
the same as fsnotify.Remove and fsnotify.Rename. There's no significant
penalty to re-adding a file to the watcher.
Also contains the following small changes from the summary of commits below:
- Minor optimization of email domain search
- Fixed api_test.go on Windows
- Add deferred File.Close() calls where needed
- Log error and return if emails file doesn't parse
These are the original commits from #89 squashed into this one:
0c6f2b6 Refactor validator_test to prepare for more tests
e0c792b Add more test cases to validator_test
a9a9d93 Minor optimization of email domain search
b763ea5 Extract LoadAuthenticatedEmailsFile()
8cdaf7f Introduce synchronized UserMap type
1b84eef Add UserMap methods, locking
af15dcf Reload authenticated-emails-file upon update
6d95548 Make UserMap operations lock-free
Per:
- http://stackoverflow.com/questions/21447463/is-assigning-a-pointer-atomic-in-golang
- https://groups.google.com/forum/#!msg/golang-nuts/ueSvaEKgyLY/ZW_74IC4PekJ
75755d5 Fix tests on Windows
d0eab2e Ignore email file watcher Chmod events
0b9798b Fix watcher on Ubuntu 12.04
3a8251a WaitForReplacement() to retry emails file watch
a57fd29 Add deferred File.Close() calls where needed
Because correctness: Don't leak file handles anywhere, and prepare for
future panics and early returns.
52ed3fd Log error and return if emails file doesn't parse
40100d4 Add gopkg.in/fsnotify.v1 dependency to Godeps file
17dfbbc Avoid a race when Remove is preceded by Chmod
2015-05-10 02:31:38 +03:00
|
|
|
return vt
|
|
|
|
}
|
2015-04-08 00:25:46 +03:00
|
|
|
|
Reload authenticated-emails-file upon update
This change extracts the UserMap class from NewValidator() so that its
LoadAuthenticatedEmailsFile() method can be called concurrently. This method
is called by a goroutine containing a fsnotify.Watcher watching the
authenticated emails file.
Watching isn't forever aborted when the authenticated emails file disappears.
The goroutine will call os.Stat() up to twenty times a second if the file is
persistently missing, but that's the pathological case, not the common one.
The common case is that some editors (including Vim) will perform a
rename-and-replace when updating a file, triggering fsnotify.Rename events,
and the file will temporarily disappear. This watcher goroutine handles that
case.
Also, on some platforms (notably Arch Linux), a remove will be preceded by a
fsnotify.Chmod, causing a race between the upcoming fsnotify.Remove and the
call to UserMap.LoadAuthenticatedEmailsFile(). Hence, we treat fsnotify.Chmod
the same as fsnotify.Remove and fsnotify.Rename. There's no significant
penalty to re-adding a file to the watcher.
Also contains the following small changes from the summary of commits below:
- Minor optimization of email domain search
- Fixed api_test.go on Windows
- Add deferred File.Close() calls where needed
- Log error and return if emails file doesn't parse
These are the original commits from #89 squashed into this one:
0c6f2b6 Refactor validator_test to prepare for more tests
e0c792b Add more test cases to validator_test
a9a9d93 Minor optimization of email domain search
b763ea5 Extract LoadAuthenticatedEmailsFile()
8cdaf7f Introduce synchronized UserMap type
1b84eef Add UserMap methods, locking
af15dcf Reload authenticated-emails-file upon update
6d95548 Make UserMap operations lock-free
Per:
- http://stackoverflow.com/questions/21447463/is-assigning-a-pointer-atomic-in-golang
- https://groups.google.com/forum/#!msg/golang-nuts/ueSvaEKgyLY/ZW_74IC4PekJ
75755d5 Fix tests on Windows
d0eab2e Ignore email file watcher Chmod events
0b9798b Fix watcher on Ubuntu 12.04
3a8251a WaitForReplacement() to retry emails file watch
a57fd29 Add deferred File.Close() calls where needed
Because correctness: Don't leak file handles anywhere, and prepare for
future panics and early returns.
52ed3fd Log error and return if emails file doesn't parse
40100d4 Add gopkg.in/fsnotify.v1 dependency to Godeps file
17dfbbc Avoid a race when Remove is preceded by Chmod
2015-05-10 02:31:38 +03:00
|
|
|
func (vt *ValidatorTest) TearDown() {
|
2015-05-14 00:30:19 +03:00
|
|
|
vt.done <- true
|
Reload authenticated-emails-file upon update
This change extracts the UserMap class from NewValidator() so that its
LoadAuthenticatedEmailsFile() method can be called concurrently. This method
is called by a goroutine containing a fsnotify.Watcher watching the
authenticated emails file.
Watching isn't forever aborted when the authenticated emails file disappears.
The goroutine will call os.Stat() up to twenty times a second if the file is
persistently missing, but that's the pathological case, not the common one.
The common case is that some editors (including Vim) will perform a
rename-and-replace when updating a file, triggering fsnotify.Rename events,
and the file will temporarily disappear. This watcher goroutine handles that
case.
Also, on some platforms (notably Arch Linux), a remove will be preceded by a
fsnotify.Chmod, causing a race between the upcoming fsnotify.Remove and the
call to UserMap.LoadAuthenticatedEmailsFile(). Hence, we treat fsnotify.Chmod
the same as fsnotify.Remove and fsnotify.Rename. There's no significant
penalty to re-adding a file to the watcher.
Also contains the following small changes from the summary of commits below:
- Minor optimization of email domain search
- Fixed api_test.go on Windows
- Add deferred File.Close() calls where needed
- Log error and return if emails file doesn't parse
These are the original commits from #89 squashed into this one:
0c6f2b6 Refactor validator_test to prepare for more tests
e0c792b Add more test cases to validator_test
a9a9d93 Minor optimization of email domain search
b763ea5 Extract LoadAuthenticatedEmailsFile()
8cdaf7f Introduce synchronized UserMap type
1b84eef Add UserMap methods, locking
af15dcf Reload authenticated-emails-file upon update
6d95548 Make UserMap operations lock-free
Per:
- http://stackoverflow.com/questions/21447463/is-assigning-a-pointer-atomic-in-golang
- https://groups.google.com/forum/#!msg/golang-nuts/ueSvaEKgyLY/ZW_74IC4PekJ
75755d5 Fix tests on Windows
d0eab2e Ignore email file watcher Chmod events
0b9798b Fix watcher on Ubuntu 12.04
3a8251a WaitForReplacement() to retry emails file watch
a57fd29 Add deferred File.Close() calls where needed
Because correctness: Don't leak file handles anywhere, and prepare for
future panics and early returns.
52ed3fd Log error and return if emails file doesn't parse
40100d4 Add gopkg.in/fsnotify.v1 dependency to Godeps file
17dfbbc Avoid a race when Remove is preceded by Chmod
2015-05-10 02:31:38 +03:00
|
|
|
os.Remove(vt.auth_email_file.Name())
|
|
|
|
}
|
|
|
|
|
2015-05-14 00:30:19 +03:00
|
|
|
func (vt *ValidatorTest) NewValidator(domains []string,
|
|
|
|
updated chan<- bool) func(string) bool {
|
|
|
|
return newValidatorImpl(domains, vt.auth_email_file.Name(),
|
2015-05-14 01:30:22 +03:00
|
|
|
vt.done, func() {
|
|
|
|
if vt.update_seen == false {
|
|
|
|
updated <- true
|
|
|
|
vt.update_seen = true
|
|
|
|
}
|
|
|
|
})
|
2015-05-14 00:30:19 +03:00
|
|
|
}
|
|
|
|
|
Reload authenticated-emails-file upon update
This change extracts the UserMap class from NewValidator() so that its
LoadAuthenticatedEmailsFile() method can be called concurrently. This method
is called by a goroutine containing a fsnotify.Watcher watching the
authenticated emails file.
Watching isn't forever aborted when the authenticated emails file disappears.
The goroutine will call os.Stat() up to twenty times a second if the file is
persistently missing, but that's the pathological case, not the common one.
The common case is that some editors (including Vim) will perform a
rename-and-replace when updating a file, triggering fsnotify.Rename events,
and the file will temporarily disappear. This watcher goroutine handles that
case.
Also, on some platforms (notably Arch Linux), a remove will be preceded by a
fsnotify.Chmod, causing a race between the upcoming fsnotify.Remove and the
call to UserMap.LoadAuthenticatedEmailsFile(). Hence, we treat fsnotify.Chmod
the same as fsnotify.Remove and fsnotify.Rename. There's no significant
penalty to re-adding a file to the watcher.
Also contains the following small changes from the summary of commits below:
- Minor optimization of email domain search
- Fixed api_test.go on Windows
- Add deferred File.Close() calls where needed
- Log error and return if emails file doesn't parse
These are the original commits from #89 squashed into this one:
0c6f2b6 Refactor validator_test to prepare for more tests
e0c792b Add more test cases to validator_test
a9a9d93 Minor optimization of email domain search
b763ea5 Extract LoadAuthenticatedEmailsFile()
8cdaf7f Introduce synchronized UserMap type
1b84eef Add UserMap methods, locking
af15dcf Reload authenticated-emails-file upon update
6d95548 Make UserMap operations lock-free
Per:
- http://stackoverflow.com/questions/21447463/is-assigning-a-pointer-atomic-in-golang
- https://groups.google.com/forum/#!msg/golang-nuts/ueSvaEKgyLY/ZW_74IC4PekJ
75755d5 Fix tests on Windows
d0eab2e Ignore email file watcher Chmod events
0b9798b Fix watcher on Ubuntu 12.04
3a8251a WaitForReplacement() to retry emails file watch
a57fd29 Add deferred File.Close() calls where needed
Because correctness: Don't leak file handles anywhere, and prepare for
future panics and early returns.
52ed3fd Log error and return if emails file doesn't parse
40100d4 Add gopkg.in/fsnotify.v1 dependency to Godeps file
17dfbbc Avoid a race when Remove is preceded by Chmod
2015-05-10 02:31:38 +03:00
|
|
|
// This will close vt.auth_email_file.
|
|
|
|
func (vt *ValidatorTest) WriteEmails(t *testing.T, emails []string) {
|
|
|
|
defer vt.auth_email_file.Close()
|
|
|
|
vt.auth_email_file.WriteString(strings.Join(emails, "\n"))
|
|
|
|
if err := vt.auth_email_file.Close(); err != nil {
|
|
|
|
t.Fatal("failed to close temp file " +
|
|
|
|
vt.auth_email_file.Name() + ": " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidatorEmpty(t *testing.T) {
|
|
|
|
vt := NewValidatorTest(t)
|
|
|
|
defer vt.TearDown()
|
|
|
|
|
|
|
|
vt.WriteEmails(t, []string(nil))
|
|
|
|
domains := []string(nil)
|
2015-05-14 00:30:19 +03:00
|
|
|
validator := vt.NewValidator(domains, nil)
|
Reload authenticated-emails-file upon update
This change extracts the UserMap class from NewValidator() so that its
LoadAuthenticatedEmailsFile() method can be called concurrently. This method
is called by a goroutine containing a fsnotify.Watcher watching the
authenticated emails file.
Watching isn't forever aborted when the authenticated emails file disappears.
The goroutine will call os.Stat() up to twenty times a second if the file is
persistently missing, but that's the pathological case, not the common one.
The common case is that some editors (including Vim) will perform a
rename-and-replace when updating a file, triggering fsnotify.Rename events,
and the file will temporarily disappear. This watcher goroutine handles that
case.
Also, on some platforms (notably Arch Linux), a remove will be preceded by a
fsnotify.Chmod, causing a race between the upcoming fsnotify.Remove and the
call to UserMap.LoadAuthenticatedEmailsFile(). Hence, we treat fsnotify.Chmod
the same as fsnotify.Remove and fsnotify.Rename. There's no significant
penalty to re-adding a file to the watcher.
Also contains the following small changes from the summary of commits below:
- Minor optimization of email domain search
- Fixed api_test.go on Windows
- Add deferred File.Close() calls where needed
- Log error and return if emails file doesn't parse
These are the original commits from #89 squashed into this one:
0c6f2b6 Refactor validator_test to prepare for more tests
e0c792b Add more test cases to validator_test
a9a9d93 Minor optimization of email domain search
b763ea5 Extract LoadAuthenticatedEmailsFile()
8cdaf7f Introduce synchronized UserMap type
1b84eef Add UserMap methods, locking
af15dcf Reload authenticated-emails-file upon update
6d95548 Make UserMap operations lock-free
Per:
- http://stackoverflow.com/questions/21447463/is-assigning-a-pointer-atomic-in-golang
- https://groups.google.com/forum/#!msg/golang-nuts/ueSvaEKgyLY/ZW_74IC4PekJ
75755d5 Fix tests on Windows
d0eab2e Ignore email file watcher Chmod events
0b9798b Fix watcher on Ubuntu 12.04
3a8251a WaitForReplacement() to retry emails file watch
a57fd29 Add deferred File.Close() calls where needed
Because correctness: Don't leak file handles anywhere, and prepare for
future panics and early returns.
52ed3fd Log error and return if emails file doesn't parse
40100d4 Add gopkg.in/fsnotify.v1 dependency to Godeps file
17dfbbc Avoid a race when Remove is preceded by Chmod
2015-05-10 02:31:38 +03:00
|
|
|
|
|
|
|
if validator("foo.bar@example.com") {
|
|
|
|
t.Error("nothing should validate when the email and " +
|
|
|
|
"domain lists are empty")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidatorSingleEmail(t *testing.T) {
|
|
|
|
vt := NewValidatorTest(t)
|
|
|
|
defer vt.TearDown()
|
|
|
|
|
|
|
|
vt.WriteEmails(t, []string{"foo.bar@example.com"})
|
|
|
|
domains := []string(nil)
|
2015-05-14 00:30:19 +03:00
|
|
|
validator := vt.NewValidator(domains, nil)
|
Reload authenticated-emails-file upon update
This change extracts the UserMap class from NewValidator() so that its
LoadAuthenticatedEmailsFile() method can be called concurrently. This method
is called by a goroutine containing a fsnotify.Watcher watching the
authenticated emails file.
Watching isn't forever aborted when the authenticated emails file disappears.
The goroutine will call os.Stat() up to twenty times a second if the file is
persistently missing, but that's the pathological case, not the common one.
The common case is that some editors (including Vim) will perform a
rename-and-replace when updating a file, triggering fsnotify.Rename events,
and the file will temporarily disappear. This watcher goroutine handles that
case.
Also, on some platforms (notably Arch Linux), a remove will be preceded by a
fsnotify.Chmod, causing a race between the upcoming fsnotify.Remove and the
call to UserMap.LoadAuthenticatedEmailsFile(). Hence, we treat fsnotify.Chmod
the same as fsnotify.Remove and fsnotify.Rename. There's no significant
penalty to re-adding a file to the watcher.
Also contains the following small changes from the summary of commits below:
- Minor optimization of email domain search
- Fixed api_test.go on Windows
- Add deferred File.Close() calls where needed
- Log error and return if emails file doesn't parse
These are the original commits from #89 squashed into this one:
0c6f2b6 Refactor validator_test to prepare for more tests
e0c792b Add more test cases to validator_test
a9a9d93 Minor optimization of email domain search
b763ea5 Extract LoadAuthenticatedEmailsFile()
8cdaf7f Introduce synchronized UserMap type
1b84eef Add UserMap methods, locking
af15dcf Reload authenticated-emails-file upon update
6d95548 Make UserMap operations lock-free
Per:
- http://stackoverflow.com/questions/21447463/is-assigning-a-pointer-atomic-in-golang
- https://groups.google.com/forum/#!msg/golang-nuts/ueSvaEKgyLY/ZW_74IC4PekJ
75755d5 Fix tests on Windows
d0eab2e Ignore email file watcher Chmod events
0b9798b Fix watcher on Ubuntu 12.04
3a8251a WaitForReplacement() to retry emails file watch
a57fd29 Add deferred File.Close() calls where needed
Because correctness: Don't leak file handles anywhere, and prepare for
future panics and early returns.
52ed3fd Log error and return if emails file doesn't parse
40100d4 Add gopkg.in/fsnotify.v1 dependency to Godeps file
17dfbbc Avoid a race when Remove is preceded by Chmod
2015-05-10 02:31:38 +03:00
|
|
|
|
|
|
|
if !validator("foo.bar@example.com") {
|
|
|
|
t.Error("email should validate")
|
|
|
|
}
|
|
|
|
if validator("baz.quux@example.com") {
|
|
|
|
t.Error("email from same domain but not in list " +
|
|
|
|
"should not validate when domain list is empty")
|
2015-04-08 00:25:46 +03:00
|
|
|
}
|
Reload authenticated-emails-file upon update
This change extracts the UserMap class from NewValidator() so that its
LoadAuthenticatedEmailsFile() method can be called concurrently. This method
is called by a goroutine containing a fsnotify.Watcher watching the
authenticated emails file.
Watching isn't forever aborted when the authenticated emails file disappears.
The goroutine will call os.Stat() up to twenty times a second if the file is
persistently missing, but that's the pathological case, not the common one.
The common case is that some editors (including Vim) will perform a
rename-and-replace when updating a file, triggering fsnotify.Rename events,
and the file will temporarily disappear. This watcher goroutine handles that
case.
Also, on some platforms (notably Arch Linux), a remove will be preceded by a
fsnotify.Chmod, causing a race between the upcoming fsnotify.Remove and the
call to UserMap.LoadAuthenticatedEmailsFile(). Hence, we treat fsnotify.Chmod
the same as fsnotify.Remove and fsnotify.Rename. There's no significant
penalty to re-adding a file to the watcher.
Also contains the following small changes from the summary of commits below:
- Minor optimization of email domain search
- Fixed api_test.go on Windows
- Add deferred File.Close() calls where needed
- Log error and return if emails file doesn't parse
These are the original commits from #89 squashed into this one:
0c6f2b6 Refactor validator_test to prepare for more tests
e0c792b Add more test cases to validator_test
a9a9d93 Minor optimization of email domain search
b763ea5 Extract LoadAuthenticatedEmailsFile()
8cdaf7f Introduce synchronized UserMap type
1b84eef Add UserMap methods, locking
af15dcf Reload authenticated-emails-file upon update
6d95548 Make UserMap operations lock-free
Per:
- http://stackoverflow.com/questions/21447463/is-assigning-a-pointer-atomic-in-golang
- https://groups.google.com/forum/#!msg/golang-nuts/ueSvaEKgyLY/ZW_74IC4PekJ
75755d5 Fix tests on Windows
d0eab2e Ignore email file watcher Chmod events
0b9798b Fix watcher on Ubuntu 12.04
3a8251a WaitForReplacement() to retry emails file watch
a57fd29 Add deferred File.Close() calls where needed
Because correctness: Don't leak file handles anywhere, and prepare for
future panics and early returns.
52ed3fd Log error and return if emails file doesn't parse
40100d4 Add gopkg.in/fsnotify.v1 dependency to Godeps file
17dfbbc Avoid a race when Remove is preceded by Chmod
2015-05-10 02:31:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidatorSingleDomain(t *testing.T) {
|
|
|
|
vt := NewValidatorTest(t)
|
|
|
|
defer vt.TearDown()
|
|
|
|
|
|
|
|
vt.WriteEmails(t, []string(nil))
|
|
|
|
domains := []string{"example.com"}
|
2015-05-14 00:30:19 +03:00
|
|
|
validator := vt.NewValidator(domains, nil)
|
Reload authenticated-emails-file upon update
This change extracts the UserMap class from NewValidator() so that its
LoadAuthenticatedEmailsFile() method can be called concurrently. This method
is called by a goroutine containing a fsnotify.Watcher watching the
authenticated emails file.
Watching isn't forever aborted when the authenticated emails file disappears.
The goroutine will call os.Stat() up to twenty times a second if the file is
persistently missing, but that's the pathological case, not the common one.
The common case is that some editors (including Vim) will perform a
rename-and-replace when updating a file, triggering fsnotify.Rename events,
and the file will temporarily disappear. This watcher goroutine handles that
case.
Also, on some platforms (notably Arch Linux), a remove will be preceded by a
fsnotify.Chmod, causing a race between the upcoming fsnotify.Remove and the
call to UserMap.LoadAuthenticatedEmailsFile(). Hence, we treat fsnotify.Chmod
the same as fsnotify.Remove and fsnotify.Rename. There's no significant
penalty to re-adding a file to the watcher.
Also contains the following small changes from the summary of commits below:
- Minor optimization of email domain search
- Fixed api_test.go on Windows
- Add deferred File.Close() calls where needed
- Log error and return if emails file doesn't parse
These are the original commits from #89 squashed into this one:
0c6f2b6 Refactor validator_test to prepare for more tests
e0c792b Add more test cases to validator_test
a9a9d93 Minor optimization of email domain search
b763ea5 Extract LoadAuthenticatedEmailsFile()
8cdaf7f Introduce synchronized UserMap type
1b84eef Add UserMap methods, locking
af15dcf Reload authenticated-emails-file upon update
6d95548 Make UserMap operations lock-free
Per:
- http://stackoverflow.com/questions/21447463/is-assigning-a-pointer-atomic-in-golang
- https://groups.google.com/forum/#!msg/golang-nuts/ueSvaEKgyLY/ZW_74IC4PekJ
75755d5 Fix tests on Windows
d0eab2e Ignore email file watcher Chmod events
0b9798b Fix watcher on Ubuntu 12.04
3a8251a WaitForReplacement() to retry emails file watch
a57fd29 Add deferred File.Close() calls where needed
Because correctness: Don't leak file handles anywhere, and prepare for
future panics and early returns.
52ed3fd Log error and return if emails file doesn't parse
40100d4 Add gopkg.in/fsnotify.v1 dependency to Godeps file
17dfbbc Avoid a race when Remove is preceded by Chmod
2015-05-10 02:31:38 +03:00
|
|
|
|
|
|
|
if !validator("foo.bar@example.com") {
|
|
|
|
t.Error("email should validate")
|
|
|
|
}
|
|
|
|
if !validator("baz.quux@example.com") {
|
|
|
|
t.Error("email from same domain should validate")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidatorMultipleEmailsMultipleDomains(t *testing.T) {
|
|
|
|
vt := NewValidatorTest(t)
|
|
|
|
defer vt.TearDown()
|
|
|
|
|
|
|
|
vt.WriteEmails(t, []string{
|
|
|
|
"xyzzy@example.com",
|
|
|
|
"plugh@example.com",
|
|
|
|
})
|
|
|
|
domains := []string{"example0.com", "example1.com"}
|
2015-05-14 00:30:19 +03:00
|
|
|
validator := vt.NewValidator(domains, nil)
|
Reload authenticated-emails-file upon update
This change extracts the UserMap class from NewValidator() so that its
LoadAuthenticatedEmailsFile() method can be called concurrently. This method
is called by a goroutine containing a fsnotify.Watcher watching the
authenticated emails file.
Watching isn't forever aborted when the authenticated emails file disappears.
The goroutine will call os.Stat() up to twenty times a second if the file is
persistently missing, but that's the pathological case, not the common one.
The common case is that some editors (including Vim) will perform a
rename-and-replace when updating a file, triggering fsnotify.Rename events,
and the file will temporarily disappear. This watcher goroutine handles that
case.
Also, on some platforms (notably Arch Linux), a remove will be preceded by a
fsnotify.Chmod, causing a race between the upcoming fsnotify.Remove and the
call to UserMap.LoadAuthenticatedEmailsFile(). Hence, we treat fsnotify.Chmod
the same as fsnotify.Remove and fsnotify.Rename. There's no significant
penalty to re-adding a file to the watcher.
Also contains the following small changes from the summary of commits below:
- Minor optimization of email domain search
- Fixed api_test.go on Windows
- Add deferred File.Close() calls where needed
- Log error and return if emails file doesn't parse
These are the original commits from #89 squashed into this one:
0c6f2b6 Refactor validator_test to prepare for more tests
e0c792b Add more test cases to validator_test
a9a9d93 Minor optimization of email domain search
b763ea5 Extract LoadAuthenticatedEmailsFile()
8cdaf7f Introduce synchronized UserMap type
1b84eef Add UserMap methods, locking
af15dcf Reload authenticated-emails-file upon update
6d95548 Make UserMap operations lock-free
Per:
- http://stackoverflow.com/questions/21447463/is-assigning-a-pointer-atomic-in-golang
- https://groups.google.com/forum/#!msg/golang-nuts/ueSvaEKgyLY/ZW_74IC4PekJ
75755d5 Fix tests on Windows
d0eab2e Ignore email file watcher Chmod events
0b9798b Fix watcher on Ubuntu 12.04
3a8251a WaitForReplacement() to retry emails file watch
a57fd29 Add deferred File.Close() calls where needed
Because correctness: Don't leak file handles anywhere, and prepare for
future panics and early returns.
52ed3fd Log error and return if emails file doesn't parse
40100d4 Add gopkg.in/fsnotify.v1 dependency to Godeps file
17dfbbc Avoid a race when Remove is preceded by Chmod
2015-05-10 02:31:38 +03:00
|
|
|
|
|
|
|
if !validator("foo.bar@example0.com") {
|
|
|
|
t.Error("email from first domain should validate")
|
|
|
|
}
|
|
|
|
if !validator("baz.quux@example1.com") {
|
|
|
|
t.Error("email from second domain should validate")
|
|
|
|
}
|
|
|
|
if !validator("xyzzy@example.com") {
|
|
|
|
t.Error("first email in list should validate")
|
|
|
|
}
|
|
|
|
if !validator("plugh@example.com") {
|
|
|
|
t.Error("second email in list should validate")
|
|
|
|
}
|
|
|
|
if validator("xyzzy.plugh@example.com") {
|
|
|
|
t.Error("email not in list that matches no domains " +
|
|
|
|
"should not validate")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidatorComparisonsAreCaseInsensitive(t *testing.T) {
|
|
|
|
vt := NewValidatorTest(t)
|
|
|
|
defer vt.TearDown()
|
2015-04-08 00:25:46 +03:00
|
|
|
|
Reload authenticated-emails-file upon update
This change extracts the UserMap class from NewValidator() so that its
LoadAuthenticatedEmailsFile() method can be called concurrently. This method
is called by a goroutine containing a fsnotify.Watcher watching the
authenticated emails file.
Watching isn't forever aborted when the authenticated emails file disappears.
The goroutine will call os.Stat() up to twenty times a second if the file is
persistently missing, but that's the pathological case, not the common one.
The common case is that some editors (including Vim) will perform a
rename-and-replace when updating a file, triggering fsnotify.Rename events,
and the file will temporarily disappear. This watcher goroutine handles that
case.
Also, on some platforms (notably Arch Linux), a remove will be preceded by a
fsnotify.Chmod, causing a race between the upcoming fsnotify.Remove and the
call to UserMap.LoadAuthenticatedEmailsFile(). Hence, we treat fsnotify.Chmod
the same as fsnotify.Remove and fsnotify.Rename. There's no significant
penalty to re-adding a file to the watcher.
Also contains the following small changes from the summary of commits below:
- Minor optimization of email domain search
- Fixed api_test.go on Windows
- Add deferred File.Close() calls where needed
- Log error and return if emails file doesn't parse
These are the original commits from #89 squashed into this one:
0c6f2b6 Refactor validator_test to prepare for more tests
e0c792b Add more test cases to validator_test
a9a9d93 Minor optimization of email domain search
b763ea5 Extract LoadAuthenticatedEmailsFile()
8cdaf7f Introduce synchronized UserMap type
1b84eef Add UserMap methods, locking
af15dcf Reload authenticated-emails-file upon update
6d95548 Make UserMap operations lock-free
Per:
- http://stackoverflow.com/questions/21447463/is-assigning-a-pointer-atomic-in-golang
- https://groups.google.com/forum/#!msg/golang-nuts/ueSvaEKgyLY/ZW_74IC4PekJ
75755d5 Fix tests on Windows
d0eab2e Ignore email file watcher Chmod events
0b9798b Fix watcher on Ubuntu 12.04
3a8251a WaitForReplacement() to retry emails file watch
a57fd29 Add deferred File.Close() calls where needed
Because correctness: Don't leak file handles anywhere, and prepare for
future panics and early returns.
52ed3fd Log error and return if emails file doesn't parse
40100d4 Add gopkg.in/fsnotify.v1 dependency to Godeps file
17dfbbc Avoid a race when Remove is preceded by Chmod
2015-05-10 02:31:38 +03:00
|
|
|
vt.WriteEmails(t, []string{"Foo.Bar@Example.Com"})
|
2015-04-08 00:25:46 +03:00
|
|
|
domains := []string{"Frobozz.Com"}
|
2015-05-14 00:30:19 +03:00
|
|
|
validator := vt.NewValidator(domains, nil)
|
2015-04-08 00:25:46 +03:00
|
|
|
|
|
|
|
if !validator("foo.bar@example.com") {
|
|
|
|
t.Error("loaded email addresses are not lower-cased")
|
|
|
|
}
|
|
|
|
if !validator("Foo.Bar@Example.Com") {
|
|
|
|
t.Error("validated email addresses are not lower-cased")
|
|
|
|
}
|
|
|
|
if !validator("foo.bar@frobozz.com") {
|
|
|
|
t.Error("loaded domains are not lower-cased")
|
|
|
|
}
|
|
|
|
if !validator("foo.bar@Frobozz.Com") {
|
|
|
|
t.Error("validated domains are not lower-cased")
|
|
|
|
}
|
|
|
|
}
|
2015-12-23 11:08:51 +03:00
|
|
|
|
|
|
|
func TestValidatorIgnoreSpacesInAuthEmails(t *testing.T) {
|
|
|
|
vt := NewValidatorTest(t)
|
|
|
|
defer vt.TearDown()
|
|
|
|
|
|
|
|
vt.WriteEmails(t, []string{" foo.bar@example.com "})
|
|
|
|
domains := []string(nil)
|
|
|
|
validator := vt.NewValidator(domains, nil)
|
|
|
|
|
|
|
|
if !validator("foo.bar@example.com") {
|
|
|
|
t.Error("email should validate")
|
|
|
|
}
|
|
|
|
}
|