Skip to content
Open
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
26 changes: 22 additions & 4 deletions internal/news/nntpd/range_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
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)
low, high, ok := parseRange("5")
if !ok || low != 5 || high != 5 {
t.Fatalf(`parseRange("5") = %d, %d, %v; want 5, 5, true`, low, high, ok)
}
}

func TestParseRangeOpenEnded(t *testing.T) {
low, high, ok := parseRange("5-")
if !ok || low != 5 || high != math.MaxInt64 {
t.Fatalf(`parseRange("5-") = %d, %d, %v; want 5, MaxInt64, true`, low, high, ok)
}
}

func TestParseRangeRejectsMalformed(t *testing.T) {
for _, spec := range []string{"abc", "1-abc", "-5", "10-5", "1-2-3"} {
if low, high, ok := parseRange(spec); ok {
t.Fatalf("parseRange(%q) = %d, %d, true; want invalid", spec, low, high)
}
}
}
35 changes: 25 additions & 10 deletions internal/news/nntpd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,25 +165,37 @@ func (s *Server) Process(nc net.Conn) {
}
}

func parseRange(spec string) (low, high int64) {
func parseRange(spec string) (low, high int64, ok bool) {
if spec == "" {
return 0, math.MaxInt64
return 0, math.MaxInt64, true
}
parts := strings.Split(spec, "-")
if len(parts) == 1 {
h, err := strconv.ParseInt(parts[0], 10, 64)
if err != nil {
h = math.MaxInt64
return 0, h
return 0, 0, false
}
return h, h
return h, h, true
}
l, _ := strconv.ParseInt(parts[0], 10, 64)
h, err := strconv.ParseInt(parts[1], 10, 64)
if len(parts) != 2 || parts[0] == "" {
return 0, 0, false
}
l, err := strconv.ParseInt(parts[0], 10, 64)
if err != nil {
h = math.MaxInt64
return 0, 0, false
}
var h int64 = math.MaxInt64
if parts[1] != "" {
parsed, err := strconv.ParseInt(parts[1], 10, 64)
if err != nil {
return 0, 0, false
}
h = parsed
}
return l, h
if h < l {
return 0, 0, false
}
return l, h, true
}

func handleOver(args []string, s *session, c *textproto.Conn) error {
Expand All @@ -194,7 +206,10 @@ func handleOver(args []string, s *session, c *textproto.Conn) error {
if len(args) > 0 {
spec = args[0]
}
from, to := parseRange(spec)
from, to, ok := parseRange(spec)
if !ok {
return ErrSyntax
}
articles, err := s.backend.GetArticles(s.group, from, to)
if err != nil {
return err
Expand Down