user.cpp 8.1 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
 * @brief DNODE module user-msg tests
S
Shengliang Guan 已提交
5
 * @version 0.1
S
Shengliang Guan 已提交
6
 * @date 2021-12-15
S
Shengliang Guan 已提交
7
 *
S
Shengliang Guan 已提交
8
 * @copyright Copyright (c) 2021
S
Shengliang Guan 已提交
9 10 11
 *
 */

S
Shengliang Guan 已提交
12
#include "base.h"
S
Shengliang Guan 已提交
13 14 15

class DndTestUser : public ::testing::Test {
 protected:
S
Shengliang Guan 已提交
16 17
  static void SetUpTestSuite() { test.Init("/tmp/dnode_test_user", 9140); }
  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 DndTestUser::test;
S
Shengliang Guan 已提交
27

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

  test.SendShowRetrieveMsg();
  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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
TEST_F(DndTestUser, 02_Create_User) {
  {
    int32_t contLen = sizeof(SCreateUserMsg);

    SCreateUserMsg* pReq = (SCreateUserMsg*)rpcMallocCont(contLen);
    strcpy(pReq->user, "");
    strcpy(pReq->pass, "p1");

    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_CREATE_USER, pReq, contLen);
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, TSDB_CODE_MND_INVALID_USER_FORMAT);
  }

  {
    int32_t contLen = sizeof(SCreateUserMsg);

    SCreateUserMsg* pReq = (SCreateUserMsg*)rpcMallocCont(contLen);
    strcpy(pReq->user, "u1");
    strcpy(pReq->pass, "");

    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_CREATE_USER, pReq, contLen);
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, TSDB_CODE_MND_INVALID_PASS_FORMAT);
  }

  {
    int32_t contLen = sizeof(SCreateUserMsg);

    SCreateUserMsg* pReq = (SCreateUserMsg*)rpcMallocCont(contLen);
    strcpy(pReq->user, "root");
    strcpy(pReq->pass, "1");

    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_CREATE_USER, pReq, contLen);
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, TSDB_CODE_MND_USER_ALREADY_EXIST);
  }

  {
    int32_t contLen = sizeof(SCreateUserMsg);

    SCreateUserMsg* pReq = (SCreateUserMsg*)rpcMallocCont(contLen);
    strcpy(pReq->user, "u1");
    strcpy(pReq->pass, "p1");

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

  test.SendShowMetaMsg(TSDB_MGMT_TABLE_USER, "");
  CHECK_META("show users", 4);

  test.SendShowRetrieveMsg();
  EXPECT_EQ(test.GetShowRows(), 2);
}

TEST_F(DndTestUser, 03_Alter_User) {
  {
    int32_t contLen = sizeof(SAlterUserMsg);

    SAlterUserMsg* pReq = (SAlterUserMsg*)rpcMallocCont(contLen);
    strcpy(pReq->user, "");
    strcpy(pReq->pass, "p1");

    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_ALTER_USER, pReq, contLen);
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, TSDB_CODE_MND_INVALID_USER_FORMAT);
  }

  {
    int32_t contLen = sizeof(SAlterUserMsg);

    SAlterUserMsg* pReq = (SAlterUserMsg*)rpcMallocCont(contLen);
    strcpy(pReq->user, "u1");
    strcpy(pReq->pass, "");

    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_ALTER_USER, pReq, contLen);
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, TSDB_CODE_MND_INVALID_PASS_FORMAT);
  }

  {
    int32_t contLen = sizeof(SAlterUserMsg);

    SAlterUserMsg* pReq = (SAlterUserMsg*)rpcMallocCont(contLen);
    strcpy(pReq->user, "u4");
    strcpy(pReq->pass, "1");

    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_ALTER_USER, pReq, contLen);
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, TSDB_CODE_MND_USER_NOT_EXIST);
  }

  {
    int32_t contLen = sizeof(SAlterUserMsg);

    SAlterUserMsg* pReq = (SAlterUserMsg*)rpcMallocCont(contLen);
    strcpy(pReq->user, "u1");
    strcpy(pReq->pass, "1");

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

TEST_F(DndTestUser, 04_Drop_User) {
  {
    int32_t contLen = sizeof(SDropUserMsg);

    SDropUserMsg* pReq = (SDropUserMsg*)rpcMallocCont(contLen);
    strcpy(pReq->user, "");

    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_DROP_USER, pReq, contLen);
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, TSDB_CODE_MND_INVALID_USER_FORMAT);
  }

  {
    int32_t contLen = sizeof(SDropUserMsg);

    SDropUserMsg* pReq = (SDropUserMsg*)rpcMallocCont(contLen);
    strcpy(pReq->user, "u4");

    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_DROP_USER, pReq, contLen);
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, TSDB_CODE_MND_USER_NOT_EXIST);
  }

  {
    int32_t contLen = sizeof(SDropUserMsg);

    SDropUserMsg* pReq = (SDropUserMsg*)rpcMallocCont(contLen);
    strcpy(pReq->user, "u1");

    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_DROP_USER, pReq, contLen);
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, 0);
  }

  test.SendShowMetaMsg(TSDB_MGMT_TABLE_USER, "");
  CHECK_META("show users", 4);

  test.SendShowRetrieveMsg();
  EXPECT_EQ(test.GetShowRows(), 1);
}

