user.cpp 8.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
 * @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 12 13 14 15
 *
 */

#include "deploy.h"

class DndTestUser : public ::testing::Test {
 protected:
S
Shengliang Guan 已提交
16 17 18 19 20
  static SServer* CreateServer(const char* path, const char* fqdn, uint16_t port, const char* firstEp) {
    SServer* pServer = createServer(path, fqdn, port, firstEp);
    ASSERT(pServer);
    return pServer;
  }
S
Shengliang Guan 已提交
21 22

  static void SetUpTestSuite() {
S
Shengliang Guan 已提交
23
    initLog("/tmp/tdlog");
S
Shengliang Guan 已提交
24

S
Shengliang Guan 已提交
25
    const char* fqdn = "localhost";
S
Shengliang Guan 已提交
26 27 28
    const char* firstEp = "localhost:9140";
    pServer = CreateServer("/tmp/dnode_test_user", fqdn, 9140, firstEp);
    pClient = createClient("root", "taosdata", fqdn, 9140);
S
Shengliang Guan 已提交
29
    taosMsleep(300);
S
Shengliang Guan 已提交
30 31 32
  }

  static void TearDownTestSuite() {
S
Shengliang Guan 已提交
33
    stopServer(pServer);
S
Shengliang Guan 已提交
34
    dropClient(pClient);
S
Shengliang Guan 已提交
35 36
    pServer = NULL;
    pClient = NULL;
S
Shengliang Guan 已提交
37 38 39 40 41 42
  }

