forget: Prevent neg. values in --keep-within* opts
This commit is contained in:
parent
b7f03d01b8
commit
84ede6ad7a
2 changed files with 59 additions and 18 deletions
|
@ -100,15 +100,16 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func verifyForgetOptions(opts *ForgetOptions) error {
|
func verifyForgetOptions(opts *ForgetOptions) error {
|
||||||
var negValFound = false
|
|
||||||
|
|
||||||
if opts.Last < -1 || opts.Hourly < -1 || opts.Daily < -1 || opts.Weekly < -1 ||
|
if opts.Last < -1 || opts.Hourly < -1 || opts.Daily < -1 || opts.Weekly < -1 ||
|
||||||
opts.Monthly < -1 || opts.Yearly < -1 {
|
opts.Monthly < -1 || opts.Yearly < -1 {
|
||||||
negValFound = true
|
return errors.Fatal("negative values other than -1 are not allowed for --keep-*")
|
||||||
}
|
}
|
||||||
|
|
||||||
if negValFound {
|
for _, d := range []restic.Duration{opts.Within, opts.WithinHourly, opts.WithinDaily,
|
||||||
return errors.Fatal("negative values other than -1 are not allowed for --keep-* options")
|
opts.WithinMonthly, opts.WithinWeekly, opts.WithinYearly} {
|
||||||
|
if d.Hours < 0 || d.Days < 0 || d.Months < 0 || d.Years < 0 {
|
||||||
|
return errors.Fatal("durations containing negative values are not allowed for --keep-within*")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -1,25 +1,65 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/restic/restic/internal/restic"
|
||||||
rtest "github.com/restic/restic/internal/test"
|
rtest "github.com/restic/restic/internal/test"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestPreventNegativeForgetOptionValues(t *testing.T) {
|
func TestForgetOptionValues(t *testing.T) {
|
||||||
invalidForgetOpts := []ForgetOptions{
|
const negValErrorMsg = "Fatal: negative values other than -1 are not allowed for --keep-*"
|
||||||
{Last: -2},
|
const negDurationValErrorMsg = "Fatal: durations containing negative values are not allowed for --keep-within*"
|
||||||
{Hourly: -2},
|
testCases := []struct {
|
||||||
{Daily: -2},
|
input ForgetOptions
|
||||||
{Weekly: -2},
|
expectsError bool
|
||||||
{Monthly: -2},
|
errorMsg string
|
||||||
{Yearly: -2},
|
}{
|
||||||
|
{ForgetOptions{Last: 1}, false, ""},
|
||||||
|
{ForgetOptions{Hourly: 1}, false, ""},
|
||||||
|
{ForgetOptions{Daily: 1}, false, ""},
|
||||||
|
{ForgetOptions{Weekly: 1}, false, ""},
|
||||||
|
{ForgetOptions{Monthly: 1}, false, ""},
|
||||||
|
{ForgetOptions{Yearly: 1}, false, ""},
|
||||||
|
{ForgetOptions{Last: 0}, false, ""},
|
||||||
|
{ForgetOptions{Hourly: 0}, false, ""},
|
||||||
|
{ForgetOptions{Daily: 0}, false, ""},
|
||||||
|
{ForgetOptions{Weekly: 0}, false, ""},
|
||||||
|
{ForgetOptions{Monthly: 0}, false, ""},
|
||||||
|
{ForgetOptions{Yearly: 0}, false, ""},
|
||||||
|
{ForgetOptions{Last: -1}, false, ""},
|
||||||
|
{ForgetOptions{Hourly: -1}, false, ""},
|
||||||
|
{ForgetOptions{Daily: -1}, false, ""},
|
||||||
|
{ForgetOptions{Weekly: -1}, false, ""},
|
||||||
|
{ForgetOptions{Monthly: -1}, false, ""},
|
||||||
|
{ForgetOptions{Yearly: -1}, false, ""},
|
||||||
|
{ForgetOptions{Last: -2}, true, negValErrorMsg},
|
||||||
|
{ForgetOptions{Hourly: -2}, true, negValErrorMsg},
|
||||||
|
{ForgetOptions{Daily: -2}, true, negValErrorMsg},
|
||||||
|
{ForgetOptions{Weekly: -2}, true, negValErrorMsg},
|
||||||
|
{ForgetOptions{Monthly: -2}, true, negValErrorMsg},
|
||||||
|
{ForgetOptions{Yearly: -2}, true, negValErrorMsg},
|
||||||
|
{ForgetOptions{Within: restic.ParseDurationOrPanic("1y2m3d3h")}, false, ""},
|
||||||
|
{ForgetOptions{WithinHourly: restic.ParseDurationOrPanic("1y2m3d3h")}, false, ""},
|
||||||
|
{ForgetOptions{WithinDaily: restic.ParseDurationOrPanic("1y2m3d3h")}, false, ""},
|
||||||
|
{ForgetOptions{WithinWeekly: restic.ParseDurationOrPanic("1y2m3d3h")}, false, ""},
|
||||||
|
{ForgetOptions{WithinMonthly: restic.ParseDurationOrPanic("2y4m6d8h")}, false, ""},
|
||||||
|
{ForgetOptions{WithinYearly: restic.ParseDurationOrPanic("2y4m6d8h")}, false, ""},
|
||||||
|
{ForgetOptions{Within: restic.ParseDurationOrPanic("-1y2m3d3h")}, true, negDurationValErrorMsg},
|
||||||
|
{ForgetOptions{WithinHourly: restic.ParseDurationOrPanic("1y-2m3d3h")}, true, negDurationValErrorMsg},
|
||||||
|
{ForgetOptions{WithinDaily: restic.ParseDurationOrPanic("1y2m-3d3h")}, true, negDurationValErrorMsg},
|
||||||
|
{ForgetOptions{WithinWeekly: restic.ParseDurationOrPanic("1y2m3d-3h")}, true, negDurationValErrorMsg},
|
||||||
|
{ForgetOptions{WithinMonthly: restic.ParseDurationOrPanic("-2y4m6d8h")}, true, negDurationValErrorMsg},
|
||||||
|
{ForgetOptions{WithinYearly: restic.ParseDurationOrPanic("2y-4m6d8h")}, true, negDurationValErrorMsg},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, opts := range invalidForgetOpts {
|
for _, testCase := range testCases {
|
||||||
err := verifyForgetOptions(&opts)
|
err := verifyForgetOptions(&testCase.input)
|
||||||
rtest.Assert(t, err != nil, fmt.Sprintf("should have returned error for %+v", opts))
|
if testCase.expectsError {
|
||||||
rtest.Equals(t, "Fatal: negative values other than -1 are not allowed for --keep-* options", err.Error())
|
rtest.Assert(t, err != nil, "should have returned error for input %+v", testCase.input)
|
||||||
|
rtest.Equals(t, testCase.errorMsg, err.Error())
|
||||||
|
} else {
|
||||||
|
rtest.Assert(t, err == nil, "expected no error for input %+v", testCase.input)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue