From: paladz <453256728@qq.com> Date: Tue, 2 Jul 2019 11:41:56 +0000 (+0800) Subject: check the accesstoken folder X-Git-Url: http://git.osdn.net/view?p=bytom%2Fvapor.git;a=commitdiff_plain;h=d54ebfdbf7c24059c09e30cf3f6dbb4a1bbf6d37 check the accesstoken folder --- diff --git a/accesstoken/accesstoken.go b/accesstoken/accesstoken.go index a2b9d858..640f4aed 100644 --- a/accesstoken/accesstoken.go +++ b/accesstoken/accesstoken.go @@ -72,7 +72,6 @@ func (cs *CredentialStore) Create(id, typ string) (*Token, error) { hashedSecret := make([]byte, tokenSize) sha3pool.Sum256(hashedSecret, secret) - token := &Token{ ID: id, Token: fmt.Sprintf("%s:%x", id, hashedSecret), @@ -84,8 +83,8 @@ func (cs *CredentialStore) Create(id, typ string) (*Token, error) { if err != nil { return nil, err } - cs.DB.Set(key, value) + cs.DB.Set(key, value) return token, nil } @@ -95,12 +94,12 @@ func (cs *CredentialStore) Check(id string, secret string) error { return errors.WithDetailf(ErrBadID, "invalid id %q", id) } - var value []byte - token := &Token{} - - if value = cs.DB.Get([]byte(id)); value == nil { + value := cs.DB.Get([]byte(id)) + if value == nil { return errors.WithDetailf(ErrNoMatchID, "check id %q nonexisting", id) } + + token := &Token{} if err := json.Unmarshal(value, token); err != nil { return err } diff --git a/accesstoken/accesstoken_test.go b/accesstoken/accesstoken_test.go index 8c0fd1d1..16c470bd 100644 --- a/accesstoken/accesstoken_test.go +++ b/accesstoken/accesstoken_test.go @@ -1,7 +1,6 @@ package accesstoken import ( - "context" "os" "strings" "testing" @@ -27,23 +26,21 @@ func TestCreate(t *testing.T) { } for _, c := range cases { - _, err := cs.Create(c.id, c.typ) - if errors.Root(err) != c.want { + if _, err := cs.Create(c.id, c.typ); errors.Root(err) != c.want { t.Errorf("Create(%s, %s) error = %s want %s", c.id, c.typ, err, c.want) } } } func TestList(t *testing.T) { - ctx := context.Background() testDB := dbm.NewDB("testdb", "leveldb", "temp") defer os.RemoveAll("temp") - cs := NewStore(testDB) + cs := NewStore(testDB) tokenMap := make(map[string]*Token) - tokenMap["ab"] = mustCreateToken(ctx, t, cs, "ab", "test") - tokenMap["bc"] = mustCreateToken(ctx, t, cs, "bc", "test") - tokenMap["cd"] = mustCreateToken(ctx, t, cs, "cd", "test") + tokenMap["ab"] = mustCreateToken(t, cs, "ab", "test") + tokenMap["bc"] = mustCreateToken(t, cs, "bc", "test") + tokenMap["cd"] = mustCreateToken(t, cs, "cd", "test") got, err := cs.List() if err != nil { @@ -53,6 +50,7 @@ func TestList(t *testing.T) { if len(got) != len(tokenMap) { t.Error("List errored: get invalid length") } + for _, v := range got { if v.Token != tokenMap[v.ID].Token { t.Errorf("List error: ID: %s, expected token: %s, DB token: %s", v.ID, *tokenMap[v.ID], v.Token) @@ -62,12 +60,11 @@ func TestList(t *testing.T) { } func TestCheck(t *testing.T) { - ctx := context.Background() testDB := dbm.NewDB("testdb", "leveldb", "temp") defer os.RemoveAll("temp") cs := NewStore(testDB) - token := mustCreateToken(ctx, t, cs, "x", "client") + token := mustCreateToken(t, cs, "x", "client") tokenParts := strings.Split(token.Token, ":") if err := cs.Check(tokenParts[0], tokenParts[1]); err != nil { @@ -80,13 +77,12 @@ func TestCheck(t *testing.T) { } func TestDelete(t *testing.T) { - ctx := context.Background() testDB := dbm.NewDB("testdb", "leveldb", "temp") defer os.RemoveAll("temp") cs := NewStore(testDB) const id = "Y" - mustCreateToken(ctx, t, cs, id, "client") + mustCreateToken(t, cs, id, "client") err := cs.Delete(id) if err != nil { @@ -104,13 +100,12 @@ func TestDeleteWithInvalidId(t *testing.T) { defer os.RemoveAll("temp") cs := NewStore(testDB) - err := cs.Delete("@") - if errors.Root(err) != ErrBadID { + if err := cs.Delete("@"); errors.Root(err) != ErrBadID { t.Errorf("Deletion with invalid id success, while it should not") } } -func mustCreateToken(ctx context.Context, t *testing.T, cs *CredentialStore, id, typ string) *Token { +func mustCreateToken(t *testing.T, cs *CredentialStore, id, typ string) *Token { token, err := cs.Create(id, typ) if err != nil { t.Fatal(err)