user.cpp 11.8 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "deploy.h"

class DndTestUser : public ::testing::Test {
 protected:
  void SetUp() override {}
  void TearDown() override {}

  static void SetUpTestSuite() {
    const char* user = "root";
    const char* pass = "taosdata";
    const char* path = "/tmp/dndTestUser";
    const char* fqdn = "localhost";
    uint16_t    port = 9524;
29
    const char* firstEp = "localhost:9524";
S
Shengliang Guan 已提交
30

31
    pServer = createServer(path, fqdn, port, firstEp);
S
Shengliang Guan 已提交
32 33 34 35 36
    ASSERT(pServer);
    pClient = createClient(user, pass, fqdn, port);
  }

  static void TearDownTestSuite() {
S
Shengliang Guan 已提交
37
    stopServer(pServer);
S
Shengliang Guan 已提交
38 39 40 41 42 43 44 45 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
    dropClient(pClient);
  }

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

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

#if 0
TEST_F(DndTestUser, ShowUser) {
  int32_t showId = 0;

  //--- meta ---
  SShowMsg* pShow = (SShowMsg*)rpcMallocCont(sizeof(SShowMsg));
  pShow->type = TSDB_MGMT_TABLE_USER;
  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);

  SShowRsp* pShowRsp = (SShowRsp*)pClient->pRsp->pCont;
  ASSERT_NE(pShowRsp, nullptr);
  pShowRsp->showId = htonl(pShowRsp->showId);
  STableMetaMsg* pMeta = &pShowRsp->tableMeta;
  pMeta->contLen = htonl(pMeta->contLen);
  pMeta->numOfColumns = htons(pMeta->numOfColumns);
  pMeta->sversion = htons(pMeta->sversion);
  pMeta->tversion = htons(pMeta->tversion);
  pMeta->tid = htonl(pMeta->tid);
  pMeta->uid = htobe64(pMeta->uid);
  pMeta->suid = htobe64(pMeta->suid);

  showId = pShowRsp->showId;

  EXPECT_NE(pShowRsp->showId, 0);
  EXPECT_EQ(pMeta->contLen, 0);
83
  EXPECT_STREQ(pMeta->tbFname, "show users");
S
Shengliang Guan 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96
  EXPECT_EQ(pMeta->numOfTags, 0);
  EXPECT_EQ(pMeta->precision, 0);
  EXPECT_EQ(pMeta->tableType, 0);
  EXPECT_EQ(pMeta->numOfColumns, 4);
  EXPECT_EQ(pMeta->sversion, 0);
  EXPECT_EQ(pMeta->tversion, 0);
  EXPECT_EQ(pMeta->tid, 0);
  EXPECT_EQ(pMeta->uid, 0);
  EXPECT_STREQ(pMeta->sTableName, "");
  EXPECT_EQ(pMeta->suid, 0);

  SSchema* pSchema = NULL;

S
Shengliang Guan 已提交
97
  pSchema = &pMeta->pSchema[0];
S
Shengliang Guan 已提交
98 99 100 101 102 103
  pSchema->bytes = htons(pSchema->bytes);
  EXPECT_EQ(pSchema->colId, 0);
  EXPECT_EQ(pSchema->type, TSDB_DATA_TYPE_BINARY);
  EXPECT_EQ(pSchema->bytes, TSDB_USER_LEN + VARSTR_HEADER_SIZE);
  EXPECT_STREQ(pSchema->name, "name");

S
Shengliang Guan 已提交
104
  pSchema = &pMeta->pSchema[1];
S
Shengliang Guan 已提交
105 106 107 108 109 110
  pSchema->bytes = htons(pSchema->bytes);
  EXPECT_EQ(pSchema->colId, 0);
  EXPECT_EQ(pSchema->type, TSDB_DATA_TYPE_BINARY);
  EXPECT_EQ(pSchema->bytes, 10 + VARSTR_HEADER_SIZE);
  EXPECT_STREQ(pSchema->name, "privilege");

S
Shengliang Guan 已提交
111
  pSchema = &pMeta->pSchema[2];
S
Shengliang Guan 已提交
112 113 114 115 116 117
  pSchema->bytes = htons(pSchema->bytes);
  EXPECT_EQ(pSchema->colId, 0);
  EXPECT_EQ(pSchema->type, TSDB_DATA_TYPE_TIMESTAMP);
  EXPECT_EQ(pSchema->bytes, 8);
  EXPECT_STREQ(pSchema->name, "create_time");

S
Shengliang Guan 已提交
118
  pSchema = &pMeta->pSchema[3];
S
Shengliang Guan 已提交
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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
  pSchema->bytes = htons(pSchema->bytes);
  EXPECT_EQ(pSchema->colId, 0);
  EXPECT_EQ(pSchema->type, TSDB_DATA_TYPE_BINARY);
  EXPECT_EQ(pSchema->bytes, TSDB_USER_LEN + VARSTR_HEADER_SIZE);
  EXPECT_STREQ(pSchema->name, "account");

