From 9f3bf0d160aa09ece781875906adba5d1f845fd1 Mon Sep 17 00:00:00 2001 From: Eric Chiang Date: Wed, 9 Dec 2015 14:47:21 -0800 Subject: [PATCH] db: strconv.ParseInt specify base 10 closes #99 --- db/refresh.go | 2 +- db/refresh_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 db/refresh_test.go diff --git a/db/refresh.go b/db/refresh.go index 4aebed13..15195a46 100644 --- a/db/refresh.go +++ b/db/refresh.go @@ -52,7 +52,7 @@ func parseToken(token string) (int64, []byte, error) { if len(parts) != 2 { return -1, nil, refresh.ErrorInvalidToken } - id, err := strconv.ParseInt(parts[0], 0, 64) + id, err := strconv.ParseInt(parts[0], 10, 64) if err != nil { return -1, nil, refresh.ErrorInvalidToken } diff --git a/db/refresh_test.go b/db/refresh_test.go new file mode 100644 index 00000000..11107816 --- /dev/null +++ b/db/refresh_test.go @@ -0,0 +1,31 @@ +package db + +import ( + "bytes" + "testing" +) + +func TestBuildAndParseToken(t *testing.T) { + tests := []struct { + id int64 + payload []byte + }{ + {11111, []byte("may the force be with you")}, + {123213, []byte("If we can hit that bullseye the rest of the dominoes will fall like a house of cards, checkmate!")}, + {1, []byte{0xd3, 0x22, 0xa8, 0x44, 0x34, 0x94, 0xd8}}, + } + + for i, tt := range tests { + id, payload, err := parseToken(buildToken(tt.id, tt.payload)) + if err != nil { + t.Errorf("case %d: failed to parse token: %v", i, err) + continue + } + if tt.id != id { + t.Errorf("case %d: want id=%d, got id=%d", i, tt.id, id) + } + if bytes.Compare(tt.payload, payload) != 0 { + t.Errorf("case %d: want payload=%x, got payload=%x", i, tt.payload, payload) + } + } +}