  static SServer* pServer;
  static SClient* pClient;
  static int32_t  connId;

S
Shengliang Guan 已提交
43 44 45
 public:
  void SetUp() override {}
  void TearDown() override {}
S
Shengliang Guan 已提交
46

S
Shengliang Guan 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  void SendTheCheckShowMetaMsg(int8_t showType, const char* showName, int32_t columns) {
    SShowMsg* pShow = (SShowMsg*)rpcMallocCont(sizeof(SShowMsg));
    pShow->type = showType;
    strcpy(pShow->db, "");

    SRpcMsg showRpcMsg = {0};
    showRpcMsg.pCont = pShow;
    showRpcMsg.contLen = sizeof(SShowMsg);
    showRpcMsg.msgType = TSDB_MSG_TYPE_SHOW;

    sendMsg(pClient, &showRpcMsg);
    ASSERT_NE(pClient->pRsp, nullptr);
    ASSERT_EQ(pClient->pRsp->code, 0);
    ASSERT_NE(pClient->pRsp->pCont, nullptr);

    SShowRsp* pShowRsp = (SShowRsp*)pClient->pRsp->pCont;
    ASSERT_NE(pShowRsp, nullptr);
    pShowRsp->showId = htonl(pShowRsp->showId);
    pMeta = &pShowRsp->tableMeta;
S
Shengliang Guan 已提交
66 67 68 69
    pMeta->numOfTags = htonl(pMeta->numOfTags);
    pMeta->numOfColumns = htonl(pMeta->numOfColumns);
    pMeta->sversion = htonl(pMeta->sversion);
    pMeta->tversion = htonl(pMeta->tversion);
S
Shengliang Guan 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    pMeta->tuid = htobe64(pMeta->tuid);
    pMeta->suid = htobe64(pMeta->suid);

    showId = pShowRsp->showId;

    EXPECT_NE(pShowRsp->showId, 0);
    EXPECT_STREQ(pMeta->tbFname, showName);
    EXPECT_EQ(pMeta->numOfTags, 0);
    EXPECT_EQ(pMeta->numOfColumns, columns);
    EXPECT_EQ(pMeta->precision, 0);
    EXPECT_EQ(pMeta->tableType, 0);
    EXPECT_EQ(pMeta->update, 0);
    EXPECT_EQ(pMeta->sversion, 0);
    EXPECT_EQ(pMeta->tversion, 0);
    EXPECT_EQ(pMeta->tuid, 0);
    EXPECT_EQ(pMeta->suid, 0);
  }
S
Shengliang Guan 已提交
87

S
Shengliang Guan 已提交
88 89 90 91 92 93 94
  void CheckSchema(int32_t index, int8_t type, int32_t bytes, const char* name) {
    SSchema* pSchema = &pMeta->pSchema[index];
    pSchema->bytes = htons(pSchema->bytes);
    EXPECT_EQ(pSchema->colId, 0);
    EXPECT_EQ(pSchema->type, type);
    EXPECT_EQ(pSchema->bytes, bytes);
    EXPECT_STREQ(pSchema->name, name);
S
Shengliang Guan 已提交
95 96
  }

S
Shengliang Guan 已提交
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
  void SendThenCheckShowRetrieveMsg(int32_t rows) {
    SRetrieveTableMsg* pRetrieve = (SRetrieveTableMsg*)rpcMallocCont(sizeof(SRetrieveTableMsg));
    pRetrieve->showId = htonl(showId);
    pRetrieve->free = 0;

    SRpcMsg retrieveRpcMsg = {0};
    retrieveRpcMsg.pCont = pRetrieve;
    retrieveRpcMsg.contLen = sizeof(SRetrieveTableMsg);
    retrieveRpcMsg.msgType = TSDB_MSG_TYPE_SHOW_RETRIEVE;

    sendMsg(pClient, &retrieveRpcMsg);

    ASSERT_NE(pClient->pRsp, nullptr);
    ASSERT_EQ(pClient->pRsp->code, 0);
    ASSERT_NE(pClient->pRsp->pCont, nullptr);

    pRetrieveRsp = (SRetrieveTableRsp*)pClient->pRsp->pCont;
    ASSERT_NE(pRetrieveRsp, nullptr);
    pRetrieveRsp->numOfRows = htonl(pRetrieveRsp->numOfRows);
    pRetrieveRsp->offset = htobe64(pRetrieveRsp->offset);
    pRetrieveRsp->useconds = htobe64(pRetrieveRsp->useconds);
    pRetrieveRsp->compLen = htonl(pRetrieveRsp->compLen);

    EXPECT_EQ(pRetrieveRsp->numOfRows, rows);
    EXPECT_EQ(pRetrieveRsp->offset, 0);
    EXPECT_EQ(pRetrieveRsp->useconds, 0);
    // EXPECT_EQ(pRetrieveRsp->completed, completed);
    EXPECT_EQ(pRetrieveRsp->precision, TSDB_TIME_PRECISION_MILLI);
    EXPECT_EQ(pRetrieveRsp->compressed, 0);
    EXPECT_EQ(pRetrieveRsp->reserved, 0);
    EXPECT_EQ(pRetrieveRsp->compLen, 0);

    pData = pRetrieveRsp->data;
    pos = 0;
  }
S
Shengliang Guan 已提交
132

S
Shengliang Guan 已提交
133 134 135 136
  void CheckInt16(int16_t val) {
    int16_t data = *((int16_t*)(pData + pos));
    pos += sizeof(int16_t);
    EXPECT_EQ(data, val);
S
Shengliang Guan 已提交
137 138
  }

S
Shengliang Guan 已提交
139 140
  void CheckInt64(int64_t val) {
    int64_t data = *((int64_t*)(pData + pos));
S
Shengliang Guan 已提交
141
    pos += sizeof(int64_t);
S
Shengliang Guan 已提交
142 143
    EXPECT_EQ(data, val);
  }
S
Shengliang Guan 已提交
144

S
Shengliang Guan 已提交
145 146
  void CheckTimestamp() {
    int64_t data = *((int64_t*)(pData + pos));
S
Shengliang Guan 已提交
147
    pos += sizeof(int64_t);
S
Shengliang Guan 已提交
148
    EXPECT_GT(data, 0);
S
Shengliang Guan 已提交
149 150
  }

S
Shengliang Guan 已提交
151
  void CheckBinary(const char* val, int32_t len) {
S
Shengliang Guan 已提交
152
    pos += sizeof(VarDataLenT);
S
Shengliang Guan 已提交
153 154 155
    char* data = (char*)(pData + pos);
    pos += len;
    EXPECT_STREQ(data, val);
S
Shengliang Guan 已提交
156
  }
S
Shengliang Guan 已提交
157 158 159 160 161 162 163 164 165 166 167 168

