memory leaks fixing

This commit is contained in:
Guillermo Roche 2020-05-31 19:24:35 +02:00
parent b8659268cc
commit 3387aa7ca1
11 changed files with 87 additions and 34 deletions

View File

@ -7,7 +7,6 @@ SOURCES += \
main.cpp \
conexion.cpp \
conexion_ssl.cpp \
thread_selector.cpp \
config_reader.cpp \
tests.cpp \
data_acces.cpp \
@ -19,7 +18,6 @@ SOURCES += \
HEADERS += \
conexion.h \
conexion_ssl.h \
thread_selector.h \
config_reader.h \
tests.h \
data_acces.h \

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.8.2, 2020-05-27T13:14:37. -->
<!-- Written by QtCreator 4.8.2, 2020-05-31T02:21:12. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>

View File

@ -56,7 +56,6 @@ void conexion::start_server(){
int client = accept(sock, (struct sockaddr*)&addr, &len);
std::thread t_client(conexion_client , client);
t_client.detach();
//cont++;
}
close(sock);
}
@ -70,8 +69,13 @@ void conexion_client(int client){
exit(EXIT_FAILURE);
}else{
session_manager* session = new session_manager(client);
while(!session->validate_pass());
bool enter=session->validate_pass();
while(!enter){
enter=session->validate_pass();
}
if(enter){
session->start_dialog();
}
delete (session);
close(client);
}

View File

