Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion internal/news/nntpd/range_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,48 @@
package nntpd

import "testing"
import (
"math"
"testing"
)

func TestParseRangeSingleArticle(t *testing.T) {
low, high := parseRange("5")
if low != 5 || high != 5 {
t.Fatalf(`parseRange("5") = %d, %d; want 5, 5`, low, high)
}
}

func TestParseRangeClosed(t *testing.T) {
low, high := parseRange("5-10")
if low != 5 || high != 10 {
t.Fatalf(`parseRange("5-10") = %d, %d; want 5, 10`, low, high)
}
}

// "low-" is a valid open-ended range meaning "from low to the highest
// article" (RFC 3977). It must not be treated as a malformed empty range,
// otherwise OVER/XOVER "n-" returns nothing instead of all articles >= n.
func TestParseRangeOpenEnded(t *testing.T) {
low, high := parseRange("5-")
if low != 5 || high != math.MaxInt64 {
t.Fatalf(`parseRange("5-") = %d, %d; want 5, %d`, low, high, int64(math.MaxInt64))
}
}

// Empty spec means "all articles".
func TestParseRangeEmptyIsAll(t *testing.T) {
low, high := parseRange("")
if low != 0 || high != math.MaxInt64 {
t.Fatalf(`parseRange("") = %d, %d; want 0, %d`, low, high, int64(math.MaxInt64))
}
}

// Malformed specs must yield an empty range (0,0), never "all articles".
func TestParseRangeMalformed(t *testing.T) {
for _, spec := range []string{"abc", "5-abc", "abc-5", "1-2-3", "-", "-5"} {
low, high := parseRange(spec)
if low != 0 || high != 0 {
t.Fatalf(`parseRange(%q) = %d, %d; want 0, 0`, spec, low, high)
}
}
}
9 changes: 9 additions & 0 deletions internal/news/nntpd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,19 @@ func parseRange(spec string) (low, high int64) {
}
return h, h
}
if len(parts) != 2 {
return 0, 0 // malformed (e.g. "1-2-3") — empty range
}
l, err := strconv.ParseInt(parts[0], 10, 64)
if err != nil {
return 0, 0 // malformed — empty range
}
// Open-ended range "low-" means "from low to the highest article"
// (RFC 3977: a range is a single number, "number-", or
// "number-number"). An empty upper bound is unbounded, not malformed.
if parts[1] == "" {
return l, math.MaxInt64
}
h, err := strconv.ParseInt(parts[1], 10, 64)
if err != nil {
return 0, 0 // malformed — empty range
Expand Down