From 9b3d7f632a79d9591e3e168cfbd6cbe4faafe98f Mon Sep 17 00:00:00 2001 From: Agnes-George1 Date: Mon, 24 Nov 2025 18:07:51 +0530 Subject: [PATCH] improve code coverage --- tree_test.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/tree_test.go b/tree_test.go index 846bf163..f2e64bad 100644 --- a/tree_test.go +++ b/tree_test.go @@ -94,6 +94,26 @@ func TestCountParams(t *testing.T) { if countParams(strings.Repeat("/:param", 256)) != 256 { t.Fail() } + // Test overflow protection - should cap at max uint16 (0xFFFF = 65535) + // Create a path with more than 65535 params (colons + stars) + // Need 65536+ colons to trigger the overflow check + overflowPath := strings.Repeat(":", 70000) // 70000 colons + if countParams(overflowPath) != 0xFFFF { + t.Errorf("countParams overflow protection failed: expected 0xFFFF, got %d", countParams(overflowPath)) + } +} + +func TestCountSections(t *testing.T) { + if countSections("/path/to/resource") != 3 { + t.Fail() + } + // Test overflow protection - should cap at max uint16 (0xFFFF = 65535) + // Create a path with more than 65535 slashes + // Need 65536+ slashes to trigger the overflow check + overflowPath := strings.Repeat("/", 70000) // 70000 slashes + if countSections(overflowPath) != 0xFFFF { + t.Errorf("countSections overflow protection failed: expected 0xFFFF, got %d", countSections(overflowPath)) + } } func TestTreeAddAndGet(t *testing.T) { @@ -497,3 +517,51 @@ func TestTreeChildConflict(t *testing.T) { } testRoutes(t, routes) } + +func TestWildcardConflictWithStringsCut(t *testing.T) { + // Test the strings.Cut usage in wildcard conflict detection (line 258 in tree.go) + tree := &node{} + + // Add a route with a wildcard parameter + tree.addRoute("/user/:name", fakeHandler("/user/:name")) + + // Try to add a conflicting route that will trigger the strings.Cut path + // This should panic with a wildcard conflict + recv := catchPanic(func() { + tree.addRoute("/user/:id/profile", fakeHandler("/user/:id/profile")) + }) + + if recv == nil { + t.Error("Expected panic for wildcard conflict, but got none") + } +} + +func TestCatchAllConflictWithStringsCut(t *testing.T) { + // Test the strings.Cut usage in catch-all conflict detection (line 382 in tree.go) + tree := &node{} + + // Add a route with a path segment + tree.addRoute("/files/list", fakeHandler("/files/list")) + + // Try to add a catch-all route that conflicts + // This should panic with a catch-all conflict + recv := catchPanic(func() { + tree.addRoute("/files/*filepath", fakeHandler("/files/*filepath")) + }) + + if recv == nil { + t.Error("Expected panic for catch-all conflict, but got none") + } + + // Also test with an empty children case to cover line 382 when len(n.children) == 0 + tree2 := &node{} + tree2.addRoute("/docs/", fakeHandler("/docs/")) + + recv2 := catchPanic(func() { + tree2.addRoute("/docs/*page", fakeHandler("/docs/*page")) + }) + + if recv2 == nil { + t.Error("Expected panic for catch-all conflict with empty children, but got none") + } +}