S
Shengliang Guan 已提交
193
TEST_F(DndTestUser, 02_Create_Drop_Alter_User) {
S
Shengliang Guan 已提交
194
  {
S
Shengliang Guan 已提交
195 196 197
    int32_t contLen = sizeof(SCreateUserMsg);

    SCreateUserMsg* pReq = (SCreateUserMsg*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
198 199 200
    strcpy(pReq->user, "u1");
    strcpy(pReq->pass, "p1");

H
Hongze Cheng 已提交
201
    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_CREATE_USER, pReq, contLen);
S
Shengliang Guan 已提交
202 203 204
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, 0);
  }
S
Shengliang Guan 已提交
205

S
Shengliang Guan 已提交
206
  {
S
Shengliang Guan 已提交
207 208 209
    int32_t contLen = sizeof(SCreateUserMsg);

    SCreateUserMsg* pReq = (SCreateUserMsg*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
210 211
    strcpy(pReq->user, "u2");
    strcpy(pReq->pass, "p2");
S
Shengliang Guan 已提交
212

H
Hongze Cheng 已提交
213
    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_CREATE_USER, pReq, contLen);
S
Shengliang Guan 已提交
214 215 216
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, 0);
  }
S
Shengliang Guan 已提交
217

S
Shengliang Guan 已提交
218 219 220 221 222 223
  test.SendShowMetaMsg(TSDB_MGMT_TABLE_USER, "");
  CHECK_META("show users", 4);

  test.SendShowRetrieveMsg();
  EXPECT_EQ(test.GetShowRows(), 3);

S
Shengliang Guan 已提交
224 225
  CheckBinary("u1", TSDB_USER_LEN);
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
226
  CheckBinary("u2", TSDB_USER_LEN);
S
Shengliang Guan 已提交
227 228
  CheckBinary("normal", 10);
  CheckBinary("super", 10);
S
Shengliang Guan 已提交
229 230
  CheckBinary("normal", 10);
  CheckTimestamp();
S
Shengliang Guan 已提交
231 232 233 234
  CheckTimestamp();
  CheckTimestamp();
  CheckBinary("root", TSDB_USER_LEN);
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
235
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
236

S
Shengliang Guan 已提交
237
  {
S
Shengliang Guan 已提交
238 239 240
    int32_t contLen = sizeof(SAlterUserMsg);

    SAlterUserMsg* pReq = (SAlterUserMsg*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
241 242
    strcpy(pReq->user, "u1");
    strcpy(pReq->pass, "p2");
S
Shengliang Guan 已提交
243

H
Hongze Cheng 已提交
244
    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_ALTER_USER, pReq, contLen);
S
Shengliang Guan 已提交
245 246 247
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, 0);
  }
S
Shengliang Guan 已提交
248 249 250 251 252 253 254

  test.SendShowMetaMsg(TSDB_MGMT_TABLE_USER, "");
  CHECK_META("show users", 4);

  test.SendShowRetrieveMsg();
  EXPECT_EQ(test.GetShowRows(), 3);

S
Shengliang Guan 已提交
255 256
  CheckBinary("u1", TSDB_USER_LEN);
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
257
  CheckBinary("u2", TSDB_USER_LEN);
S
Shengliang Guan 已提交
258 259
  CheckBinary("normal", 10);
  CheckBinary("super", 10);
S
Shengliang Guan 已提交
260 261
  CheckBinary("normal", 10);
  CheckTimestamp();
S
Shengliang Guan 已提交
262 263 264 265
  CheckTimestamp();
  CheckTimestamp();
  CheckBinary("root", TSDB_USER_LEN);
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
266
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
267

S
Shengliang Guan 已提交
268
  {
S
Shengliang Guan 已提交
269
    int32_t contLen = sizeof(SDropUserMsg);
S
Shengliang Guan 已提交
270

S
Shengliang Guan 已提交
271 272
    SDropUserMsg* pReq = (SDropUserMsg*)rpcMallocCont(contLen);
    strcpy(pReq->user, "u1");
S
Shengliang Guan 已提交
273

H
Hongze Cheng 已提交
274
    SRpcMsg* pMsg = test.SendMsg(TDMT_MND_DROP_USER, pReq, contLen);
S
Shengliang Guan 已提交
275 276 277
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, 0);
  }
S
Shengliang Guan 已提交
278 279 280 281 282 283 284

  test.SendShowMetaMsg(TSDB_MGMT_TABLE_USER, "");
  CHECK_META("show users", 4);

  test.SendShowRetrieveMsg();
  EXPECT_EQ(test.GetShowRows(), 2);

S
Shengliang Guan 已提交
285
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
286
  CheckBinary("u2", TSDB_USER_LEN);
S
Shengliang Guan 已提交
287
  CheckBinary("super", 10);
S
Shengliang Guan 已提交
288 289
  CheckBinary("normal", 10);
  CheckTimestamp();
S
Shengliang Guan 已提交
290 291
  CheckTimestamp();
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
292 293
  CheckBinary("root", TSDB_USER_LEN);

S
Shengliang Guan 已提交
294
  // restart
S
Shengliang Guan 已提交
295
  test.Restart();
S
Shengliang Guan 已提交
296

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

S
Shengliang Guan 已提交
300 301
  test.SendShowRetrieveMsg();
  EXPECT_EQ(test.GetShowRows(), 2);
S
Shengliang Guan 已提交
302 303 304 305 306 307 308 309 310

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