user.cpp 18.2 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 "sut.h"
S
Shengliang Guan 已提交
13

S
Shengliang Guan 已提交
14
class MndTestUser : public ::testing::Test {
S
Shengliang Guan 已提交
15
 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 MndTestUser::test;
S
Shengliang Guan 已提交
27

S
Shengliang Guan 已提交
28
TEST_F(MndTestUser, 01_Show_User) {
S
Shengliang Guan 已提交
29
  test.SendShowMetaReq(TSDB_MGMT_TABLE_USER, "");
S
Shengliang Guan 已提交
30 31 32 33 34 35 36
  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");

S
Shengliang Guan 已提交
37
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
38
  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
TEST_F(MndTestUser, 02_Create_User) {
S
Shengliang Guan 已提交
47
  {
S
Shengliang Guan 已提交
48 49 50
    SCreateUserReq createReq = {0};
    strcpy(createReq.user, "");
    strcpy(createReq.pass, "p1");
S
Shengliang Guan 已提交
51

S
Shengliang Guan 已提交
52
    int32_t contLen = tSerializeSCreateUserReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
53
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
54
    tSerializeSCreateUserReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
55

S
Shengliang Guan 已提交
56 57 58
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_USER_FORMAT);
S
Shengliang Guan 已提交
59 60 61
  }

  {
S
Shengliang Guan 已提交
62 63 64
    SCreateUserReq createReq = {0};
    strcpy(createReq.user, "u1");
    strcpy(createReq.pass, "");
S
Shengliang Guan 已提交
65

S
Shengliang Guan 已提交
66
    int32_t contLen = tSerializeSCreateUserReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
67
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
68
    tSerializeSCreateUserReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
69

S
Shengliang Guan 已提交
70 71 72
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_PASS_FORMAT);
S
Shengliang Guan 已提交
73 74 75
  }

  {
S
Shengliang Guan 已提交
76 77 78
    SCreateUserReq createReq = {0};
    strcpy(createReq.user, "root");
    strcpy(createReq.pass, "1");
S
Shengliang Guan 已提交
79

S
Shengliang Guan 已提交
80
    int32_t contLen = tSerializeSCreateUserReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
81
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
82
    tSerializeSCreateUserReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
83

S
Shengliang Guan 已提交
84 85 86
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_USER_ALREADY_EXIST);
S
Shengliang Guan 已提交
87 88 89
  }

  {
S
Shengliang Guan 已提交
90 91 92
    SCreateUserReq createReq = {0};
    strcpy(createReq.user, "u1");
    strcpy(createReq.pass, "p1");
S
Shengliang Guan 已提交
93

S
Shengliang Guan 已提交
94
    int32_t contLen = tSerializeSCreateUserReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
95
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
96
    tSerializeSCreateUserReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
97

S
Shengliang Guan 已提交
98 99 100
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113

    test.SendShowMetaReq(TSDB_MGMT_TABLE_USER, "");
    test.SendShowRetrieveReq();
    EXPECT_EQ(test.GetShowRows(), 2);

    CheckBinary("u1", TSDB_USER_LEN);
    CheckBinary("root", TSDB_USER_LEN);
    CheckBinary("normal", 10);
    CheckBinary("super", 10);
    CheckTimestamp();
    CheckTimestamp();
    CheckBinary("root", TSDB_USER_LEN);
    CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
114 115
  }

S
Shengliang Guan 已提交
116 117 118
  {
    SDropUserReq dropReq = {0};
    strcpy(dropReq.user, "u1");
S
Shengliang Guan 已提交
119

S
Shengliang Guan 已提交
120
    int32_t contLen = tSerializeSDropUserReq(NULL, 0, &dropReq);
S
Shengliang Guan 已提交
121
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
122
    tSerializeSDropUserReq(pReq, contLen, &dropReq);
S
Shengliang Guan 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);

    test.SendShowMetaReq(TSDB_MGMT_TABLE_USER, "");
    test.SendShowRetrieveReq();
    EXPECT_EQ(test.GetShowRows(), 1);
  }

  {
    SCreateUserReq createReq = {0};
    strcpy(createReq.user, "u2");
    strcpy(createReq.pass, "p1");
    createReq.superUser = 1;

S
Shengliang Guan 已提交
139
    int32_t contLen = tSerializeSCreateUserReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
140
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
141
    tSerializeSCreateUserReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);

    test.SendShowMetaReq(TSDB_MGMT_TABLE_USER, "");
    test.SendShowRetrieveReq();
    EXPECT_EQ(test.GetShowRows(), 2);

    CheckBinary("root", TSDB_USER_LEN);
    CheckBinary("u2", TSDB_USER_LEN);
    CheckBinary("super", 10);
    CheckBinary("super", 10);
    CheckTimestamp();
    CheckTimestamp();
    CheckBinary("root", TSDB_USER_LEN);
    CheckBinary("root", TSDB_USER_LEN);
  }

  {
    SDropUserReq dropReq = {0};
    strcpy(dropReq.user, "u2");

S
Shengliang Guan 已提交
165
    int32_t contLen = tSerializeSDropUserReq(NULL, 0, &dropReq);
S
Shengliang Guan 已提交
166
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
167
    tSerializeSDropUserReq(pReq, contLen, &dropReq);
S
Shengliang Guan 已提交
168 169 170 171 172 173 174 175 176

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);

    test.SendShowMetaReq(TSDB_MGMT_TABLE_USER, "");
    test.SendShowRetrieveReq();
    EXPECT_EQ(test.GetShowRows(), 1);
  }
S
Shengliang Guan 已提交
177 178
}

S
Shengliang Guan 已提交
179
TEST_F(MndTestUser, 03_Alter_User) {
S
Shengliang Guan 已提交
180 181 182 183 184 185
  {
    SCreateUserReq createReq = {0};
    strcpy(createReq.user, "u3");
    strcpy(createReq.pass, "p1");
    createReq.superUser = 1;

S
Shengliang Guan 已提交
186
    int32_t contLen = tSerializeSCreateUserReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
187
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
188
    tSerializeSCreateUserReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
189 190 191 192 193 194 195 196 197 198

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);

    test.SendShowMetaReq(TSDB_MGMT_TABLE_USER, "");
    test.SendShowRetrieveReq();
    EXPECT_EQ(test.GetShowRows(), 2);
  }

S
Shengliang Guan 已提交
199
  {
S
Shengliang Guan 已提交
200 201 202 203
    SAlterUserReq alterReq = {0};
    alterReq.alterType = TSDB_ALTER_USER_PASSWD;
    strcpy(alterReq.user, "");
    strcpy(alterReq.pass, "p1");
S
Shengliang Guan 已提交
204

S
Shengliang Guan 已提交
205
    int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq);
S
Shengliang Guan 已提交
206
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
207
    tSerializeSAlterUserReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
208

S
Shengliang Guan 已提交
209 210 211
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_ALTER_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_USER_FORMAT);
S
Shengliang Guan 已提交
212 213 214
  }

  {
S
Shengliang Guan 已提交
215 216
    SAlterUserReq alterReq = {0};
    alterReq.alterType = TSDB_ALTER_USER_PASSWD;
S
Shengliang Guan 已提交
217
    strcpy(alterReq.user, "u3");
S
Shengliang Guan 已提交
218
    strcpy(alterReq.pass, "");
S
Shengliang Guan 已提交
219

S
Shengliang Guan 已提交
220
    int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq);
S
Shengliang Guan 已提交
221
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
222
    tSerializeSAlterUserReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
223

S
Shengliang Guan 已提交
224 225 226
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_ALTER_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_PASS_FORMAT);
S
Shengliang Guan 已提交
227 228 229
  }

  {
S
Shengliang Guan 已提交
230 231 232 233
    SAlterUserReq alterReq = {0};
    alterReq.alterType = TSDB_ALTER_USER_PASSWD;
    strcpy(alterReq.user, "u4");
    strcpy(alterReq.pass, "1");
S
Shengliang Guan 已提交
234

S
Shengliang Guan 已提交
235
    int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq);
