user.cpp 8.1 KB
Newer Older
S
Shengliang Guan 已提交
1
/**
S
Shengliang Guan 已提交
2
 * @file user.cpp
S
Shengliang Guan 已提交
3
 * @author slguan (slguan@taosdata.com)
S
Shengliang Guan 已提交
4 5
 * @brief MNODE module user tests
 * @version 1.0
S
Shengliang Guan 已提交
6
 * @date 2022-01-04
S
Shengliang Guan 已提交
7
 *
S
Shengliang Guan 已提交
8
 * @copyright Copyright (c) 2022
S
Shengliang Guan 已提交
9 10 11
 *
 */

S
Shengliang Guan 已提交
12
#include "base.h"
S
Shengliang Guan 已提交
13 14 15

class DndTestUser : public ::testing::Test {
 protected:
S
Shengliang Guan 已提交
16
  static void SetUpTestSuite() { test.Init("/tmp/mnode_test_user", 9011); }
S
Shengliang Guan 已提交
17
  static void TearDownTestSuite() { test.Cleanup(); }
S
Shengliang Guan 已提交
18

S
Shengliang Guan 已提交
19
  static Testbase test;
S
Shengliang Guan 已提交
20

S
Shengliang Guan 已提交
21 22 23 24 25
 public:
  void SetUp() override {}
  void TearDown() override {}
};

S
Shengliang Guan 已提交
26
Testbase DndTestUser::test;
S
Shengliang Guan 已提交
27

S
Shengliang Guan 已提交
28
TEST_F(DndTestUser, 01_ShowUser) {
S
Shengliang Guan 已提交
29 30 31 32 33 34 35 36 37 38
  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);
S
Shengliang Guan 已提交
39 40 41 42 43

  CheckBinary("root", TSDB_USER_LEN);
  CheckBinary("super", 10);
  CheckTimestamp();
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
44 45
}

