未验证 提交 f22c0c07 编写于 作者: sangshuduo's avatar sangshuduo 提交者: GitHub

Hotfix/sangshuduo/td 3197 taosdemo coverity scan for master (#8397)

* [TD-3197]<fix>: taosdemo and taosdump coverity scan issues.

* exit if read sample file failed.

* fix converity scan issue.

* fix coverity scan issue.

* fix coverity scan memory leak.

* fix resource leak reported by coverity scan.

* fix taosdemo coverity scan issue.

* fix tcsetattr and getchar return value determination bug.

* fix coverity scan issue.

* fix two more coverity scan issues.

* fix stmt batch coverity scan issue.

* change to portable format.

* fix coverity scan issue

* [TD-10578]<fix>: taosdemo rest interface slow.

* fix missed 200

* fix taosdemo coverity scan issue.

* fix taosdemo coverity scan issue.

* fix taosdemo testcase for restful result.
Co-authored-by: NShuduo Sang <sdsang@taosdata.com>
上级 fc8b47ea
...@@ -3498,12 +3498,20 @@ static int postProceSql(char *host, struct sockaddr_in *pServAddr, uint16_t port ...@@ -3498,12 +3498,20 @@ static int postProceSql(char *host, struct sockaddr_in *pServAddr, uint16_t port
memset(response_buf, 0, RESP_BUF_LEN); memset(response_buf, 0, RESP_BUF_LEN);
resp_len = sizeof(response_buf) - 1; resp_len = sizeof(response_buf) - 1;
received = 0; received = 0;
char resEncodingChunk[] = "Encoding: chunked";
char resHttp[] = "HTTP/1.1 ";
int resHttpLen = strlen(resHttp);
char resHttpOk[] = "HTTP/1.1 200 OK";
int resHttpOkLen = strlen(resHttpOk);
do { do {
#ifdef WINDOWS #ifdef WINDOWS
bytes = recv(sockfd, response_buf + received, resp_len - received, 0); bytes = recv(sockfd, response_buf + received, resp_len - received, 0);
#else #else
bytes = read(sockfd, response_buf + received, resp_len - received); bytes = read(sockfd, response_buf + received, resp_len - received);
#endif #endif
verbosePrint("%s() LN%d: bytes:%d\n", __func__, __LINE__, bytes);
if (bytes < 0) { if (bytes < 0) {
free(request_buf); free(request_buf);
ERROR_EXIT("reading response from socket"); ERROR_EXIT("reading response from socket");
...@@ -3511,6 +3519,23 @@ static int postProceSql(char *host, struct sockaddr_in *pServAddr, uint16_t port ...@@ -3511,6 +3519,23 @@ static int postProceSql(char *host, struct sockaddr_in *pServAddr, uint16_t port
if (bytes == 0) if (bytes == 0)
break; break;
received += bytes; received += bytes;
response_buf[RESP_BUF_LEN - 1] = '\0';
if (strlen(response_buf)) {
verbosePrint("%s() LN%d: received:%d resp_len:%d, response_buf:\n%s\n",
__func__, __LINE__, received, resp_len, response_buf);
if (((NULL == strstr(response_buf, resEncodingChunk))
&& (0 == strncmp(response_buf, resHttp, resHttpLen)))
|| ((0 == strncmp(response_buf, resHttpOk, resHttpOkLen))
&& (NULL != strstr(response_buf, "\"status\":")))) {
debugPrint(
"%s() LN%d: received:%d resp_len:%d, response_buf:\n%s\n",
__func__, __LINE__, received, resp_len, response_buf);
break;
}
}
} while(received < resp_len); } while(received < resp_len);
if (received == resp_len) { if (received == resp_len) {
...@@ -3518,9 +3543,6 @@ static int postProceSql(char *host, struct sockaddr_in *pServAddr, uint16_t port ...@@ -3518,9 +3543,6 @@ static int postProceSql(char *host, struct sockaddr_in *pServAddr, uint16_t port
ERROR_EXIT("storing complete response from socket"); ERROR_EXIT("storing complete response from socket");
} }
response_buf[RESP_BUF_LEN - 1] = '\0';
printf("Response:\n%s\n", response_buf);
if (strlen(pThreadInfo->filePath) > 0) { if (strlen(pThreadInfo->filePath) > 0) {
appendResultBufToFile(response_buf, pThreadInfo); appendResultBufToFile(response_buf, pThreadInfo);
} }
...@@ -3533,6 +3555,11 @@ static int postProceSql(char *host, struct sockaddr_in *pServAddr, uint16_t port ...@@ -3533,6 +3555,11 @@ static int postProceSql(char *host, struct sockaddr_in *pServAddr, uint16_t port
close(sockfd); close(sockfd);
#endif #endif
if (NULL == strstr(response_buf, "\"status\":\"succ\"")) {
errorPrint("%s() LN%d, Response:\n%s\n",
__func__, __LINE__, response_buf);
return -1;
}
return 0; return 0;
} }
...@@ -12107,4 +12134,4 @@ int main(int argc, char *argv[]) { ...@@ -12107,4 +12134,4 @@ int main(int argc, char *argv[]) {
} }
return 0; return 0;
} }
\ No newline at end of file
...@@ -20,14 +20,16 @@ from util.dnodes import * ...@@ -20,14 +20,16 @@ from util.dnodes import *
import time import time
from datetime import datetime from datetime import datetime
import ast import ast
import re
# from assertpy import assert_that # from assertpy import assert_that
import subprocess import subprocess
class TDTestCase: class TDTestCase:
def init(self, conn, logSql): def init(self, conn, logSql):
tdLog.debug("start to execute %s" % __file__) tdLog.debug("start to execute %s" % __file__)
tdSql.init(conn.cursor(), logSql) tdSql.init(conn.cursor(), logSql)
def getBuildPath(self): def getBuildPath(self):
selfPath = os.path.dirname(os.path.realpath(__file__)) selfPath = os.path.dirname(os.path.realpath(__file__))
...@@ -40,52 +42,54 @@ class TDTestCase: ...@@ -40,52 +42,54 @@ class TDTestCase:
if ("taosd" in files): if ("taosd" in files):
rootRealPath = os.path.dirname(os.path.realpath(root)) rootRealPath = os.path.dirname(os.path.realpath(root))
if ("packaging" not in rootRealPath): if ("packaging" not in rootRealPath):
buildPath = root[:len(root)-len("/build/bin")] buildPath = root[:len(root) - len("/build/bin")]
break break
return buildPath return buildPath
# 获取taosc接口查询的结果文件中的内容,返回每行数据,并断言数据的第一列内容。 # 获取taosc接口查询的结果文件中的内容,返回每行数据,并断言数据的第一列内容。
def assertfileDataTaosc(self,filename,expectResult): def assertfileDataTaosc(self, filename, expectResult):
self.filename = filename self.filename = filename
self.expectResult = expectResult self.expectResult = expectResult
with open("%s" % filename, 'r+') as f1: with open("%s" % filename, 'r+') as f1:
for line in f1.readlines(): for line in f1.readlines():
queryResult = line.strip().split()[0] queryResult = line.strip().split()[0]
self.assertCheck(filename,queryResult,expectResult) self.assertCheck(filename, queryResult, expectResult)
# 获取restful接口查询的结果文件中的关键内容,目前的关键内容找到第一个key就跳出循,所以就只有一个数据。后续再修改多个结果文件。 # 获取restful接口查询的结果文件中的关键内容,目前的关键内容找到第一个key就跳出循,所以就只有一个数据。后续再修改多个结果文件。
def getfileDataRestful(self,filename): def getfileDataRestful(self, filename):
self.filename = filename self.filename = filename
with open("%s" % filename, 'r+') as f1: with open("%s" % filename, 'r+') as f1:
for line in f1.readlines(): for line in f1.readlines():
contents = line.strip() contents = line.strip()
if contents.find("data") != -1: if contents.find("data") != -1:
pattern = re.compile("{.*}")
contents = pattern.search(contents).group()
contentsDict = ast.literal_eval(contents) # 字符串转换为字典 contentsDict = ast.literal_eval(contents) # 字符串转换为字典
queryResult = contentsDict['data'][0][0] queryResult = contentsDict['data'][0][0]
break break
return queryResult return queryResult
# 获取taosc接口查询次数 # 获取taosc接口查询次数
def queryTimesTaosc(self,filename): def queryTimesTaosc(self, filename):
self.filename = filename self.filename = filename
command = 'cat %s |wc -l'% filename command = 'cat %s |wc -l' % filename
times = int(subprocess.getstatusoutput(command)[1]) times = int(subprocess.getstatusoutput(command)[1])
return times return times
# 获取restful接口查询次数 # 获取restful接口查询次数
def queryTimesRestful(self,filename): def queryTimesRestful(self, filename):
self.filename = filename self.filename = filename
command = 'cat %s |grep "200 OK" |wc -l'% filename command = 'cat %s |grep "200 OK" |wc -l' % filename
times = int(subprocess.getstatusoutput(command)[1]) times = int(subprocess.getstatusoutput(command)[1])
return times return times
# 定义断言结果是否正确。不正确返回错误结果,正确即通过。 # 定义断言结果是否正确。不正确返回错误结果,正确即通过。
def assertCheck(self,filename,queryResult,expectResult): def assertCheck(self, filename, queryResult, expectResult):
self.filename = filename self.filename = filename
self.queryResult = queryResult self.queryResult = queryResult
self.expectResult = expectResult self.expectResult = expectResult
args0 = (filename, queryResult, expectResult) args0 = (filename, queryResult, expectResult)
assert queryResult == expectResult , "Queryfile:%s ,result is %s != expect: %s" % args0 assert queryResult == expectResult, "Queryfile:%s ,result is %s != expect: %s" % args0
def run(self): def run(self):
buildPath = self.getBuildPath() buildPath = self.getBuildPath()
...@@ -93,109 +97,144 @@ class TDTestCase: ...@@ -93,109 +97,144 @@ class TDTestCase:
tdLog.exit("taosd not found!") tdLog.exit("taosd not found!")
else: else:
tdLog.info("taosd found in %s" % buildPath) tdLog.info("taosd found in %s" % buildPath)
binPath = buildPath+ "/build/bin/" binPath = buildPath + "/build/bin/"
# delete useless files # delete useless files
os.system("rm -rf ./query_res*") os.system("rm -rf ./query_res*")
os.system("rm -rf ./all_query*") os.system("rm -rf ./all_query*")
# taosc query: query specified table and query super table # taosc query: query specified table and query super table
os.system("%staosdemo -f tools/taosdemoAllTest/queryInsertdata.json" % binPath) os.system(
os.system("%staosdemo -f tools/taosdemoAllTest/queryTaosc.json" % binPath) "%staosdemo -f tools/taosdemoAllTest/queryInsertdata.json" %
binPath)
os.system(
"%staosdemo -f tools/taosdemoAllTest/queryTaosc.json" %
binPath)
os.system("cat query_res0.txt* > all_query_res0_taosc.txt") os.system("cat query_res0.txt* > all_query_res0_taosc.txt")
os.system("cat query_res1.txt* > all_query_res1_taosc.txt") os.system("cat query_res1.txt* > all_query_res1_taosc.txt")
os.system("cat query_res2.txt* > all_query_res2_taosc.txt") os.system("cat query_res2.txt* > all_query_res2_taosc.txt")
# correct Times testcases # correct Times testcases
queryTimes0Taosc = self.queryTimesTaosc("all_query_res0_taosc.txt") queryTimes0Taosc = self.queryTimesTaosc("all_query_res0_taosc.txt")
self.assertCheck("all_query_res0_taosc.txt",queryTimes0Taosc,6) self.assertCheck("all_query_res0_taosc.txt", queryTimes0Taosc, 6)
queryTimes1Taosc = self.queryTimesTaosc("all_query_res1_taosc.txt") queryTimes1Taosc = self.queryTimesTaosc("all_query_res1_taosc.txt")
self.assertCheck("all_query_res1_taosc.txt",queryTimes1Taosc,6) self.assertCheck("all_query_res1_taosc.txt", queryTimes1Taosc, 6)
queryTimes2Taosc = self.queryTimesTaosc("all_query_res2_taosc.txt") queryTimes2Taosc = self.queryTimesTaosc("all_query_res2_taosc.txt")
self.assertCheck("all_query_res2_taosc.txt",queryTimes2Taosc,20) self.assertCheck("all_query_res2_taosc.txt", queryTimes2Taosc, 20)
# correct data testcase # correct data testcase
self.assertfileDataTaosc("all_query_res0_taosc.txt","1604160000099") self.assertfileDataTaosc("all_query_res0_taosc.txt", "1604160000099")
self.assertfileDataTaosc("all_query_res1_taosc.txt","100") self.assertfileDataTaosc("all_query_res1_taosc.txt", "100")
self.assertfileDataTaosc("all_query_res2_taosc.txt","1604160000199") self.assertfileDataTaosc("all_query_res2_taosc.txt", "1604160000199")
# delete useless files # delete useless files
os.system("rm -rf ./query_res*") os.system("rm -rf ./query_res*")
os.system("rm -rf ./all_query*") os.system("rm -rf ./all_query*")
# use restful api to query # use restful api to query
os.system("%staosdemo -f tools/taosdemoAllTest/queryInsertrestdata.json" % binPath) os.system(
os.system("%staosdemo -f tools/taosdemoAllTest/queryRestful.json" % binPath) "%staosdemo -f tools/taosdemoAllTest/queryInsertrestdata.json" %
binPath)
os.system(
"%staosdemo -f tools/taosdemoAllTest/queryRestful.json" %
binPath)
os.system("cat query_res0.txt* > all_query_res0_rest.txt") os.system("cat query_res0.txt* > all_query_res0_rest.txt")
os.system("cat query_res1.txt* > all_query_res1_rest.txt") os.system("cat query_res1.txt* > all_query_res1_rest.txt")
os.system("cat query_res2.txt* > all_query_res2_rest.txt") os.system("cat query_res2.txt* > all_query_res2_rest.txt")
# correct Times testcases # correct Times testcases
queryTimes0Restful = self.queryTimesRestful("all_query_res0_rest.txt") queryTimes0Restful = self.queryTimesRestful("all_query_res0_rest.txt")
self.assertCheck("all_query_res0_rest.txt",queryTimes0Restful,6) self.assertCheck("all_query_res0_rest.txt", queryTimes0Restful, 6)
queryTimes1Restful = self.queryTimesRestful("all_query_res1_rest.txt") queryTimes1Restful = self.queryTimesRestful("all_query_res1_rest.txt")
self.assertCheck("all_query_res1_rest.txt",queryTimes1Restful,6) self.assertCheck("all_query_res1_rest.txt", queryTimes1Restful, 6)
queryTimes2Restful = self.queryTimesRestful("all_query_res2_rest.txt") queryTimes2Restful = self.queryTimesRestful("all_query_res2_rest.txt")
self.assertCheck("all_query_res2_rest.txt",queryTimes2Restful,4) self.assertCheck("all_query_res2_rest.txt", queryTimes2Restful, 4)
# correct data testcase # correct data testcase
data0 = self.getfileDataRestful("all_query_res0_rest.txt") data0 = self.getfileDataRestful("all_query_res0_rest.txt")
self.assertCheck('all_query_res0_rest.txt',data0,"2020-11-01 00:00:00.009") self.assertCheck(
'all_query_res0_rest.txt',
data0,
"2020-11-01 00:00:00.009")
data1 = self.getfileDataRestful("all_query_res1_rest.txt") data1 = self.getfileDataRestful("all_query_res1_rest.txt")
self.assertCheck('all_query_res1_rest.txt',data1,10) self.assertCheck('all_query_res1_rest.txt', data1, 10)
data2 = self.getfileDataRestful("all_query_res2_rest.txt") data2 = self.getfileDataRestful("all_query_res2_rest.txt")
self.assertCheck('all_query_res2_rest.txt',data2,"2020-11-01 00:00:00.004") self.assertCheck(
'all_query_res2_rest.txt',
data2,
"2020-11-01 00:00:00.004")
# query times less than or equal to 100 # query times less than or equal to 100
os.system("%staosdemo -f tools/taosdemoAllTest/queryInsertdata.json" % binPath) os.system(
os.system("%staosdemo -f tools/taosdemoAllTest/querySpeciMutisql100.json" % binPath) "%staosdemo -f tools/taosdemoAllTest/queryInsertdata.json" %
os.system("%staosdemo -f tools/taosdemoAllTest/querySuperMutisql100.json" % binPath) binPath)
os.system(
#query result print QPS "%staosdemo -f tools/taosdemoAllTest/querySpeciMutisql100.json" %
os.system("%staosdemo -f tools/taosdemoAllTest/queryInsertdata.json" % binPath) binPath)
os.system("%staosdemo -f tools/taosdemoAllTest/queryQps.json" % binPath) os.system(
"%staosdemo -f tools/taosdemoAllTest/querySuperMutisql100.json" %
binPath)
# query result print QPS
os.system(
"%staosdemo -f tools/taosdemoAllTest/queryInsertdata.json" %
binPath)
os.system(
"%staosdemo -f tools/taosdemoAllTest/queryQps.json" %
binPath)
# use illegal or out of range parameters query json file # use illegal or out of range parameters query json file
os.system("%staosdemo -f tools/taosdemoAllTest/queryInsertdata.json" % binPath) os.system(
exceptcode = os.system("%staosdemo -f tools/taosdemoAllTest/queryTimes0.json" % binPath) "%staosdemo -f tools/taosdemoAllTest/queryInsertdata.json" %
binPath)
exceptcode = os.system(
"%staosdemo -f tools/taosdemoAllTest/queryTimes0.json" %
binPath)
assert exceptcode != 0 assert exceptcode != 0
exceptcode0 = os.system("%staosdemo -f tools/taosdemoAllTest/queryTimesless0.json" % binPath) exceptcode0 = os.system(
"%staosdemo -f tools/taosdemoAllTest/queryTimesless0.json" %
binPath)
assert exceptcode0 != 0 assert exceptcode0 != 0
exceptcode1 = os.system("%staosdemo -f tools/taosdemoAllTest/queryConcurrentless0.json" % binPath) exceptcode1 = os.system(
"%staosdemo -f tools/taosdemoAllTest/queryConcurrentless0.json" %
binPath)
assert exceptcode1 != 0 assert exceptcode1 != 0
exceptcode2 = os.system("%staosdemo -f tools/taosdemoAllTest/queryConcurrent0.json" % binPath) exceptcode2 = os.system(
"%staosdemo -f tools/taosdemoAllTest/queryConcurrent0.json" %
binPath)
assert exceptcode2 != 0 assert exceptcode2 != 0
exceptcode3 = os.system("%staosdemo -f tools/taosdemoAllTest/querrThreadsless0.json" % binPath) exceptcode3 = os.system(
"%staosdemo -f tools/taosdemoAllTest/querrThreadsless0.json" %
binPath)
assert exceptcode3 != 0 assert exceptcode3 != 0
exceptcode4 = os.system("%staosdemo -f tools/taosdemoAllTest/querrThreads0.json" % binPath) exceptcode4 = os.system(
"%staosdemo -f tools/taosdemoAllTest/querrThreads0.json" %
binPath)
assert exceptcode4 != 0 assert exceptcode4 != 0
# delete useless files # delete useless files
os.system("rm -rf ./insert_res.txt") os.system("rm -rf ./insert_res.txt")
os.system("rm -rf tools/taosdemoAllTest/*.py.sql") os.system("rm -rf tools/taosdemoAllTest/*.py.sql")
os.system("rm -rf ./querySystemInfo*") os.system("rm -rf ./querySystemInfo*")
os.system("rm -rf ./query_res*") os.system("rm -rf ./query_res*")
os.system("rm -rf ./all_query*") os.system("rm -rf ./all_query*")
os.system("rm -rf ./test_query_res0.txt") os.system("rm -rf ./test_query_res0.txt")
def stop(self): def stop(self):
tdSql.close() tdSql.close()
tdLog.success("%s successfully executed" % __file__) tdLog.success("%s successfully executed" % __file__)
tdCases.addWindows(__file__, TDTestCase()) tdCases.addWindows(__file__, TDTestCase())
tdCases.addLinux(__file__, TDTestCase()) tdCases.addLinux(__file__, TDTestCase())
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册