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
    createReq.update = 0;
    createReq.cacheLastRow = 0;
S
Shengliang Guan 已提交
306
    createReq.ttl = 1;
S
Shengliang Guan 已提交
307 308 309 310 311
    createReq.ignoreExist = 1;

    int32_t contLen = tSerializeSCreateDbReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateDbReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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