commit 57fae4fd068fd5a29498f1c3e81c94772b6d48fa
parent d10472b3356f92c5d640ed1a09f243dd6721a0a7
Author: Steve Wills <steve@mouf.net>
Date: Tue, 16 Jul 2024 00:03:37 -0400
sdp: test invalid durations
Diffstat:
2 files changed, 56 insertions(+), 9 deletions(-)
diff --git a/sdp/parser.go b/sdp/parser.go
@@ -245,9 +245,10 @@ type Repeat struct {
func parseRepeat(s string) (Repeat, error) {
// guard against negative durations, decimals.
// these are valid for time.ParseDuration, but not for our Repeat.
- if i := strings.IndexAny(s, "-."); i > 0 {
- return Repeat{}, fmt.Errorf("illegal character in duration: %c", s[i])
+ if strings.Contains(s, "-") || strings.Contains(s, ".") {
+ return Repeat{}, errors.New("invalid duration")
}
+
fields := strings.Fields(s)
if len(fields) < 3 {
return Repeat{}, fmt.Errorf("short line: have %d, want at least %d fields", len(fields), 3)
diff --git a/sdp/sdp_test.go b/sdp/sdp_test.go
@@ -215,18 +215,64 @@ func TestParseRepeat(t *testing.T) {
func TestDuration(t *testing.T) {
var cases = []struct {
- s string
- want time.Duration
+ name string
+ s string
+ want time.Duration
+ wantErr bool
}{
- {"86400", 24 * time.Hour},
- {"24h", 24 * time.Hour},
- {"1d", 24 * time.Hour},
- {"69s", 69 * time.Second},
+ {
+ name: "dayOfSeconds",
+ s: "86400",
+ want: 24 * time.Hour,
+ },
+ {
+ name: "twentyFourHours",
+ s: "24h",
+ want: 24 * time.Hour,
+ },
+ {
+ name: "oneDay",
+ s: "1d",
+ want: 24 * time.Hour,
+ },
+ {
+ name: "nice",
+ s: "69s",
+ want: 69 * time.Second,
+ },
+ {
+ name: "negative",
+ s: "-01s",
+ want: time.Duration(-1) * time.Second,
+ },
+ {
+ name: "decimal",
+ s: "1.5h",
+ want: time.Duration(5400) * time.Second,
+ },
+ {
+ name: "badSuffix",
+ s: "13k",
+ want: 0,
+ wantErr: true,
+ },
+ {
+ name: "2Days",
+ s: "2d",
+ want: 48 * time.Hour,
+ },
+ {
+ name: "aDay",
+ s: "Ad",
+ want: 0,
+ wantErr: true,
+ },
}
+
for _, tt := range cases {
t.Run(tt.s, func(t *testing.T) {
got, err := parseDuration(tt.s)
- if err != nil {
+ if (err != nil) != tt.wantErr {
t.Fatal(err)
}
if got != tt.want {