HyperDbg Debugger
Loading...
Searching...
No Matches
hwdbg-tests.cpp File Reference

Test cases for testing hwdbg. More...

#include "pch.h"

Functions

std::string CommentContent (const std::string &Content)
 function to comment each line by adding ';' at the start
BOOLEAN ReadDirectoryAndCreateHwdbgTestCases (const CHAR *HwdbgScriptTestCasesPath)
 Read directory of hwdbg script test cases and run each of them.
BOOLEAN HwdbgTestCreateTestCases ()
 Create test cases for hwdbg.

Detailed Description

Test cases for testing hwdbg.

Author
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.11
Date
2024-09-30

Function Documentation

◆ CommentContent()

std::string CommentContent ( const std::string & Content)

function to comment each line by adding ';' at the start

Parameters
Contentthe content to comment out
Returns
std::string
24{
25 std::istringstream Iss(Content);
26 std::string Line;
27 std::string CommentedContent;
28
29 while (std::getline(Iss, Line))
30 {
31 CommentedContent += Line + "\n; ";
32 }
33
34 return CommentedContent;
35}

◆ HwdbgTestCreateTestCases()

BOOLEAN HwdbgTestCreateTestCases ( )

Create test cases for hwdbg.

Returns
BOOLEAN
196{
197 INT32 TestNum = 0;
198 CHAR dirPath[MAX_PATH] = {0};
199
200 //
201 // Parse the hwdbg test cases from the file
202 // Setup the path for the filenames
203 //
205 {
206 //
207 // Error could not find the test case files
208 //
209 cout << "[-] Could not find the hwdbg test case files" << endl;
210 return FALSE;
211 }
212
213 //
214 // Run test cases
215 //
217}
#define HWDBG_SCRIPT_TEST_CASE_CODE_DIRECTORY
Test cases directory name for hwdbg script (code) tests.
Definition Definition.h:110
signed int INT32
Definition BasicTypes.h:50
#define FALSE
Definition BasicTypes.h:113
char CHAR
Definition BasicTypes.h:33
IMPORT_EXPORT_LIBHYPERDBG BOOLEAN hyperdbg_u_setup_path_for_filename(const CHAR *filename, CHAR *file_location, UINT32 buffer_len, BOOLEAN check_file_existence)
Setip the path for the filename.
Definition export.cpp:725
BOOLEAN ReadDirectoryAndCreateHwdbgTestCases(const CHAR *HwdbgScriptTestCasesPath)
Read directory of hwdbg script test cases and run each of them.
Definition hwdbg-tests.cpp:45

◆ ReadDirectoryAndCreateHwdbgTestCases()

BOOLEAN ReadDirectoryAndCreateHwdbgTestCases ( const CHAR * HwdbgScriptTestCasesPath)

Read directory of hwdbg script test cases and run each of them.

