variable shadowing of token (#503)

* Added test for $INCLUDE statement parser in zone files

* FIX: localized l to switch statement, shadowed later call to os.Open(l.token)
This commit is contained in:
Tim Esselens 2017-08-09 00:19:10 +02:00 committed by Miek Gieben
parent 0f3adef2e2
commit bbca4873b3
2 changed files with 46 additions and 2 deletions

View File

@ -278,8 +278,7 @@ func parseZone(r io.Reader, origin, f string, t chan *Token, include int) {
return
}
neworigin := origin // There may be optionally a new origin set after the filename, if not use current one
l := <-c
switch l.value {
switch l := <-c; l.value {
case zBlank:
l := <-c
if l.value == zString {

45
scan_test.go Normal file
View File

@ -0,0 +1,45 @@
package dns
import (
"io/ioutil"
"os"
"strings"
"testing"
)
func TestParseZoneInclude(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "dns")
if err != nil {
t.Fatalf("could not create tmpfile for test: %s", err)
}
if _, err := tmpfile.WriteString("foo\tIN\tA\t127.0.0.1"); err != nil {
t.Fatalf("unable to write content to tmpfile %q: %s", tmpfile.Name(), err)
}
if err := tmpfile.Close(); err != nil {
t.Fatalf("could not close tmpfile %q: %s", tmpfile.Name(), err)
}
zone := "$INCLUDE " + tmpfile.Name()
tok := ParseZone(strings.NewReader(zone), "", "")
for x := range tok {
if x.Error != nil {
t.Fatalf("expected no error, but got %s", x.Error)
}
}
os.Remove(tmpfile.Name())
tok = ParseZone(strings.NewReader(zone), "", "")
for x := range tok {
if x.Error == nil {
t.Fatalf("expected first token to contain an error but it didn't")
}
if !strings.Contains(x.Error.Error(), "failed to open") ||
!strings.Contains(x.Error.Error(), tmpfile.Name()) {
t.Fatalf(`expected error to contain: "failed to open" and %q but got: %s`, tmpfile.Name(), x.Error)
}
}
}