diff --git a/handlers/hosts/handlers/analyzer/analyzer.go b/handlers/hosts/handlers/analyzer/analyzer.go new file mode 100644 index 0000000..220b283 --- /dev/null +++ b/handlers/hosts/handlers/analyzer/analyzer.go @@ -0,0 +1 @@ +package analyzer diff --git a/handlers/hosts/handlers/analyzer/double_ips.go b/handlers/hosts/handlers/analyzer/double_ips.go new file mode 100644 index 0000000..76561eb --- /dev/null +++ b/handlers/hosts/handlers/analyzer/double_ips.go @@ -0,0 +1,36 @@ +package analyzer + +import ( + "config-lsp/common" + "config-lsp/handlers/hosts/tree" + "net" +) + +func ipToString(ip net.IPAddr) string { + return ip.IP.String() +} + +func analyzeDoubleIPs(p tree.HostsParser) []common.LSPError { + errors := make([]common.LSPError, 0) + + ips := make(map[string]uint32) + + for lineNumber, entry := range p.Tree.Entries { + if entry.IPAddress != nil { + key := ipToString(entry.IPAddress.Value) + + if foundLine, found := ips[key]; found { + errors = append(errors, common.LSPError{ + Range: entry.IPAddress.Location, + Err: DuplicateIPDeclaration{ + AlreadyFoundAt: foundLine, + }, + }) + } else { + ips[key] = lineNumber + } + } + } + + return errors +} diff --git a/handlers/hosts/handlers/analyzer/double_ips_test.go b/handlers/hosts/handlers/analyzer/double_ips_test.go new file mode 100644 index 0000000..be09fd5 --- /dev/null +++ b/handlers/hosts/handlers/analyzer/double_ips_test.go @@ -0,0 +1,62 @@ +package analyzer + +import ( + "config-lsp/handlers/hosts/tree" + "config-lsp/utils" + "testing" +) + +func TestWorksWithNonDoubleIPs( + t *testing.T, +) { + input := utils.Dedent(` +1.2.3.4 hello.com +5.5.5.5 world.com +1.2.3.5 foo.com +1.2.3.6 bar.com +`) + + parser := tree.CreateNewHostsParser() + errors := parser.Parse(input) + + if len(errors) != 0 { + t.Fatalf("PARER FAILED! Expected no errors, but got %v", errors) + } + + errors = analyzeDoubleIPs(parser) + + if len(errors) != 0 { + t.Errorf("Expected no errors, but got %v", errors) + } +} + +func TestWorksWithDoubleIPs( + t *testing.T, +) { + input := utils.Dedent(` +1.2.3.4 hello.com +5.5.5.5 world.com +1.2.3.4 foo.com +`) + + parser := tree.CreateNewHostsParser() + errors := parser.Parse(input) + + if len(errors) != 0 { + t.Fatalf("PARER FAILED! Expected no errors, but got %v", errors) + } + + errors = analyzeDoubleIPs(parser) + + if !(len(errors) == 1) { + t.Errorf("Expected 1 error, but got %v", len(errors)) + } + + if !(errors[0].Range.Start.Line == 3) { + t.Errorf("Expected error on line 3, but got %v", errors[0].Range.Start.Line) + } + + if !(errors[0].Err.(DuplicateIPDeclaration).AlreadyFoundAt == 1) { + t.Errorf("Expected error on line 1, but got %v", errors[0].Err.(DuplicateIPDeclaration).AlreadyFoundAt) + } +} diff --git a/handlers/hosts/handlers/errors.go b/handlers/hosts/handlers/analyzer/errors.go similarity index 51% rename from handlers/hosts/handlers/errors.go rename to handlers/hosts/handlers/analyzer/errors.go index af08ee3..48909bc 100644 --- a/handlers/hosts/handlers/errors.go +++ b/handlers/hosts/handlers/analyzer/errors.go @@ -1,4 +1,4 @@ -package handlers +package analyzer import "fmt" @@ -10,3 +10,11 @@ type DuplicateHostEntry struct { func (d DuplicateHostEntry) Error() string { return fmt.Sprintf("'%s' already defined on line %d", d.Hostname, d.AlreadyFoundAt) } + +type DuplicateIPDeclaration struct { + AlreadyFoundAt uint32 +} + +func (d DuplicateIPDeclaration) Error() string { + return fmt.Sprintf("This IP address is already defined on line %d", d.AlreadyFoundAt) +} diff --git a/handlers/hosts/handlers/analyzer.go b/handlers/hosts/handlers/analyzer/resolver.go similarity index 99% rename from handlers/hosts/handlers/analyzer.go rename to handlers/hosts/handlers/analyzer/resolver.go index a14c35f..d0c4a23 100644 --- a/handlers/hosts/handlers/analyzer.go +++ b/handlers/hosts/handlers/analyzer/resolver.go @@ -1,4 +1,4 @@ -package handlers +package analyzer import ( "config-lsp/common" diff --git a/handlers/hosts/handlers/analyzer_test.go b/handlers/hosts/handlers/analyzer/resolver_test.go similarity index 99% rename from handlers/hosts/handlers/analyzer_test.go rename to handlers/hosts/handlers/analyzer/resolver_test.go index 9f55a94..b24c4b8 100644 --- a/handlers/hosts/handlers/analyzer_test.go +++ b/handlers/hosts/handlers/analyzer/resolver_test.go @@ -1,4 +1,4 @@ -package handlers +package analyzer import ( "config-lsp/handlers/hosts/tree"