user.cpp 15.8 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.SendShowReq(TSDB_MGMT_TABLE_USER, "user_users", "");
S
Shengliang Guan 已提交
30
  EXPECT_EQ(test.GetShowRows(), 1);
S
Shengliang Guan 已提交
31 32
}

S
Shengliang Guan 已提交
33
TEST_F(MndTestUser, 02_Create_User) {
S
Shengliang Guan 已提交
34
  {
S
Shengliang Guan 已提交
35 36 37
    SCreateUserReq createReq = {0};
    strcpy(createReq.user, "");
    strcpy(createReq.pass, "p1");
S
Shengliang Guan 已提交
38

S
Shengliang Guan 已提交
39
    int32_t contLen = tSerializeSCreateUserReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
40
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
41
    tSerializeSCreateUserReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
42

S
Shengliang Guan 已提交
43 44 45
    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 已提交
46 47 48
  }

  {
S
Shengliang Guan 已提交
49 50 51
    SCreateUserReq createReq = {0};
    strcpy(createReq.user, "u1");
    strcpy(createReq.pass, "");
S
Shengliang Guan 已提交
52

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

S
Shengliang Guan 已提交
57 58 59
    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 已提交
60 61 62
  }

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

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

S
Shengliang Guan 已提交
71 72 73
    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 已提交
74 75 76
  }

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

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

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

S
Shengliang Guan 已提交
89
    test.SendShowReq(TSDB_MGMT_TABLE_USER, "user_users", "");
S
Shengliang Guan 已提交
90
    EXPECT_EQ(test.GetShowRows(), 2);
S
Shengliang Guan 已提交
91 92
  }

S
Shengliang Guan 已提交
93 94 95
  {
    SDropUserReq dropReq = {0};
    strcpy(dropReq.user, "u1");
S
Shengliang Guan 已提交
96

S
Shengliang Guan 已提交
97
    int32_t contLen = tSerializeSDropUserReq(NULL, 0, &dropReq);
S
Shengliang Guan 已提交
98
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
99
    tSerializeSDropUserReq(pReq, contLen, &dropReq);
S
Shengliang Guan 已提交
100 101 102 103 104

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

S
Shengliang Guan 已提交
105
    test.SendShowReq(TSDB_MGMT_TABLE_USER, "user_users", "");
S
Shengliang Guan 已提交
106 107 108 109 110 111 112 113 114
    EXPECT_EQ(test.GetShowRows(), 1);
  }

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

S
Shengliang Guan 已提交
115
    int32_t contLen = tSerializeSCreateUserReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
116
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
117
    tSerializeSCreateUserReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
118 119 120 121 122

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

S
Shengliang Guan 已提交
123
    test.SendShowReq(TSDB_MGMT_TABLE_USER, "user_users", "");
S
Shengliang Guan 已提交
124 125 126 127 128 129 130
    EXPECT_EQ(test.GetShowRows(), 2);
  }

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

S
Shengliang Guan 已提交
131
    int32_t contLen = tSerializeSDropUserReq(NULL, 0, &dropReq);
S
Shengliang Guan 已提交
132
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
133
    tSerializeSDropUserReq(pReq, contLen, &dropReq);
S
Shengliang Guan 已提交
134 135 136 137 138

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

S
Shengliang Guan 已提交
139
    test.SendShowReq(TSDB_MGMT_TABLE_USER, "user_users", "");
S
Shengliang Guan 已提交
140 141
    EXPECT_EQ(test.GetShowRows(), 1);
  }
S
Shengliang Guan 已提交
142 143
}