  //--- retrieve ---
  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);

  SRetrieveTableRsp* 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, 2);
  EXPECT_EQ(pRetrieveRsp->offset, 0);
  EXPECT_EQ(pRetrieveRsp->useconds, 0);
  EXPECT_EQ(pRetrieveRsp->completed, 1);
  EXPECT_EQ(pRetrieveRsp->precision, TSDB_TIME_PRECISION_MILLI);
  EXPECT_EQ(pRetrieveRsp->compressed, 0);
  EXPECT_EQ(pRetrieveRsp->reserved, 0);
  EXPECT_EQ(pRetrieveRsp->compLen, 0);

  char*   pData = pRetrieveRsp->data;
  int32_t pos = 0;
  char*   strVal = NULL;
  int64_t int64Val = 0;

  //--- name ---
  {
    pos += sizeof(VarDataLenT);
    strVal = (char*)(pData + pos);
    pos += TSDB_USER_LEN;
    EXPECT_STREQ(strVal, "root");

    pos += sizeof(VarDataLenT);
    strVal = (char*)(pData + pos);
    pos += TSDB_USER_LEN;
    EXPECT_STREQ(strVal, "_root");
  }

  //--- privilege ---
  {
    pos += sizeof(VarDataLenT);
    strVal = (char*)(pData + pos);
    pos += 10;
    EXPECT_STREQ(strVal, "super");

    pos += sizeof(VarDataLenT);
    strVal = (char*)(pData + pos);
    pos += 10;
    EXPECT_STREQ(strVal, "writable");
  }

  //--- create_time ---
  {
    int64Val = *((int64_t*)(pData + pos));
    pos += sizeof(int64_t);
    EXPECT_GT(int64Val, 0);

    int64Val = *((int64_t*)(pData + pos));
    pos += sizeof(int64_t);
    EXPECT_GT(int64Val, 0);
  }

  //--- account ---
  {
    pos += sizeof(VarDataLenT);
    strVal = (char*)(pData + pos);
    pos += TSDB_USER_LEN;
    EXPECT_STREQ(strVal, "root");

    pos += sizeof(VarDataLenT);
    strVal = (char*)(pData + pos);
    pos += TSDB_USER_LEN;
    EXPECT_STREQ(strVal, "root");
  }
}
#endif

