From b6a6a659f437417a8ee867f273ae59c02d9755ef Mon Sep 17 00:00:00 2001 From: Dmytro Kozlov Date: Sun, 7 Aug 2022 23:07:57 +0300 Subject: [PATCH] lib/auth: add tests for NewToken function (#2921) * lib/auth: add tests from NewToken function * lib/auth: update test, fix problem with type conversion * lib/auth: update test description * lib/auth: simplify failure tests --- lib/auth/auth.go | 4 +-- lib/auth/auth_test.go | 59 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 lib/auth/auth_test.go diff --git a/lib/auth/auth.go b/lib/auth/auth.go index 7a4d2c11c..27d1175d1 100644 --- a/lib/auth/auth.go +++ b/lib/auth/auth.go @@ -19,13 +19,13 @@ func NewToken(authToken string) (*Token, error) { return nil, fmt.Errorf("unexpected number of items in authToken %q; got %d; want 1 or 2", authToken, len(tmp)) } var at Token - accountID, err := strconv.Atoi(tmp[0]) + accountID, err := strconv.ParseUint(tmp[0], 10, 32) if err != nil { return nil, fmt.Errorf("cannot parse accountID from %q: %w", tmp[0], err) } at.AccountID = uint32(accountID) if len(tmp) > 1 { - projectID, err := strconv.Atoi(tmp[1]) + projectID, err := strconv.ParseUint(tmp[1], 10, 32) if err != nil { return nil, fmt.Errorf("cannot parse projectID from %q: %w", tmp[1], err) } diff --git a/lib/auth/auth_test.go b/lib/auth/auth_test.go new file mode 100644 index 000000000..81ed7f6b1 --- /dev/null +++ b/lib/auth/auth_test.go @@ -0,0 +1,59 @@ +package auth + +import ( + "fmt" + "testing" +) + +func TestNewTokenSuccess(t *testing.T) { + f := func(name string, token string, want string) { + t.Helper() + t.Run(name, func(t *testing.T) { + newToken, err := NewToken(token) + if err != nil { + t.Fatalf("expecting nil error") + } + got := fmt.Sprintf("%d:%d", newToken.AccountID, newToken.ProjectID) + if got != want { + t.Errorf("NewToken() got = %v, want %v", newToken, want) + } + }) + + } + f("token with accountID and projectID", "1:2", "1:2") + f("max uint32 accountID", "4294967295:1", "4294967295:1") + f("max uint32 projectID", "1:4294967295", "1:4294967295") + f("max uint32 accountID and projectID", "4294967295:4294967295", "4294967295:4294967295") +} + +func TestNewTokenFailure(t *testing.T) { + f := func(name string, token string) { + t.Helper() + t.Run(name, func(t *testing.T) { + newToken, err := NewToken(token) + if err == nil { + t.Fatalf("expecting non-nil error") + } + if newToken != nil { + t.Fatalf("expecting nil token") + } + }) + } + f("empty token", "") + f("empty accountID and projectID", ":") + f("accountID and projectID not int values", "a:b") + f("missed projectID", "1:") + f("missed accountID", ":2") + f("large int value for accountID", "9223372036854775808:1") + f("large int value for projectID", "2:9223372036854775808") + f("both large int values incorrect", "9223372036854775809:9223372036854775808") + f("large uint32 values incorrect", "4294967297:4294967295") + f("negative accountID", "-100:100") + f("negative projectID", "100:-100") + f("negative accountID and projectID", "-100:-100") + f("accountID is string", "abcd:2") + f("projectID is string", "2:abcd") + f("empty many parts in the token", "::") + f("many string parts in the token", "a:b:c") + f("many int parts in the token", "1:2:3") +}