Skip to content

Commit 3433f49

Browse files
committed
Merge remote-tracking branch
'origin/GP-2983_caheckman_PR-3998_ekilmer_fix-decompiler-test-cli-parsing' (Closes NationalSecurityAgency#3998)
2 parents 539d5b2 + 735c6d3 commit 3433f49

File tree

4 files changed

+43
-13
lines changed

4 files changed

+43
-13
lines changed

Ghidra/Features/Decompiler/src/decompile/cpp/test.cc

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ vector<UnitTest *> UnitTest::tests;
2121
/// Run all the tests unless a non-empty set of names is passed in.
2222
/// In which case, only the named tests in the set are run.
2323
/// \param testNames is the set of names
24-
void UnitTest::run(set<string> &testNames)
24+
/// \return number of failed tests
25+
int UnitTest::run(set<string> &testNames)
2526

2627
{
2728
int total = 0;
@@ -42,6 +43,7 @@ void UnitTest::run(set<string> &testNames)
4243
}
4344
std::cerr << "==============================" << std::endl;
4445
std::cerr << passed << "/" << total << " tests passed." << std::endl;
46+
return total - passed;
4547
}
4648

4749
/// Create list of the absolute path of all tests to be run
@@ -75,6 +77,24 @@ void gatherDataTests(const string &dirname,set<string> &testNames,vector<string>
7577
}
7678
}
7779

80+
/// \brief This function performs a saturating add on two numbers where the
81+
/// result is to be used as an exit code for a CLI application.
82+
///
83+
/// \param current The current return code
84+
/// \param add A number to add to the current return code
85+
/// \return A number that can be used as an exit code up to 255.
86+
int add_exit_code(int current, int add) {
87+
const int CLAMP = 255;
88+
int ret = current + add;
89+
if (current < 0 || // Sanity checks
90+
current > CLAMP ||
91+
ret < current || // Can only happen due to overflow
92+
ret > CLAMP) { // Check clamp value
93+
ret = CLAMP; // Set to max exit code
94+
}
95+
return ret;
96+
}
97+
7898
int main(int argc, char **argv) {
7999
bool runUnitTests = true;
80100
bool runDataTests = true;
@@ -85,7 +105,7 @@ int main(int argc, char **argv) {
85105
set<string> dataTestNames;
86106
string dirname("../datatests");
87107
string sleighdirname("../../../../../../..");
88-
if (argc > 0) {
108+
while (argc > 0) {
89109
string command(argv[0]);
90110
if (command == "-path") {
91111
dirname = argv[1];
@@ -109,30 +129,39 @@ int main(int argc, char **argv) {
109129
argv += 1;
110130
argc -= 1;
111131
}
112-
}
113-
if (argc > 0) {
114-
string command(argv[0]);
115-
if (command == "unittests") {
132+
else if (command == "unittests") {
116133
runUnitTests = true;
117134
runDataTests = false; // Run only unit tests
118135
unitTestNames.insert(argv + 1,argv + argc);
136+
break;
119137
}
120138
else if (command == "datatests") {
121139
runUnitTests = false; // Run only data-tests
122140
runDataTests = true;
123141
dataTestNames.insert(argv + 1,argv + argc);
142+
break;
124143
}
125144
else {
126-
cout << "USAGE: ghidra_test [-path <datatestdir>] [[unittests|datatests] [testname1 testname2 ...]]" << endl;
145+
cout << "USAGE: ghidra_test [-usesleighenv] [-sleighpath <sleighdir>] [-path <datatestdir>] [[unittests|datatests] [testname1 testname2 ...]]" << endl;
146+
return -1;
127147
}
128148
}
129149
startDecompilerLibrary(sleighdirname.c_str());
130-
if (runUnitTests)
131-
UnitTest::run(unitTestNames);
150+
151+
// Keep track of failed tests as return code to indicate failures, clamped at
152+
// max exit code value in add_exit_code
153+
int failedTests = 0;
154+
if (runUnitTests) {
155+
int errors = UnitTest::run(unitTestNames);
156+
failedTests = add_exit_code(failedTests, errors);
157+
}
132158
if (runDataTests) {
133159
vector<string> testFiles;
134160
gatherDataTests(dirname,dataTestNames,testFiles);
135161
cout << endl << endl;
136-
FunctionTestCollection::runTestFiles(testFiles,cout);
162+
int errors = FunctionTestCollection::runTestFiles(testFiles,cout);
163+
failedTests = add_exit_code(failedTests, errors);
137164
}
165+
166+
return failedTests;
138167
}

Ghidra/Features/Decompiler/src/decompile/cpp/test.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ struct UnitTest {
5454
tests.push_back(this);
5555
}
5656

57-
static void run(std::set<std::string> &testNames); ///< Run all the instantiated tests
57+
static int run(std::set<std::string> &testNames); ///< Run all the instantiated tests
5858
};
5959

6060

Ghidra/Features/Decompiler/src/decompile/cpp/testfunction.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ void FunctionTestCollection::runTests(list<string> &lateStream)
304304
/// Run through all XML files in the given list, processing each in turn.
305305
/// \param testFiles is the given list of test files
306306
/// \param s is the output stream to print results to
307-
void FunctionTestCollection::runTestFiles(const vector<string> &testFiles,ostream &s)
307+
int FunctionTestCollection::runTestFiles(const vector<string> &testFiles,ostream &s)
308308

309309
{
310310
int4 totalTestsApplied = 0;
@@ -344,4 +344,5 @@ void FunctionTestCollection::runTestFiles(const vector<string> &testFiles,ostrea
344344
if (iter == failures.end()) break;
345345
}
346346
}
347+
return totalTestsApplied - totalTestsSucceeded;
347348
}

Ghidra/Features/Decompiler/src/decompile/cpp/testfunction.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public:
9191
void restoreXml(DocumentStorage &store,const Element *el); ///< Load tests from a \<decompilertest> tag.
9292
void restoreXmlOldForm(DocumentStorage &store,const Element *el); ///< Load tests from \<binaryimage> tag.
9393
void runTests(list<string> &lateStream); ///< Run the script and perform the tests
94-
static void runTestFiles(const vector<string> &testFiles,ostream &s); ///< Run tests for each listed file
94+
static int runTestFiles(const vector<string> &testFiles,ostream &s); ///< Run tests for each listed file
9595
};
9696

9797
#endif

0 commit comments

Comments
 (0)