diff --git a/server/api/admin.go b/server/api/admin.go index 1dddf57ba8f..9c33da1d5ab 100644 --- a/server/api/admin.go +++ b/server/api/admin.go @@ -59,13 +59,18 @@ func (h *adminHandler) ResetTS(w http.ResponseWriter, r *http.Request) { if err := apiutil.ReadJSONRespondError(h.rd, w, r.Body, &input); err != nil { return } - ts, ok := input["tso"].(float64) - if !ok || ts < 0 { + tsValue, ok := input["tso"].(string) + if !ok || len(tsValue) == 0 { + h.rd.JSON(w, http.StatusBadRequest, "invalid tso value") + return + } + ts, err := strconv.ParseUint(tsValue, 10, 64) + if err != nil { h.rd.JSON(w, http.StatusBadRequest, "invalid tso value") return } - if err := handler.ResetTS(uint64(ts)); err != nil { + if err = handler.ResetTS(ts); err != nil { if err == server.ErrServerNotStarted { h.rd.JSON(w, http.StatusInternalServerError, err.Error()) } else { diff --git a/server/api/admin_test.go b/server/api/admin_test.go index 9752d50318b..a6a8fbc4e2c 100644 --- a/server/api/admin_test.go +++ b/server/api/admin_test.go @@ -16,7 +16,6 @@ package api import ( "encoding/json" "fmt" - "math" "net/http" "strings" "time" @@ -92,9 +91,9 @@ type testTSOSuite struct { urlPrefix string } -func makeTS(offset time.Duration) int64 { +func makeTS(offset time.Duration) uint64 { physical := time.Now().Add(offset).UnixNano() / int64(time.Millisecond) - return physical << 18 + return uint64(physical << 18) } func (s *testTSOSuite) SetUpSuite(c *C) { @@ -115,7 +114,7 @@ func (s *testTSOSuite) TestResetTS(c *C) { args := make(map[string]interface{}) t1 := makeTS(time.Hour) url := s.urlPrefix - args["tso"] = t1 + args["tso"] = fmt.Sprintf("%d", t1) values, err := json.Marshal(args) c.Assert(err, IsNil) err = postJSON(url, values, @@ -126,7 +125,7 @@ func (s *testTSOSuite) TestResetTS(c *C) { c.Assert(err, IsNil) t2 := makeTS(32 * time.Hour) - args["tso"] = t2 + args["tso"] = fmt.Sprintf("%d", t2) values, err = json.Marshal(args) c.Assert(err, IsNil) err = postJSON(url, values, @@ -135,7 +134,7 @@ func (s *testTSOSuite) TestResetTS(c *C) { c.Assert(strings.Contains(err.Error(), "too large"), IsTrue) t3 := makeTS(-2 * time.Hour) - args["tso"] = t3 + args["tso"] = fmt.Sprintf("%d", t3) values, err = json.Marshal(args) c.Assert(err, IsNil) err = postJSON(url, values, @@ -143,8 +142,7 @@ func (s *testTSOSuite) TestResetTS(c *C) { c.Assert(err, NotNil) c.Assert(strings.Contains(err.Error(), "small"), IsTrue) - t4 := math.MinInt64 - args["tso"] = t4 + args["tso"] = "" values, err = json.Marshal(args) c.Assert(err, IsNil) err = postJSON(url, values, @@ -152,4 +150,11 @@ func (s *testTSOSuite) TestResetTS(c *C) { c.Assert(err, NotNil) c.Assert(err.Error(), Equals, "\"invalid tso value\"\n") + args["tso"] = "test" + values, err = json.Marshal(args) + c.Assert(err, IsNil) + err = postJSON(url, values, + func(_ []byte, code int) { c.Assert(code, Equals, http.StatusBadRequest) }) + c.Assert(err, NotNil) + c.Assert(err.Error(), Equals, "\"invalid tso value\"\n") }