Skip to content

Commit 7f1df67

Browse files
committed
Adding test for XML encryption/decryption
1 parent 2482506 commit 7f1df67

File tree

6 files changed

+104
-1
lines changed

6 files changed

+104
-1
lines changed

.github/workflows/test.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,3 +497,16 @@ jobs:
497497
run: |
498498
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
499499
./.github/scripts/build.sh
500+
501+
test_xml_encryption:
502+
name: 'Encryption/Decryption test'
503+
strategy:
504+
fail-fast: false
505+
needs: Build
506+
runs-on: ubuntu-22.04
507+
508+
steps:
509+
- name: Test_Encryption_Decryption
510+
shell: bash
511+
run: |
512+
valgrind ./build/libs/libdecrypt/test/test_encrypt_decrypt

libs/libdecrypt/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,6 @@ target_link_libraries(libdecrypt
4444
${OPENSSL_LIBRARIES}
4545
)
4646
install(TARGETS libdecrypt DESTINATION bin)
47-
install(FILES ${LIB_HEADERS} DESTINATION include/libdecrypt)
47+
install(FILES ${LIB_HEADERS} DESTINATION include/libdecrypt)
48+
49+
add_subdirectory(test)

libs/libdecrypt/test/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
project(EncryptionDecryptionTest)
3+
4+
# Find OpenSSL
5+
find_package(OpenSSL REQUIRED)
6+
7+
add_executable(test_encrypt_decrypt src/test.cpp src/main.cpp)
8+
9+
set(PUBLIC_KEY_FILE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../libencrypt/public_key.pem")
10+
target_compile_definitions(test_encrypt_decrypt PRIVATE PUBLIC_KEY_FILE="${PUBLIC_KEY_FILE_PATH}")
11+
12+
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../../libencrypt/src"
13+
"${CMAKE_CURRENT_SOURCE_DIR}/../libdecrypt/src"
14+
"${CMAKE_CURRENT_SOURCE_DIR}/src")
15+
16+
target_link_libraries(test_encrypt_decrypt
17+
libpugixml
18+
libdecrypt
19+
libencrypt)

libs/libdecrypt/test/src/main.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "test.h"
2+
3+
int main() {
4+
std::string testFilePath = "test.xml";
5+
std::string publicKeyFile = PUBLIC_KEY_FILE; // Path to your public key file
6+
std::string encryptedFilePath = "test.xmle";
7+
8+
// Step 1: Create a test XML file
9+
createTestXMLFile(testFilePath);
10+
11+
std::string originalContent = readFileToString(testFilePath);
12+
13+
// Step 2: Encrypt XML content
14+
Encryption encryption;
15+
if (!encryption.encryptFile(publicKeyFile, testFilePath)) {
16+
std::cerr << "Encryption failed" << std::endl;
17+
return 1;
18+
}
19+
20+
// Step 3: Decrypt XML content
21+
Decryption decryption(encryptedFilePath);
22+
std::string decryptedContent = decryption.getDecryptedContent();
23+
24+
// Step 4: Compare original XML content with decrypted XML content
25+
if (decryptedContent == originalContent) {
26+
std::cout << "Test passed: Decrypted content matches original content" << std::endl;
27+
} else {
28+
std::cerr << "Test failed: Decrypted content does not match original content" << std::endl;
29+
return 1;
30+
}
31+
32+
return 0;
33+
}

libs/libdecrypt/test/src/test.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "test.h"
2+
3+
#ifndef PUBLIC_KEY_FILE
4+
#define PUBLIC_KEY_FILE "public_key.pem" // Default public key file
5+
#endif
6+
7+
// Function to create a sample XML file for testing
8+
void createTestXMLFile(const std::string& filePath) {
9+
std::ofstream outFile(filePath);
10+
outFile << "<Test>\n";
11+
outFile << " <Data>Encryption Data</Data>\n";
12+
outFile << "</Test>";
13+
outFile.close();
14+
}
15+
16+
// Function to read the contents of a file into a string
17+
std::string readFileToString(const std::string& filePath) {
18+
std::ifstream inFile(filePath);
19+
std::string content((std::istreambuf_iterator<char>(inFile)), std::istreambuf_iterator<char>());
20+
inFile.close();
21+
return content;
22+
}

libs/libdecrypt/test/src/test.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef TEST_H
2+
#define TEST_H
3+
4+
#include <iostream>
5+
#include <fstream>
6+
#include <string>
7+
#include "encryption.h"
8+
#include "decryption.h"
9+
10+
void createTestXMLFile(const std::string& filePath);
11+
std::string readFileToString(const std::string& filePath);
12+
13+
14+
#endif // TEST_H

0 commit comments

Comments
 (0)