user.cpp 9.9 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
  {
H
Haojun Liao 已提交
48 49 50
    SCreateUserReq createReq = {0};
    strcpy(createReq.user, "");
    strcpy(createReq.pass, "p1");
S
Shengliang Guan 已提交
51

H
Haojun Liao 已提交
52 53 54 55
    int32_t contLen = tSerializeSCreateUserReq(NULL, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    void*   pBuf = pReq;
    tSerializeSCreateUserReq(&pBuf, &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_USER_FORMAT);
S
Shengliang Guan 已提交
60 61 62
  }

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

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

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

  {
H
Haojun Liao 已提交
78 79 80
    SCreateUserReq createReq = {0};
    strcpy(createReq.user, "root");
    strcpy(createReq.pass, "1");
S
Shengliang Guan 已提交
81

H
Haojun Liao 已提交
82 83 84 85
    int32_t contLen = tSerializeSCreateUserReq(NULL, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    void*   pBuf = pReq;
    tSerializeSCreateUserReq(&pBuf, &createReq);
S
Shengliang Guan 已提交
86

S
Shengliang Guan 已提交
87 88 89
    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 已提交
90 91 92
  }

  {
H
Haojun Liao 已提交
93 94 95
    SCreateUserReq createReq = {0};
    strcpy(createReq.user, "u1");
    strcpy(createReq.pass, "p1");
S
Shengliang Guan 已提交
96

H
Haojun Liao 已提交
97 98 99 100
    int32_t contLen = tSerializeSCreateUserReq(NULL, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    void*   pBuf = pReq;
    tSerializeSCreateUserReq(&pBuf, &createReq);
S
Shengliang Guan 已提交
101

S
Shengliang Guan 已提交
102 103 104
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
105 106
  }

S
Shengliang Guan 已提交
107
  test.SendShowMetaReq(TSDB_MGMT_TABLE_USER, "");
S
Shengliang Guan 已提交
108 109
  CHECK_META("show users", 4);

S
Shengliang Guan 已提交
110
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
111 112 113
  EXPECT_EQ(test.GetShowRows(), 2);
}

S
Shengliang Guan 已提交
114
TEST_F(MndTestUser, 03_Alter_User) {
S
Shengliang Guan 已提交
115
  {
H
Haojun Liao 已提交
116 117 118 119
    SAlterUserReq alterReq = {0};
    alterReq.alterType = TSDB_ALTER_USER_PASSWD;
    strcpy(alterReq.user, "");
    strcpy(alterReq.pass, "p1");
S
Shengliang Guan 已提交
120

H
Haojun Liao 已提交
121 122 123 124
    int32_t contLen = tSerializeSAlterUserReq(NULL, &alterReq);
    void*   pReq = rpcMallocCont(contLen);
    void*   pBuf = pReq;
    tSerializeSAlterUserReq(&pBuf, &alterReq);
S
Shengliang Guan 已提交
125

S
Shengliang Guan 已提交
126 127 128
    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 已提交
129 130 131
  }

  {
H
Haojun Liao 已提交
132 133 134 135
    SAlterUserReq alterReq = {0};
    alterReq.alterType = TSDB_ALTER_USER_PASSWD;
    strcpy(alterReq.user, "u1");
    strcpy(alterReq.pass, "");
S
Shengliang Guan 已提交
136

H
Haojun Liao 已提交
137 138 139 140
    int32_t contLen = tSerializeSAlterUserReq(NULL, &alterReq);
    void*   pReq = rpcMallocCont(contLen);
    void*   pBuf = pReq;
    tSerializeSAlterUserReq(&pBuf, &alterReq);
S
Shengliang Guan 已提交
141

S
Shengliang Guan 已提交
142 143 144
    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 已提交
145 146 147
  }

  {
H
Haojun Liao 已提交
148 149 150 151
    SAlterUserReq alterReq = {0};
    alterReq.alterType = TSDB_ALTER_USER_PASSWD;
    strcpy(alterReq.user, "u4");
    strcpy(alterReq.pass, "1");
S
Shengliang Guan 已提交
152

H
Haojun Liao 已提交
153 154 155 156
    int32_t contLen = tSerializeSAlterUserReq(NULL, &alterReq);
    void*   pReq = rpcMallocCont(contLen);
    void*   pBuf = pReq;
    tSerializeSAlterUserReq(&pBuf, &alterReq);
S
Shengliang Guan 已提交
157

S
Shengliang Guan 已提交
158 159 160
    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 已提交
161 162 163
  }

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

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

S
Shengliang Guan 已提交
174 175 176
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_ALTER_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
177 178 179
  }
}

S
Shengliang Guan 已提交
180
TEST_F(MndTestUser, 04_Drop_User) {
S
Shengliang Guan 已提交
181
  {
H
Haojun Liao 已提交
182 183
    SDropUserReq dropReq = {0};
    strcpy(dropReq.user, "");
S
Shengliang Guan 已提交
184

H
Haojun Liao 已提交
185 186 187 188
    int32_t contLen = tSerializeSDropUserReq(NULL, &dropReq);
    void*   pReq = rpcMallocCont(contLen);
    void*   pBuf = pReq;
    tSerializeSDropUserReq(&pBuf, &dropReq);
S
Shengliang Guan 已提交
189

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

  {
H
Haojun Liao 已提交
196 197
    SDropUserReq dropReq = {0};
    strcpy(dropReq.user, "u4");
S
Shengliang Guan 已提交
198

H
Haojun Liao 已提交
199 200 201 202
    int32_t contLen = tSerializeSDropUserReq(NULL, &dropReq);
    void*   pReq = rpcMallocCont(contLen);
    void*   pBuf = pReq;
    tSerializeSDropUserReq(&pBuf, &dropReq);
S
Shengliang Guan 已提交
203

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

  {
H
Haojun Liao 已提交
210 211
    SDropUserReq dropReq = {0};
    strcpy(dropReq.user, "u1");
S
Shengliang Guan 已提交
212

H
Haojun Liao 已提交
213 214 215 216
    int32_t contLen = tSerializeSDropUserReq(NULL, &dropReq);
    void*   pReq = rpcMallocCont(contLen);
    void*   pBuf = pReq;
    tSerializeSDropUserReq(&pBuf, &dropReq);
S
Shengliang Guan 已提交
217

S
Shengliang Guan 已提交
218 219 220
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
221 222
  }

S
Shengliang Guan 已提交
223
  test.SendShowMetaReq(TSDB_MGMT_TABLE_USER, "");
S
Shengliang Guan 已提交
224 225
  CHECK_META("show users", 4);

S
Shengliang Guan 已提交
226
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
227 228 229
  EXPECT_EQ(test.GetShowRows(), 1);
}

S
Shengliang Guan 已提交
230
TEST_F(MndTestUser, 05_Create_Drop_Alter_User) {
S
Shengliang Guan 已提交
231
  {
H
Haojun Liao 已提交
232 233 234
    SCreateUserReq createReq = {0};
    strcpy(createReq.user, "u1");
    strcpy(createReq.pass, "p1");
S
Shengliang Guan 已提交
235

H
Haojun Liao 已提交
236 237 238 239
    int32_t contLen = tSerializeSCreateUserReq(NULL, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    void*   pBuf = pReq;
    tSerializeSCreateUserReq(&pBuf, &createReq);
S
Shengliang Guan 已提交
240

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

S
Shengliang Guan 已提交
246
  {
H
Haojun Liao 已提交
247 248 249
    SCreateUserReq createReq = {0};
    strcpy(createReq.user, "u2");
    strcpy(createReq.pass, "p2");
S
Shengliang Guan 已提交
250

H
Haojun Liao 已提交
251 252 253 254
    int32_t contLen = tSerializeSCreateUserReq(NULL, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    void*   pBuf = pReq;
    tSerializeSCreateUserReq(&pBuf, &createReq);
S
Shengliang Guan 已提交
255

S
Shengliang Guan 已提交
256 257 258
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
259
  }
S
Shengliang Guan 已提交
260

S
Shengliang Guan 已提交
261
  test.SendShowMetaReq(TSDB_MGMT_TABLE_USER, "");
S
Shengliang Guan 已提交
262 263
  CHECK_META("show users", 4);

S
Shengliang Guan 已提交
264
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
265 266
  EXPECT_EQ(test.GetShowRows(), 3);

S
Shengliang Guan 已提交
267 268
  CheckBinary("u1", TSDB_USER_LEN);
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
269
  CheckBinary("u2", TSDB_USER_LEN);
S
Shengliang Guan 已提交
270 271
  CheckBinary("normal", 10);
  CheckBinary("super", 10);
S
Shengliang Guan 已提交
272 273
  CheckBinary("normal", 10);
  CheckTimestamp();
S
Shengliang Guan 已提交
274 275 276 277
  CheckTimestamp();
  CheckTimestamp();
  CheckBinary("root", TSDB_USER_LEN);
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
278
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
279

S
Shengliang Guan 已提交
280
  {
H
Haojun Liao 已提交
281 282 283 284
    SAlterUserReq alterReq = {0};
    alterReq.alterType = TSDB_ALTER_USER_PASSWD;
    strcpy(alterReq.user, "u1");
    strcpy(alterReq.pass, "p2");
S
Shengliang Guan 已提交
285

H
Haojun Liao 已提交
286 287 288 289
    int32_t contLen = tSerializeSAlterUserReq(NULL, &alterReq);
    void*   pReq = rpcMallocCont(contLen);
    void*   pBuf = pReq;
    tSerializeSAlterUserReq(&pBuf, &alterReq);
S
Shengliang Guan 已提交
290

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

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

S
Shengliang Guan 已提交
299
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
300 301
  EXPECT_EQ(test.GetShowRows(), 3);

S
Shengliang Guan 已提交
302 303
  CheckBinary("u1", TSDB_USER_LEN);
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
304
  CheckBinary("u2", TSDB_USER_LEN);
S
Shengliang Guan 已提交
305 306
  CheckBinary("normal", 10);
  CheckBinary("super", 10);
S
Shengliang Guan 已提交
307 308
  CheckBinary("normal", 10);
  CheckTimestamp();
S
Shengliang Guan 已提交
309 310 311 312
  CheckTimestamp();
  CheckTimestamp();
  CheckBinary("root", TSDB_USER_LEN);
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
313
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
314

S
Shengliang Guan 已提交
315
  {
H
Haojun Liao 已提交
316 317
    SDropUserReq dropReq = {0};
    strcpy(dropReq.user, "u1");
S
Shengliang Guan 已提交
318

H
Haojun Liao 已提交
319 320 321 322
    int32_t contLen = tSerializeSDropUserReq(NULL, &dropReq);
    void*   pReq = rpcMallocCont(contLen);
    void*   pBuf = pReq;
    tSerializeSDropUserReq(&pBuf, &dropReq);
S
Shengliang Guan 已提交
323

S
Shengliang Guan 已提交
324 325 326
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_USER, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
327
  }
S
Shengliang Guan 已提交
328

S
Shengliang Guan 已提交
329
  test.SendShowMetaReq(TSDB_MGMT_TABLE_USER, "");
S
Shengliang Guan 已提交
330 331
  CHECK_META("show users", 4);

S
Shengliang Guan 已提交
332
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
333 334
  EXPECT_EQ(test.GetShowRows(), 2);

S
Shengliang Guan 已提交
335
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
336
  CheckBinary("u2", TSDB_USER_LEN);
S
Shengliang Guan 已提交
337
  CheckBinary("super", 10);
S
Shengliang Guan 已提交
338 339
  CheckBinary("normal", 10);
  CheckTimestamp();
S
Shengliang Guan 已提交
340 341
  CheckTimestamp();
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
342 343
  CheckBinary("root", TSDB_USER_LEN);

S
Shengliang Guan 已提交
344
  // restart
S
Shengliang Guan 已提交
345
  test.Restart();
S
Shengliang Guan 已提交
346

S
Shengliang Guan 已提交
347
  test.SendShowMetaReq(TSDB_MGMT_TABLE_USER, "");
S
Shengliang Guan 已提交
348
  CHECK_META("show users", 4);
S
Shengliang Guan 已提交
349

S
Shengliang Guan 已提交
350
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
351
  EXPECT_EQ(test.GetShowRows(), 2);
S
Shengliang Guan 已提交
352 353 354 355 356 357 358 359 360

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