Skip to content

Commit

Permalink
+cxx/main.hxx: for issues #3, #14: cross-language
Browse files Browse the repository at this point in the history
	unit tests.

?`cxx/main.cxx`:
	`%s/testHarnesses/unitTestsCxx/`
	?`unitTestsCxx`:
		Use `expects`, `ensures`, `noexcept`.
		Process unit test return values, return the bitmask of all unit test return values. [Notice: Before this, `0` was always the return value.]
		TODO: execute `a.out` in `build.sh` (so that _GitHub_ runners will perform unit tests; if `a.out` returns a non-zero value, the value will show which tests to fix.) [Notice: this will cause _GitHub_ to post that the compilation failed (unless all unit tests pass,) thus have not done so in this commit.]

?`cxx/main.*xx`:
	+`susuwuUnitTests`: is `extern "C" {` wrap for `Susuwu::unitTestsCxx`, to support more languages.
	?`main`: put in `extern "C" {`.

?`build.sh`:
	Now executes unit tests, with `std::cerr` outputs.
	Now has (although commented-out) support to pass through unit test return values.

?`README.md`: howto interpret `a.out` return values.

?`SECURITY.md`:
	`%s/testHarnesses/susuwuUnitTests/`: thus doesn't assume that just C++ projects will use this.
	Text is now clickable.

?`posts/VirusAnalysis`: include what's new.