  int32_t            showId;
  STableMetaMsg*     pMeta;
  SRetrieveTableRsp* pRetrieveRsp;
  char*              pData;
  int32_t            pos;
};

SServer* DndTestUser::pServer;
SClient* DndTestUser::pClient;
int32_t  DndTestUser::connId;

S
Shengliang Guan 已提交
169
TEST_F(DndTestUser, 01_ShowUser) {
S
Shengliang Guan 已提交
170 171 172 173 174 175 176 177 178 179 180
  SendTheCheckShowMetaMsg(TSDB_MGMT_TABLE_USER, "show users", 4);
  CheckSchema(0, TSDB_DATA_TYPE_BINARY, TSDB_USER_LEN + VARSTR_HEADER_SIZE, "name");
  CheckSchema(1, TSDB_DATA_TYPE_BINARY, 10 + VARSTR_HEADER_SIZE, "privilege");
  CheckSchema(2, TSDB_DATA_TYPE_TIMESTAMP, 8, "create time");
  CheckSchema(3, TSDB_DATA_TYPE_BINARY, TSDB_USER_LEN + VARSTR_HEADER_SIZE, "account");

  SendThenCheckShowRetrieveMsg(1);
  CheckBinary("root", TSDB_USER_LEN);
  CheckBinary("super", 10);
  CheckTimestamp();
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
181 182
}

S
Shengliang Guan 已提交
183
TEST_F(DndTestUser, 02_Create_Drop_Alter_User) {
S
Shengliang Guan 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
  {
    SCreateUserMsg* pReq = (SCreateUserMsg*)rpcMallocCont(sizeof(SCreateUserMsg));
    strcpy(pReq->user, "u1");
    strcpy(pReq->pass, "p1");

    SRpcMsg rpcMsg = {0};
    rpcMsg.pCont = pReq;
    rpcMsg.contLen = sizeof(SCreateUserMsg);
    rpcMsg.msgType = TSDB_MSG_TYPE_CREATE_USER;

    sendMsg(pClient, &rpcMsg);
    SRpcMsg* pMsg = pClient->pRsp;
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, 0);
  }
S
Shengliang Guan 已提交
199

S
Shengliang Guan 已提交
200 201 202 203
  {
    SCreateUserMsg* pReq = (SCreateUserMsg*)rpcMallocCont(sizeof(SCreateUserMsg));
    strcpy(pReq->user, "u2");
    strcpy(pReq->pass, "p2");
S
Shengliang Guan 已提交
204

S
Shengliang Guan 已提交
205 206 207 208 209 210 211 212 213 214
    SRpcMsg rpcMsg = {0};
    rpcMsg.pCont = pReq;
    rpcMsg.contLen = sizeof(SCreateUserMsg);
    rpcMsg.msgType = TSDB_MSG_TYPE_CREATE_USER;

    sendMsg(pClient, &rpcMsg);
    SRpcMsg* pMsg = pClient->pRsp;
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, 0);
  }
S
Shengliang Guan 已提交
215

S
Shengliang Guan 已提交
216
  SendTheCheckShowMetaMsg(TSDB_MGMT_TABLE_USER, "show users", 4);
S
Shengliang Guan 已提交
217
  SendThenCheckShowRetrieveMsg(3);
S
Shengliang Guan 已提交
218 219
  CheckBinary("u1", TSDB_USER_LEN);
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
220
  CheckBinary("u2", TSDB_USER_LEN);
S
Shengliang Guan 已提交
221 222
  CheckBinary("normal", 10);
  CheckBinary("super", 10);
S
Shengliang Guan 已提交
223 224
  CheckBinary("normal", 10);
  CheckTimestamp();
S
Shengliang Guan 已提交
225 226 227 228
  CheckTimestamp();
  CheckTimestamp();
  CheckBinary("root", TSDB_USER_LEN);
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
229
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
230