TEST_F(DndTestUser, CreateUser_01) {
  ASSERT_NE(pClient, nullptr);

  //--- create user ---
  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);

  //--- meta ---
  SShowMsg* pShow = (SShowMsg*)rpcMallocCont(sizeof(SShowMsg));
  pShow->type = TSDB_MGMT_TABLE_USER;
  SRpcMsg showRpcMsg = {0};
  showRpcMsg.pCont = pShow;
  showRpcMsg.contLen = sizeof(SShowMsg);
  showRpcMsg.msgType = TSDB_MSG_TYPE_SHOW;

  sendMsg(pClient, &showRpcMsg);
  SShowRsp*      pShowRsp = (SShowRsp*)pClient->pRsp->pCont;
  STableMetaMsg* pMeta = &pShowRsp->tableMeta;
  pMeta->numOfColumns = htons(pMeta->numOfColumns);
  EXPECT_EQ(pMeta->numOfColumns, 4);

  //--- retrieve ---
  SRetrieveTableMsg* pRetrieve = (SRetrieveTableMsg*)rpcMallocCont(sizeof(SRetrieveTableMsg));
  pRetrieve->showId = pShowRsp->showId;
  SRpcMsg retrieveRpcMsg = {0};
  retrieveRpcMsg.pCont = pRetrieve;
  retrieveRpcMsg.contLen = sizeof(SRetrieveTableMsg);
  retrieveRpcMsg.msgType = TSDB_MSG_TYPE_SHOW_RETRIEVE;

  sendMsg(pClient, &retrieveRpcMsg);
  SRetrieveTableRsp* pRetrieveRsp = (SRetrieveTableRsp*)pClient->pRsp->pCont;
  pRetrieveRsp->numOfRows = htonl(pRetrieveRsp->numOfRows);
  EXPECT_EQ(pRetrieveRsp->numOfRows, 3);

  char*   pData = pRetrieveRsp->data;
  int32_t pos = 0;
  char*   strVal = NULL;

  //--- name ---
  {
    pos += sizeof(VarDataLenT);
    strVal = (char*)(pData + pos);
    pos += TSDB_USER_LEN;
    EXPECT_STREQ(strVal, "u1");

    pos += sizeof(VarDataLenT);
    strVal = (char*)(pData + pos);
    pos += TSDB_USER_LEN;
    EXPECT_STREQ(strVal, "root");

    pos += sizeof(VarDataLenT);
    strVal = (char*)(pData + pos);
    pos += TSDB_USER_LEN;
    EXPECT_STREQ(strVal, "_root");
  }
}

S
Shengliang Guan 已提交
280 281 282 283 284 285 286 287 288 289
TEST_F(DndTestUser, AlterUser_01) {
  ASSERT_NE(pClient, nullptr);

  //--- drop user ---
  SAlterUserMsg* pReq = (SAlterUserMsg*)rpcMallocCont(sizeof(SAlterUserMsg));
  strcpy(pReq->user, "u1");
  strcpy(pReq->pass, "p2");

  SRpcMsg rpcMsg = {0};
  rpcMsg.pCont = pReq;
S
Shengliang Guan 已提交
290
  rpcMsg.contLen = sizeof(SAlterUserMsg);
S
Shengliang Guan 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
  rpcMsg.msgType = TSDB_MSG_TYPE_ALTER_USER;

  sendMsg(pClient, &rpcMsg);
  SRpcMsg* pMsg = pClient->pRsp;
  ASSERT_NE(pMsg, nullptr);
  ASSERT_EQ(pMsg->code, 0);

  //--- meta ---
  SShowMsg* pShow = (SShowMsg*)rpcMallocCont(sizeof(SShowMsg));
  pShow->type = TSDB_MGMT_TABLE_USER;
  SRpcMsg showRpcMsg = {0};
  showRpcMsg.pCont = pShow;
  showRpcMsg.contLen = sizeof(SShowMsg);
  showRpcMsg.msgType = TSDB_MSG_TYPE_SHOW;

  sendMsg(pClient, &showRpcMsg);
  SShowRsp*      pShowRsp = (SShowRsp*)pClient->pRsp->pCont;
  STableMetaMsg* pMeta = &pShowRsp->tableMeta;
  pMeta->numOfColumns = htons(pMeta->numOfColumns);
  EXPECT_EQ(pMeta->numOfColumns, 4);

  //--- retrieve ---
  SRetrieveTableMsg* pRetrieve = (SRetrieveTableMsg*)rpcMallocCont(sizeof(SRetrieveTableMsg));
  pRetrieve->showId = pShowRsp->showId;
  SRpcMsg retrieveRpcMsg = {0};
  retrieveRpcMsg.pCont = pRetrieve;
  retrieveRpcMsg.contLen = sizeof(SRetrieveTableMsg);
  retrieveRpcMsg.msgType = TSDB_MSG_TYPE_SHOW_RETRIEVE;

  sendMsg(pClient, &retrieveRpcMsg);
  SRetrieveTableRsp* pRetrieveRsp = (SRetrieveTableRsp*)pClient->pRsp->pCont;
  pRetrieveRsp->numOfRows = htonl(pRetrieveRsp->numOfRows);
  EXPECT_EQ(pRetrieveRsp->numOfRows, 3);

  char*   pData = pRetrieveRsp->data;
  int32_t pos = 0;
  char*   strVal = NULL;

  //--- name ---
  {
    pos += sizeof(VarDataLenT);
    strVal = (char*)(pData + pos);
    pos += TSDB_USER_LEN;
    EXPECT_STREQ(strVal, "u1");

    pos += sizeof(VarDataLenT);
    strVal = (char*)(pData + pos);
    pos += TSDB_USER_LEN;
    EXPECT_STREQ(strVal, "root");

    pos += sizeof(VarDataLenT);
    strVal = (char*)(pData + pos);
    pos += TSDB_USER_LEN;
    EXPECT_STREQ(strVal, "_root");
  }
}
S
Shengliang Guan 已提交
347