S
Shengliang Guan 已提交
46 47
TEST_F(DndTestUser, 02_Create_User) {
  {
S
Shengliang Guan 已提交
48
    int32_t contLen = sizeof(SCreateUserReq);
S
Shengliang Guan 已提交
49

S
Shengliang Guan 已提交
50
    SCreateUserReq* pReq = (SCreateUserReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
51 52 53 54 55 56 57 58 59
    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);
  }

  {
S
Shengliang Guan 已提交
60
    int32_t contLen = sizeof(SCreateUserReq);
S
Shengliang Guan 已提交
61

S
Shengliang Guan 已提交
62
    SCreateUserReq* pReq = (SCreateUserReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
63 64 65 66 67 68 69 70 71
    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);
  }

  {
S
Shengliang Guan 已提交
72
    int32_t contLen = sizeof(SCreateUserReq);
S
Shengliang Guan 已提交
73

S
Shengliang Guan 已提交
74
    SCreateUserReq* pReq = (SCreateUserReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
75 76 77 78 79 80 81 82 83
    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);
  }

  {
S
Shengliang Guan 已提交
84
    int32_t contLen = sizeof(SCreateUserReq);
S
Shengliang Guan 已提交
85

S
Shengliang Guan 已提交
86
    SCreateUserReq* pReq = (SCreateUserReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    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) {
  {
S
Shengliang Guan 已提交
104
    int32_t contLen = sizeof(SAlterUserReq);
S
Shengliang Guan 已提交
105

S
Shengliang Guan 已提交
106
    SAlterUserReq* pReq = (SAlterUserReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
107 108 109 110 111 112 113 114 115
    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);
  }

  {
S
Shengliang Guan 已提交
116
    int32_t contLen = sizeof(SAlterUserReq);
S
Shengliang Guan 已提交
117

S
Shengliang Guan 已提交
118
    SAlterUserReq* pReq = (SAlterUserReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
119 120 121 122 123 124 125 126 127
    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);
  }

  {
S
Shengliang Guan 已提交
128
    int32_t contLen = sizeof(SAlterUserReq);
S
Shengliang Guan 已提交
129

S
Shengliang Guan 已提交
130
    SAlterUserReq* pReq = (SAlterUserReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
131 132 133 134 135 136 137 138 139
    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);
  }

  {
S
Shengliang Guan 已提交
140
    int32_t contLen = sizeof(SAlterUserReq);
S
Shengliang Guan 已提交
141

S
Shengliang Guan 已提交
142
    SAlterUserReq* pReq = (SAlterUserReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
143 144 145 146 147 148 149 150 151 152 153
    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) {
  {
S
Shengliang Guan 已提交
154
    int32_t contLen = sizeof(SDropUserReq);
S
Shengliang Guan 已提交
155

S
Shengliang Guan 已提交
156
    SDropUserReq* pReq = (SDropUserReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
157 158 159 160 161 162 163 164
    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);
  }

  {
S
Shengliang Guan 已提交
165
    int32_t contLen = sizeof(SDropUserReq);
S
Shengliang Guan 已提交
166

S
Shengliang Guan 已提交
167
    SDropUserReq* pReq = (SDropUserReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
168 169 170 171 172 173 174 175
    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);
  }

  {
S
Shengliang Guan 已提交
176
    int32_t contLen = sizeof(SDropUserReq);
S
Shengliang Guan 已提交
177

S
Shengliang Guan 已提交
178
    SDropUserReq* pReq = (SDropUserReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192
    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);
}

S
Shengliang Guan 已提交
193
TEST_F(DndTestUser, 05_Create_Drop_Alter_User) {
S
Shengliang Guan 已提交
194
  {
S
Shengliang Guan 已提交
195
    int32_t contLen = sizeof(SCreateUserReq);
S
Shengliang Guan 已提交
196

S
Shengliang Guan 已提交
197
    SCreateUserReq* pReq = (SCreateUserReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
198 199 200
    strcpy(pReq->user, "u1");
    strcpy(pReq->pass, "p1");

H
Hongze Cheng 已提交
201
    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_CREATE_USER, pReq, contLen);
S
Shengliang Guan 已提交
202 203 204
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, 0);
  }
S
Shengliang Guan 已提交
205

S
Shengliang Guan 已提交
206
  {
S
Shengliang Guan 已提交
207
    int32_t contLen = sizeof(SCreateUserReq);
S
Shengliang Guan 已提交
208

S
Shengliang Guan 已提交
209
    SCreateUserReq* pReq = (SCreateUserReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
210 211
    strcpy(pReq->user, "u2");
    strcpy(pReq->pass, "p2");
S
Shengliang Guan 已提交
212

H
Hongze Cheng 已提交
213
    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_CREATE_USER, pReq, contLen);
S
Shengliang Guan 已提交
214 215 216
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, 0);
  }
S
Shengliang Guan 已提交
217

S
Shengliang Guan 已提交
218 219 220 221 222 223
  test.SendShowMetaMsg(TSDB_MGMT_TABLE_USER, "");
  CHECK_META("show users", 4);

  test.SendShowRetrieveMsg();
  EXPECT_EQ(test.GetShowRows(), 3);

S
Shengliang Guan 已提交
224 225
  CheckBinary("u1", TSDB_USER_LEN);
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
226
  CheckBinary("u2", TSDB_USER_LEN);
S
Shengliang Guan 已提交
227 228
  CheckBinary("normal", 10);
  CheckBinary("super", 10);
S
Shengliang Guan 已提交
229 230
  CheckBinary("normal", 10);
  CheckTimestamp();
S
Shengliang Guan 已提交
231 232 233 234
  CheckTimestamp();
  CheckTimestamp();
  CheckBinary("root", TSDB_USER_LEN);
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
235
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
236

S
Shengliang Guan 已提交
237
  {
S
Shengliang Guan 已提交
238
    int32_t contLen = sizeof(SAlterUserReq);
S
Shengliang Guan 已提交
239

S
Shengliang Guan 已提交
240
    SAlterUserReq* pReq = (SAlterUserReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
241 242
    strcpy(pReq->user, "u1");
    strcpy(pReq->pass, "p2");
S
Shengliang Guan 已提交
243

H
Hongze Cheng 已提交
244
    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_ALTER_USER, pReq, contLen);
S
Shengliang Guan 已提交
245 246 247
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, 0);
  }
S
Shengliang Guan 已提交
248 249 250 251 252 253 254

  test.SendShowMetaMsg(TSDB_MGMT_TABLE_USER, "");
  CHECK_META("show users", 4);

  test.SendShowRetrieveMsg();
  EXPECT_EQ(test.GetShowRows(), 3);

S
Shengliang Guan 已提交
255 256
  CheckBinary("u1", TSDB_USER_LEN);
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
257
  CheckBinary("u2", TSDB_USER_LEN);
S
Shengliang Guan 已提交
258 259
  CheckBinary("normal", 10);
  CheckBinary("super", 10);
S
Shengliang Guan 已提交
260 261
  CheckBinary("normal", 10);
  CheckTimestamp();
S
Shengliang Guan 已提交
262 263 264 265
  CheckTimestamp();
  CheckTimestamp();
  CheckBinary("root", TSDB_USER_LEN);
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
266
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
267

S
Shengliang Guan 已提交
268
  {
S
Shengliang Guan 已提交
269
    int32_t contLen = sizeof(SDropUserReq);
S
Shengliang Guan 已提交
270

S
Shengliang Guan 已提交
271
    SDropUserReq* pReq = (SDropUserReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
272
    strcpy(pReq->user, "u1");
S
Shengliang Guan 已提交
273

H
Hongze Cheng 已提交
274
    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_DROP_USER, pReq, contLen);
S
Shengliang Guan 已提交
275 276 277
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, 0);
  }
S
Shengliang Guan 已提交
278 279 280 281 282 283 284

  test.SendShowMetaMsg(TSDB_MGMT_TABLE_USER, "");
  CHECK_META("show users", 4);

  test.SendShowRetrieveMsg();
  EXPECT_EQ(test.GetShowRows(), 2);

S
Shengliang Guan 已提交
285
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
286
  CheckBinary("u2", TSDB_USER_LEN);
S
Shengliang Guan 已提交
287
  CheckBinary("super", 10);
S
Shengliang Guan 已提交
288 289
  CheckBinary("normal", 10);
  CheckTimestamp();
S
Shengliang Guan 已提交
290 291
  CheckTimestamp();
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
292 293
  CheckBinary("root", TSDB_USER_LEN);

S
Shengliang Guan 已提交
294
  // restart
S
Shengliang Guan 已提交
295
  test.Restart();
S
Shengliang Guan 已提交
296

S
Shengliang Guan 已提交
297 298
  test.SendShowMetaMsg(TSDB_MGMT_TABLE_USER, "");
  CHECK_META("show users", 4);
S
Shengliang Guan 已提交
299

S
Shengliang Guan 已提交
300 301
  test.SendShowRetrieveMsg();
  EXPECT_EQ(test.GetShowRows(), 2);
S
Shengliang Guan 已提交
302 303 304 305 306 307 308 309 310

  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);
S
Shengliang Guan 已提交
311
}