S
Shengliang Guan 已提交
231 232 233 234
  {
    SAlterUserMsg* pReq = (SAlterUserMsg*)rpcMallocCont(sizeof(SAlterUserMsg));
    strcpy(pReq->user, "u1");
    strcpy(pReq->pass, "p2");
S
Shengliang Guan 已提交
235

S
Shengliang Guan 已提交
236 237 238 239
    SRpcMsg rpcMsg = {0};
    rpcMsg.pCont = pReq;
    rpcMsg.contLen = sizeof(SAlterUserMsg);
    rpcMsg.msgType = TSDB_MSG_TYPE_ALTER_USER;
S
Shengliang Guan 已提交
240

S
Shengliang Guan 已提交
241 242 243 244 245
    sendMsg(pClient, &rpcMsg);
    SRpcMsg* pMsg = pClient->pRsp;
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, 0);
  }
S
Shengliang Guan 已提交
246
  SendTheCheckShowMetaMsg(TSDB_MGMT_TABLE_USER, "show users", 4);
S
Shengliang Guan 已提交
247
  SendThenCheckShowRetrieveMsg(3);
S
Shengliang Guan 已提交
248 249
  CheckBinary("u1", TSDB_USER_LEN);
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
250
  CheckBinary("u2", TSDB_USER_LEN);
S
Shengliang Guan 已提交
251 252
  CheckBinary("normal", 10);
  CheckBinary("super", 10);
S
Shengliang Guan 已提交
253 254
  CheckBinary("normal", 10);
  CheckTimestamp();
S
Shengliang Guan 已提交
255 256 257 258
  CheckTimestamp();
  CheckTimestamp();
  CheckBinary("root", TSDB_USER_LEN);
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
259
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
260

S
Shengliang Guan 已提交
261 262 263
  {
    SDropUserMsg* pReq = (SDropUserMsg*)rpcMallocCont(sizeof(SDropUserMsg));
    strcpy(pReq->user, "u1");
S
Shengliang Guan 已提交
264

S
Shengliang Guan 已提交
265 266 267 268
    SRpcMsg rpcMsg = {0};
    rpcMsg.pCont = pReq;
    rpcMsg.contLen = sizeof(SDropUserMsg);
    rpcMsg.msgType = TSDB_MSG_TYPE_DROP_USER;
S
Shengliang Guan 已提交
269

S
Shengliang Guan 已提交
270 271 272 273 274
    sendMsg(pClient, &rpcMsg);
    SRpcMsg* pMsg = pClient->pRsp;
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, 0);
  }
S
Shengliang Guan 已提交
275
  SendTheCheckShowMetaMsg(TSDB_MGMT_TABLE_USER, "show users", 4);
S
Shengliang Guan 已提交
276
  SendThenCheckShowRetrieveMsg(2);
S
Shengliang Guan 已提交
277
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
278
  CheckBinary("u2", TSDB_USER_LEN);
S
Shengliang Guan 已提交
279
  CheckBinary("super", 10);
S
Shengliang Guan 已提交
280 281
  CheckBinary("normal", 10);
  CheckTimestamp();
S
Shengliang Guan 已提交
282 283
  CheckTimestamp();
  CheckBinary("root", TSDB_USER_LEN);
S
Shengliang Guan 已提交
284 285
  CheckBinary("root", TSDB_USER_LEN);

S
Shengliang Guan 已提交
286
  // restart
S
Shengliang Guan 已提交
287 288 289 290 291 292
  stopServer(pServer);
  pServer = NULL;

  uInfo("start all server");

  const char* fqdn = "localhost";
S
Shengliang Guan 已提交
293 294
  const char* firstEp = "localhost:9140";
  pServer = startServer("/tmp/dnode_test_user", fqdn, 9140, firstEp);
S
Shengliang Guan 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307

  uInfo("all server is running");

  SendTheCheckShowMetaMsg(TSDB_MGMT_TABLE_USER, "show users", 4);
  SendThenCheckShowRetrieveMsg(2);
  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 已提交
308
}