S
Shengliang Guan 已提交
144
TEST_F(MndTestUser, 03_Alter_User) {
S
Shengliang Guan 已提交
145 146 147 148 149 150
  {
    SCreateUserReq createReq = {0};
    strcpy(createReq.user, "u3");
    strcpy(createReq.pass, "p1");
    createReq.superUser = 1;

S
Shengliang Guan 已提交
151
    int32_t contLen = tSerializeSCreateUserReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
152
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
153
    tSerializeSCreateUserReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
154 155 156 157 158

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

S
Shengliang Guan 已提交
159
    test.SendShowReq(TSDB_MGMT_TABLE_USER, "user_users", "");
S
Shengliang Guan 已提交
160 161 162
    EXPECT_EQ(test.GetShowRows(), 2);
  }

S
Shengliang Guan 已提交
163
  {
S
Shengliang Guan 已提交
164 165 166 167
    SAlterUserReq alterReq = {0};
    alterReq.alterType = TSDB_ALTER_USER_PASSWD;
    strcpy(alterReq.user, "");
    strcpy(alterReq.pass, "p1");
S
Shengliang Guan 已提交
168

S
Shengliang Guan 已提交
169
    int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq);
S
Shengliang Guan 已提交
170
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
171
    tSerializeSAlterUserReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
172

S
Shengliang Guan 已提交
173 174 175
    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 已提交
176 177 178
  }

  {
S
Shengliang Guan 已提交
179 180
    SAlterUserReq alterReq = {0};
    alterReq.alterType = TSDB_ALTER_USER_PASSWD;
S
Shengliang Guan 已提交
181
    strcpy(alterReq.user, "u3");
S
Shengliang Guan 已提交
182
    strcpy(alterReq.pass, "");
S
Shengliang Guan 已提交
183

S
Shengliang Guan 已提交
184
    int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq);
S
Shengliang Guan 已提交
185
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
186
    tSerializeSAlterUserReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
187

S
Shengliang Guan 已提交
188 189 190
    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 已提交
191 192 193
  }

  {
S
Shengliang Guan 已提交
194 195 196 197
    SAlterUserReq alterReq = {0};
    alterReq.alterType = TSDB_ALTER_USER_PASSWD;
    strcpy(alterReq.user, "u4");
    strcpy(alterReq.pass, "1");
S
Shengliang Guan 已提交
198

S
Shengliang Guan 已提交
199
    int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq);
S
Shengliang Guan 已提交
200
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
201
    tSerializeSAlterUserReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
202

S
Shengliang Guan 已提交
203 204 205
    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 已提交
206 207 208
  }

  {
S
Shengliang Guan 已提交
209 210
    SAlterUserReq alterReq = {0};
    alterReq.alterType = TSDB_ALTER_USER_PASSWD;
S
Shengliang Guan 已提交
211 212 213
    strcpy(alterReq.user, "u3");
    strcpy(alterReq.pass, "1");

S
Shengliang Guan 已提交
214
    int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq);
S
Shengliang Guan 已提交
215
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
216
    tSerializeSAlterUserReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229

    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 已提交
230
    int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq);
S
Shengliang Guan 已提交
231
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
232
    tSerializeSAlterUserReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
233 234 235 236 237 238 239 240 241 242

    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 已提交
243
    strcpy(alterReq.pass, "1");
S
Shengliang Guan 已提交
244

S
Shengliang Guan 已提交
245
    int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq);
S
Shengliang Guan 已提交
246
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
247
    tSerializeSAlterUserReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
248

S
Shengliang Guan 已提交
249 250 251
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_ALTER_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
252
  }
S
Shengliang Guan 已提交
253 254 255 256 257 258 259

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

S
Shengliang Guan 已提交
260
    int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq);
S
Shengliang Guan 已提交
261
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
262
    tSerializeSAlterUserReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275

    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 已提交
276
    int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq);
S
Shengliang Guan 已提交
277
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
278
    tSerializeSAlterUserReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
279 280 281 282 283 284 285

    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 已提交
286 287 288 289 290
    SCreateDbReq createReq = {0};
    strcpy(createReq.db, "1.d2");
    createReq.numOfVgroups = 2;
    createReq.cacheBlockSize = 16;
    createReq.totalBlocks = 10;
291 292 293 294
    createReq.daysPerFile = 10 * 1440;
    createReq.daysToKeep0 = 3650 * 1440;
    createReq.daysToKeep1 = 3650 * 1440;
    createReq.daysToKeep2 = 3650 * 1440;
S
Shengliang Guan 已提交
295 296 297 298 299 300 301 302
    createReq.minRows = 100;
    createReq.maxRows = 4096;
    createReq.commitTime = 3600;
    createReq.fsyncPeriod = 3000;
    createReq.walLevel = 1;
    createReq.precision = 0;
    createReq.compression = 2;
    createReq.replications = 1;