S
Shengliang Guan 已提交
236
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
237
    tSerializeSAlterUserReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
238

S
Shengliang Guan 已提交
239 240 241
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_ALTER_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_USER_NOT_EXIST);
S
Shengliang Guan 已提交
242 243 244
  }

  {
S
Shengliang Guan 已提交
245 246
    SAlterUserReq alterReq = {0};
    alterReq.alterType = TSDB_ALTER_USER_PASSWD;
S
Shengliang Guan 已提交
247 248 249
    strcpy(alterReq.user, "u3");
    strcpy(alterReq.pass, "1");

S
Shengliang Guan 已提交
250
    int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq);
S
Shengliang Guan 已提交
251
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
252
    tSerializeSAlterUserReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_ALTER_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
  }

  {
    SAlterUserReq alterReq = {0};
    alterReq.alterType = TSDB_ALTER_USER_SUPERUSER;
    strcpy(alterReq.user, "u3");
    strcpy(alterReq.pass, "1");
    alterReq.superUser = 1;

S
Shengliang Guan 已提交
266
    int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq);
S
Shengliang Guan 已提交
267
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
268
    tSerializeSAlterUserReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
269 270 271 272 273 274 275 276 277 278

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_ALTER_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
  }

  {
    SAlterUserReq alterReq = {0};
    alterReq.alterType = TSDB_ALTER_USER_CLEAR_WRITE_DB;
    strcpy(alterReq.user, "u3");
S
Shengliang Guan 已提交
279
    strcpy(alterReq.pass, "1");
S
Shengliang Guan 已提交
280

S
Shengliang Guan 已提交
281
    int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq);
S
Shengliang Guan 已提交
282
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
283
    tSerializeSAlterUserReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
284

S
Shengliang Guan 已提交
285 286 287
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_ALTER_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
288
  }
S
Shengliang Guan 已提交
289 290 291 292 293 294 295

  {
    SAlterUserReq alterReq = {0};
    alterReq.alterType = TSDB_ALTER_USER_CLEAR_READ_DB;
    strcpy(alterReq.user, "u3");
    strcpy(alterReq.pass, "1");

S
Shengliang Guan 已提交
296
    int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq);
S
Shengliang Guan 已提交
297
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
298
    tSerializeSAlterUserReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_ALTER_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
  }

  {
    SAlterUserReq alterReq = {0};
    alterReq.alterType = TSDB_ALTER_USER_ADD_READ_DB;
    strcpy(alterReq.user, "u3");
    strcpy(alterReq.pass, "1");
    strcpy(alterReq.dbname, "d1");

S
Shengliang Guan 已提交
312
    int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq);
S
Shengliang Guan 已提交
313
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
314
    tSerializeSAlterUserReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_ALTER_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_DB_NOT_EXIST);
  }

  {
    int32_t contLen = sizeof(SCreateDbReq);

    SCreateDbReq* pReq = (SCreateDbReq*)rpcMallocCont(contLen);
    strcpy(pReq->db, "1.d2");
    pReq->numOfVgroups = htonl(2);
    pReq->cacheBlockSize = htonl(16);
    pReq->totalBlocks = htonl(10);
    pReq->daysPerFile = htonl(10);
    pReq->daysToKeep0 = htonl(3650);
    pReq->daysToKeep1 = htonl(3650);
    pReq->daysToKeep2 = htonl(3650);
    pReq->minRows = htonl(100);
    pReq->maxRows = htonl(4096);
    pReq->commitTime = htonl(3600);
    pReq->fsyncPeriod = htonl(3000);
    pReq->walLevel = 1;
    pReq->precision = 0;
    pReq->compression = 2;
    pReq->replications = 1;
    pReq->quorum = 1;
    pReq->update = 0;
    pReq->cacheLastRow = 0;
    pReq->ignoreExist = 1;

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_DB, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
  }

  {
    SAlterUserReq alterReq = {0};
    alterReq.alterType = TSDB_ALTER_USER_ADD_READ_DB;
    strcpy(alterReq.user, "u3");
    strcpy(alterReq.pass, "1");
    strcpy(alterReq.dbname, "1.d2");

S
Shengliang Guan 已提交
358
    int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq);
S
Shengliang Guan 已提交
359
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
360
    tSerializeSAlterUserReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_ALTER_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
  }

  {
    SAlterUserReq alterReq = {0};
    alterReq.alterType = TSDB_ALTER_USER_ADD_READ_DB;
    strcpy(alterReq.user, "u3");
    strcpy(alterReq.pass, "1");
    strcpy(alterReq.dbname, "1.d2");

S
Shengliang Guan 已提交
374
    int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq);
S
Shengliang Guan 已提交
375
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
376
    tSerializeSAlterUserReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
377 378 379 380 381 382

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_ALTER_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
  }

S
Shengliang Guan 已提交
383 384 385
  {
    SGetUserAuthReq authReq = {0};
    strcpy(authReq.user, "u3");
S
Shengliang Guan 已提交
386
    int32_t contLen = tSerializeSGetUserAuthReq(NULL, 0, &authReq);
S
Shengliang Guan 已提交
387
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
388
    tSerializeSGetUserAuthReq(pReq, contLen, &authReq);
S
Shengliang Guan 已提交
389 390 391 392 393 394

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_GET_USER_AUTH, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);

    SGetUserAuthRsp authRsp = {0};
S
Shengliang Guan 已提交
395
    tDeserializeSGetUserAuthRsp(pRsp->pCont, pRsp->contLen, &authRsp);
S
Shengliang Guan 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408 409
    EXPECT_STREQ(authRsp.user, "u3");
    EXPECT_EQ(authRsp.superAuth, 1);
    int32_t numOfReadDbs = taosHashGetSize(authRsp.readDbs);
    int32_t numOfWriteDbs = taosHashGetSize(authRsp.writeDbs);
    EXPECT_EQ(numOfReadDbs, 1);
    EXPECT_EQ(numOfWriteDbs, 0);

    char* dbname = (char*)taosHashGet(authRsp.readDbs, "1.d2", 5);
    EXPECT_STREQ(dbname, "1.d2");

    taosHashCleanup(authRsp.readDbs);
    taosHashCleanup(authRsp.writeDbs);
  }

S
Shengliang Guan 已提交
410 411 412 413 414 415 416
  {
    SAlterUserReq alterReq = {0};
    alterReq.alterType = TSDB_ALTER_USER_REMOVE_READ_DB;
    strcpy(alterReq.user, "u3");
    strcpy(alterReq.pass, "1");
    strcpy(alterReq.dbname, "1.d2");

S
Shengliang Guan 已提交
417
    int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq);
S
Shengliang Guan 已提交
418
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
419
    tSerializeSAlterUserReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
420 421 422 423 424 425 426 427 428 429

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_ALTER_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
  }

  {
    SDropUserReq dropReq = {0};
    strcpy(dropReq.user, "u3");

S
Shengliang Guan 已提交
430
    int32_t contLen = tSerializeSDropUserReq(NULL, 0, &dropReq);
S
Shengliang Guan 已提交
431
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
432
    tSerializeSDropUserReq(pReq, contLen, &dropReq);
S
Shengliang Guan 已提交
433 434 435 436 437 438 439 440 441

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);

    test.SendShowMetaReq(TSDB_MGMT_TABLE_USER, "");
    test.SendShowRetrieveReq();
    EXPECT_EQ(test.GetShowRows(), 1);
  }
S
Shengliang Guan 已提交
442 443
}

S
Shengliang Guan 已提交
444
TEST_F(MndTestUser, 05_Drop_User) {
S
Shengliang Guan 已提交
445
  {
S
Shengliang Guan 已提交
446 447
    SDropUserReq dropReq = {0};
    strcpy(dropReq.user, "");
S
Shengliang Guan 已提交
448

S
Shengliang Guan 已提交
449
    int32_t contLen = tSerializeSDropUserReq(NULL, 0, &dropReq);
S
Shengliang Guan 已提交
450
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
451
    tSerializeSDropUserReq(pReq, contLen, &dropReq);
S
Shengliang Guan 已提交
452

S
Shengliang Guan 已提交
453 454 455
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_USER_FORMAT);
S
Shengliang Guan 已提交
456 457 458
  }

  {
S
Shengliang Guan 已提交
459 460
    SDropUserReq dropReq = {0};
    strcpy(dropReq.user, "u4");
S
Shengliang Guan 已提交
461

S
Shengliang Guan 已提交
462
    int32_t contLen = tSerializeSDropUserReq(NULL, 0, &dropReq);
S
Shengliang Guan 已提交
463
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
464
    tSerializeSDropUserReq(pReq, contLen, &dropReq);
S
Shengliang Guan 已提交
465

S
Shengliang Guan 已提交
466 467 468
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_USER_NOT_EXIST);
S
Shengliang Guan 已提交
469 470
  }

S
Shengliang Guan 已提交
471 472 473 474 475
  {
    SCreateUserReq createReq = {0};
    strcpy(createReq.user, "u1");
    strcpy(createReq.pass, "p1");

S
Shengliang Guan 已提交
476
    int32_t contLen = tSerializeSCreateUserReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
477
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
478
    tSerializeSCreateUserReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
479 480 481 482 483 484

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
  }

S
Shengliang Guan 已提交
485
  {
S
Shengliang Guan 已提交
486 487
    SDropUserReq dropReq = {0};
    strcpy(dropReq.user, "u1");
S
Shengliang Guan 已提交
488

S
Shengliang Guan 已提交
489
    int32_t contLen = tSerializeSDropUserReq(NULL, 0, &dropReq);
S
Shengliang Guan 已提交
490
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
491
    tSerializeSDropUserReq(pReq, contLen, &dropReq);
S
Shengliang Guan 已提交
492

S
Shengliang Guan 已提交
493 494 495
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
496 497
  }

S
Shengliang Guan 已提交
498
  test.SendShowMetaReq(TSDB_MGMT_TABLE_USER, "");
S
Shengliang Guan 已提交
499 500
  CHECK_META("show users", 4);

S
Shengliang Guan 已提交
501
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
502 503 504
  EXPECT_EQ(test.GetShowRows(), 1);
}

S
Shengliang Guan 已提交
505
TEST_F(MndTestUser, 06_Create_Drop_Alter_User) {
S
Shengliang Guan 已提交
506
  {
S
Shengliang Guan 已提交
507 508 509
    SCreateUserReq createReq = {0};
    strcpy(createReq.user, "u1");
    strcpy(createReq.pass, "p1");
S
Shengliang Guan 已提交
510

S
Shengliang Guan 已提交
511
    int32_t contLen = tSerializeSCreateUserReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
512
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
513
    tSerializeSCreateUserReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
514

S
Shengliang Guan 已提交
515 516 517
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
518
  }
S
Shengliang Guan 已提交
519

S
Shengliang Guan 已提交
520
  {
S
Shengliang Guan 已提交
521 522 523
    SCreateUserReq createReq = {0};
    strcpy(createReq.user, "u2");
    strcpy(createReq.pass, "p2");
S
Shengliang Guan 已提交
524

S
Shengliang Guan 已提交
525
    int32_t contLen = tSerializeSCreateUserReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
526
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
527
    tSerializeSCreateUserReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
528

S
Shengliang Guan 已提交
529 530 531
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
532
  }
S
Shengliang Guan 已提交
533

S
Shengliang Guan 已提交
534
  test.SendShowMetaReq(TSDB_MGMT_TABLE_USER, "");
S
Shengliang Guan 已提交
535 536
  CHECK_META("show users", 4);

S
Shengliang Guan 已提交
537
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
538 539
  EXPECT_EQ(test.GetShowRows(), 3);

S
Shengliang Guan 已提交
540 541
  CheckBinary("u1", TSDB_USER_LEN);
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
542
  CheckBinary("u2", TSDB_USER_LEN);
S
Shengliang Guan 已提交
543 544
  CheckBinary("normal", 10);
  CheckBinary("super", 10);
S
Shengliang Guan 已提交
545 546
  CheckBinary("normal", 10);
  CheckTimestamp();
S
Shengliang Guan 已提交
547 548 549 550
  CheckTimestamp();
  CheckTimestamp();
  CheckBinary("root", TSDB_USER_LEN);
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
551
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
552

S
Shengliang Guan 已提交
553
  {
S
Shengliang Guan 已提交
554 555 556 557
    SAlterUserReq alterReq = {0};
    alterReq.alterType = TSDB_ALTER_USER_PASSWD;
    strcpy(alterReq.user, "u1");
    strcpy(alterReq.pass, "p2");
S
Shengliang Guan 已提交
558

S
Shengliang Guan 已提交
559
    int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq);
S
Shengliang Guan 已提交
560
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
561
    tSerializeSAlterUserReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
562

S
Shengliang Guan 已提交
563 564 565
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_ALTER_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
566
  }
S
Shengliang Guan 已提交
567

S
Shengliang Guan 已提交
568
  test.SendShowMetaReq(TSDB_MGMT_TABLE_USER, "");
S
Shengliang Guan 已提交
569 570
  CHECK_META("show users", 4);

S
Shengliang Guan 已提交
571
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
572 573
  EXPECT_EQ(test.GetShowRows(), 3);

S
Shengliang Guan 已提交
574 575
  CheckBinary("u1", TSDB_USER_LEN);
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
576
  CheckBinary("u2", TSDB_USER_LEN);
S
Shengliang Guan 已提交
577 578
  CheckBinary("normal", 10);
  CheckBinary("super", 10);
S
Shengliang Guan 已提交
579 580
  CheckBinary("normal", 10);
  CheckTimestamp();
S
Shengliang Guan 已提交
581 582 583 584
  CheckTimestamp();
  CheckTimestamp();
  CheckBinary("root", TSDB_USER_LEN);
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
585
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
586

S
Shengliang Guan 已提交
587
  {
S
Shengliang Guan 已提交
588 589
    SDropUserReq dropReq = {0};
    strcpy(dropReq.user, "u1");
S
Shengliang Guan 已提交
590

S
Shengliang Guan 已提交
591
    int32_t contLen = tSerializeSDropUserReq(NULL, 0, &dropReq);
S
Shengliang Guan 已提交
592
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
593
    tSerializeSDropUserReq(pReq, contLen, &dropReq);
S
Shengliang Guan 已提交
594

S
Shengliang Guan 已提交
595 596 597
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
598
  }
S
Shengliang Guan 已提交
599

S
Shengliang Guan 已提交
600
  test.SendShowMetaReq(TSDB_MGMT_TABLE_USER, "");
S
Shengliang Guan 已提交
601 602
  CHECK_META("show users", 4);

S
Shengliang Guan 已提交
603
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
604 605
  EXPECT_EQ(test.GetShowRows(), 2);

S
Shengliang Guan 已提交
606
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
607
  CheckBinary("u2", TSDB_USER_LEN);
S
Shengliang Guan 已提交
608
  CheckBinary("super", 10);
S
Shengliang Guan 已提交
609 610
  CheckBinary("normal", 10);
  CheckTimestamp();
S
Shengliang Guan 已提交
611 612
  CheckTimestamp();
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
613 614
  CheckBinary("root", TSDB_USER_LEN);

S
Shengliang Guan 已提交
615
  // restart
S
Shengliang Guan 已提交
616
  test.Restart();
S
Shengliang Guan 已提交
617

S
Shengliang Guan 已提交
618
  test.SendShowMetaReq(TSDB_MGMT_TABLE_USER, "");
S
Shengliang Guan 已提交
619
  CHECK_META("show users", 4);
S
Shengliang Guan 已提交
620

S
Shengliang Guan 已提交
621
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
622
  EXPECT_EQ(test.GetShowRows(), 2);
S
Shengliang Guan 已提交
623 624 625 626 627 628 629 630 631

  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 已提交
632
}