You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kvrocks.apache.org by ti...@apache.org on 2022/09/06 16:29:26 UTC
[incubator-kvrocks] branch unstable updated: refactor: unit/auth to gocase (#830)
This is an automated email from the ASF dual-hosted git repository.
tison pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/incubator-kvrocks.git
The following commit(s) were added to refs/heads/unstable by this push:
new 3f0fb40 refactor: unit/auth to gocase (#830)
3f0fb40 is described below
commit 3f0fb401030332aff76a4d2638887cbf0de240fc
Author: tison <wa...@gmail.com>
AuthorDate: Wed Sep 7 00:29:21 2022 +0800
refactor: unit/auth to gocase (#830)
Signed-off-by: tison <wa...@gmail.com>
Signed-off-by: tison <wa...@gmail.com>
---
src/redis_connection.cc | 14 ++++---
tests/gocase/unit/auth/auth_test.go | 73 +++++++++++++++++++++++++++++++++++++
tests/gocase/util/server.go | 3 +-
tests/tcl/tests/test_helper.tcl | 1 -
tests/tcl/tests/unit/auth.tcl | 50 -------------------------
5 files changed, 83 insertions(+), 58 deletions(-)
diff --git a/src/redis_connection.cc b/src/redis_connection.cc
index 35a276a..801df96 100644
--- a/src/redis_connection.cc
+++ b/src/redis_connection.cc
@@ -316,6 +316,14 @@ void Connection::ExecuteCommands(std::deque<CommandTokens> *to_process_cmds) {
if (IsFlagEnabled(Redis::Connection::kCloseAfterReply) &&
!IsFlagEnabled(Connection::kMultiExec)) break;
+
+ auto s = svr_->LookupAndCreateCommand(cmd_tokens.front(), ¤t_cmd_);
+ if (!s.IsOK()) {
+ if (IsFlagEnabled(Connection::kMultiExec)) multi_error_ = true;
+ Reply(Redis::Error("ERR unknown command " + cmd_tokens.front()));
+ continue;
+ }
+
if (GetNamespace().empty()) {
if (!password.empty() && Util::ToLower(cmd_tokens.front()) != "auth") {
Reply(Redis::Error("NOAUTH Authentication required."));
@@ -327,12 +335,6 @@ void Connection::ExecuteCommands(std::deque<CommandTokens> *to_process_cmds) {
}
}
- auto s = svr_->LookupAndCreateCommand(cmd_tokens.front(), ¤t_cmd_);
- if (!s.IsOK()) {
- if (IsFlagEnabled(Connection::kMultiExec)) multi_error_ = true;
- Reply(Redis::Error("ERR unknown command " + cmd_tokens.front()));
- continue;
- }
const auto attributes = current_cmd_->GetAttributes();
auto cmd_name = attributes->name;
diff --git a/tests/gocase/unit/auth/auth_test.go b/tests/gocase/unit/auth/auth_test.go
new file mode 100644
index 0000000..6613c09
--- /dev/null
+++ b/tests/gocase/unit/auth/auth_test.go
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package auth
+
+import (
+ "context"
+ "testing"
+
+ "github.com/apache/incubator-kvrocks/tests/gocase/util"
+ "github.com/stretchr/testify/require"
+)
+
+func TestNoAuth(t *testing.T) {
+ srv := util.StartServer(t, map[string]string{})
+ defer srv.Close()
+
+ ctx := context.Background()
+ rdb := srv.NewClient()
+ defer func() { require.NoError(t, rdb.Close()) }()
+
+ t.Run("AUTH fails if there is no password configured server side", func(t *testing.T) {
+ r := rdb.Do(ctx, "AUTH", "foo")
+ require.ErrorContains(t, r.Err(), "no password")
+ })
+}
+
+func TestAuth(t *testing.T) {
+ srv := util.StartServer(t, map[string]string{
+ "requirepass": "foobar",
+ })
+ defer srv.Close()
+
+ ctx := context.Background()
+ rdb := srv.NewClient()
+ defer func() { require.NoError(t, rdb.Close()) }()
+
+ t.Run("AUTH fails when a wrong password is given", func(t *testing.T) {
+ r := rdb.Do(ctx, "AUTH", "wrong!")
+ require.ErrorContains(t, r.Err(), "invalid password")
+ })
+
+ t.Run("Arbitrary command gives an error when AUTH is required", func(t *testing.T) {
+ r := rdb.Set(ctx, "foo", "bar", 0)
+ require.ErrorContains(t, r.Err(), "NOAUTH Authentication required.")
+ })
+
+ t.Run("AUTH succeeds when the right password is given", func(t *testing.T) {
+ r := rdb.Do(ctx, "AUTH", "foobar")
+ require.Equal(t, "OK", r.Val())
+ })
+
+ t.Run("Once AUTH succeeded we can actually send commands to the server", func(t *testing.T) {
+ require.Equal(t, "OK", rdb.Set(ctx, "foo", 100, 0).Val())
+ require.EqualValues(t, 101, rdb.Incr(ctx, "foo").Val())
+ })
+}
diff --git a/tests/gocase/util/server.go b/tests/gocase/util/server.go
index 7316fa6..a2ce842 100644
--- a/tests/gocase/util/server.go
+++ b/tests/gocase/util/server.go
@@ -94,7 +94,8 @@ func StartServer(t *testing.T, configs map[string]string) *KvrocksServer {
c := redis.NewClient(&redis.Options{Addr: addr.String()})
defer func() { require.NoError(t, c.Close()) }()
require.Eventually(t, func() bool {
- return c.Ping(context.Background()).Err() == nil
+ err := c.Ping(context.Background()).Err()
+ return err == nil || err.Error() == "NOAUTH Authentication required."
}, time.Minute, time.Second)
return &KvrocksServer{
diff --git a/tests/tcl/tests/test_helper.tcl b/tests/tcl/tests/test_helper.tcl
index 04c98c3..8fa937d 100644
--- a/tests/tcl/tests/test_helper.tcl
+++ b/tests/tcl/tests/test_helper.tcl
@@ -33,7 +33,6 @@ source tests/support/test.tcl
source tests/support/util.tcl
set ::all_tests {
- unit/auth
unit/keyspace
unit/scan
unit/type/string
diff --git a/tests/tcl/tests/unit/auth.tcl b/tests/tcl/tests/unit/auth.tcl
deleted file mode 100644
index 2611cf1..0000000
--- a/tests/tcl/tests/unit/auth.tcl
+++ /dev/null
@@ -1,50 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-
-# Copyright (c) 2006-2020, Salvatore Sanfilippo
-# See bundled license file licenses/LICENSE.redis for details.
-
-# This file is copied and modified from the Redis project,
-# which started out as: https://github.com/redis/redis/blob/dbcc0a8/tests/unit/auth.tcl
-
-start_server {tags {"auth"}} {
- test {AUTH fails if there is no password configured server side} {
- catch {r auth foo} err
- set _ $err
- } {ERR*no password*}
-}
-
-start_server {tags {"auth"} overrides {requirepass foobar}} {
- test {AUTH fails when a wrong password is given} {
- catch {r auth wrong!} err
- set _ $err
- } {*invalid password*}
-
- test {Arbitrary command gives an error when AUTH is required} {
- catch {r set foo bar} err
- set _ $err
- } {NOAUTH*}
-
- test {AUTH succeeds when the right password is given} {
- r auth foobar
- } {OK}
-
- test {Once AUTH succeeded we can actually send commands to the server} {
- r set foo 100
- r incr foo
- } {101}
-}
\ No newline at end of file