S
Shengliang Guan 已提交
303
    createReq.strict = 1;
S
Shengliang Guan 已提交
304 305 306 307 308 309 310
    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 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323

    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 已提交
324
    int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq);
S
Shengliang Guan 已提交
325
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
326
    tSerializeSAlterUserReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339

    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 已提交
340
    int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq);
S
Shengliang Guan 已提交
341
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
342
    tSerializeSAlterUserReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
343 344 345 346 347 348

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

S
Shengliang Guan 已提交
349 350 351
  {
    SGetUserAuthReq authReq = {0};
    strcpy(authReq.user, "u3");
S
Shengliang Guan 已提交
352
    int32_t contLen = tSerializeSGetUserAuthReq(NULL, 0, &authReq);
S
Shengliang Guan 已提交
353
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
354
    tSerializeSGetUserAuthReq(pReq, contLen, &authReq);
S
Shengliang Guan 已提交
355 356 357 358 359 360

    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 已提交
361
    tDeserializeSGetUserAuthRsp(pRsp->pCont, pRsp->contLen, &authRsp);
S
Shengliang Guan 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374 375
    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 已提交
376 377 378 379 380 381 382
  {
    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 已提交
383
    int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq);
S
Shengliang Guan 已提交
384
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
385
    tSerializeSAlterUserReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
386 387 388 389 390 391 392 393 394 395

    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 已提交
396
    int32_t contLen = tSerializeSDropUserReq(NULL, 0, &dropReq);
S
Shengliang Guan 已提交
397
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
398
    tSerializeSDropUserReq(pReq, contLen, &dropReq);
S
Shengliang Guan 已提交
399 400 401 402 403

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

S
Shengliang Guan 已提交
404
    test.SendShowReq(TSDB_MGMT_TABLE_USER, "user_users", "");
S
Shengliang Guan 已提交
405 406
    EXPECT_EQ(test.GetShowRows(), 1);
  }
S
Shengliang Guan 已提交
407 408
}

