eliminate faults. First stable version works as separate .exe file with configurations file
This commit is contained in:
@@ -30,6 +30,14 @@
|
||||
#include "html_report.h"
|
||||
#include "crc8.h"
|
||||
|
||||
typedef enum
|
||||
{
|
||||
protocol_ID_none = 0,
|
||||
UART_text = 1,
|
||||
UART_bin_readStruct = 2,
|
||||
PROTOCOL_ID_MAX = 255
|
||||
} protocol_ID_en;
|
||||
|
||||
struct comSet_t {
|
||||
int addrRS485;
|
||||
QString name;
|
||||
@@ -56,6 +64,8 @@ typedef comSet_t comSettings_t;
|
||||
typedef struct {
|
||||
QString MK_elf_file;
|
||||
QString MK_map_file;
|
||||
QString test_plan_path;
|
||||
QString test_cases_path;
|
||||
QString test_log_path;
|
||||
QString stand_report_html_path;
|
||||
QString map_parser;
|
||||
|
||||
@@ -1,59 +1,110 @@
|
||||
#include "protocol_handler.h"
|
||||
|
||||
|
||||
ProtocolHandler::ProtocolHandler(const QJsonObject& expectations)
|
||||
ProtocolHandler::ProtocolHandler(const QJsonObject& expectations,
|
||||
QString caseFile,
|
||||
QString jsonObjName)
|
||||
{
|
||||
expStr = jsonObjName;
|
||||
json.setJsonFile(caseFile);
|
||||
exp = expectations;
|
||||
}
|
||||
|
||||
void ProtocolHandler::feed(const QByteArray& data)
|
||||
bool ProtocolHandler::feed(const QByteArray& data, QString& msgError)
|
||||
{
|
||||
QString text = QString::fromUtf8(data);
|
||||
try
|
||||
{
|
||||
QString text = QString::fromUtf8(data);
|
||||
bool status = false;
|
||||
|
||||
buffer += text;
|
||||
messages.push_back(text);
|
||||
buffer += text;
|
||||
messages.push_back(text);
|
||||
|
||||
QJsonValue startExp = exp["start"];
|
||||
QJsonValue endExp = exp["end"];
|
||||
QJsonValue startExp = exp["start"];
|
||||
QJsonValue endExp = exp["end"];
|
||||
|
||||
if (startExp.isNull())
|
||||
{ startReceived = true; }
|
||||
if (startExp.isNull()) { startReceived = true; }
|
||||
|
||||
if (!startReceived)
|
||||
{ checkStart(); }
|
||||
if (!startReceived)
|
||||
{
|
||||
status = checkStart(msgError);
|
||||
if (status == false) { return status; }
|
||||
}
|
||||
|
||||
if (startReceived)
|
||||
{ checkSequence(); }
|
||||
if (startReceived)
|
||||
{
|
||||
status = checkSequence(msgError);
|
||||
if (status == false) { return status; }
|
||||
}
|
||||
|
||||
if (endExp.isNull())
|
||||
{ endReceived = true; }
|
||||
if (endExp.isNull()) { endReceived = true; }
|
||||
|
||||
if (!endReceived)
|
||||
{ checkEnd(); }
|
||||
if (!endReceived)
|
||||
{
|
||||
status = checkEnd(msgError);
|
||||
if (status == false) { return status; }
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (ErrOpenFile &errOpen)
|
||||
{
|
||||
msgError = QString("%1, path: %2").arg(errOpen.getMessage(), errOpen.getFilePath());
|
||||
return false;
|
||||
}
|
||||
catch (ErrInJsonSet &jsonSet)
|
||||
{
|
||||
if (jsonSet.checkErrFromJson())
|
||||
{
|
||||
msgError = QString("%1. %2 : %3. file: %4")
|
||||
.arg(jsonSet.getIntro(), jsonSet.getErrMsg(),
|
||||
jsonSet.getErrFromJson(), jsonSet.getFname());
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
msgError = QString("%1. %2: \nJSON parameter: %3\nJSON object: %4.\nfile: %5")
|
||||
.arg(jsonSet.getIntro(), jsonSet.getErrMsg(), jsonSet.getParam(),
|
||||
jsonSet.getJsonObj(), jsonSet.getFname());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ProtocolHandler::checkStart()
|
||||
bool ProtocolHandler::checkStart(QString &msgErr)
|
||||
{
|
||||
if (!buffer.contains("{"))
|
||||
return;
|
||||
{
|
||||
msgErr = "missing '{' in received data UART";
|
||||
return false;
|
||||
}
|
||||
|
||||
int start = buffer.indexOf("{");
|
||||
int end = buffer.indexOf("}");
|
||||
|
||||
if (end < 0)
|
||||
return;
|
||||
{
|
||||
msgErr = "missing '}' in received data UART";
|
||||
return false;
|
||||
}
|
||||
|
||||
QString jsonText = buffer.mid(start, end - start + 1);
|
||||
|
||||
QJsonDocument doc = QJsonDocument::fromJson(jsonText.toUtf8());
|
||||
|
||||
if (!doc.isObject()) return;
|
||||
if (!doc.isObject())
|
||||
{
|
||||
msgErr = "error in first message in received data UART";
|
||||
return false;
|
||||
}
|
||||
|
||||
QJsonObject msg = doc.object();
|
||||
QJsonObject expected = exp["start"].toObject()["fields"].toObject();
|
||||
|
||||
QJsonObject expected = exp["start"]
|
||||
.toObject()["fields"]
|
||||
.toObject();
|
||||
if (expected.isEmpty())
|
||||
{
|
||||
msgErr = "missing 'start' or 'fields' field";
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ok = true;
|
||||
|
||||
@@ -80,49 +131,66 @@ void ProtocolHandler::checkStart()
|
||||
}
|
||||
|
||||
buffer = buffer.mid(end + 1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ProtocolHandler::checkSequence()
|
||||
bool ProtocolHandler::checkSequence(QString& msgErr)
|
||||
{
|
||||
QJsonArray seq = exp["sequence"].toArray();
|
||||
QString type, value;
|
||||
|
||||
while (sequenceIndex < seq.size())
|
||||
{
|
||||
QJsonObject current =
|
||||
seq[sequenceIndex].toObject();
|
||||
QJsonObject current = seq[sequenceIndex].toObject();
|
||||
|
||||
QString type = current["type"].toString();
|
||||
QString value = current["value"].toString();
|
||||
if (current.isEmpty())
|
||||
{
|
||||
msgErr = "incorrect 'sequence' group";
|
||||
return false;
|
||||
}
|
||||
|
||||
json.jsonGetStrValue(current, "type", type, "sequence");
|
||||
json.jsonGetStrValue(current, "value", value, "sequence");
|
||||
|
||||
bool matched = false;
|
||||
|
||||
if (type == "contains")
|
||||
{
|
||||
matched = buffer.contains(value);
|
||||
}
|
||||
{ matched = buffer.contains(value); }
|
||||
else if (type == "regex")
|
||||
{
|
||||
QRegularExpression re(value);
|
||||
matched = re.match(buffer).hasMatch();
|
||||
}
|
||||
|
||||
if (matched)
|
||||
{
|
||||
sequenceIndex++;
|
||||
else {
|
||||
msgErr = QString("unsupported format '%1' in 'sequence' group!").arg(type);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!matched)
|
||||
{
|
||||
msgErr = QString("in received UART data the following string is not found: %1").arg(value);
|
||||
return false;
|
||||
}
|
||||
sequenceIndex++;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ProtocolHandler::checkEnd()
|
||||
bool ProtocolHandler::checkEnd(QString &msgErr)
|
||||
{
|
||||
QJsonObject endExp = exp["end"].toObject();
|
||||
QString type, value;
|
||||
|
||||
if (endExp.isEmpty()) return;
|
||||
if (endExp.isEmpty())
|
||||
{
|
||||
msgErr = "incorrect 'end' group";
|
||||
return false;
|
||||
}
|
||||
|
||||
QString type = endExp["type"].toString();
|
||||
QString value = endExp["value"].toString();
|
||||
json.jsonGetStrValue(endExp, "type", type, "end");
|
||||
json.jsonGetStrValue(endExp, "value", value, "end");
|
||||
|
||||
if (type == "contains")
|
||||
{
|
||||
@@ -130,8 +198,6 @@ void ProtocolHandler::checkEnd()
|
||||
{
|
||||
endReceived = true;
|
||||
|
||||
// sequence incomplete
|
||||
|
||||
QJsonArray seq = exp["sequence"].toArray();
|
||||
|
||||
if (sequenceIndex < seq.size())
|
||||
@@ -140,16 +206,21 @@ void ProtocolHandler::checkEnd()
|
||||
|
||||
m["stage"] = "sequence";
|
||||
m["step"] = sequenceIndex;
|
||||
m["expected"] =
|
||||
seq[sequenceIndex].toObject()
|
||||
m["expected"] = seq[sequenceIndex].toObject()
|
||||
.toVariantMap()["value"];
|
||||
|
||||
m["actual_buffer"] = buffer.right(200);
|
||||
|
||||
mismatches.append(m);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
msgErr = QString("not support %1 in 'end' group").arg(type);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ProtocolHandler::isDone(QString &msg) const
|
||||
@@ -158,37 +229,21 @@ bool ProtocolHandler::isDone(QString &msg) const
|
||||
bool endOk = true;
|
||||
|
||||
QJsonValue startExp = exp["start"];
|
||||
|
||||
QJsonValue endExp = exp["end"];
|
||||
|
||||
if (!startExp.isNull())
|
||||
{
|
||||
startOk = startReceived;
|
||||
}
|
||||
{ startOk = startReceived; }
|
||||
|
||||
if (!endExp.isNull())
|
||||
{
|
||||
endOk = endReceived;
|
||||
}
|
||||
{ endOk = endReceived; }
|
||||
|
||||
if (!startOk)
|
||||
{
|
||||
msg.append("\nstart sequence did not receive");
|
||||
}
|
||||
{ msg.append("\nstart sequence did not receive"); }
|
||||
|
||||
if (!endOk)
|
||||
{
|
||||
msg.append("\nend sequence did not receive");
|
||||
}
|
||||
{ msg.append("\nend sequence did not receive"); }
|
||||
|
||||
int seqSize =
|
||||
exp["sequence"]
|
||||
.toArray()
|
||||
.size();
|
||||
int seqSize = exp["sequence"].toArray().size();
|
||||
|
||||
return (
|
||||
startOk
|
||||
&& sequenceIndex >= seqSize
|
||||
&& endOk
|
||||
);
|
||||
return (startOk && sequenceIndex >= seqSize && endOk);
|
||||
}
|
||||
|
||||
@@ -1,26 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include "declarations.h"
|
||||
#include <json_processor.h>
|
||||
|
||||
class ProtocolHandler
|
||||
{
|
||||
public:
|
||||
ProtocolHandler(const QJsonObject& expectations);
|
||||
|
||||
void feed(const QByteArray& data);
|
||||
|
||||
ProtocolHandler(const QJsonObject& expectations,
|
||||
QString caseFile, QString jsonObjName);
|
||||
bool feed(const QByteArray& data, QString& msgError);
|
||||
bool isDone(QString &msg) const;
|
||||
|
||||
QStringList messages;
|
||||
QVariantList mismatches;
|
||||
|
||||
private:
|
||||
void checkStart();
|
||||
void checkSequence();
|
||||
void checkEnd();
|
||||
bool checkStart(QString& msgErr);
|
||||
bool checkSequence(QString& msgErr);
|
||||
bool checkEnd(QString& msgErr);
|
||||
|
||||
private:
|
||||
QJsonObject exp;
|
||||
QString expStr;
|
||||
JsonProcessor json;
|
||||
|
||||
QString buffer;
|
||||
|
||||
bool startReceived = false;
|
||||
|
||||
@@ -4,13 +4,22 @@ QStringList TestLoader::loadCases(const QString& group,
|
||||
const QString& caseFilter,
|
||||
int repeat)
|
||||
{
|
||||
QString fname = "C:/Danila/work/embedded_test_stand/configs/test_plan.json";
|
||||
QFile f(fname);
|
||||
JsonProcessor json;
|
||||
QJsonObject jsonObj;
|
||||
QString jsonPath = "Path";
|
||||
path_t path;
|
||||
|
||||
json.openJsonFile("../config.json", jsonObj);
|
||||
QJsonObject objPath = jsonObj.value(jsonPath).toObject();
|
||||
json.jsonGetStrValue(objPath, "test_plan_path", path.test_plan_path, jsonPath);
|
||||
json.jsonGetStrValue(objPath, "test_cases_path", path.test_cases_path, jsonPath);
|
||||
|
||||
QFile f(path.test_plan_path);
|
||||
QStringList result;
|
||||
|
||||
if (!f.open(QIODevice::ReadOnly))
|
||||
{
|
||||
throw(ErrOpenFile("cannot open json file", fname));
|
||||
throw(ErrOpenFile("cannot open json file", path.test_plan_path));
|
||||
}
|
||||
|
||||
QJsonObject root = QJsonDocument::fromJson(f.readAll()).object();
|
||||
@@ -19,7 +28,7 @@ QStringList TestLoader::loadCases(const QString& group,
|
||||
|
||||
if (arr.count() == 0)
|
||||
{
|
||||
throw(ErrOpenFile("invalid format in json file!", fname));
|
||||
throw(ErrOpenFile("invalid format in json file!", path.test_plan_path));
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +48,7 @@ QStringList TestLoader::loadCases(const QString& group,
|
||||
}
|
||||
|
||||
for (int i = 0; i < repeat; ++i)
|
||||
{ result << QString("C:/Danila/work/embedded_test_stand/configs/test_cases/%1").arg(name); }
|
||||
{ result << QString("%1%2").arg(path.test_cases_path, name); }
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "exceptions_handle.h"
|
||||
#include "json_processor.h"
|
||||
|
||||
class TestLoader
|
||||
{
|
||||
public:
|
||||
|
||||
static QStringList loadCases(const QString& group,
|
||||
const QString& caseFilter = QString(),
|
||||
int repeat = 1);
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user