Is followup to 7dbb0db (+`cxx/Macros.cxx`: Unit tests. ?`cxx/Macros.hxx`:), 388affe (?`cxx/main.cxx`:`testHarnesses` test `classSysGetInput()`, don't reenable input if it was off.), 1ca8d4d (...?cxx/main.cxx:?main /* Store {argc, args} through classSysInit; now other functions can process as {classSysArgc, classSysArgs} */), d3dd3e3 (?cxx/main.cxx:testHarnesses /* Pass funcName to templateMatchAll */)

Is progress to issues #3, #14.
  • Loading branch information
SwuduSusuwu committed Nov 11, 2024
1 parent 7dbb0db commit 7a9f52b
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 39 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Dual licenses: choose [_Creative Commons_](https://creativecommons.org/share-you
Targets: Windows/Linux/Android/OSX/iOS; all compilers with `201102 <= __cplusplus` (minimum [_C++11_](https://gcc.gnu.org/projects/cxx-status.html#cxx11), due to use of `auto`, `decltype`, `for(x: list) {}`nullptr`, `__func__`). [TODO: It is possible that `[modernize-use-default-member-init]` fix (commit 579a9089248a018005f36522968026ee464fc061) bumped this up to [_C++14_](https://gcc.gnu.org/projects/cxx-status.html#cxx14) (if so will undo, as was told that not all microcontrollers have compilers which support this).]

Usage: [`./build.sh`](https://github.com/SwuduSusuwu/SubStack/blob/trunk/build.sh) produces `*.o` static libraries (for distribution to others,) plus `a.out` to do unit tests (test harnesses).
- [`./cxx/main.hxx`](https://github.com/SwuduSusuwu/SubStack/blob/trunk/cxx/main.hxx) has constants to use to interpret `a.out`'s return values.

- Allowed flags; `./build.sh --debug` (default; includes frame-pointers/debug symbols (`-g`), includes `valgrind`-replacement tools (such as `-fsanitize=address`), `./build.sh --release` (excludes `--debug`, strips frame-pointers/symbols, optimizes with `-O2`), `./build.sh --mingw` (if on _Linux_, can use with `--release` or `--debug`, produces _Portable Executable_ for _Windows_. If on _Windows_, the default is to produce _Portable Executable_'s for _Windows_)
- Extra flags (`vim build.sh` to use); other than `_PREFER_`/`_SKIP_`, most use more resources if set to `true`:
Expand Down
2 changes: 1 addition & 1 deletion SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Supported Versions

Users can expect that past 2024 June 26, `trunk` passes `testHarnesses` ( https://github.com/SwuduSusuwu/SubStack/blob/trunk/cxx/main.cxx#L16 ) + `CodeQL` ( https://github.com/SwuduSusuwu/SubStack/security/code-scanning )
Users can expect that past 2024 June 26, `trunk` passes [`susuwuUnitTests`/`Susuwu::unitTestsCxx`](https://github.com/SwuduSusuwu/SubStack/blob/trunk/cxx/main.cxx#L16) (was `testHarnesses`) + [_CodeQL_](https://github.com/SwuduSusuwu/SubStack/security/code-scanning)

## Reporting a Vulnerability

Expand Down
2 changes: 2 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ if [ 0 -eq $STATUS ]; then
FILE_OUT="a.exe"
fi
SUSUWU_PRINT "${SUSUWU_SH_SUCCESS}" "produced \`${FILE_OUT}\` (`stat -c%s ${FILE_OUT}` bytes)"
./${FILE_OUT} && SUSUWU_PRINT "${SUSUWU_SH_NOTICE}" "./${FILE_OUT} return code is $?."
#STATUS=$? #commented-out, as unit tests fail due to `classSysHexStr`/`classSysHexOs` corruption (unknown specifics, but guess is those two functions) on Ubuntu
fi
return $STATUS

61 changes: 48 additions & 13 deletions cxx/main.cxx
Original file line number Diff line number Diff line change
@@ -1,48 +1,83 @@
/* Licenses: allows all uses ("Creative Commons"/"Apache 2") */
#ifndef INCLUDES_cxx_main_cxx
#define INCLUDES_cxx_main_cxx
#include "main.hxx"
#include "AssistantCns.hxx" /* assistantCnsTestsNoexcept */
#include "ClassSha2.hxx" /* classSha2TestsNoexcept */
#include "ClassSys.hxx" /* classSysSetConsoleInput classSysTestsNoexcept templateCatchAll */
#include "Macros.hxx" /* macrosTestsNoexcept SUSUWU_EXPECTS SUSUWU_ENSURES SUSUWU_NOEXCEPT */
#include "VirusAnalysis.hxx" /* virusAnalysisTestsNoexcept */
#include <iostream> /* std::cout std::flush std::endl */
#include <string> /* std::to_string */
#ifdef SUSUWU_CXX17 /* `type_traits` is C++11 but `is_nothrow_invocable` is C++17 */
# include <type_traits> /* std::is_nothrow_invocable */
#endif /* def SUSUWU_CXX17 */
namespace Susuwu {
const int testHarnesses() {
/* `clang-tidy` off: NOLINTBEGIN(hicpp-signed-bitwise, readability-simplify-boolean-expr) */
static const SusuwuUnitTestsBitmask unitTestsCxx() SUSUWU_EXPECTS(std::cout.good()) SUSUWU_ENSURES(0 == macrosTestsNoexcept() && true == classSysTestsNoexcept() && true == classSha2TestsNoexcept() && true == virusAnalysisTestsNoexcept() && true == assistantCnsTestsNoexcept())
#ifdef SUSUWU_CXX17 /* `type_traits` is C++11 but `is_nothrow_invocable` is C++17 */
SUSUWU_NOEXCEPT(std::is_nothrow_invocable<decltype(std::cout << ""), decltype(std::cout), decltype("")>::value)
#endif /* def SUSUWU_CXX17 */
{
int susuwuUnitTestsErrno = 0;
if(!std::cout.good()) {
susuwuUnitTestsErrno |= susuwuUnitTestsConsoleBit;
}
const bool consoleHasInput = classSysGetConsoleInput();
if(consoleHasInput) {
classSysSetConsoleInput(false);
classSysSetConsoleInput(false); /* disable prompts for unit tests. Moved down to prevent `assert` failures if `cxx/ClassSys.hxx` fails. Notice: this move assumes that the tests above won't block on input */
}
if(true == classSysGetConsoleInput()) {
susuwuUnitTestsErrno |= susuwuUnitTestsConsoleBit;
}
assert(!classSysGetConsoleInput());
std::cout << "macrosTestsNoexcept(): " << std::flush /* flush, to show which test starts last if it crashes */;
const int macrosTestsErrno = macrosTestsNoexcept();
std::cout << (0 == macrosTestsErrno ? "pass" : ("error#" + std::to_string(macrosTestsErrno))) << std::endl;
if(0 == macrosTestsErrno) {
std::cout << "pass" << std::endl;
} else {
std::cout << "error#" << std::to_string(macrosTestsErrno) << std::endl;
susuwuUnitTestsErrno |= susuwuUnitTestsMacrosBit;
}
std::cout << "classSysTestsNoexcept(): " << std::flush;
classSysTestsNoexcept();
if(true != classSysTestsNoexcept()) {
susuwuUnitTestsErrno |= susuwuUnitTestsClassSysBit;
}
std::cout << "classSha2TestsNoexcept(): " << std::flush;
std::cout << (classSha2TestsNoexcept() ? "pass" : "error") << std::endl;
if(true == classSha2TestsNoexcept()) {
std::cout << "pass" << std::endl;
} else {
std::cout << "error" << std::endl;
susuwuUnitTestsErrno |= susuwuUnitTestsClassSha2Bit;
}
std::cout << "virusAnalysisTestsNoexcept(): " << std::flush;
if(virusAnalysisTestsNoexcept()) {
std::cout << "pass" << std::endl;
} else {
std::cout << "error" << std::endl;
susuwuUnitTestsErrno |= susuwuUnitTestsVirusAnalysisBit;
}
if(consoleHasInput) {
assert(classSysSetConsoleInput(true));
if(consoleHasInput && false == classSysSetConsoleInput(true)) {
susuwuUnitTestsErrno |= susuwuUnitTestsConsoleBit;
}
std::cout << "assistantCnsTestsNoexcept(): " << std::flush;
if(assistantCnsTestsNoexcept()) {
std::cout << "pass" << std::endl;
} else {
std::cout << "error" << std::endl;
susuwuUnitTestsErrno |= susuwuUnitTestsAssistantCnsBit;
}
return 0;
return susuwuUnitTestsErrno;
}
}; /* namespace Susuwu */
int main(int argc, const char **args) {
const bool classSysInitSuccess = Susuwu::classSysInit(argc, args);
assert(classSysInitSuccess);
return Susuwu::testHarnesses();
const SusuwuUnitTestsBitmask susuwuUnitTests() {
return Susuwu::unitTestsCxx();
}
SusuwuUnitTestsBitmask main(int argc, const char **args) {
if(true != Susuwu::classSysInit(argc, args)) {
return susuwuUnitTestsClassSysBit;
}
return Susuwu::unitTestsCxx();
}
/* `clang-tidy` on: NOLINTEND(hicpp-signed-bitwise, readability-simplify-boolean-expr) */
#endif /* ndef INCLUDES_cxx_main_cxx */

22 changes: 22 additions & 0 deletions cxx/main.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* Licenses: allows all uses ("Creative Commons"/"Apache 2") */
#ifndef INCLUDES_cxx_main_hxx
#define INCLUDES_cxx_main_hxx
#ifdef __cplusplus
extern "C" { /* progress to https://github.com/SwuduSusuwu/SubStack/issues/3 , such that other languages can execute unit tests */
#endif /* def __cplusplus */
/* `clang-tidy` on: NOLINTBEGIN(hicpp-signed-bitwise) */
typedef int SusuwuUnitTestsBitmask; /* normal `int`, but used as bitmask (non-zero return value says which tests failed) */
static const int susuwuUnitTestsMacrosBit = 1 << 0; /* 1: `Macros.hxx`:`macrosTestsNoexcept()` */
static const int susuwuUnitTestsClassSysBit = 1 << 1; /* 2: `ClassSys.hxx`:`classSysTestsNoexcept()` */
static const int susuwuUnitTestsClassSha2Bit = 1 << 2; /* 4: `ClassSha2.hxx`:`classSha2TestsNoexcept()` */
static const int susuwuUnitTestsVirusAnalysisBit = 1 << 3; /* 8: `VirusAnalysis.hxx`:`virusAnalysisTestsNoexcept()` */
static const int susuwuUnitTestsAssistantCnsBit = 1 << 4; /* 16: `AssistantCns.hxx`:`assistantCnsTestsNoexcept()` */
static const int susuwuUnitTestsConsoleBit = 1 << 5; /* 32: `classSys.hxx`:`classSysSetConsoleInput()` */
/* `clang-tidy` on: NOLINTEND(hicpp-signed-bitwise) */
const SusuwuUnitTestsBitmask susuwuUnitTests();
SusuwuUnitTestsBitmask main(int argc, const char **args);
#ifdef __cplusplus
} /* extern "C" { */
#endif /* def __cplusplus */
#endif /* ndef INCLUDES_cxx_main_hxx */

87 changes: 62 additions & 25 deletions posts/VirusAnalysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ _[This post](https://swudususuwu.substack.com/p/howto-produce-better-virus-scann
Static analysis + sandbox + CNS = 1 second (approx) analysis of **new executables** (protects all app launches,) but _caches_ reduce this to **less than 1ms** (just cost to lookup `ResultList::hashes`, which is `std::unordered_set<decltype(sha2(const FileBytecode &))>`; a hashmap of hashes).

`Licenses: allows all uses ("Creative Commons"/"Apache 2")`
[Removed duplicate licenses, `#if` guards, `#include`s, `namespace`s, from all except `main.cxx`; follow URLs for whole sources]
[Removed duplicate licenses, `#if` guards, `#include`s, `namespace`s, from all except `main.hxx`; follow URLs for whole sources]
For the most new sources (+ static libs), use apps such as [iSH](https://apps.apple.com/us/app/ish-shell/id1436902243) (for **iOS**) or [Termux](https://play.google.com/store/apps/details?id=com.termux) (for **Android OS**) to run this:
`git clone https://github.com/SwuduSusuwu/SubStack.git && cd ./Substack/ && ./build`
`less` [cxx/Macros.hxx](https://github.com/SwuduSusuwu/SubStack/blob/trunk/cxx/Macros.hxx) /* removed: disabled color codes + unused OSC codes */
Expand Down Expand Up @@ -1491,55 +1491,92 @@ const FileBytecode cnsVirusFix(const PortableExecutable &file, const Cns &cns /*
return cns.processToString(file.bytecode);
}
```
`less` [cxx/main.cxx](https://github.com/SwuduSusuwu/SubStack/blob/trunk/cxx/main.cxx) /* with boilerplate */
`less` [cxx/main.hxx](https://github.com/SwuduSusuwu/SubStack/blob/trunk/cxx/main.hxx) /* with boilerplate */
```
/* Licenses: allows all uses ("Creative Commons"/"Apache 2") */
#ifndef INCLUDES_cxx_main_cxx
#define INCLUDES_cxx_main_cxx
#include "AssistantCns.hxx" /* assistantCnsTestsNoexcept */
#include "ClassSha2.hxx" /* classSha2TestsNoexcept */
#include "ClassSys.hxx" /* classSysSetConsoleInput classSysTestsNoexcept templateCatchAll */
#include "Macros.hxx" /* macrosTestsNoexcept SUSUWU_EXPECTS SUSUWU_ENSURES SUSUWU_NOEXCEPT */
#include "VirusAnalysis.hxx" /* virusAnalysisTestsNoexcept */
#include <iostream> /* std::cout std::flush std::endl */
#ifndef INCLUDES_cxx_main_hxx
#define INCLUDES_cxx_main_hxx
#ifdef __cplusplus
extern "C" { /* progress to https://github.com/SwuduSusuwu/SubStack/issues/3 , such that other languages can execute unit tests */
#endif /* def __cplusplus */
typedef int SusuwuUnitTestsBitmask; /* normal `int`, but used as bitmask (non-zero return value says which tests failed) */
static const int susuwuUnitTestsMacrosBit = 1 << 0; /* 1: `Macros.hxx`:`macrosTestsNoexcept()` */
static const int susuwuUnitTestsClassSysBit = 1 << 1; /* 2: `ClassSys.hxx`:`classSysTestsNoexcept()` */
static const int susuwuUnitTestsClassSha2Bit = 1 << 2; /* 4: `ClassSha2.hxx`:`classSha2TestsNoexcept()` */
static const int susuwuUnitTestsVirusAnalysisBit = 1 << 3; /* 8: `VirusAnalysis.hxx`:`virusAnalysisTestsNoexcept()` */
static const int susuwuUnitTestsAssistantCnsBit = 1 << 4; /* 16: `AssistantCns.hxx`:`assistantCnsTestsNoexcept()` */
static const int susuwuUnitTestsConsoleBit = 1 << 5; /* 32: `classSys.hxx`:`classSysSetConsoleInput()` */
const SusuwuUnitTestsBitmask susuwuTestHarnesses();
SusuwuUnitTestsBitmask main(int argc, const char **args);
#ifdef __cplusplus
} /* extern "C" { */
#endif /* def __cplusplus */
#endif /* ndef INCLUDES_cxx_main_hxx */
```
`less` [cxx/main.cxx](https://github.com/SwuduSusuwu/SubStack/blob/trunk/cxx/main.cxx)
```
namespace Susuwu {
const int testHarnesses() {
static const SusuwuUnitTestsBitmask unitTestsCxx() SUSUWU_EXPECTS(std::cout.good()) SUSUWU_ENSURES(0 == macrosTestsNoexcept() && true == classSysTestsNoexcept() && true == classSha2TestsNoexcept() && true == virusAnalysisTestsNoexcept() && true == assistantCnsTestsNoexcept()) SUSUWU_NOEXCEPT(std::is_nothrow_invocable<decltype(std::cout << ""), decltype(std::cout), decltype("")>::value) {
int susuwuUnitTestsErrno = 0;
if(!std::cout.good()) {
susuwuUnitTestsErrno |= susuwuUnitTestsConsoleBit;
}
const bool consoleHasInput = classSysGetConsoleInput();
if(consoleHasInput) {
classSysSetConsoleInput(false);
classSysSetConsoleInput(false); /* disable prompts for unit tests. Moved down to prevent `assert` failures if `cxx/ClassSys.hxx` fails. Notice: this move assumes that the tests above won't block on input */
}
assert(!classSysGetConsoleInput());
std::cout << "macrosTestsNoexcept(): " << std::flush;
if(true == classSysGetConsoleInput()) {
susuwuUnitTestsErrno |= susuwuUnitTestsConsoleBit;
}
std::cout << "macrosTestsNoexcept(): " << std::flush /* flush, to show which test starts last if it crashes */;
const int macrosTestsErrno = macrosTestsNoexcept();
std::cout << (0 == macrosTestsErrno ? "pass" : ("error#" + std::to_string(macrosTestsErrno))) << std::endl;
if(0 == macrosTestsErrno) {
std::cout << "pass" << std::endl;
} else {
std::cout << "error#" << std::to_string(macrosTestsErrno) << std::endl;
susuwuUnitTestsErrno |= susuwuUnitTestsMacrosBit;
}
std::cout << "classSysTestsNoexcept(): " << std::flush;
classSysTestsNoexcept();
if(true != classSysTestsNoexcept()) {
susuwuUnitTestsErrno |= susuwuUnitTestsClassSysBit;
}
std::cout << "classSha2TestsNoexcept(): " << std::flush;
std::cout << (classSha2TestsNoexcept() ? "pass" : "error") << std::endl;
if(true == classSha2TestsNoexcept()) {
std::cout << "pass" << std::endl;
} else {
std::cout << "error" << std::endl;
susuwuUnitTestsErrno |= susuwuUnitTestsClassSha2Bit;
}
std::cout << "virusAnalysisTestsNoexcept(): " << std::flush;
if(virusAnalysisTestsNoexcept()) {
std::cout << "pass" << std::endl;
} else {
std::cout << "error" << std::endl;
susuwuUnitTestsErrno |= susuwuUnitTestsVirusAnalysisBit;
}
if(consoleHasInput) {
assert(classSysSetConsoleInput(true));
if(consoleHasInput && false == classSysSetConsoleInput(true)) {
susuwuUnitTestsErrno |= susuwuUnitTestsConsoleBit;
}
std::cout << "assistantCnsTestsNoexcept(): " << std::flush;
if(assistantCnsTestsNoexcept()) {
std::cout << "pass" << std::endl;
} else {
std::cout << "error" << std::endl;
susuwuUnitTestsErrno |= susuwuUnitTestsAssistantCnsBit;
}
return 0;
return susuwuUnitTestsErrno;
}
}; /* namespace Susuwu */
int main(int argc, const char **args) {
const bool classSysInitSuccess = Susuwu::classSysInit(argc, args);
assert(classSysInitSuccess);
return Susuwu::testHarnesses();
const SusuwuUnitTestsBitmask susuwuUnitTests() {
return Susuwu::unitTestsCxx();
}
SusuwuUnitTestsBitmask main(int argc, const char **args) {
if(true != Susuwu::classSysInit(argc, args)) {
return susuwuUnitTestsClassSysBit;
}
return Susuwu::unitTestsCxx();
}
#endif /* ndef INCLUDES_cxx_main_cxx */
```
To run most of this fast (lag less,) use `CXXFLAGS` which auto-vectorizes/auto-parallelizes, and to setup CNS synapses (`Cns::setupSynapses()`) fast, use _TensorFlow_'s `MapReduce`. Resources: [How to have computers process fast](https://swudususuwu.substack.com/p/howto-run-devices-phones-laptops).

Expand Down

0 comments on commit 7a9f52b

Please sign in to comment.