@@ -8,30 +8,34 @@ import (
88)
99
1010type ValidatorTest struct {
11− authEmailFile *os.File
12− done chan bool
13− updateSeen bool
11+ authEmailFileName string
12+ done chan bool
13+ updateSeen bool
1414}
1515
1616func NewValidatorTest(t *testing.T) *ValidatorTest {
1717 vt := &ValidatorTest{}
1818 var err error
19− vt.authEmailFile, err = ioutil.TempFile("", "test_auth_emails_")
19+ f, err := ioutil.TempFile("", "test_auth_emails_")
2020 if err != nil {
21− t.Fatal("failed to create temp file: " + err.Error())
21+ t.Fatalf("failed to create temp file: %v", err)
2222 }
23+ if err := f.Close(); err != nil {
24+ t.Fatalf("failed to close temp file: %v", err)
25+ }
26+ vt.authEmailFileName = f.Name()
2327 vt.done = make(chan bool, 1)
2428 return vt
2529}
2630
2731func (vt *ValidatorTest) TearDown() {
2832 vt.done <- true
29− os.Remove(vt.authEmailFile.Name())
33+ os.Remove(vt.authEmailFileName)
3034}
3135
3236func (vt *ValidatorTest) NewValidator(domains []string,
3337 updated chan<- bool) func(string) bool {
34− return newValidatorImpl(domains, vt.authEmailFile.Name(),
38+ return newValidatorImpl(domains, vt.authEmailFileName,
3539 vt.done, func() {
3640 if vt.updateSeen == false {
3741 updated <- true
@@ -40,13 +44,18 @@ func (vt *ValidatorTest) NewValidator(domains []string,
4044 })
4145}
4246
43−// This will close vt.authEmailFile.
4447func (vt *ValidatorTest) WriteEmails(t *testing.T, emails []string) {
45− defer vt.authEmailFile.Close()
46− vt.authEmailFile.WriteString(strings.Join(emails, "\n"))
47− if err := vt.authEmailFile.Close(); err != nil {
48− t.Fatal("failed to close temp file " +
49− vt.authEmailFile.Name() + ": " + err.Error())
48+ f, err := os.OpenFile(vt.authEmailFileName, os.O_WRONLY, 0600)
49+ if err != nil {
50+ t.Fatalf("failed to open auth email file: %v", err)
51+ }
52+
53+ if _, err := f.WriteString(strings.Join(emails, "\n")); err != nil {
54+ t.Fatalf("failed to write emails to auth email file: %v", err)
55+ }
56+
57+ if err := f.Close(); err != nil {
58+ t.Fatalf("failed to close auth email file: %v", err)
5059 }
5160}
5261
@@ -160,3 +169,43 @@ func TestValidatorIgnoreSpacesInAuthEmails(t *testing.T) {
160169 t.Error("email should validate")
161170 }
162171}
172+
173+func TestValidatorOverwriteEmailListDirectly(t *testing.T) {
174+ vt := NewValidatorTest(t)
175+ defer vt.TearDown()
176+
177+ vt.WriteEmails(t, []string{
178+ "xyzzy@example.com",
179+ "plugh@example.com",
180+ })
181+ domains := []string(nil)
182+ updated := make(chan bool)
183+ validator := vt.NewValidator(domains, updated)
184+
185+ if !validator("xyzzy@example.com") {
186+ t.Error("first email in list should validate")
187+ }
188+ if !validator("plugh@example.com") {
189+ t.Error("second email in list should validate")
190+ }
191+ if validator("xyzzy.plugh@example.com") {
192+ t.Error("email not in list that matches no domains " +
193+ "should not validate")
194+ }
195+
196+ vt.WriteEmails(t, []string{
197+ "xyzzy.plugh@example.com",
198+ "plugh@example.com",
199+ })
200+ <-updated
201+
202+ if validator("xyzzy@example.com") {
203+ t.Error("email removed from list should not validate")
204+ }
205+ if !validator("plugh@example.com") {
206+ t.Error("email retained in list should validate")
207+ }
208+ if !validator("xyzzy.plugh@example.com") {
209+ t.Error("email added to list should validate")
210+ }
211+}