S
Shengliang Guan 已提交
348 349
TEST_F(DndTestUser, DropUser_01) {
  ASSERT_NE(pClient, nullptr);
S
Shengliang Guan 已提交
350

S
Shengliang Guan 已提交
351 352 353
  //--- drop user ---
  SDropUserMsg* pReq = (SDropUserMsg*)rpcMallocCont(sizeof(SDropUserMsg));
  strcpy(pReq->user, "u1");
S
Shengliang Guan 已提交
354

S
Shengliang Guan 已提交
355 356 357 358
  SRpcMsg rpcMsg = {0};
  rpcMsg.pCont = pReq;
  rpcMsg.contLen = sizeof(SDropUserMsg);
  rpcMsg.msgType = TSDB_MSG_TYPE_DROP_USER;
S
Shengliang Guan 已提交
359

S
Shengliang Guan 已提交
360 361 362 363
  sendMsg(pClient, &rpcMsg);
  SRpcMsg* pMsg = pClient->pRsp;
  ASSERT_NE(pMsg, nullptr);
  ASSERT_EQ(pMsg->code, 0);
S
Shengliang Guan 已提交
364

S
Shengliang Guan 已提交
365 366 367 368 369 370 371
  //--- meta ---
  SShowMsg* pShow = (SShowMsg*)rpcMallocCont(sizeof(SShowMsg));
  pShow->type = TSDB_MGMT_TABLE_USER;
  SRpcMsg showRpcMsg = {0};
  showRpcMsg.pCont = pShow;
  showRpcMsg.contLen = sizeof(SShowMsg);
  showRpcMsg.msgType = TSDB_MSG_TYPE_SHOW;
S
Shengliang Guan 已提交
372

S
Shengliang Guan 已提交
373 374 375 376 377
  sendMsg(pClient, &showRpcMsg);
  SShowRsp*      pShowRsp = (SShowRsp*)pClient->pRsp->pCont;
  STableMetaMsg* pMeta = &pShowRsp->tableMeta;
  pMeta->numOfColumns = htons(pMeta->numOfColumns);
  EXPECT_EQ(pMeta->numOfColumns, 4);
S
Shengliang Guan 已提交
378

S
Shengliang Guan 已提交
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
  //--- retrieve ---
  SRetrieveTableMsg* pRetrieve = (SRetrieveTableMsg*)rpcMallocCont(sizeof(SRetrieveTableMsg));
  pRetrieve->showId = pShowRsp->showId;
  SRpcMsg retrieveRpcMsg = {0};
  retrieveRpcMsg.pCont = pRetrieve;
  retrieveRpcMsg.contLen = sizeof(SRetrieveTableMsg);
  retrieveRpcMsg.msgType = TSDB_MSG_TYPE_SHOW_RETRIEVE;

  sendMsg(pClient, &retrieveRpcMsg);
  SRetrieveTableRsp* pRetrieveRsp = (SRetrieveTableRsp*)pClient->pRsp->pCont;
  pRetrieveRsp->numOfRows = htonl(pRetrieveRsp->numOfRows);
  EXPECT_EQ(pRetrieveRsp->numOfRows, 2);

  char*   pData = pRetrieveRsp->data;
  int32_t pos = 0;
  char*   strVal = NULL;

  //--- name ---
  {
    pos += sizeof(VarDataLenT);
    strVal = (char*)(pData + pos);
    pos += TSDB_USER_LEN;
    EXPECT_STREQ(strVal, "root");

    pos += sizeof(VarDataLenT);
    strVal = (char*)(pData + pos);
    pos += TSDB_USER_LEN;
    EXPECT_STREQ(strVal, "_root");
  }
}