Commit 6391f957424e2946aba4a8b044fdfedc03cc0ed0

Authored by Eric Menezes Noronha
1 parent 2586f1df
Exists in master

cacic-teste: adicionado 4 novos testes;

install-cacic: pegando chave do login e gravando em arquivo;
caciccomm: carregando sessao em atributo privado da classe ao logar;
ccacic: novos métodos de auxilio ao json (setJsonToFile e
getJsonFromFile);
caciccomp: pegando nome do so tambem, modificado o nome das variaveis;
criado metodo toJsonObject nas classes cacicomp e operatingsystem;
cacic-teste/cacic-teste.pro
@@ -20,7 +20,8 @@ win32 { @@ -20,7 +20,8 @@ win32 {
20 SOURCES += \ 20 SOURCES += \
21 testcacic.cpp \ 21 testcacic.cpp \
22 ../src/ccacic.cpp \ 22 ../src/ccacic.cpp \
23 - ../src/cacic_computer.cpp 23 + ../src/cacic_computer.cpp \
  24 + ../src/operatingsystem.cpp
24 25
25 26
26 HEADERS += \ 27 HEADERS += \
@@ -28,7 +29,8 @@ HEADERS += \ @@ -28,7 +29,8 @@ HEADERS += \
28 ../src/ccacic.h \ 29 ../src/ccacic.h \
29 ../src/cacic_computer.h \ 30 ../src/cacic_computer.h \
30 ../src/cacic_comm.h \ 31 ../src/cacic_comm.h \
31 - ../src/console.h 32 + ../src/console.h \
  33 + ../src/operatingsystem.h
32 34
33 INCLUDEPATH += ../src \ 35 INCLUDEPATH += ../src \
34 ../src/crypto++/include/ 36 ../src/crypto++/include/
cacic-teste/testcacic.cpp
@@ -88,14 +88,15 @@ void CTestCacic::testInterfaceDeRede(){ @@ -88,14 +88,15 @@ void CTestCacic::testInterfaceDeRede(){
88 88
89 void CTestCacic::testpegarOS(){ 89 void CTestCacic::testpegarOS(){
90 //basta que seja um retorno válido, não precisa validar todos. 90 //basta que seja um retorno válido, não precisa validar todos.
91 - QVERIFY(OCacicComp.getOs().getIdSo() != -1 ); 91 + QVERIFY(OCacicComp.getOs().getIdOs() != -1 );
92 } 92 }
93 93
94 void CTestCacic::testConsole() 94 void CTestCacic::testConsole()
95 { 95 {
96 ConsoleObject console; 96 ConsoleObject console;
97 97
98 - QVERIFY(console("echo teste").toStdString() == "teste\n"); 98 +// QVERIFY(console("echo teste").toStdString() == "teste\n");
  99 + QVERIFY(false);
99 } 100 }
100 101
101 void CTestCacic::testPegarUsu(){ 102 void CTestCacic::testPegarUsu(){
@@ -165,7 +166,24 @@ void CTestCacic::testCacicCompToJsonObject() @@ -165,7 +166,24 @@ void CTestCacic::testCacicCompToJsonObject()
165 QVERIFY(!OCacicComp.toJsonObject().empty()); 166 QVERIFY(!OCacicComp.toJsonObject().empty());
166 } 167 }
167 168
168 -void CTestCacic::cleanupTestCase() 169 +void CTestCacic::testOperatingSystemToJsonObject()
  170 +{
  171 + QVERIFY(!OCacicComp.getOs().toJsonObject().empty());
  172 +}
  173 +
  174 +void CTestCacic::testJsonToFile()
169 { 175 {
  176 + QJsonObject json;
  177 + json["teste"] = QJsonValue::fromVariant(QString("teste"));
  178 + QVERIFY(OCacic.setJsonToFile(json, "teste.json"));
  179 +}
170 180
  181 +void CTestCacic::testJsonFromFile()
  182 +{
  183 + QVERIFY(OCacic.getJsonFromFile("teste.json")["teste"].toString() == "teste");
  184 +}
  185 +
  186 +void CTestCacic::cleanupTestCase()
  187 +{
  188 + OCacic.deleteFile("teste.json");
171 } 189 }
cacic-teste/testcacic.h
@@ -53,6 +53,9 @@ private slots: @@ -53,6 +53,9 @@ private slots:
53 void testDeCrypt(); 53 void testDeCrypt();
54 void testInstallCacicStart(); 54 void testInstallCacicStart();
55 void testCacicCompToJsonObject(); 55 void testCacicCompToJsonObject();
  56 + void testOperatingSystemToJsonObject();
  57 + void testJsonToFile();
  58 + void testJsonFromFile();
56 void cleanupTestCase(); 59 void cleanupTestCase();
57 }; 60 };
58 61
install-cacic/install-cacic.pro
@@ -24,13 +24,15 @@ TEMPLATE = app @@ -24,13 +24,15 @@ TEMPLATE = app
24 SOURCES += main.cpp \ 24 SOURCES += main.cpp \
25 installcacic.cpp \ 25 installcacic.cpp \
26 ../src/ccacic.cpp \ 26 ../src/ccacic.cpp \
27 - ../src/cacic_computer.cpp 27 + ../src/cacic_computer.cpp \
  28 + ../src/operatingsystem.cpp
28 29
29 HEADERS += \ 30 HEADERS += \
30 installcacic.h \ 31 installcacic.h \
31 ../src/ccacic.h \ 32 ../src/ccacic.h \
32 ../src/cacic_computer.h \ 33 ../src/cacic_computer.h \
33 - ../src/cacic_comm.h 34 + ../src/cacic_comm.h \
  35 + ../src/operatingsystem.h
34 36
35 INCLUDEPATH += ../src \ 37 INCLUDEPATH += ../src \
36 ../src/crypto++/include/ 38 ../src/crypto++/include/
install-cacic/installcacic.cpp
@@ -19,7 +19,10 @@ void InstallCacic::run(QStringList argv, int argc) { @@ -19,7 +19,10 @@ void InstallCacic::run(QStringList argv, int argc) {
19 QJsonObject jsonLogin = oCacicComm.login(&ok); 19 QJsonObject jsonLogin = oCacicComm.login(&ok);
20 if (ok){ 20 if (ok){
21 //TODO: Conectado. armazenar chave em arquivo e sessao na comm; 21 //TODO: Conectado. armazenar chave em arquivo e sessao na comm;
22 - qDebug() << "Logado"; 22 + oCacic.setChaveCrypt(jsonLogin["reply"].toObject()["chavecrip"].toString());
  23 + QJsonObject configToSave;
  24 + configToSave["chaveCrypt"] = QJsonValue::fromVariant(oCacic.getChaveCrypt());
  25 + oCacic.setJsonToFile(configToSave, "cacicTeste.json");
23 } 26 }
24 else 27 else
25 qDebug() << "falha no login"; 28 qDebug() << "falha no login";
src/cacic_comm.h
@@ -22,7 +22,7 @@ private: @@ -22,7 +22,7 @@ private:
22 // FIXME: Get from configuration 22 // FIXME: Get from configuration
23 QString usuario; 23 QString usuario;
24 QString password; 24 QString password;
25 - std::string session; 25 + QString session;
26 26
27 public: 27 public:
28 CacicComm (){ 28 CacicComm (){
@@ -166,6 +166,7 @@ public: @@ -166,6 +166,7 @@ public:
166 // Cria conexão e retorna Json da sessão 166 // Cria conexão e retorna Json da sessão
167 // qDebug() << "Conectando."; 167 // qDebug() << "Conectando.";
168 QJsonObject retorno = this->comm("/ws/neo/login", ok, QJsonObject::fromVariantMap(login), true); 168 QJsonObject retorno = this->comm("/ws/neo/login", ok, QJsonObject::fromVariantMap(login), true);
  169 + this->session = retorno["reply"].toObject()["session"].toString();
169 return retorno; 170 return retorno;
170 } 171 }
171 172
src/cacic_computer.cpp
@@ -2,7 +2,6 @@ @@ -2,7 +2,6 @@
2 2
3 CACIC_Computer::CACIC_Computer() 3 CACIC_Computer::CACIC_Computer()
4 { 4 {
5 - operatingSystem = OperatingSystem();  
6 networkInterface = networkInterfacesRunning(); 5 networkInterface = networkInterfacesRunning();
7 usuario = pegarUsu(); 6 usuario = pegarUsu();
8 } 7 }
@@ -56,6 +55,7 @@ QJsonObject CACIC_Computer::toJsonObject() @@ -56,6 +55,7 @@ QJsonObject CACIC_Computer::toJsonObject()
56 QJsonObject network; 55 QJsonObject network;
57 int count = 1; 56 int count = 1;
58 json["usuario"] = QJsonValue::fromVariant(QString::fromStdString(this->usuario)); 57 json["usuario"] = QJsonValue::fromVariant(QString::fromStdString(this->usuario));
  58 + json["operatingSystem"] = this->oOperatingSystem.toJsonObject();
59 foreach(QVariantMap auxMap, this->getNetworkInterface()){ 59 foreach(QVariantMap auxMap, this->getNetworkInterface()){
60 network["network" + QVariant::fromValue(count).toString()] = QJsonObject::fromVariantMap(auxMap); 60 network["network" + QVariant::fromValue(count).toString()] = QJsonObject::fromVariantMap(auxMap);
61 count++; 61 count++;
@@ -89,7 +89,7 @@ std::string CACIC_Computer::pegarUsu(){ @@ -89,7 +89,7 @@ std::string CACIC_Computer::pegarUsu(){
89 */ 89 */
90 OperatingSystem CACIC_Computer::getOs() const 90 OperatingSystem CACIC_Computer::getOs() const
91 { 91 {
92 - return operatingSystem; 92 + return oOperatingSystem;
93 } 93 }
94 94
95 std::string CACIC_Computer::getUser() const { 95 std::string CACIC_Computer::getUser() const {
src/cacic_computer.h
@@ -7,7 +7,6 @@ @@ -7,7 +7,6 @@
7 #include <QtSerialPort/QSerialPortInfo> 7 #include <QtSerialPort/QSerialPortInfo>
8 #include <QtNetwork/QtNetwork> 8 #include <QtNetwork/QtNetwork>
9 #include <QSysInfo> 9 #include <QSysInfo>
10 -  
11 #include <operatingsystem.h> 10 #include <operatingsystem.h>
12 11
13 class CACIC_Computer 12 class CACIC_Computer
@@ -23,7 +22,7 @@ public: @@ -23,7 +22,7 @@ public:
23 22
24 private: 23 private:
25 24
26 - OperatingSystem operatingSystem; 25 + OperatingSystem oOperatingSystem;
27 int pegarOS(); 26 int pegarOS();
28 std::string pegarUsu(); 27 std::string pegarUsu();
29 28
src/ccacic.cpp
@@ -113,6 +113,26 @@ QJsonValue CCacic::jsonValueFromJsonString(QString json, QString key) @@ -113,6 +113,26 @@ QJsonValue CCacic::jsonValueFromJsonString(QString json, QString key)
113 return QJsonDocument::fromJson(json.toUtf8()).object()[key]; 113 return QJsonDocument::fromJson(json.toUtf8()).object()[key];
114 } 114 }
115 115
  116 +bool CCacic::setJsonToFile(QJsonObject json, QString filepath)
  117 +{
  118 + QFile configFile(filepath);
  119 + if (!configFile.open(QIODevice::WriteOnly)){
  120 + qDebug() << "Não foi possivel abrir o arquivo.";
  121 + }
  122 + QJsonDocument docJson(json);
  123 + return (configFile.write(docJson.toJson()) != -1);
  124 +}
  125 +
  126 +QJsonObject CCacic::getJsonFromFile(QString filepath)
  127 +{
  128 + QFile configFile(filepath);
  129 + QJsonObject json;
  130 + if (!configFile.open(QIODevice::ReadOnly))
  131 + qDebug() << "Nao foi possivel ler o arquivo";
  132 + json = QJsonDocument::fromJson(configFile.readAll()).object();
  133 + return json;
  134 +}
  135 +
116 /*enCrypt 136 /*enCrypt
117 * @parameter std::string str_in: string que será encriptada (url). 137 * @parameter std::string str_in: string que será encriptada (url).
118 * std::string key: chave utilizada na encriptação (32 caracteres) 32*8 = 256 bits 138 * std::string key: chave utilizada na encriptação (32 caracteres) 32*8 = 256 bits
@@ -208,6 +228,16 @@ void CCacic::setChksisInfFilePath(const QString &amp;value) @@ -208,6 +228,16 @@ void CCacic::setChksisInfFilePath(const QString &amp;value)
208 { 228 {
209 chksisInfFilePath = value; 229 chksisInfFilePath = value;
210 } 230 }
  231 +QString CCacic::getChaveCrypt() const
  232 +{
  233 + return chaveCrypt;
  234 +}
  235 +
  236 +void CCacic::setChaveCrypt(const QString &value)
  237 +{
  238 + chaveCrypt = value;
  239 +}
  240 +
211 241
212 /*Getters/Setters 242 /*Getters/Setters
213 * End. 243 * End.
@@ -32,6 +32,8 @@ public: @@ -32,6 +32,8 @@ public:
32 bool deleteFolder(QString path); 32 bool deleteFolder(QString path);
33 bool deleteFile(QString path); 33 bool deleteFile(QString path);
34 QJsonValue jsonValueFromJsonString(QString json, QString key); 34 QJsonValue jsonValueFromJsonString(QString json, QString key);
  35 + bool setJsonToFile(QJsonObject json, QString filepath);
  36 + QJsonObject getJsonFromFile(QString filepath);
35 37
36 //Geters/seters: 38 //Geters/seters:
37 39
@@ -50,12 +52,16 @@ public: @@ -50,12 +52,16 @@ public:
50 QString getChksisInfFilePath() const; 52 QString getChksisInfFilePath() const;
51 void setChksisInfFilePath(const QString &value); 53 void setChksisInfFilePath(const QString &value);
52 54
  55 + QString getChaveCrypt() const;
  56 + void setChaveCrypt(const QString &value);
  57 +
53 private: 58 private:
54 QString cacicMainFolder; 59 QString cacicMainFolder;
55 QString mainModuleName; 60 QString mainModuleName;
56 QString urlGerente; 61 QString urlGerente;
57 QString gerColsInfFilePath; 62 QString gerColsInfFilePath;
58 QString chksisInfFilePath; 63 QString chksisInfFilePath;
  64 + QString chaveCrypt;
59 65
60 signals: 66 signals:
61 67
src/operatingsystem.cpp
1 #include "operatingsystem.h" 1 #include "operatingsystem.h"
2 2
  3 +OperatingSystem::OperatingSystem()
  4 +{
  5 + this->idOs = this->coletaIdOs();
  6 + this->nomeOs = this->coletaNomeOs();
  7 +}
  8 +
3 /*pegarOS 9 /*pegarOS
4 * @return: int; 10 * @return: int;
5 * retorna um id referente a versão do SO. 11 * retorna um id referente a versão do SO.
@@ -10,7 +16,7 @@ @@ -10,7 +16,7 @@
10 * 176 = Windows 8.1 16 * 176 = Windows 8.1
11 * 200 = Linux 17 * 200 = Linux
12 */ 18 */
13 -int OperatingSystem::pegarOS(){ 19 +int OperatingSystem::coletaIdOs(){
14 #if defined (Q_OS_WIN) 20 #if defined (Q_OS_WIN)
15 return QSysInfo::WindowsVersion; 21 return QSysInfo::WindowsVersion;
16 #elif defined (Q_OS_LINUX) 22 #elif defined (Q_OS_LINUX)
@@ -20,16 +26,36 @@ int OperatingSystem::pegarOS(){ @@ -20,16 +26,36 @@ int OperatingSystem::pegarOS(){
20 #endif 26 #endif
21 } 27 }
22 28
23 -int OperatingSystem::getIdSo() const 29 +QString OperatingSystem::coletaNomeOs()
24 { 30 {
25 - return idSo; 31 + QString text;
  32 + QStringList environment = QProcessEnvironment::systemEnvironment().toStringList();
  33 + foreach (text, environment) {
  34 + if (text.contains("OS=" , Qt::CaseInsensitive) ||
  35 + text.contains("SESSION=", Qt::CaseInsensitive) ){
  36 + QStringList split = text.split("=");
  37 + // qDebug() << split[1];
  38 + return split[1];
  39 + }
  40 + }
  41 + return "";
26 } 42 }
27 43
28 -void OperatingSystem::setIdSo(int value) 44 +QJsonObject OperatingSystem::toJsonObject()
29 { 45 {
30 - idSo = value; 46 + QJsonObject json;
  47 + json["idOs"] = QJsonValue::fromVariant(QVariant::fromValue(this->idOs));
  48 + json["nomeOs"] = QJsonValue::fromVariant(this->nomeOs);
  49 +// qDebug() << json;
  50 + return json;
31 } 51 }
32 52
33 -OperatingSystem::OperatingSystem() 53 +QString OperatingSystem::getNomeOs()
  54 +{
  55 + return this->nomeOs;
  56 +}
  57 +
  58 +int OperatingSystem::getIdOs() const
34 { 59 {
  60 + return this->idOs;
35 } 61 }
src/operatingsystem.h
@@ -3,16 +3,18 @@ @@ -3,16 +3,18 @@
3 3
4 #include <QtCore> 4 #include <QtCore>
5 #include <QSysInfo> 5 #include <QSysInfo>
  6 +#include <ccacic.h>
6 7
7 class OperatingSystem 8 class OperatingSystem
8 { 9 {
9 private: 10 private:
10 - int idSo;  
11 -  
12 - int pegarOS(); 11 + int idOs;
  12 + int coletaIdOs();
  13 + QString nomeOs;
  14 + QString coletaNomeOs();
13 public: 15 public:
14 OperatingSystem(); 16 OperatingSystem();
15 - 17 + QJsonObject toJsonObject();
16 enum OsList { 18 enum OsList {
17 MAC, 19 MAC,
18 LINUX_DEBIAN, 20 LINUX_DEBIAN,
@@ -21,8 +23,8 @@ public: @@ -21,8 +23,8 @@ public:
21 }; 23 };
22 static const enum OsList OsList; 24 static const enum OsList OsList;
23 25
24 - int getIdSo() const;  
25 - void setIdSo(int value); 26 + QString getNomeOs();
  27 + int getIdOs() const;
26 }; 28 };
27 29
28 #endif // OPERATINGSYSTEM_H 30 #endif // OPERATINGSYSTEM_H