@ -29,7 +29,6 @@ void conexion_ssl::start_server(){
perror("bad port in config file");
}
sock = this->create_socket(atoi(port.data()));
/* Handle connections */
SSL_CTX *ctx;
init_openssl();
@ -37,8 +36,6 @@ void conexion_ssl::start_server(){
this->configure_context(ctx);
//std::thread *hilos=new thread[50];
//int cont=0;
while(1) {
struct sockaddr_in addr;
uint len = sizeof(addr);
@ -46,7 +43,6 @@ void conexion_ssl::start_server(){
int client = accept(sock, (struct sockaddr*)&addr, &len);
std::thread t_client(conexion_client,ctx , client);
t_client.detach();
//cont++;
}
close(sock);
SSL_CTX_free(ctx);
@ -107,8 +103,6 @@ void conexion_ssl::configure_context(SSL_CTX *ctx)
}
void conexion_client(SSL_CTX *ctx,int client){
char buf [256];
if (client < 0) {
perror("Unable to accept");
exit(EXIT_FAILURE);
@ -122,9 +116,13 @@ void conexion_client(SSL_CTX *ctx,int client){
}
else {
session_manager* session = new session_manager_ssl(ssl);
while(!session->validate_pass());
bool enter=session->validate_pass();
while(!enter){
enter=session->validate_pass();
}
if(enter){
session->start_dialog();
//SSL_write(ssl,std::to_string(la->execute()).data() , sizeof (int));
}
delete (session);
}
SSL_shutdown(ssl);

View File

@ -1,6 +1,18 @@
#include "data_acces.h"
#include <openssl/sha.h>
#include <openssl/evp.h>
#include <string.h>
data_acces::data_acces()
{
}
char* data_acces::get_hash(char *data){
unsigned char hash[SHA512_DIGEST_LENGTH];
SHA512(reinterpret_cast<unsigned const char*>(data), strlen(data), hash);
unsigned char* encodedData= new unsigned char[200];
EVP_EncodeBlock(encodedData, hash, sizeof (hash));
return reinterpret_cast<char*>(encodedData);
}

View File

@ -17,6 +17,6 @@ public:
virtual bool get_package_exists(std::string package)=0;
virtual void create_user(std::string user, std::string pass, bool admin)=0;
virtual void remove_user(std::string user)=0;
static char* get_hash(char *data);
};
#endif // DATA_ACCES_H

View File

@ -5,11 +5,15 @@
#include "conexion_ssl.h"
#include "config_reader.h"
#include "tests.h"
#include "msql_acces.h"
#define file_config "config"
CPPUNIT_TEST_SUITE_REGISTRATION( test_basic );
int main()
{
//msql_acces data;
//data.create_user("otro", "otro", true);
//inicio de los tests
CPPUNIT_NS::TestResult testresult;
CPPUNIT_NS::TestResultCollector collectedresults;

View File

@ -20,6 +20,7 @@ std::string msql_acces::get_passwd(std::string username){
while(res->next())
ret = res->getString("passwd");
delete res;
delete pstmt;
return ret;
}
@ -31,6 +32,7 @@ bool msql_acces::get_admin(std::string username){
while(res->next())
ret = res->getBoolean("admin");
delete res;
delete pstmt;
return ret;
}
@ -55,6 +57,7 @@ std::list<std::string> msql_acces::get_pinfo(){
ret.push_back(aux);
}
delete res;
delete pstmt;
return ret;
}
@ -76,6 +79,7 @@ std::list<std::string> msql_acces::get_uinfo(){
ret.push_back(aux);
}
delete res;
delete pstmt;
return ret;
}
@ -85,6 +89,7 @@ void msql_acces::write_install(std::string package, std::string user){
pstmt->setString(1,package);
pstmt->setString(2,user);
pstmt->executeUpdate();
delete pstmt;
}
void msql_acces::write_remove(std::string package){
@ -92,6 +97,7 @@ void msql_acces::write_remove(std::string package){
con->prepareStatement("delete from packages where name=?");
pstmt->setString(1,package);
pstmt->executeUpdate();
delete pstmt;
}
bool msql_acces::get_package_exists(std::string package){
@ -99,22 +105,20 @@ bool msql_acces::get_package_exists(std::string package){
pstmt->setString(1,package);
sql::ResultSet *res = pstmt->executeQuery();
int ret=0;
while(res->next())
while(res->next()){
ret = res->getInt(1);
}
delete pstmt;
return ret>0;
}
void msql_acces::create_user(std::string user, std::string pass, bool admin){
sql::PreparedStatement *pstmt = con->prepareStatement("insert into users(username, passwd, admin) values(?, ?, ?)");
unsigned char hash[SHA512_DIGEST_LENGTH];
SHA512(reinterpret_cast<unsigned const char*>(pass.data()), strlen(pass.data()), hash);
unsigned char encodedData[200];
EVP_EncodeBlock(encodedData, hash, sizeof (hash));
pstmt->setString(1,user);
pstmt->setString(2,std::string(reinterpret_cast<char*>(encodedData)));
pstmt->setString(2,std::string(data_acces::get_hash(&pass[0])));
pstmt->setBoolean(3,admin);
pstmt->executeQuery();
delete pstmt;
}
void msql_acces::remove_user(std::string user){
@ -122,4 +126,5 @@ void msql_acces::remove_user(std::string user){
con->prepareStatement("delete from users where username=?");
pstmt->setString(1,user);
pstmt->executeUpdate();
delete pstmt;
}

View File

@ -19,12 +19,8 @@ bool session_manager::validate_pass(){
this->read_data(buffer, 256);
std::string user=buffer;
this->read_data(buffer, 256);
// std::string pass=buffer;
unsigned char hash[SHA512_DIGEST_LENGTH];
SHA512(reinterpret_cast<unsigned const char*>(buffer), strlen(buffer), hash);
unsigned char encodedData[200];
EVP_EncodeBlock(encodedData, hash, sizeof (hash));
std::string pass=std::string(reinterpret_cast<char*>(encodedData));
std::string pass=std::string(data_acces::get_hash(buffer));
delete[] (buffer);
if(this->data->get_passwd(user)==pass){
this->write_data("pass");
if(this->data->get_admin(user)){
@ -62,6 +58,7 @@ void session_manager::start_dialog(){
break;
}
}
delete[] (buffer);
}
int session_manager::execute(){
@ -75,11 +72,13 @@ int session_manager::execute(){
this->read_data(use_conf,256);
conf.change_uses(use_conf);
}else if(strcmp(use_conf,"n")!=0){
delete[] (n_package);
perror("fail in protocol comunication");
return -1;
}
delete [] (use_conf);
std::string result = this->appli_command("--ask", n_package);
delete[] (n_package);
if(result=="err"){
return -1;
}else{
@ -94,6 +93,7 @@ int session_manager::remove(){
char* n_package = new char[256];
this->read_data(n_package, 256);
std::string result = this->appli_command("--unmerge",n_package);
delete[] (n_package);
if(result=="err"){
return -1;
}else{
@ -121,10 +121,8 @@ std::string session_manager::appli_command(char comand[], char* n_package){
if(status>0){
this->write_data("ok");
ret = n_package;
delete[] (n_package);
}else{
this->write_data("bad");
delete[] (n_package);
ret = "err";
}
}
@ -167,6 +165,7 @@ void session_manager::remove_user(){
this->read_data(buffer,256);
this->read_data(buffer,256);
this->data->remove_user(std::string(buffer));
delete[] (buffer);
}
int session_manager::read_data(char* input, int size){

View File

@ -1,10 +1,15 @@
#include "tests.h"
#include "msql_acces.h"
#include <openssl/evp.h>
#include <openssl/sha.h>
#include <string.h>
void test_basic::setUp(){
this->conf=new config_reader("config");
this->data=new msql_acces();
}
void test_basic::testInitial(){
void test_basic::test_initial(){
std::string res;
CPPUNIT_ASSERT(true==this->conf->get_param("port",res));
CPPUNIT_ASSERT("4433"==res);
@ -20,3 +25,23 @@ void test_basic::pass_tests(){
testrunner.addTest (CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest ());
testrunner.run(testresult);
}
void test_basic::hash_test(){
CPPUNIT_ASSERT(strcmp(data_acces::get_hash("test1")
,data_acces::get_hash("test1"))==0);
CPPUNIT_ASSERT(strcmp(data_acces::get_hash("test1")
,data_acces::get_hash("test2"))!=0);
}
void test_basic::msql_loggin_test(){
std::string pass = this->data->get_passwd("test");
CPPUNIT_ASSERT(pass==std::string(data_acces::get_hash("ok")));
CPPUNIT_ASSERT(pass!=std::string(data_acces::get_hash("fail")));
}
void test_basic::msql_package_test(){
data->write_install("p1","test");
CPPUNIT_ASSERT(data->get_package_exists("p1"));
data->write_remove("p1");
CPPUNIT_ASSERT(!data->get_package_exists("p1"));
}

12
tests.h
View File

@ -12,17 +12,25 @@
#include <cppunit/CompilerOutputter.h>
#include <cppunit/XmlOutputter.h>
#include "config_reader.h"
#include "data_acces.h"
using namespace CppUnit;
class test_basic : public CppUnit::TestFixture{
CPPUNIT_TEST_SUITE(test_basic);
CPPUNIT_TEST(testInitial);
CPPUNIT_TEST(test_initial);
CPPUNIT_TEST(hash_test);
CPPUNIT_TEST(msql_loggin_test);
CPPUNIT_TEST(msql_package_test);
CPPUNIT_TEST_SUITE_END();
public:
void setUp(void);
void pass_tests();
private:
void testInitial(void);
void test_initial();
void hash_test();
void msql_loggin_test();
void msql_package_test();
config_reader *conf;
data_acces *data;
};
#endif // TESTS_H