diff --git a/pkg/parser/parser.go b/pkg/parser/parser.go index 20b1857..9da9d4c 100644 --- a/pkg/parser/parser.go +++ b/pkg/parser/parser.go @@ -152,12 +152,13 @@ func parseRouteLine(str string) (*types.Route, bool) { } ip := net.ParseIP(p[i]) + hostnames := p[i+1:] - if ip == nil { + if ip == nil || len(hostnames) == 0 { return nil, false } - return &types.Route{IP: ip, HostNames: p[i+1:]}, true + return &types.Route{IP: ip, HostNames: hostnames}, true } // ParseProfile creates a new profile reading lines from a reader. diff --git a/pkg/parser/parser_test.go b/pkg/parser/parser_test.go index 078ccfb..bfe7967 100644 --- a/pkg/parser/parser_test.go +++ b/pkg/parser/parser_test.go @@ -44,6 +44,22 @@ func TestHostFile(t *testing.T) { }) } +func TestWrongHostFile(t *testing.T) { + testFile := ` +# This file should be parsed as comment +# 1.1.1.1 +` + + t.Run("Wrong Content", func(t *testing.T) { + f := strings.NewReader(testFile) + + data, err := Parse(f) + assert.NoError(t, err) + assert.Len(t, data.ProfileNames, 0) + assert.Equal(t, data.DefaultProfile[2].Comment, "# 1.1.1.1") + }) +} + func appendLine(p *types.Profile, line string) { if line == "" { return