Parameters
HwdbgScriptTestCasesPathPath to the hwdbg script test cases
Returns
BOOLEAN
46{
47 CHAR TempFilePath[MAX_PATH] = {0};
48
49 //
50 // Iterate through the directory
51 //
52 try
53 {
54 for (const auto & entry : fs::directory_iterator(HwdbgScriptTestCasesPath))
55 {
56 //
57 // Check if the entry is a file
58 //
59 if (entry.is_regular_file())
60 {
61 //
62 // Get the file path
63 //
64 std::string FilePath = entry.path().string();
65
66 //
67 // Output the file name
68 //
69 // std::cout << "Test case file: " << entry.path().filename().string() << std::endl;
70
71 //
72 // Open the file and read its contents
73 //
74 std::ifstream File(FilePath);
75 if (File.is_open())
76 {
77 std::string Content((std::istreambuf_iterator<char>(File)),
78 std::istreambuf_iterator<char>());
79
80 //
81 // Display the content of the file
82 //
83 std::cout << "Reading script test case file " << entry.path().filename().string() << std::endl;
84
85 // std::cout << content << std::endl;
86
87 std::string CompiledVersionFilePath = HWDBG_SCRIPT_TEST_CASE_COMPILED_SCRIPTS_DIRECTORY "\\" + entry.path().filename().string() + ".hex.txt";
88
89 //
90 // Run the test case command
91 //
92 printf("File content: %s\n", Content.c_str());
93
94 if (!hwdbg_script_run_script(Content.c_str(),
96 CompiledVersionFilePath.c_str(),
98 {
99 std::cout << "[-] Could not run the script: " << FilePath << std::endl;
100 return FALSE;
101 }
102
103 //
104 // Now open the compiled_version_file_path file and add content at the top
105 //
106
107 //
108 // Parse the hwdbg compiled test cases from the file
109 //
110 if (!hyperdbg_u_setup_path_for_filename(CompiledVersionFilePath.c_str(), TempFilePath, MAX_PATH, FALSE))
111 {
112 //
113 // Error could not find the test case files
114 //
115 std::cout << "[-] Could not find the compiled version of the hwdbg test case file" << endl;
116 return FALSE;
117 }
118
119 std::ifstream CompiledFile(TempFilePath);
120 if (CompiledFile.is_open())
121 {
122 //
123 // Read the existing content of the compiled file
124 //
125 std::string CompiledContent((std::istreambuf_iterator<char>(CompiledFile)),
126 std::istreambuf_iterator<char>());
127 CompiledFile.close(); // Close the file after reading
128
129 //
130 // Comment the content
131 //
132 std::string CommentedContent = CommentContent(Content);
133
134 //
135 // Concatenate the new content (prepend the original content)
136 //
137 std::string NewContent = "; The raw script file is available at: " HWDBG_SCRIPT_TEST_CASE_COMPILED_SCRIPTS_DIRECTORY "\\" +
138 entry.path().filename().string() +
139 "\n;\n; !hw script " +
140 CommentedContent +
141 "\n" +
142 CompiledContent;
143
144 //
145 // Write the new content back to the file (overwriting it)
146 //
147 std::ofstream CompiledFileOut(TempFilePath);
148 if (CompiledFileOut.is_open())
149 {
150 CompiledFileOut << NewContent;
151 CompiledFileOut.close();
152 }
153 else
154 {
155 std::cerr << "Could not open file for writing: " << CompiledVersionFilePath << std::endl;
156 }
157 }
158 else
159 {
160 std::cerr << "Could not open compiled file: " << CompiledVersionFilePath << std::endl;
161 }
162
163 std::cout << "--------------------------------------------" << std::endl;
164
165 //
166 // Close the file
167 //
168 File.close();
169 }
170 else
171 {
172 std::cerr << "Could not open file: " << FilePath << std::endl;
173 }
174 }
175 }
176 }
177 catch (const fs::filesystem_error & e)
178 {
179 std::cerr << "Filesystem error: " << e.what() << std::endl;
180 return FALSE;
181 }
182
183 //
184 // Return success
185 //
186 return TRUE;
187}
#define HWDBG_SCRIPT_TEST_CASE_COMPILED_SCRIPTS_DIRECTORY
Test cases directory name for hwdbg script (compiled-scripts) tests.
Definition Definition.h:115
#define TRUE
Definition BasicTypes.h:114
#define HWDBG_TEST_READ_INSTANCE_INFO_PATH
Path to read the sample of the instance info.
Definition HardwareDebugger.h:42
#define DEFAULT_INITIAL_BRAM_BUFFER_SIZE
Initial default buffer size (BRAN Size).
Definition HardwareDebugger.h:36
IMPORT_EXPORT_LIBHYPERDBG BOOLEAN hwdbg_script_run_script(const CHAR *script, const CHAR *instance_filepath_to_read, const CHAR *hardware_script_file_path_to_save, UINT32 initial_bram_buffer_size)
Run hwdbg script.
Definition export.cpp:853
printf("ho")
std::string CommentContent(const std::string &Content)
function to comment each line by adding ';' at the start
Definition hwdbg-tests.cpp:23