Commit 983b9970 authored by jamesge's avatar jamesge

Modify RedisTest.auth to adapt redis 6.0

parent c347f589
...@@ -356,18 +356,25 @@ TEST_F(RedisTest, by_components) { ...@@ -356,18 +356,25 @@ TEST_F(RedisTest, by_components) {
AssertResponseEqual(response2, response, 2); AssertResponseEqual(response2, response, 2);
} }
static std::string GeneratePassword() {
std::string result;
result.reserve(12);
for (size_t i = 0; i < result.capacity(); ++i) {
result.push_back(butil::fast_rand_in('a', 'z'));
}
return result;
}
TEST_F(RedisTest, auth) { TEST_F(RedisTest, auth) {
if (g_redis_pid < 0) { if (g_redis_pid < 0) {
puts("Skipped due to absence of redis-server"); puts("Skipped due to absence of redis-server");
return; return;
} }
// generate a random password // generate a random password
char PASSWORD[12]; const std::string passwd1 = GeneratePassword();
for (size_t i = 0; i < arraysize(PASSWORD) - 1; ++i) { const std::string passwd2 = GeneratePassword();
PASSWORD[i] = butil::fast_rand_in('a', 'z'); LOG(INFO) << "Generated passwd1=" << passwd1 << " passwd2=" << passwd2;
}
PASSWORD[arraysize(PASSWORD)-1] = '\0';
LOG(INFO) << "Generated password=" << PASSWORD;
// config auth // config auth
{ {
brpc::ChannelOptions options; brpc::ChannelOptions options;
...@@ -378,10 +385,10 @@ TEST_F(RedisTest, auth) { ...@@ -378,10 +385,10 @@ TEST_F(RedisTest, auth) {
brpc::RedisResponse response; brpc::RedisResponse response;
brpc::Controller cntl; brpc::Controller cntl;
request.AddCommand("set passwd %s", PASSWORD); request.AddCommand("set mykey %s", passwd1.c_str());
request.AddCommand("config set requirepass %s", PASSWORD); request.AddCommand("config set requirepass %s", passwd1.c_str());
request.AddCommand("auth %s", PASSWORD); request.AddCommand("auth %s", passwd1.c_str());
request.AddCommand("get passwd"); request.AddCommand("get mykey");
channel.CallMethod(NULL, &cntl, &request, &response, NULL); channel.CallMethod(NULL, &cntl, &request, &response, NULL);
ASSERT_FALSE(cntl.Failed()) << cntl.ErrorText(); ASSERT_FALSE(cntl.Failed()) << cntl.ErrorText();
...@@ -393,7 +400,7 @@ TEST_F(RedisTest, auth) { ...@@ -393,7 +400,7 @@ TEST_F(RedisTest, auth) {
ASSERT_EQ(brpc::REDIS_REPLY_STATUS, response.reply(2).type()); ASSERT_EQ(brpc::REDIS_REPLY_STATUS, response.reply(2).type());
ASSERT_STREQ("OK", response.reply(2).c_str()); ASSERT_STREQ("OK", response.reply(2).c_str());
ASSERT_EQ(brpc::REDIS_REPLY_STRING, response.reply(3).type()); ASSERT_EQ(brpc::REDIS_REPLY_STRING, response.reply(3).type());
ASSERT_STREQ(PASSWORD, response.reply(3).c_str()); ASSERT_STREQ(passwd1.c_str(), response.reply(3).c_str());
} }
// Auth failed // Auth failed
...@@ -406,54 +413,58 @@ TEST_F(RedisTest, auth) { ...@@ -406,54 +413,58 @@ TEST_F(RedisTest, auth) {
brpc::RedisResponse response; brpc::RedisResponse response;
brpc::Controller cntl; brpc::Controller cntl;
request.AddCommand("get passwd"); request.AddCommand("get mykey");
channel.CallMethod(NULL, &cntl, &request, &response, NULL); channel.CallMethod(NULL, &cntl, &request, &response, NULL);
ASSERT_FALSE(cntl.Failed()) << cntl.ErrorText(); ASSERT_FALSE(cntl.Failed()) << cntl.ErrorText();
ASSERT_EQ(1, response.reply_size()); ASSERT_EQ(1, response.reply_size());
ASSERT_EQ(brpc::REDIS_REPLY_ERROR, response.reply(0).type()); ASSERT_EQ(brpc::REDIS_REPLY_ERROR, response.reply(0).type());
} }
// Auth with RedisAuthenticator && clear auth // Auth with RedisAuthenticator and change to passwd2 (setting to empty
// pass does not work on redis 6.0.6)
{ {
brpc::ChannelOptions options; brpc::ChannelOptions options;
options.protocol = brpc::PROTOCOL_REDIS; options.protocol = brpc::PROTOCOL_REDIS;
brpc::Channel channel; brpc::Channel channel;
brpc::policy::RedisAuthenticator* auth = brpc::policy::RedisAuthenticator* auth =
new brpc::policy::RedisAuthenticator(PASSWORD); new brpc::policy::RedisAuthenticator(passwd1.c_str());
options.auth = auth; options.auth = auth;
ASSERT_EQ(0, channel.Init("0.0.0.0:" REDIS_SERVER_PORT, &options)); ASSERT_EQ(0, channel.Init("0.0.0.0:" REDIS_SERVER_PORT, &options));
brpc::RedisRequest request; brpc::RedisRequest request;
brpc::RedisResponse response; brpc::RedisResponse response;
brpc::Controller cntl; brpc::Controller cntl;
request.AddCommand("get passwd"); request.AddCommand("get mykey");
request.AddCommand("config set requirepass ''"); request.AddCommand("config set requirepass %s", passwd2.c_str());
channel.CallMethod(NULL, &cntl, &request, &response, NULL); channel.CallMethod(NULL, &cntl, &request, &response, NULL);
ASSERT_FALSE(cntl.Failed()) << cntl.ErrorText(); ASSERT_FALSE(cntl.Failed()) << cntl.ErrorText();
ASSERT_EQ(2, response.reply_size()); ASSERT_EQ(2, response.reply_size());
ASSERT_EQ(brpc::REDIS_REPLY_STRING, response.reply(0).type()); ASSERT_EQ(brpc::REDIS_REPLY_STRING, response.reply(0).type());
ASSERT_STREQ(PASSWORD, response.reply(0).c_str()); ASSERT_STREQ(passwd1.c_str(), response.reply(0).c_str());
ASSERT_EQ(brpc::REDIS_REPLY_STATUS, response.reply(1).type()); ASSERT_EQ(brpc::REDIS_REPLY_STATUS, response.reply(1).type());
ASSERT_STREQ("OK", response.reply(1).c_str()); ASSERT_STREQ("OK", response.reply(1).c_str());
} }
// check noauth. // Auth with passwd2
{ {
brpc::ChannelOptions options; brpc::ChannelOptions options;
options.protocol = brpc::PROTOCOL_REDIS; options.protocol = brpc::PROTOCOL_REDIS;
brpc::policy::RedisAuthenticator* auth =
new brpc::policy::RedisAuthenticator(passwd2.c_str());
options.auth = auth;
brpc::Channel channel; brpc::Channel channel;
ASSERT_EQ(0, channel.Init("0.0.0.0:" REDIS_SERVER_PORT, &options)); ASSERT_EQ(0, channel.Init("0.0.0.0:" REDIS_SERVER_PORT, &options));
brpc::RedisRequest request; brpc::RedisRequest request;
brpc::RedisResponse response; brpc::RedisResponse response;
brpc::Controller cntl; brpc::Controller cntl;
request.AddCommand("get passwd"); request.AddCommand("get mykey");
channel.CallMethod(NULL, &cntl, &request, &response, NULL); channel.CallMethod(NULL, &cntl, &request, &response, NULL);
ASSERT_FALSE(cntl.Failed()) << cntl.ErrorText(); ASSERT_FALSE(cntl.Failed()) << cntl.ErrorText();
ASSERT_EQ(1, response.reply_size()); ASSERT_EQ(1, response.reply_size());
ASSERT_EQ(brpc::REDIS_REPLY_STRING, response.reply(0).type()) << response.reply(0); ASSERT_EQ(brpc::REDIS_REPLY_STRING, response.reply(0).type()) << response.reply(0);
ASSERT_STREQ(PASSWORD, response.reply(0).c_str()); ASSERT_STREQ(passwd1.c_str(), response.reply(0).c_str());
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment