user.cpp 18.3 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

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

  {
S
Shengliang Guan 已提交
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
    SCreateDbReq createReq = {0};
    strcpy(createReq.db, "1.d2");
    createReq.numOfVgroups = 2;
    createReq.cacheBlockSize = 16;
    createReq.totalBlocks = 10;
    createReq.daysPerFile = 10;
    createReq.daysToKeep0 = 3650;
    createReq.daysToKeep1 = 3650;
    createReq.daysToKeep2 = 3650;
    createReq.minRows = 100;
    createReq.maxRows = 4096;
    createReq.commitTime = 3600;
    createReq.fsyncPeriod = 3000;
    createReq.walLevel = 1;
    createReq.precision = 0;
    createReq.compression = 2;
    createReq.replications = 1;
    createReq.quorum = 1;
    createReq.update = 0;
    createReq.cacheLastRow = 0;
    createReq.ignoreExist = 1;

    int32_t contLen = tSerializeSCreateDbReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateDbReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359

    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 已提交
360
    int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq);
S
Shengliang Guan 已提交
361
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
362
    tSerializeSAlterUserReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
363 364 365 366 367 368 369 370 371 372 373 374 375

    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 已提交
376
    int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq);
S
Shengliang Guan 已提交
377
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
378
    tSerializeSAlterUserReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
379 380 381 382 383 384

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

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

    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 已提交
397
    tDeserializeSGetUserAuthRsp(pRsp->pCont, pRsp->contLen, &authRsp);
S
Shengliang Guan 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410 411
    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 已提交
412 413 414 415 416 417 418
  {
    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 已提交
419
    int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq);
S
Shengliang Guan 已提交
420
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
421
    tSerializeSAlterUserReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
422 423 424 425 426 427 428 429 430 431

    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 已提交
432
    int32_t contLen = tSerializeSDropUserReq(NULL, 0, &dropReq);
S
Shengliang Guan 已提交
433
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
434
    tSerializeSDropUserReq(pReq, contLen, &dropReq);
S
Shengliang Guan 已提交
435 436 437 438 439 440 441 442 443

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

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

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

S
Shengliang Guan 已提交
455 456 457
    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 已提交
458 459 460
  }

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

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

S
Shengliang Guan 已提交
468 469 470
    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 已提交
471 472
  }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
617
  // restart
S
Shengliang Guan 已提交
618
  test.Restart();
S
Shengliang Guan 已提交
619

dengyihao's avatar
fix bug  
dengyihao 已提交
620
  taosMsleep(1000);
S
Shengliang Guan 已提交
621
  test.SendShowMetaReq(TSDB_MGMT_TABLE_USER, "");
S
Shengliang Guan 已提交
622
  CHECK_META("show users", 4);
S
Shengliang Guan 已提交
623

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

  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);
dengyihao's avatar
fix bug  
dengyihao 已提交
635
}