From 592ab485a70ab4c8e4cefc37bbdfb76110f9205e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Frings-F=C3=BCrst?= Date: Sun, 9 Jan 2022 18:59:51 +0100 Subject: New upstream version 0.9.6 --- test/MemoryManagerSuite.cpp | 175 ++++++++++++++++++++++++++++++-------------- 1 file changed, 119 insertions(+), 56 deletions(-) (limited to 'test/MemoryManagerSuite.cpp') diff --git a/test/MemoryManagerSuite.cpp b/test/MemoryManagerSuite.cpp index a828d76..e4af977 100644 --- a/test/MemoryManagerSuite.cpp +++ b/test/MemoryManagerSuite.cpp @@ -36,76 +36,99 @@ extern "C" { namespace { -class CallCountLog { -public: - unsigned int callCountFree; - CallCountLog() : callCountFree(0) { - // no-op - } -}; +static void * failingMalloc(UriMemoryManager * memory, size_t size); +static void * failingCalloc(UriMemoryManager * memory, size_t nmemb, size_t size); +static void * failingRealloc(UriMemoryManager * memory, void * ptr, size_t size); +static void * failingReallocarray(UriMemoryManager * memory, void * ptr, size_t nmemb, size_t size); +static void countingFree(UriMemoryManager * memory, void * ptr); -static void * failingMalloc(UriMemoryManager * URI_UNUSED(memory), - size_t URI_UNUSED(size)) { - return NULL; -} +class FailingMemoryManager { +private: + UriMemoryManager memoryManager; + unsigned int callCountAlloc; + unsigned int callCountFree; + unsigned int failAllocAfterTimes; + friend void * failingMalloc(UriMemoryManager * memory, size_t size); + friend void * failingCalloc(UriMemoryManager * memory, size_t nmemb, size_t size); + friend void * failingRealloc(UriMemoryManager * memory, void * ptr, size_t size); + friend void * failingReallocarray(UriMemoryManager * memory, void * ptr, size_t nmemb, size_t size); + friend void countingFree(UriMemoryManager * memory, void * ptr); -static void * failingCalloc(UriMemoryManager * URI_UNUSED(memory), - size_t URI_UNUSED(nmemb), size_t URI_UNUSED(size)) { - return NULL; -} +public: + FailingMemoryManager(unsigned int failAllocAfterTimes = 0) + : callCountAlloc(0), callCountFree(0), + failAllocAfterTimes(failAllocAfterTimes) { + this->memoryManager.malloc = failingMalloc; + this->memoryManager.calloc = failingCalloc; + this->memoryManager.realloc = failingRealloc; + this->memoryManager.reallocarray = failingReallocarray; + this->memoryManager.free = countingFree; + this->memoryManager.userData = this; + } + UriMemoryManager * operator&() { + return &(this->memoryManager); + } + unsigned int getCallCountFree() const { + return this->callCountFree; + } +}; -static void * failingRealloc(UriMemoryManager * URI_UNUSED(memory), - void * URI_UNUSED(ptr), size_t URI_UNUSED(size)) { - return NULL; + + +static void * failingMalloc(UriMemoryManager * memory, size_t size) { + FailingMemoryManager * const fmm = static_cast(memory->userData); + fmm->callCountAlloc++; + if (fmm->callCountAlloc > fmm->failAllocAfterTimes) { + errno = ENOMEM; + return NULL; + } + return malloc(size); } -static void * failingReallocarray(UriMemoryManager * URI_UNUSED(memory), - void * URI_UNUSED(ptr), size_t URI_UNUSED(nmemb), - size_t URI_UNUSED(size)) { - return NULL; +static void * failingCalloc(UriMemoryManager * memory, size_t nmemb, size_t size) { + FailingMemoryManager * const fmm = static_cast(memory->userData); + fmm->callCountAlloc++; + if (fmm->callCountAlloc > fmm->failAllocAfterTimes) { + errno = ENOMEM; + return NULL; + } + return calloc(nmemb, size); } -static void countingFree(UriMemoryManager * memory, void * ptr) { - static_cast(memory->userData)->callCountFree++; - free(ptr); +static void * failingRealloc(UriMemoryManager * memory, void * ptr, size_t size) { + FailingMemoryManager * const fmm = static_cast(memory->userData); + fmm->callCountAlloc++; + if (fmm->callCountAlloc > fmm->failAllocAfterTimes) { + errno = ENOMEM; + return NULL; + } + return realloc(ptr, size); } -class FailingMemoryManager { -private: - UriMemoryManager memoryManager; - CallCountLog callCountLog; +static void * failingReallocarray(UriMemoryManager * memory, void * ptr, size_t nmemb, size_t size) { + return uriEmulateReallocarray(memory, ptr, nmemb, size); +} -public: - FailingMemoryManager() { - this->memoryManager.malloc = failingMalloc; - this->memoryManager.calloc = failingCalloc; - this->memoryManager.realloc = failingRealloc; - this->memoryManager.reallocarray = failingReallocarray; - this->memoryManager.free = countingFree; - this->memoryManager.userData = &(this->callCountLog); - } - UriMemoryManager * operator&() { - return &(this->memoryManager); - } - unsigned int getCallCountFree() const { - return this->callCountLog.callCountFree; - } -}; +static void countingFree(UriMemoryManager * memory, void * ptr) { + FailingMemoryManager * const fmm = static_cast(memory->userData); + fmm->callCountFree++; + return free(ptr); +} @@ -318,11 +341,11 @@ TEST(FailingMemoryManagerSuite, DissectQueryMallocExMm) { TEST(FailingMemoryManagerSuite, FreeQueryListMm) { UriQueryListA * const queryList = parseQueryList("k1=v1"); FailingMemoryManager failingMemoryManager; - ASSERT_EQ(failingMemoryManager.getCallCountFree(), 0); + ASSERT_EQ(failingMemoryManager.getCallCountFree(), 0U); uriFreeQueryListMmA(queryList, &failingMemoryManager); - ASSERT_GE(failingMemoryManager.getCallCountFree(), 1); + ASSERT_GE(failingMemoryManager.getCallCountFree(), 1U); } @@ -330,25 +353,65 @@ TEST(FailingMemoryManagerSuite, FreeQueryListMm) { TEST(FailingMemoryManagerSuite, FreeUriMembersMm) { UriUriA uri = parse("http://example.org/"); FailingMemoryManager failingMemoryManager; - ASSERT_EQ(failingMemoryManager.getCallCountFree(), 0); + ASSERT_EQ(failingMemoryManager.getCallCountFree(), 0U); uriFreeUriMembersMmA(&uri, &failingMemoryManager); - ASSERT_GE(failingMemoryManager.getCallCountFree(), 1); + ASSERT_GE(failingMemoryManager.getCallCountFree(), 1U); uriFreeUriMembersA(&uri); } +namespace { + void testNormalizeSyntaxWithFailingMallocCallsFreeTimes(const char * uriString, + unsigned int mask, + unsigned int failAllocAfterTimes = 0, + unsigned int expectedCallCountFree = 0) { + UriUriA uri = parse(uriString); + FailingMemoryManager failingMemoryManager(failAllocAfterTimes); + ASSERT_EQ(uriNormalizeSyntaxExMmA(&uri, mask, &failingMemoryManager), + URI_ERROR_MALLOC); -TEST(FailingMemoryManagerSuite, NormalizeSyntaxExMm) { - UriUriA uri = parse("hTTp://example.org/path"); - const unsigned int mask = URI_NORMALIZE_SCHEME; // anything but URI_NORMALIZED - FailingMemoryManager failingMemoryManager; + EXPECT_EQ(failingMemoryManager.getCallCountFree(), expectedCallCountFree); - ASSERT_EQ(uriNormalizeSyntaxExMmA(&uri, mask, &failingMemoryManager), - URI_ERROR_MALLOC); + uriFreeUriMembersA(&uri); + } +} // namespace - uriFreeUriMembersA(&uri); +TEST(FailingMemoryManagerSuite, NormalizeSyntaxExMmScheme) { + testNormalizeSyntaxWithFailingMallocCallsFreeTimes("hTTp://example.org/path", URI_NORMALIZE_SCHEME); +} + +TEST(FailingMemoryManagerSuite, NormalizeSyntaxExMmEmptyUserInfo) { + testNormalizeSyntaxWithFailingMallocCallsFreeTimes("//@:123", URI_NORMALIZE_USER_INFO); +} + +TEST(FailingMemoryManagerSuite, NormalizeSyntaxExMmEmptyHostRegname) { + testNormalizeSyntaxWithFailingMallocCallsFreeTimes("//:123", URI_NORMALIZE_HOST); +} + +TEST(FailingMemoryManagerSuite, NormalizeSyntaxExMmEmptyQuery) { + testNormalizeSyntaxWithFailingMallocCallsFreeTimes("//:123?", URI_NORMALIZE_QUERY); +} + +TEST(FailingMemoryManagerSuite, NormalizeSyntaxExMmEmptyFragment) { + testNormalizeSyntaxWithFailingMallocCallsFreeTimes("//:123#", URI_NORMALIZE_FRAGMENT); +} + +TEST(FailingMemoryManagerSuite, NormalizeSyntaxExMmHostTextIp4) { // issue #121 + testNormalizeSyntaxWithFailingMallocCallsFreeTimes("//192.0.2.0:123" /* RFC 5737 */, URI_NORMALIZE_HOST, 1, 1); +} + +TEST(FailingMemoryManagerSuite, NormalizeSyntaxExMmHostTextIp6) { // issue #121 + testNormalizeSyntaxWithFailingMallocCallsFreeTimes("//[2001:db8::]:123" /* RFC 3849 */, URI_NORMALIZE_HOST, 1, 1); +} + +TEST(FailingMemoryManagerSuite, NormalizeSyntaxExMmHostTextRegname) { // issue #121 + testNormalizeSyntaxWithFailingMallocCallsFreeTimes("//host123.test:123" /* RFC 6761 */, URI_NORMALIZE_HOST, 1, 1); +} + +TEST(FailingMemoryManagerSuite, NormalizeSyntaxExMmHostTextFuture) { // issue #121 + testNormalizeSyntaxWithFailingMallocCallsFreeTimes("//[v7.X]:123" /* arbitrary IPvFuture */, URI_NORMALIZE_HOST, 1, 1); } -- cgit v1.2.3