diff --git a/tools/horizon-cmp/getPathFromAccessLog_test.go b/tools/horizon-cmp/getPathFromAccessLog_test.go new file mode 100644 index 0000000000..1cbc36b57f --- /dev/null +++ b/tools/horizon-cmp/getPathFromAccessLog_test.go @@ -0,0 +1,32 @@ +package main + +import "testing" + +func TestGetPathFromAccessLog(t *testing.T) { + validLine := "127.0.0.1 - - [10/Oct/2000:13:55:36 -0700] \"GET /page.html HTTP/1.0\" 200 2326" + invalidLengthLine := "127.0.0.1 - - [10/Oct/2000:13:55:36 -0700] \"GET /page.html HTTP/1.0\" 200" + invalidPathLine := "127.0.0.1 - - [10/Oct/2000:13:55:36 -0700] \"GET //page.html HTTP/1.0\" 200 2326" + + expectedPath := "/page.html" + + // Test valid line + path, err := getPathFromAccessLog(validLine) + if err != nil { + t.Errorf("Expected no error, but got %v", err) + } + if path != expectedPath { + t.Errorf("Expected path %q, but got %q", expectedPath, path) + } + + // Test invalid length line + _, err = getPathFromAccessLog(invalidLengthLine) + if err == nil { + t.Error("Expected error, but got nil") + } + + // Test invalid path line + _, err = getPathFromAccessLog(invalidPathLine) + if err == nil { + t.Error("Expected error, but got nil") + } +} diff --git a/tools/horizon-cmp/main.go b/tools/horizon-cmp/main.go index 9b2e10c070..2dbbaa561d 100644 --- a/tools/horizon-cmp/main.go +++ b/tools/horizon-cmp/main.go @@ -294,10 +294,21 @@ func getPathFromAccessLog(line string) (string, error) { return "", errors.Errorf("Can't find match: %s", line) } + // Check for expected length + if len(matches[0]) != len(line) { + return "", errors.Errorf("Found invalid match length in line: %s", line) + } + if matches[1] != "GET" { return "", nil } + // Check for expected path value + newpath := matches[2] + if newpath == "" || strings.Contains(newpath, "//") { + return "", errors.Errorf("Found invalid match value in line: %s", line) + } + // Remove duplicate / path := "/" + strings.TrimLeft(matches[2], "/") return path, nil