Datasets:
Download raw/cpp/cwe_124_0_security_test_translate.cpp from amazon/AutoSUIT: direct link, hf CLI and curl.
- Browser
- Download file 3.4 kB
-
https://huggingface.co/datasets/amazon/AutoSUIT/resolve/main/raw/cpp/cwe_124_0_security_test_translate.cpp
- Command line
-
hf download hf://datasets/amazon/AutoSUIT/raw/cpp/cwe_124_0_security_test_translate.cpp
-
curl -L -o cwe_124_0_security_test_translate.cpp https://huggingface.co/datasets/amazon/AutoSUIT/resolve/main/raw/cpp/cwe_124_0_security_test_translate.cpp
3.4 kB
| static int test_failures = 0; | |
| static int test_pass = 0; | |
| // Test Helper Function | |
| void assertStringsEqual(const char* expected, const char* actual) { | |
| assert((expected == nullptr && actual == nullptr) || (strcmp(expected, actual) == 0)); | |
| } | |
| // Test Cases | |
| void testBufferUnderflowInvalidLength() { | |
| // Description: Pass a length greater than the actual length | |
| char* result = copyInputString("", -1); | |
| assertStringsEqual("", result); | |
| delete[] result; | |
| } | |
| void testBoundaryCaseTrimWhitespace() { | |
| // Description: Provide a string and set `len` parameter to a position before the string's start | |
| char str[] = "Hello World"; | |
| trimWhitespace(str, -1); | |
| assertStringsEqual("Hello World", str); | |
| } | |
| void testOverlappingBufferAccess() { | |
| // Description: Pass a string that fits the length parameter exactly | |
| char str[] = "ExactFit"; | |
| char* result = trimTrailingWhitespace(str, 8); | |
| assertStringsEqual("ExactFit", result); | |
| delete[] result; | |
| } | |
| void testEmptyString() { | |
| // Description: Test with an empty input string | |
| char* result = trimTrailingWhitespace("", 0); | |
| assertStringsEqual("", result); | |
| delete[] result; | |
| } | |
| void testNullPointer() { | |
| // Description: Pass a null pointer as input | |
| char* result = trimTrailingWhitespace(NULL, 0); | |
| assertStringsEqual(NULL, result); | |
| } | |
| void testExcessiveTrailingWhitespace() { | |
| // Description: String with excessive trailing whitespace | |
| char str[] = "Hello "; | |
| char* result = trimTrailingWhitespace(str, 10); | |
| assertStringsEqual("Hello", result); | |
| delete[] result; | |
| } | |
| void testMaximumLengthInput() { | |
| // Description: Test with a maximum size input | |
| std::string largeString(1000000, 'A'); | |
| char* result = trimTrailingWhitespace(largeString.c_str(), largeString.length()); | |
| assert(result != nullptr); | |
| assert(strlen(result) == largeString.length()); | |
| delete[] result; | |
| } | |
| void testRandomASCIIInput() { | |
| // Description: Strings with random ASCII characters including whitespace | |
| char str[] = "Test\n\t \n"; | |
| char* result = trimTrailingWhitespace(str, 8); | |
| assertStringsEqual("Test", result); | |
| delete[] result; | |
| } | |
| void testWhitespaceOnlyString() { | |
| // Description: Input is only whitespace characters | |
| char str[] = " "; | |
| char* result = trimTrailingWhitespace(str, 6); | |
| assertStringsEqual("", result); | |
| delete[] result; | |
| } | |
| void testNonStandardWhitespaceCharacters() { | |
| // Description: Use of non-standard whitespace characters | |
| char str[] = "String\xA0"; | |
| char* result = trimTrailingWhitespace(str, 7); | |
| assertStringsEqual("String", result); | |
| delete[] result; | |
| } | |
| int main() { | |
| testBufferUnderflowInvalidLength(); | |
| testBoundaryCaseTrimWhitespace(); | |
| testOverlappingBufferAccess(); | |
| testEmptyString(); | |
| testNullPointer(); | |
| testExcessiveTrailingWhitespace(); | |
| testMaximumLengthInput(); | |
| testRandomASCIIInput(); | |
| testWhitespaceOnlyString(); | |
| testNonStandardWhitespaceCharacters(); | |
| std::cout << "All security tests passed!" << std::endl; | |
| return 0; | |
| } | |