TDengine/source/dnode/mnode/impl/test/user/user.cpp

311 lines
8.1 KiB
C++
Raw Normal View History

2021-12-16 09:05:13 +00:00
/**
2021-12-16 12:16:35 +00:00
* @file user.cpp
2021-12-16 09:05:13 +00:00
* @author slguan (slguan@taosdata.com)
2022-01-04 09:17:25 +00:00
* @brief MNODE module user tests
* @version 1.0
2021-12-16 09:07:40 +00:00
* @date 2021-12-15
2021-12-07 12:14:22 +00:00
*
2021-12-16 09:05:13 +00:00
* @copyright Copyright (c) 2021
2021-12-07 12:14:22 +00:00
*
*/
2021-12-22 10:39:32 +00:00
#include "base.h"
2021-12-07 12:14:22 +00:00
class DndTestUser : public ::testing::Test {
protected:
2022-01-04 09:17:25 +00:00
static void SetUpTestSuite() { test.Init("/tmp/mnode_test_user", 9011); }
2021-12-22 10:39:32 +00:00
static void TearDownTestSuite() { test.Cleanup(); }
2021-12-07 12:14:22 +00:00
2021-12-22 10:39:32 +00:00
static Testbase test;
2021-12-07 12:14:22 +00:00
2021-12-13 10:19:09 +00:00
public:
void SetUp() override {}
void TearDown() override {}
};
2021-12-22 10:39:32 +00:00
Testbase DndTestUser::test;
2021-12-13 10:19:09 +00:00
2021-12-15 08:56:56 +00:00
TEST_F(DndTestUser, 01_ShowUser) {
2021-12-22 10:39:32 +00:00
test.SendShowMetaMsg(TSDB_MGMT_TABLE_USER, "");
CHECK_META("show users", 4);
CHECK_SCHEMA(0, TSDB_DATA_TYPE_BINARY, TSDB_USER_LEN + VARSTR_HEADER_SIZE, "name");
CHECK_SCHEMA(1, TSDB_DATA_TYPE_BINARY, 10 + VARSTR_HEADER_SIZE, "privilege");
CHECK_SCHEMA(2, TSDB_DATA_TYPE_TIMESTAMP, 8, "create_time");
CHECK_SCHEMA(3, TSDB_DATA_TYPE_BINARY, TSDB_USER_LEN + VARSTR_HEADER_SIZE, "account");
test.SendShowRetrieveMsg();
EXPECT_EQ(test.GetShowRows(), 1);
2021-12-13 10:19:09 +00:00
CheckBinary("root", TSDB_USER_LEN);
CheckBinary("super", 10);
CheckTimestamp();
CheckBinary("root", TSDB_USER_LEN);
2021-12-07 12:14:22 +00:00
}
2021-12-31 10:40:49 +00:00
TEST_F(DndTestUser, 02_Create_User) {
{
int32_t contLen = sizeof(SCreateUserMsg);
SCreateUserMsg* pReq = (SCreateUserMsg*)rpcMallocCont(contLen);
strcpy(pReq->user, "");
strcpy(pReq->pass, "p1");
SRpcMsg* pMsg = test.SendMsg(TDMT_MND_CREATE_USER, pReq, contLen);
ASSERT_NE(pMsg, nullptr);
ASSERT_EQ(pMsg->code, TSDB_CODE_MND_INVALID_USER_FORMAT);
}
{
int32_t contLen = sizeof(SCreateUserMsg);
SCreateUserMsg* pReq = (SCreateUserMsg*)rpcMallocCont(contLen);
strcpy(pReq->user, "u1");
strcpy(pReq->pass, "");
SRpcMsg* pMsg = test.SendMsg(TDMT_MND_CREATE_USER, pReq, contLen);
ASSERT_NE(pMsg, nullptr);
ASSERT_EQ(pMsg->code, TSDB_CODE_MND_INVALID_PASS_FORMAT);
}
{
int32_t contLen = sizeof(SCreateUserMsg);
SCreateUserMsg* pReq = (SCreateUserMsg*)rpcMallocCont(contLen);
strcpy(pReq->user, "root");
strcpy(pReq->pass, "1");
SRpcMsg* pMsg = test.SendMsg(TDMT_MND_CREATE_USER, pReq, contLen);
ASSERT_NE(pMsg, nullptr);
ASSERT_EQ(pMsg->code, TSDB_CODE_MND_USER_ALREADY_EXIST);
}
{
int32_t contLen = sizeof(SCreateUserMsg);
SCreateUserMsg* pReq = (SCreateUserMsg*)rpcMallocCont(contLen);
strcpy(pReq->user, "u1");
strcpy(pReq->pass, "p1");
SRpcMsg* pMsg = test.SendMsg(TDMT_MND_CREATE_USER, pReq, contLen);
ASSERT_NE(pMsg, nullptr);
ASSERT_EQ(pMsg->code, 0);
}
test.SendShowMetaMsg(TSDB_MGMT_TABLE_USER, "");
CHECK_META("show users", 4);
test.SendShowRetrieveMsg();
EXPECT_EQ(test.GetShowRows(), 2);
}
TEST_F(DndTestUser, 03_Alter_User) {
{
int32_t contLen = sizeof(SAlterUserMsg);
SAlterUserMsg* pReq = (SAlterUserMsg*)rpcMallocCont(contLen);
strcpy(pReq->user, "");
strcpy(pReq->pass, "p1");
SRpcMsg* pMsg = test.SendMsg(TDMT_MND_ALTER_USER, pReq, contLen);
ASSERT_NE(pMsg, nullptr);
ASSERT_EQ(pMsg->code, TSDB_CODE_MND_INVALID_USER_FORMAT);
}
{
int32_t contLen = sizeof(SAlterUserMsg);
SAlterUserMsg* pReq = (SAlterUserMsg*)rpcMallocCont(contLen);
strcpy(pReq->user, "u1");
strcpy(pReq->pass, "");
SRpcMsg* pMsg = test.SendMsg(TDMT_MND_ALTER_USER, pReq, contLen);
ASSERT_NE(pMsg, nullptr);
ASSERT_EQ(pMsg->code, TSDB_CODE_MND_INVALID_PASS_FORMAT);
}
{
int32_t contLen = sizeof(SAlterUserMsg);
SAlterUserMsg* pReq = (SAlterUserMsg*)rpcMallocCont(contLen);
strcpy(pReq->user, "u4");
strcpy(pReq->pass, "1");
SRpcMsg* pMsg = test.SendMsg(TDMT_MND_ALTER_USER, pReq, contLen);
ASSERT_NE(pMsg, nullptr);
ASSERT_EQ(pMsg->code, TSDB_CODE_MND_USER_NOT_EXIST);
}
{
int32_t contLen = sizeof(SAlterUserMsg);
SAlterUserMsg* pReq = (SAlterUserMsg*)rpcMallocCont(contLen);
strcpy(pReq->user, "u1");
strcpy(pReq->pass, "1");
SRpcMsg* pMsg = test.SendMsg(TDMT_MND_ALTER_USER, pReq, contLen);
ASSERT_NE(pMsg, nullptr);
ASSERT_EQ(pMsg->code, 0);
}
}
TEST_F(DndTestUser, 04_Drop_User) {
{
int32_t contLen = sizeof(SDropUserMsg);
SDropUserMsg* pReq = (SDropUserMsg*)rpcMallocCont(contLen);
strcpy(pReq->user, "");
SRpcMsg* pMsg = test.SendMsg(TDMT_MND_DROP_USER, pReq, contLen);
ASSERT_NE(pMsg, nullptr);
ASSERT_EQ(pMsg->code, TSDB_CODE_MND_INVALID_USER_FORMAT);
}
{
int32_t contLen = sizeof(SDropUserMsg);
SDropUserMsg* pReq = (SDropUserMsg*)rpcMallocCont(contLen);
strcpy(pReq->user, "u4");
SRpcMsg* pMsg = test.SendMsg(TDMT_MND_DROP_USER, pReq, contLen);
ASSERT_NE(pMsg, nullptr);
ASSERT_EQ(pMsg->code, TSDB_CODE_MND_USER_NOT_EXIST);
}
{
int32_t contLen = sizeof(SDropUserMsg);
SDropUserMsg* pReq = (SDropUserMsg*)rpcMallocCont(contLen);
strcpy(pReq->user, "u1");
SRpcMsg* pMsg = test.SendMsg(TDMT_MND_DROP_USER, pReq, contLen);
ASSERT_NE(pMsg, nullptr);
ASSERT_EQ(pMsg->code, 0);
}
test.SendShowMetaMsg(TSDB_MGMT_TABLE_USER, "");
CHECK_META("show users", 4);
test.SendShowRetrieveMsg();
EXPECT_EQ(test.GetShowRows(), 1);
}
2022-01-04 09:17:25 +00:00
TEST_F(DndTestUser, 05_Create_Drop_Alter_User) {
2021-12-13 11:02:26 +00:00
{
2021-12-22 10:39:32 +00:00
int32_t contLen = sizeof(SCreateUserMsg);
SCreateUserMsg* pReq = (SCreateUserMsg*)rpcMallocCont(contLen);
2021-12-13 11:02:26 +00:00
strcpy(pReq->user, "u1");
strcpy(pReq->pass, "p1");
2021-12-24 07:00:51 +00:00
SRpcMsg* pMsg = test.SendMsg(TDMT_MND_CREATE_USER, pReq, contLen);
2021-12-13 11:02:26 +00:00
ASSERT_NE(pMsg, nullptr);
ASSERT_EQ(pMsg->code, 0);
}
2021-12-07 12:14:22 +00:00
2021-12-13 11:02:26 +00:00
{
2021-12-22 10:39:32 +00:00
int32_t contLen = sizeof(SCreateUserMsg);
SCreateUserMsg* pReq = (SCreateUserMsg*)rpcMallocCont(contLen);
2021-12-13 11:02:26 +00:00
strcpy(pReq->user, "u2");
strcpy(pReq->pass, "p2");
2021-12-07 12:14:22 +00:00
2021-12-24 07:00:51 +00:00
SRpcMsg* pMsg = test.SendMsg(TDMT_MND_CREATE_USER, pReq, contLen);
2021-12-13 11:02:26 +00:00
ASSERT_NE(pMsg, nullptr);
ASSERT_EQ(pMsg->code, 0);
}
2021-12-07 12:14:22 +00:00
2021-12-22 10:39:32 +00:00
test.SendShowMetaMsg(TSDB_MGMT_TABLE_USER, "");
CHECK_META("show users", 4);
test.SendShowRetrieveMsg();
EXPECT_EQ(test.GetShowRows(), 3);
2021-12-13 10:19:09 +00:00
CheckBinary("u1", TSDB_USER_LEN);
CheckBinary("root", TSDB_USER_LEN);
2021-12-13 11:02:26 +00:00
CheckBinary("u2", TSDB_USER_LEN);
2021-12-13 10:19:09 +00:00
CheckBinary("normal", 10);
CheckBinary("super", 10);
2021-12-13 11:02:26 +00:00
CheckBinary("normal", 10);
CheckTimestamp();
2021-12-13 10:19:09 +00:00
CheckTimestamp();
CheckTimestamp();
CheckBinary("root", TSDB_USER_LEN);
CheckBinary("root", TSDB_USER_LEN);
2021-12-13 11:02:26 +00:00
CheckBinary("root", TSDB_USER_LEN);
2021-12-07 13:10:36 +00:00
2021-12-16 06:02:39 +00:00
{
2021-12-22 10:39:32 +00:00
int32_t contLen = sizeof(SAlterUserMsg);
SAlterUserMsg* pReq = (SAlterUserMsg*)rpcMallocCont(contLen);
2021-12-16 06:02:39 +00:00
strcpy(pReq->user, "u1");
strcpy(pReq->pass, "p2");
2021-12-07 13:10:36 +00:00
2021-12-24 07:00:51 +00:00
SRpcMsg* pMsg = test.SendMsg(TDMT_MND_ALTER_USER, pReq, contLen);
2021-12-16 06:02:39 +00:00
ASSERT_NE(pMsg, nullptr);
ASSERT_EQ(pMsg->code, 0);
}
2021-12-22 10:39:32 +00:00
test.SendShowMetaMsg(TSDB_MGMT_TABLE_USER, "");
CHECK_META("show users", 4);
test.SendShowRetrieveMsg();
EXPECT_EQ(test.GetShowRows(), 3);
2021-12-13 10:19:09 +00:00
CheckBinary("u1", TSDB_USER_LEN);
CheckBinary("root", TSDB_USER_LEN);
2021-12-13 11:02:26 +00:00
CheckBinary("u2", TSDB_USER_LEN);
2021-12-13 10:19:09 +00:00
CheckBinary("normal", 10);
CheckBinary("super", 10);
2021-12-13 11:02:26 +00:00
CheckBinary("normal", 10);
CheckTimestamp();
2021-12-13 10:19:09 +00:00
CheckTimestamp();
CheckTimestamp();
CheckBinary("root", TSDB_USER_LEN);
CheckBinary("root", TSDB_USER_LEN);
2021-12-13 11:02:26 +00:00
CheckBinary("root", TSDB_USER_LEN);
2021-12-07 12:14:22 +00:00
2021-12-16 06:02:39 +00:00
{
2021-12-22 10:39:32 +00:00
int32_t contLen = sizeof(SDropUserMsg);
2021-12-07 12:14:22 +00:00
2021-12-22 10:39:32 +00:00
SDropUserMsg* pReq = (SDropUserMsg*)rpcMallocCont(contLen);
strcpy(pReq->user, "u1");
2021-12-07 12:14:22 +00:00
2021-12-24 07:00:51 +00:00
SRpcMsg* pMsg = test.SendMsg(TDMT_MND_DROP_USER, pReq, contLen);
2021-12-16 06:02:39 +00:00
ASSERT_NE(pMsg, nullptr);
ASSERT_EQ(pMsg->code, 0);
}
2021-12-22 10:39:32 +00:00
test.SendShowMetaMsg(TSDB_MGMT_TABLE_USER, "");
CHECK_META("show users", 4);
test.SendShowRetrieveMsg();
EXPECT_EQ(test.GetShowRows(), 2);
2021-12-13 10:19:09 +00:00
CheckBinary("root", TSDB_USER_LEN);
2021-12-13 11:02:26 +00:00
CheckBinary("u2", TSDB_USER_LEN);
2021-12-13 10:19:09 +00:00
CheckBinary("super", 10);
2021-12-13 11:02:26 +00:00
CheckBinary("normal", 10);
CheckTimestamp();
2021-12-13 10:19:09 +00:00
CheckTimestamp();
CheckBinary("root", TSDB_USER_LEN);
2021-12-13 11:02:26 +00:00
CheckBinary("root", TSDB_USER_LEN);
2021-12-16 06:02:39 +00:00
// restart
2021-12-22 10:39:32 +00:00
test.Restart();
2021-12-13 11:02:26 +00:00
2021-12-22 10:39:32 +00:00
test.SendShowMetaMsg(TSDB_MGMT_TABLE_USER, "");
CHECK_META("show users", 4);
2021-12-13 11:02:26 +00:00
2021-12-22 10:39:32 +00:00
test.SendShowRetrieveMsg();
EXPECT_EQ(test.GetShowRows(), 2);
2021-12-13 11:02:26 +00:00
CheckBinary("root", TSDB_USER_LEN);
CheckBinary("u2", TSDB_USER_LEN);
CheckBinary("super", 10);
CheckBinary("normal", 10);
CheckTimestamp();
CheckTimestamp();
CheckBinary("root", TSDB_USER_LEN);
CheckBinary("root", TSDB_USER_LEN);
2021-12-31 10:40:49 +00:00
}