passwdTest.c 11.2 KB
Newer Older
K
kailixu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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/>.
 */

// TAOS standard API example. The same syntax as MySQL, but only a subset
// to compile: gcc -o demo demo.c -ltaos

K
kailixu 已提交
19 20 21 22 23
/**
 *  passwdTest.c
 *   - Run the test case in clear TDengine environment with default root passwd 'taosdata'
 */

K
kailixu 已提交
24 25 26 27 28 29 30
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "taos.h"  // TAOS header file

K
kailixu 已提交
31 32 33
#define nDup     1
#define nRoot    10
#define nUser    10
K
kailixu 已提交
34
#define USER_LEN 24
K
kailixu 已提交
35
#define BUF_LEN  1024
K
kailixu 已提交
36 37 38 39 40 41 42 43 44 45

typedef uint16_t VarDataLenT;

#define TSDB_NCHAR_SIZE    sizeof(int32_t)
#define VARSTR_HEADER_SIZE sizeof(VarDataLenT)

#define GET_FLOAT_VAL(x)  (*(float *)(x))
#define GET_DOUBLE_VAL(x) (*(double *)(x))

#define varDataLen(v) ((VarDataLenT *)(v))[0]
K
kailixu 已提交
46

K
kailixu 已提交
47
void createUsers(TAOS *taos, const char *host, char *qstr);
K
kailixu 已提交
48
void passVerTestMulti(const char *host, char *qstr);
K
kailixu 已提交
49
void sysInfoTest(TAOS *taos, const char *host, char *qstr);
K
kailixu 已提交
50

K
kailixu 已提交
51 52 53
int   nPassVerNotified = 0;
TAOS *taosu[nRoot] = {0};
char  users[nUser][USER_LEN] = {0};
K
kailixu 已提交
54 55 56 57

void __taos_notify_cb(void *param, void *ext, int type) {
  switch (type) {
    case TAOS_NOTIFY_PASSVER: {
K
kailixu 已提交
58
      ++nPassVerNotified;
K
kailixu 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72
      printf("%s:%d type:%d user:%s ver:%d\n", __func__, __LINE__, type, param ? (char *)param : "NULL", *(int *)ext);
      break;
    }
    default:
      printf("%s:%d unknown type:%d\n", __func__, __LINE__, type);
      break;
  }
}

static void queryDB(TAOS *taos, char *command) {
  int       i;
  TAOS_RES *pSql = NULL;
  int32_t   code = -1;

K
kailixu 已提交
73
  for (i = 0; i < nDup; ++i) {
K
kailixu 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    if (NULL != pSql) {
      taos_free_result(pSql);
      pSql = NULL;
    }

    pSql = taos_query(taos, command);
    code = taos_errno(pSql);
    if (0 == code) {
      break;
    }
  }

  if (code != 0) {
    fprintf(stderr, "failed to run: %s, reason: %s\n", command, taos_errstr(pSql));
    taos_free_result(pSql);
    taos_close(taos);
    exit(EXIT_FAILURE);
  } else {
    fprintf(stderr, "success to run: %s\n", command);
  }

  taos_free_result(pSql);
}

K
kailixu 已提交
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
int printRow(char *str, TAOS_ROW row, TAOS_FIELD *fields, int numFields) {
  int  len = 0;
  char split = ' ';

  for (int i = 0; i < numFields; ++i) {
    if (i > 0) {
      str[len++] = split;
    }

    if (row[i] == NULL) {
      len += sprintf(str + len, "%s", "NULL");
      continue;
    }

    switch (fields[i].type) {
      case TSDB_DATA_TYPE_TINYINT:
        len += sprintf(str + len, "%d", *((int8_t *)row[i]));
        break;
      case TSDB_DATA_TYPE_UTINYINT:
        len += sprintf(str + len, "%u", *((uint8_t *)row[i]));
        break;
      case TSDB_DATA_TYPE_SMALLINT:
        len += sprintf(str + len, "%d", *((int16_t *)row[i]));
        break;
      case TSDB_DATA_TYPE_USMALLINT:
        len += sprintf(str + len, "%u", *((uint16_t *)row[i]));
        break;
      case TSDB_DATA_TYPE_INT:
        len += sprintf(str + len, "%d", *((int32_t *)row[i]));
        break;
      case TSDB_DATA_TYPE_UINT:
        len += sprintf(str + len, "%u", *((uint32_t *)row[i]));
        break;
      case TSDB_DATA_TYPE_BIGINT:
        len += sprintf(str + len, "%" PRId64, *((int64_t *)row[i]));
        break;
      case TSDB_DATA_TYPE_UBIGINT:
        len += sprintf(str + len, "%" PRIu64, *((uint64_t *)row[i]));
        break;
      case TSDB_DATA_TYPE_FLOAT: {
        float fv = 0;
        fv = GET_FLOAT_VAL(row[i]);
        len += sprintf(str + len, "%f", fv);
      } break;
      case TSDB_DATA_TYPE_DOUBLE: {
        double dv = 0;
        dv = GET_DOUBLE_VAL(row[i]);
        len += sprintf(str + len, "%lf", dv);
      } break;
      case TSDB_DATA_TYPE_BINARY:
      case TSDB_DATA_TYPE_NCHAR:
      case TSDB_DATA_TYPE_GEOMETRY: {
        int32_t charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
        memcpy(str + len, row[i], charLen);
        len += charLen;
      } break;
      case TSDB_DATA_TYPE_TIMESTAMP:
        len += sprintf(str + len, "%" PRId64, *((int64_t *)row[i]));
        break;
      case TSDB_DATA_TYPE_BOOL:
        len += sprintf(str + len, "%d", *((int8_t *)row[i]));
      default:
        break;
    }
  }
  return len;
}

static int printResult(TAOS_RES *res, char *output) {
  int         numFields = taos_num_fields(res);
  TAOS_FIELD *fields = taos_fetch_fields(res);
  char        header[BUF_LEN] = {0};
  int         len = 0;
  for (int i = 0; i < numFields; ++i) {
    len += sprintf(header + len, "%s ", fields[i].name);
  }
  puts(header);
  if (output) {
    strncpy(output, header, BUF_LEN);
  }

  TAOS_ROW row = NULL;
  while ((row = taos_fetch_row(res))) {
    char temp[BUF_LEN] = {0};
    printRow(temp, row, fields, numFields);
    puts(temp);
  }
}

K
kailixu 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200
int main(int argc, char *argv[]) {
  char qstr[1024];

  // connect to server
  if (argc < 2) {
    printf("please input server-ip \n");
    return 0;
  }

  TAOS *taos = taos_connect(argv[1], "root", "taosdata", NULL, 0);
  if (taos == NULL) {
    printf("failed to connect to server, reason:%s\n", "null taos" /*taos_errstr(taos)*/);
    exit(1);
  }
K
kailixu 已提交
201
  createUsers(taos, argv[1], qstr);
K
kailixu 已提交
202
  passVerTestMulti(argv[1], qstr);
K
kailixu 已提交
203
  sysInfoTest(taos, argv[1], qstr);
K
kailixu 已提交
204 205 206 207 208

  taos_close(taos);
  taos_cleanup();
}

K
kailixu 已提交
209
void createUsers(TAOS *taos, const char *host, char *qstr) {
K
kailixu 已提交
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
  // users
  for (int i = 0; i < nUser; ++i) {
    sprintf(users[i], "user%d", i);
    sprintf(qstr, "CREATE USER %s PASS 'taosdata'", users[i]);
    queryDB(taos, qstr);

    taosu[i] = taos_connect(host, users[i], "taosdata", NULL, 0);
    if (taosu[i] == NULL) {
      printf("failed to connect to server, user:%s, reason:%s\n", users[i], "null taos" /*taos_errstr(taos)*/);
      exit(1);
    }

    int code = taos_set_notify_cb(taosu[i], __taos_notify_cb, users[i], TAOS_NOTIFY_PASSVER);

    if (code != 0) {
      fprintf(stderr, "failed to run: taos_set_notify_cb for user:%s since %d\n", users[i], code);
    } else {
      fprintf(stderr, "success to run: taos_set_notify_cb for user:%s\n", users[i]);
    }

    // alter pass for users
    sprintf(qstr, "alter user %s pass 'taos'", users[i]);
    queryDB(taos, qstr);
  }
}

K
kailixu 已提交
236
void passVerTestMulti(const char *host, char *qstr) {
K
kailixu 已提交
237 238 239
  // root
  TAOS *taos[nRoot] = {0};
  char  userName[USER_LEN] = "root";
K
kailixu 已提交
240

K
kailixu 已提交
241
  for (int i = 0; i < nRoot; ++i) {
K
kailixu 已提交
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
    taos[i] = taos_connect(host, "root", "taosdata", NULL, 0);
    if (taos[i] == NULL) {
      printf("failed to connect to server, reason:%s\n", "null taos" /*taos_errstr(taos)*/);
      exit(1);
    }

    int code = taos_set_notify_cb(taos[i], __taos_notify_cb, userName, TAOS_NOTIFY_PASSVER);

    if (code != 0) {
      fprintf(stderr, "failed to run: taos_set_notify_cb since %d\n", code);
    } else {
      fprintf(stderr, "success to run: taos_set_notify_cb\n");
    }
  }

  queryDB(taos[0], "create database if not exists demo1 vgroups 1 minrows 10");
  queryDB(taos[0], "create database if not exists demo2 vgroups 1 minrows 10");
  queryDB(taos[0], "create database if not exists demo3 vgroups 1 minrows 10");

  queryDB(taos[0], "create table demo1.stb (ts timestamp, c1 int) tags(t1 int)");
  queryDB(taos[0], "create table demo2.stb (ts timestamp, c1 int) tags(t1 int)");
  queryDB(taos[0], "create table demo3.stb (ts timestamp, c1 int) tags(t1 int)");

  strcpy(qstr, "alter user root pass 'taos'");
  queryDB(taos[0], qstr);

K
kailixu 已提交
268 269 270
  // calculate the nPassVerNotified for root and users
  int nConn = nRoot + nUser;

K
kailixu 已提交
271 272 273
  for (int i = 0; i < 15; ++i) {
    printf("%s:%d [%d] second(s) elasped, passVer notification received:%d, total:%d\n", __func__, __LINE__, i,
           nPassVerNotified, nConn);
K
kailixu 已提交
274
    if (nPassVerNotified >= nConn) break;
K
kailixu 已提交
275 276 277
    sleep(1);
  }

K
kailixu 已提交
278 279 280 281
  // close the taos_conn
  for (int i = 0; i < nRoot; ++i) {
    taos_close(taos[i]);
    printf("%s:%d close taos[%d]\n", __func__, __LINE__, i);
K
kailixu 已提交
282
    // sleep(1);
K
kailixu 已提交
283 284
  }

K
kailixu 已提交
285 286 287
  for (int i = 0; i < nUser; ++i) {
    taos_close(taosu[i]);
    printf("%s:%d close taosu[%d]\n", __func__, __LINE__, i);
K
kailixu 已提交
288
    // sleep(1);
K
kailixu 已提交
289
  }
K
kailixu 已提交
290

K
kailixu 已提交
291
  fprintf(stderr, "######## %s #########\n", __func__);
K
kailixu 已提交
292
  if (nPassVerNotified >= nConn) {
K
kailixu 已提交
293 294
    fprintf(stderr, ">>> succeed to get passVer notification since nNotify %d >= nConn %d\n", nPassVerNotified,
            nConn);
K
kailixu 已提交
295
  } else {
K
kailixu 已提交
296
    fprintf(stderr, ">>> failed to get passVer notification since nNotify %d < nConn %d\n", nPassVerNotified, nConn);
K
kailixu 已提交
297
  }
K
kailixu 已提交
298
  fprintf(stderr, "######## %s #########\n", __func__);
K
kailixu 已提交
299
  // sleep(300);
K
kailixu 已提交
300 301
}

