Commit bf68278d9ac1c35fac12f049dfe71e17c7d4f424
1 parent
6391f957
Exists in
master
Pequeno refinamento no código do install cacic e no teste
Showing
7 changed files
with
56 additions
and
31 deletions
Show diff stats
cacic-teste/testcacic.cpp
@@ -111,9 +111,8 @@ void CTestCacic::testJsonValueFromJsonString() | @@ -111,9 +111,8 @@ void CTestCacic::testJsonValueFromJsonString() | ||
111 | void CTestCacic::testLogin(){ | 111 | void CTestCacic::testLogin(){ |
112 | bool ok; | 112 | bool ok; |
113 | QJsonObject jsonReply = OCacicComm.login(&ok); | 113 | QJsonObject jsonReply = OCacicComm.login(&ok); |
114 | - QJsonValue sessionvalue = jsonReply["reply"]; | ||
115 | -// qDebug() << sessionvalue.toObject()["session"].toString(); | ||
116 | -// qDebug() << sessionvalue.toString(); | 114 | + QJsonObject sessionvalue = jsonReply["reply"].toObject(); |
115 | + OCacic.setChaveCrypt(sessionvalue["chavecrip"].toString()); | ||
117 | QVERIFY(ok); | 116 | QVERIFY(ok); |
118 | } | 117 | } |
119 | 118 | ||
@@ -127,17 +126,16 @@ void CTestCacic::testSslConnection() | @@ -127,17 +126,16 @@ void CTestCacic::testSslConnection() | ||
127 | } | 126 | } |
128 | 127 | ||
129 | void CTestCacic::testEnCrypt(){ | 128 | void CTestCacic::testEnCrypt(){ |
130 | - std::string key = "qwertyuiopasdfghjklzxcvbnmqwerty"; //chave de 256 bits (32 caracteres) | ||
131 | std::string IV = "0123456789123456"; //iv nunca se repete para a mesma senha. | 129 | std::string IV = "0123456789123456"; //iv nunca se repete para a mesma senha. |
132 | std::string input = "aqui vai a url que sera encriptada"; | 130 | std::string input = "aqui vai a url que sera encriptada"; |
133 | - QVERIFY(OCacic.enCrypt(input, key, IV) == "Va2WiUrdTVrn93tCrtx0njjU4HDpn7VFCsCVr/+YgaBCVQ=="); | 131 | + this->cripTeste = OCacic.enCrypt(input, IV); |
132 | + QVERIFY(!this->cripTeste.isNull()); | ||
134 | } | 133 | } |
135 | 134 | ||
136 | void CTestCacic::testDeCrypt(){ | 135 | void CTestCacic::testDeCrypt(){ |
137 | - std::string key = "qwertyuiopasdfghjklzxcvbnmqwerty"; //chave de 256 bits (32 caracteres) | ||
138 | std::string IV = "0123456789123456asas"; //iv nunca se repete para a mesma senha. | 136 | std::string IV = "0123456789123456asas"; //iv nunca se repete para a mesma senha. |
139 | - std::string input = "Va2WiUrdTVrn93tCrtx0njjU4HDpn7VFCsCVr/+YgaBCVQ=="; | ||
140 | - QVERIFY(OCacic.deCrypt(input, key, IV) == "aqui vai a url que sera encriptada"); | 137 | + std::string input = this->cripTeste.toStdString(); |
138 | + QVERIFY(OCacic.deCrypt(input, IV) == "aqui vai a url que sera encriptada"); | ||
141 | 139 | ||
142 | } | 140 | } |
143 | 141 |
cacic-teste/testcacic.h
install-cacic/install-cacic.pro.user
1 | <?xml version="1.0" encoding="UTF-8"?> | 1 | <?xml version="1.0" encoding="UTF-8"?> |
2 | <!DOCTYPE QtCreatorProject> | 2 | <!DOCTYPE QtCreatorProject> |
3 | -<!-- Written by QtCreator 3.1.2, 2014-08-14T10:57:32. --> | 3 | +<!-- Written by QtCreator 3.1.2, 2014-08-14T17:39:38. --> |
4 | <qtcreator> | 4 | <qtcreator> |
5 | <data> | 5 | <data> |
6 | <variable>ProjectExplorer.Project.ActiveTarget</variable> | 6 | <variable>ProjectExplorer.Project.ActiveTarget</variable> |
@@ -57,7 +57,7 @@ | @@ -57,7 +57,7 @@ | ||
57 | <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 5.3.0 MinGW 32bit</value> | 57 | <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 5.3.0 MinGW 32bit</value> |
58 | <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 5.3.0 MinGW 32bit</value> | 58 | <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 5.3.0 MinGW 32bit</value> |
59 | <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.53.win32_mingw482_kit</value> | 59 | <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.53.win32_mingw482_kit</value> |
60 | - <value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value> | 60 | + <value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">1</value> |
61 | <value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value> | 61 | <value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value> |
62 | <value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value> | 62 | <value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value> |
63 | <valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0"> | 63 | <valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0"> |
@@ -114,7 +114,7 @@ | @@ -114,7 +114,7 @@ | ||
114 | <value type="bool" key="Qt4ProjectManager.Qt4BuildConfiguration.UseShadowBuild">true</value> | 114 | <value type="bool" key="Qt4ProjectManager.Qt4BuildConfiguration.UseShadowBuild">true</value> |
115 | </valuemap> | 115 | </valuemap> |
116 | <valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.1"> | 116 | <valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.1"> |
117 | - <value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">E:/LightBase/cacic-agente-project/cacic-agente/build-install-cacic-Release</value> | 117 | + <value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">E:/LightBase/cacic-agente-project/builds/install-cacic</value> |
118 | <valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0"> | 118 | <valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0"> |
119 | <valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0"> | 119 | <valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0"> |
120 | <value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value> | 120 | <value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value> |
install-cacic/installcacic.cpp
@@ -18,20 +18,28 @@ void InstallCacic::run(QStringList argv, int argc) { | @@ -18,20 +18,28 @@ void InstallCacic::run(QStringList argv, int argc) { | ||
18 | oCacicComm.setPassword(this->argumentos["password"]); | 18 | oCacicComm.setPassword(this->argumentos["password"]); |
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 | + //conectado, grava a chave na classe; |
22 | oCacic.setChaveCrypt(jsonLogin["reply"].toObject()["chavecrip"].toString()); | 22 | oCacic.setChaveCrypt(jsonLogin["reply"].toObject()["chavecrip"].toString()); |
23 | - QJsonObject configToSave; | 23 | + oCacic.setCacicMainFolder("c:/cacic"); |
24 | + oCacic.createFolder(oCacic.getCacicMainFolder()); | ||
25 | + //grava chave em arquivo json; | ||
26 | + QJsonObject configJson = oCacic.getJsonFromFile(oCacic.getCacicMainFolder() + "/cacicTeste.json"); | ||
27 | + QJsonObject configToSave = configJson["configs"].toObject(); | ||
24 | configToSave["chaveCrypt"] = QJsonValue::fromVariant(oCacic.getChaveCrypt()); | 28 | configToSave["chaveCrypt"] = QJsonValue::fromVariant(oCacic.getChaveCrypt()); |
25 | - oCacic.setJsonToFile(configToSave, "cacicTeste.json"); | 29 | + configJson["configs"] = configToSave; |
30 | + oCacic.setJsonToFile(configJson, oCacic.getCacicMainFolder() + "/cacicTeste.json"); | ||
26 | } | 31 | } |
27 | else | 32 | else |
28 | - qDebug() << "falha no login"; | 33 | + std::cout << "Nao foi possivel realizar o login.\n " |
34 | + << jsonLogin["error"].toString().toStdString(); | ||
29 | } else { | 35 | } else { |
30 | - std::cout << "Parametros incorretos. ([obrigatorios] <opcional>)\n"; | ||
31 | - std::cout << " [-host=url_gerente] <Descricao aqui>\n"; | ||
32 | - std::cout << " [-user=usuario] <Descricao aqui>\n"; | ||
33 | - std::cout << " [-password=senha] <Descricao aqui>\n"; | ||
34 | - std::cout << " <-help> <Descricao aqui>\n"; | 36 | + std::cout << "\nInstalador do Agente Cacic.\n\n" |
37 | + << "Parametros incorretos. (<obrigatorios> [opcional])\n\n" | ||
38 | + << "<-host=url_gerente> <-user=usuario> <-password=senha> [-help]\n\n" | ||
39 | + << " <-host=url_gerente> url_gerente: Caminho para a aplicação do gerente.\n" | ||
40 | + << " <-user=usuario> usuario: usuário de login no gerente.\n" | ||
41 | + << " <-password=senha> senha: senha de login no gerente\n" | ||
42 | + << " [-help] Lista todos comandos.\n"; | ||
35 | } | 43 | } |
36 | 44 | ||
37 | emit finished(); | 45 | emit finished(); |
src/cacic_comm.h
@@ -166,7 +166,8 @@ public: | @@ -166,7 +166,8 @@ 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 | + if (*ok) |
170 | + this->session = retorno["reply"].toObject()["session"].toString(); | ||
170 | return retorno; | 171 | return retorno; |
171 | } | 172 | } |
172 | 173 |
src/ccacic.cpp
@@ -112,23 +112,38 @@ QJsonValue CCacic::jsonValueFromJsonString(QString json, QString key) | @@ -112,23 +112,38 @@ QJsonValue CCacic::jsonValueFromJsonString(QString json, QString key) | ||
112 | { | 112 | { |
113 | return QJsonDocument::fromJson(json.toUtf8()).object()[key]; | 113 | return QJsonDocument::fromJson(json.toUtf8()).object()[key]; |
114 | } | 114 | } |
115 | - | 115 | +/*setJsonToFile |
116 | + * Grava json em um arquivo. | ||
117 | + * IMPORTANTE: não parei pra olhar a fundo, mas a princípio é necessário ler o arquivo | ||
118 | + * para pegar o json das informações anteriores, senão informações serão duplicadas ou | ||
119 | + * excluidas; | ||
120 | + * @parameter QJsonObject json: json que será gravado | ||
121 | + * @parameter QString filepath: caminho do arquivo onde será gravado | ||
122 | + * | ||
123 | + */ | ||
116 | bool CCacic::setJsonToFile(QJsonObject json, QString filepath) | 124 | bool CCacic::setJsonToFile(QJsonObject json, QString filepath) |
117 | { | 125 | { |
118 | QFile configFile(filepath); | 126 | QFile configFile(filepath); |
119 | if (!configFile.open(QIODevice::WriteOnly)){ | 127 | if (!configFile.open(QIODevice::WriteOnly)){ |
120 | - qDebug() << "Não foi possivel abrir o arquivo."; | 128 | + return false; |
121 | } | 129 | } |
122 | QJsonDocument docJson(json); | 130 | QJsonDocument docJson(json); |
131 | + | ||
123 | return (configFile.write(docJson.toJson()) != -1); | 132 | return (configFile.write(docJson.toJson()) != -1); |
124 | } | 133 | } |
125 | 134 | ||
135 | +/*getJsonFromFile | ||
136 | + * Carrega json de um arquivo. | ||
137 | + * @return QJsonObject: json que será recuperado | ||
138 | + * @parameter QString filepath: caminho do arquivo onde será recuperado | ||
139 | + */ | ||
126 | QJsonObject CCacic::getJsonFromFile(QString filepath) | 140 | QJsonObject CCacic::getJsonFromFile(QString filepath) |
127 | { | 141 | { |
128 | QFile configFile(filepath); | 142 | QFile configFile(filepath); |
129 | QJsonObject json; | 143 | QJsonObject json; |
130 | - if (!configFile.open(QIODevice::ReadOnly)) | ||
131 | - qDebug() << "Nao foi possivel ler o arquivo"; | 144 | + if (!configFile.open(QIODevice::ReadOnly)){ |
145 | + return json; | ||
146 | + } | ||
132 | json = QJsonDocument::fromJson(configFile.readAll()).object(); | 147 | json = QJsonDocument::fromJson(configFile.readAll()).object(); |
133 | return json; | 148 | return json; |
134 | } | 149 | } |
@@ -142,8 +157,9 @@ QJsonObject CCacic::getJsonFromFile(QString filepath) | @@ -142,8 +157,9 @@ QJsonObject CCacic::getJsonFromFile(QString filepath) | ||
142 | * exemplo de iv: 0123456789123456 | 157 | * exemplo de iv: 0123456789123456 |
143 | * @return std:string: retorna a string encriptada convertida em base64. | 158 | * @return std:string: retorna a string encriptada convertida em base64. |
144 | * */ | 159 | * */ |
145 | -std::string CCacic::enCrypt(std::string str_in, std::string key, std::string iv) { | 160 | +QString CCacic::enCrypt(std::string str_in, std::string iv) { |
146 | std::string str_out; | 161 | std::string str_out; |
162 | + std::string key = this->getChaveCrypt().toStdString(); | ||
147 | CryptoPP::CFB_Mode<CryptoPP::AES>::Encryption encryption((byte*)key.c_str(), key.length(), (byte*)iv.c_str()); | 163 | CryptoPP::CFB_Mode<CryptoPP::AES>::Encryption encryption((byte*)key.c_str(), key.length(), (byte*)iv.c_str()); |
148 | CryptoPP::StringSource encryptor(str_in, true, | 164 | CryptoPP::StringSource encryptor(str_in, true, |
149 | new CryptoPP::StreamTransformationFilter(encryption, | 165 | new CryptoPP::StreamTransformationFilter(encryption, |
@@ -153,7 +169,7 @@ std::string CCacic::enCrypt(std::string str_in, std::string key, std::string iv) | @@ -153,7 +169,7 @@ std::string CCacic::enCrypt(std::string str_in, std::string key, std::string iv) | ||
153 | ) | 169 | ) |
154 | ); | 170 | ); |
155 | //qDebug(QString::fromStdString(str_out).toLocal8Bit()); | 171 | //qDebug(QString::fromStdString(str_out).toLocal8Bit()); |
156 | - return str_out; | 172 | + return QString::fromStdString(str_out); |
157 | } | 173 | } |
158 | 174 | ||
159 | /*deCrypt | 175 | /*deCrypt |
@@ -166,8 +182,9 @@ std::string CCacic::enCrypt(std::string str_in, std::string key, std::string iv) | @@ -166,8 +182,9 @@ std::string CCacic::enCrypt(std::string str_in, std::string key, std::string iv) | ||
166 | // * *exemplo de iv: 0123456789123456 | 182 | // * *exemplo de iv: 0123456789123456 |
167 | // * @return QString: retorna a string desencriptada convertida em base64. | 183 | // * @return QString: retorna a string desencriptada convertida em base64. |
168 | // * */ | 184 | // * */ |
169 | -std::string CCacic::deCrypt(std::string str_in, std::string key, std::string iv) { | 185 | +QString CCacic::deCrypt(std::string str_in, std::string iv) { |
170 | std::string str_out; | 186 | std::string str_out; |
187 | + std::string key = this->getChaveCrypt().toStdString(); | ||
171 | CryptoPP::CFB_Mode<CryptoPP::AES>::Decryption decryption((byte*)key.c_str(), key.length(), (byte*)iv.c_str()); | 188 | CryptoPP::CFB_Mode<CryptoPP::AES>::Decryption decryption((byte*)key.c_str(), key.length(), (byte*)iv.c_str()); |
172 | 189 | ||
173 | CryptoPP::StringSource decryptor(str_in, true, | 190 | CryptoPP::StringSource decryptor(str_in, true, |
@@ -176,7 +193,7 @@ std::string CCacic::deCrypt(std::string str_in, std::string key, std::string iv) | @@ -176,7 +193,7 @@ std::string CCacic::deCrypt(std::string str_in, std::string key, std::string iv) | ||
176 | new CryptoPP::StringSink(str_out)) | 193 | new CryptoPP::StringSink(str_out)) |
177 | ) | 194 | ) |
178 | ); | 195 | ); |
179 | - return str_out; | 196 | + return QString::fromStdString(str_out); |
180 | } | 197 | } |
181 | 198 | ||
182 | 199 |
src/ccacic.h
@@ -26,8 +26,8 @@ public: | @@ -26,8 +26,8 @@ public: | ||
26 | QString getValueFromFile(QString sectionName, QString keyName, QString filePath); | 26 | QString getValueFromFile(QString sectionName, QString keyName, QString filePath); |
27 | void setValueToFile(QString sectionName, QString keyName, QString value, QString filePath); | 27 | void setValueToFile(QString sectionName, QString keyName, QString value, QString filePath); |
28 | QString getValueFromTags(QString fullString, QString tag, QString tagType = "[]"); | 28 | QString getValueFromTags(QString fullString, QString tag, QString tagType = "[]"); |
29 | - std::string enCrypt(std::string str_in, std::string key, std::string iv); | ||
30 | - std::string deCrypt(std::string str_in, std::string key, std::string iv); | 29 | + QString enCrypt(std::string str_in, std::string iv); |
30 | + QString deCrypt(std::string str_in, std::string iv); | ||
31 | bool createFolder(QString path); | 31 | bool createFolder(QString path); |
32 | bool deleteFolder(QString path); | 32 | bool deleteFolder(QString path); |
33 | bool deleteFile(QString path); | 33 | bool deleteFile(QString path); |