S
Shengliang Guan 已提交
409
TEST_F(MndTestUser, 05_Drop_User) {
S
Shengliang Guan 已提交
410
  {
S
Shengliang Guan 已提交
411 412
    SDropUserReq dropReq = {0};
    strcpy(dropReq.user, "");
S
Shengliang Guan 已提交
413

S
Shengliang Guan 已提交
414
    int32_t contLen = tSerializeSDropUserReq(NULL, 0, &dropReq);
S
Shengliang Guan 已提交
415
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
416
    tSerializeSDropUserReq(pReq, contLen, &dropReq);
S
Shengliang Guan 已提交
417

S
Shengliang Guan 已提交
418 419 420
    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 已提交
421 422 423
  }

  {
S
Shengliang Guan 已提交
424 425
    SDropUserReq dropReq = {0};
    strcpy(dropReq.user, "u4");
S
Shengliang Guan 已提交
426

S
Shengliang Guan 已提交
427
    int32_t contLen = tSerializeSDropUserReq(NULL, 0, &dropReq);
S
Shengliang Guan 已提交
428
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
429
    tSerializeSDropUserReq(pReq, contLen, &dropReq);
S
Shengliang Guan 已提交
430

S
Shengliang Guan 已提交
431 432 433
    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 已提交
434 435
  }

S
Shengliang Guan 已提交
436 437 438 439 440
  {
    SCreateUserReq createReq = {0};
    strcpy(createReq.user, "u1");
    strcpy(createReq.pass, "p1");

S
Shengliang Guan 已提交
441
    int32_t contLen = tSerializeSCreateUserReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
442
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
443
    tSerializeSCreateUserReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
444 445 446 447 448 449

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

S
Shengliang Guan 已提交
450
  {
S
Shengliang Guan 已提交
451 452
    SDropUserReq dropReq = {0};
    strcpy(dropReq.user, "u1");
S
Shengliang Guan 已提交
453

S
Shengliang Guan 已提交
454
    int32_t contLen = tSerializeSDropUserReq(NULL, 0, &dropReq);
S
Shengliang Guan 已提交
455
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
456
    tSerializeSDropUserReq(pReq, contLen, &dropReq);
S
Shengliang Guan 已提交
457

S
Shengliang Guan 已提交
458 459 460
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
461 462
  }

S
Shengliang Guan 已提交
463
  test.SendShowReq(TSDB_MGMT_TABLE_USER, "user_users", "");
S
Shengliang Guan 已提交
464 465 466
  EXPECT_EQ(test.GetShowRows(), 1);
}

S
Shengliang Guan 已提交
467
TEST_F(MndTestUser, 06_Create_Drop_Alter_User) {
S
Shengliang Guan 已提交
468
  {
S
Shengliang Guan 已提交
469 470 471
    SCreateUserReq createReq = {0};
    strcpy(createReq.user, "u1");
    strcpy(createReq.pass, "p1");
S
Shengliang Guan 已提交
472

S
Shengliang Guan 已提交
473
    int32_t contLen = tSerializeSCreateUserReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
474
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
475
    tSerializeSCreateUserReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
476

S
Shengliang Guan 已提交
477 478 479
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
480
  }
S
Shengliang Guan 已提交
481

S
Shengliang Guan 已提交
482
  {
S
Shengliang Guan 已提交
483 484 485
    SCreateUserReq createReq = {0};
    strcpy(createReq.user, "u2");
    strcpy(createReq.pass, "p2");
S
Shengliang Guan 已提交
486

S
Shengliang Guan 已提交
487
    int32_t contLen = tSerializeSCreateUserReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
488
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
489
    tSerializeSCreateUserReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
490

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

S
Shengliang Guan 已提交
496
  test.SendShowReq(TSDB_MGMT_TABLE_USER, "user_users", "");
S
Shengliang Guan 已提交
497 498
  EXPECT_EQ(test.GetShowRows(), 3);

S
Shengliang Guan 已提交
499
  {
S
Shengliang Guan 已提交
500 501 502 503
    SAlterUserReq alterReq = {0};
    alterReq.alterType = TSDB_ALTER_USER_PASSWD;
    strcpy(alterReq.user, "u1");
    strcpy(alterReq.pass, "p2");
S
Shengliang Guan 已提交
504

S
Shengliang Guan 已提交
505
    int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq);
S
Shengliang Guan 已提交
506
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
507
    tSerializeSAlterUserReq(pReq, contLen, &alterReq);
S
Shengliang Guan 已提交
508

S
Shengliang Guan 已提交
509 510 511
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_ALTER_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
512
  }
S
Shengliang Guan 已提交
513

S
Shengliang Guan 已提交
514
  test.SendShowReq(TSDB_MGMT_TABLE_USER, "user_users", "");
S
Shengliang Guan 已提交
515
  EXPECT_EQ(test.GetShowRows(), 3);
S
Shengliang Guan 已提交
516
  {
S
Shengliang Guan 已提交
517 518
    SDropUserReq dropReq = {0};
    strcpy(dropReq.user, "u1");
S
Shengliang Guan 已提交
519

S
Shengliang Guan 已提交
520
    int32_t contLen = tSerializeSDropUserReq(NULL, 0, &dropReq);
S
Shengliang Guan 已提交
521
    void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
522
    tSerializeSDropUserReq(pReq, contLen, &dropReq);
S
Shengliang Guan 已提交
523

S
Shengliang Guan 已提交
524 525 526
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
527
  }
S
Shengliang Guan 已提交
528

S
Shengliang Guan 已提交
529
  test.SendShowReq(TSDB_MGMT_TABLE_USER, "user_users", "");
S
Shengliang Guan 已提交
530 531
  EXPECT_EQ(test.GetShowRows(), 2);

S
Shengliang Guan 已提交
532
  // restart
S
Shengliang Guan 已提交
533
  test.Restart();
S
Shengliang Guan 已提交
534

dengyihao's avatar
fix bug  
dengyihao 已提交
535
  taosMsleep(1000);
S
Shengliang Guan 已提交
536
  test.SendShowReq(TSDB_MGMT_TABLE_USER, "user_users", "");
S
Shengliang Guan 已提交
537
  EXPECT_EQ(test.GetShowRows(), 2);
dengyihao's avatar
fix bug  
dengyihao 已提交
538
}