K
kailixu 已提交
302
void sysInfoTest(TAOS *taosRoot, const char *host, char *qstr) {
K
kailixu 已提交
303
  TAOS *taos[nRoot] = {0};
K
kailixu 已提交
304
  char  userName[USER_LEN] = "user0";
K
kailixu 已提交
305 306

  for (int i = 0; i < nRoot; ++i) {
K
kailixu 已提交
307
    taos[i] = taos_connect(host, "user0", "taos", NULL, 0);
K
kailixu 已提交
308 309 310 311 312 313
    if (taos[i] == NULL) {
      fprintf(stderr, "failed to connect to server, reason:%s\n", "null taos" /*taos_errstr(taos)*/);
      exit(1);
    }
  }

K
kailixu 已提交
314 315 316
  queryDB(taosRoot, "create database if not exists demo11 vgroups 1 minrows 10");
  queryDB(taosRoot, "create database if not exists demo12 vgroups 1 minrows 10");
  queryDB(taosRoot, "create database if not exists demo13 vgroups 1 minrows 10");
K
kailixu 已提交
317

K
kailixu 已提交
318 319 320
  queryDB(taosRoot, "create table demo11.stb (ts timestamp, c1 int) tags(t1 int)");
  queryDB(taosRoot, "create table demo12.stb (ts timestamp, c1 int) tags(t1 int)");
  queryDB(taosRoot, "create table demo13.stb (ts timestamp, c1 int) tags(t1 int)");
K
kailixu 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341

  sprintf(qstr, "show grants");
  char      output[BUF_LEN];
  TAOS_RES *res = NULL;
  int32_t   nRep = 0;

_REP: 
  fprintf(stderr, "######## %s loop:%d #########\n", __func__, nRep);
  res = taos_query(taos[0], qstr);
  if (taos_errno(res) != 0) {
    fprintf(stderr, "%s:%d failed to execute: %s since %s\n", __func__, __LINE__, qstr, taos_errstr(res));
    taos_free_result(res);
    exit(EXIT_FAILURE);
  }
  printResult(res, output);
  taos_free_result(res);
  if (!strstr(output, "timeseries")) {
    fprintf(stderr, "%s:%d expected output: 'timeseries' not occur\n", __func__, __LINE__);
    exit(EXIT_FAILURE);
  }

K
kailixu 已提交
342
  queryDB(taosRoot, "alter user user0 sysinfo 0");
K
kailixu 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358

  fprintf(stderr, "%s:%d sleep 2 seconds to wait HB take effect\n", __func__, __LINE__);
  for (int i = 1; i <= 2; ++i) {
    sleep(1);
  }

  res = taos_query(taos[0], qstr);
  if (taos_errno(res) != 0) {
    if (!strstr(taos_errstr(res), "Permission denied")) {
      fprintf(stderr, "%s:%d expected error: 'Permission denied' not occur\n", __func__, __LINE__);
      taos_free_result(res);
      exit(EXIT_FAILURE);
    }
  }
  taos_free_result(res);

K
kailixu 已提交
359
  queryDB(taosRoot, "alter user user0 sysinfo 1");
K
kailixu 已提交
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
  fprintf(stderr, "%s:%d sleep 2 seconds to wait HB take effect\n", __func__, __LINE__);
  for (int i = 1; i <= 2; ++i) {
    sleep(1);
  }

  if(++nRep < 5) {
    goto _REP;
  }

  // close the taos_conn
  for (int i = 0; i < nRoot; ++i) {
    taos_close(taos[i]);
    fprintf(stderr, "%s:%d close taos[%d]\n", __func__, __LINE__, i);
  }

  fprintf(stderr, "######## %s #########\n", __func__);
  fprintf(stderr, ">>> succeed to run sysInfoTest\n");
  fprintf(stderr, "######## %s #########\n", __func__);
K
kailixu 已提交
378
}