"Fossies" - the Fresh Open Source Software Archive

Member "uriparser-0.9.5/test/MemoryManagerSuite.cpp" (18 Mar 2021, 10317 Bytes) of package /linux/www/uriparser-0.9.5.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "MemoryManagerSuite.cpp": 0.9.4_vs_0.9.5.

    1 /*
    2  * uriparser - RFC 3986 URI parsing library
    3  *
    4  * Copyright (C) 2007, Weijia Song <songweijia@gmail.com>
    5  * Copyright (C) 2007, Sebastian Pipping <sebastian@pipping.org>
    6  *
    7  * This library is free software; you can redistribute it and/or
    8  * modify it under the terms of the GNU Lesser General Public
    9  * License as published by the Free Software Foundation; either
   10  * version 2.1 of the License, or (at your option) any later version.
   11  *
   12  * This library is distributed in the hope that it will be useful,
   13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   15  * Lesser General Public License for more details.
   16  *
   17  * You should have received a copy of the GNU Lesser General Public
   18  * License along with this library; if not, write to the Free Software
   19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
   20  */
   21 
   22 #undef NDEBUG  // because we rely on assert(3) further down
   23 
   24 #include <cassert>
   25 #include <cerrno>
   26 #include <cstring>  // memcpy
   27 #include <gtest/gtest.h>
   28 
   29 #include <uriparser/Uri.h>
   30 
   31 // For defaultMemoryManager
   32 extern "C" {
   33 #include "../src/UriMemory.h"
   34 }
   35 
   36 
   37 namespace {
   38 
   39 class CallCountLog {
   40 public:
   41     unsigned int callCountFree;
   42 
   43     CallCountLog() : callCountFree(0) {
   44         // no-op
   45     }
   46 };
   47 
   48 
   49 
   50 static void * failingMalloc(UriMemoryManager * URI_UNUSED(memory),
   51         size_t URI_UNUSED(size)) {
   52     return NULL;
   53 }
   54 
   55 
   56 
   57 static void * failingCalloc(UriMemoryManager * URI_UNUSED(memory),
   58         size_t URI_UNUSED(nmemb), size_t URI_UNUSED(size)) {
   59     return NULL;
   60 }
   61 
   62 
   63 
   64 static void * failingRealloc(UriMemoryManager * URI_UNUSED(memory),
   65         void * URI_UNUSED(ptr), size_t URI_UNUSED(size)) {
   66     return NULL;
   67 }
   68 
   69 
   70 
   71 static void * failingReallocarray(UriMemoryManager * URI_UNUSED(memory),
   72         void * URI_UNUSED(ptr), size_t URI_UNUSED(nmemb),
   73         size_t URI_UNUSED(size)) {
   74     return NULL;
   75 }
   76 
   77 
   78 
   79 static void countingFree(UriMemoryManager * memory, void * ptr) {
   80     static_cast<CallCountLog *>(memory->userData)->callCountFree++;
   81     free(ptr);
   82 }
   83 
   84 
   85 
   86 class FailingMemoryManager {
   87 private:
   88     UriMemoryManager memoryManager;
   89     CallCountLog callCountLog;
   90 
   91 public:
   92     FailingMemoryManager() {
   93         this->memoryManager.malloc = failingMalloc;
   94         this->memoryManager.calloc = failingCalloc;
   95         this->memoryManager.realloc = failingRealloc;
   96         this->memoryManager.reallocarray = failingReallocarray;
   97         this->memoryManager.free = countingFree;
   98         this->memoryManager.userData = &(this->callCountLog);
   99     }
  100 
  101     UriMemoryManager * operator&() {
  102         return &(this->memoryManager);
  103     }
  104 
  105     unsigned int getCallCountFree() const {
  106         return this->callCountLog.callCountFree;
  107     }
  108 };
  109 
  110 
  111 
  112 static UriUriA parse(const char * sourceUriString) {
  113     UriParserStateA state;
  114     UriUriA uri;
  115     state.uri = &uri;
  116     assert(uriParseUriA(&state, sourceUriString) == URI_SUCCESS);
  117     return uri;
  118 }
  119 
  120 
  121 
  122 static UriQueryListA * parseQueryList(const char * queryString) {
  123     UriQueryListA * queryList;
  124     const char * const first = queryString;
  125     const char * const afterLast = first + strlen(first);
  126     assert(uriDissectQueryMallocA(&queryList, NULL, first, afterLast)
  127             == URI_SUCCESS);
  128     return queryList;
  129 }
  130 
  131 }  // namespace
  132 
  133 
  134 
  135 TEST(MemoryManagerCompletenessSuite, AllFunctionMembersRequired) {
  136     UriUriA uri = parse("whatever");
  137     UriMemoryManager memory;
  138 
  139     memcpy(&memory, &defaultMemoryManager, sizeof(UriMemoryManager));
  140     memory.malloc = NULL;
  141     ASSERT_EQ(uriFreeUriMembersMmA(&uri, &memory),
  142               URI_ERROR_MEMORY_MANAGER_INCOMPLETE);
  143 
  144     memcpy(&memory, &defaultMemoryManager, sizeof(UriMemoryManager));
  145     memory.calloc = NULL;
  146     ASSERT_EQ(uriFreeUriMembersMmA(&uri, &memory),
  147               URI_ERROR_MEMORY_MANAGER_INCOMPLETE);
  148 
  149     memcpy(&memory, &defaultMemoryManager, sizeof(UriMemoryManager));
  150     memory.realloc = NULL;
  151     ASSERT_EQ(uriFreeUriMembersMmA(&uri, &memory),
  152               URI_ERROR_MEMORY_MANAGER_INCOMPLETE);
  153 
  154     memcpy(&memory, &defaultMemoryManager, sizeof(UriMemoryManager));
  155     memory.reallocarray = NULL;
  156     ASSERT_EQ(uriFreeUriMembersMmA(&uri, &memory),
  157               URI_ERROR_MEMORY_MANAGER_INCOMPLETE);
  158 
  159     memcpy(&memory, &defaultMemoryManager, sizeof(UriMemoryManager));
  160     memory.free = NULL;
  161     ASSERT_EQ(uriFreeUriMembersMmA(&uri, &memory),
  162               URI_ERROR_MEMORY_MANAGER_INCOMPLETE);
  163 
  164     memcpy(&memory, &defaultMemoryManager, sizeof(UriMemoryManager));
  165     ASSERT_EQ(uriFreeUriMembersMmA(&uri, &memory), URI_SUCCESS);
  166 }
  167 
  168 
  169 
  170 TEST(MemoryManagerCompletenessSuite, MallocAndFreeRequiredOnly) {
  171     UriMemoryManager memory;
  172     UriMemoryManager backend;
  173 
  174     memcpy(&backend, &defaultMemoryManager, sizeof(UriMemoryManager));
  175     backend.malloc = NULL;
  176     ASSERT_EQ(uriCompleteMemoryManager(&memory, &backend),
  177               URI_ERROR_MEMORY_MANAGER_INCOMPLETE);
  178 
  179     memcpy(&backend, &defaultMemoryManager, sizeof(UriMemoryManager));
  180     backend.free = NULL;
  181     ASSERT_EQ(uriCompleteMemoryManager(&memory, &backend),
  182               URI_ERROR_MEMORY_MANAGER_INCOMPLETE);
  183 }
  184 
  185 
  186 
  187 TEST(MemoryManagerTestingSuite, DefaultMemoryManager) {
  188     ASSERT_EQ(uriTestMemoryManager(&defaultMemoryManager), URI_SUCCESS);
  189 }
  190 
  191 
  192 
  193 TEST(MemoryManagerTestingSuite, CompleteMemoryManager) {
  194     UriMemoryManager memory;
  195     UriMemoryManager backend;
  196 
  197     memset(&backend, 0, sizeof(UriMemoryManager));
  198     backend.malloc = defaultMemoryManager.malloc;
  199     backend.free = defaultMemoryManager.free;
  200 
  201     ASSERT_EQ(uriCompleteMemoryManager(&memory, &backend),
  202             URI_SUCCESS);
  203 
  204     ASSERT_EQ(uriTestMemoryManager(&memory), URI_SUCCESS);
  205 }
  206 
  207 
  208 
  209 TEST(MemoryManagerTestingSuite, EmulateCalloc) {
  210     UriMemoryManager partialEmulationMemoryManager;
  211     memcpy(&partialEmulationMemoryManager, &defaultMemoryManager,
  212             sizeof(UriMemoryManager));
  213     partialEmulationMemoryManager.calloc = uriEmulateCalloc;
  214 
  215     ASSERT_EQ(uriTestMemoryManager(&partialEmulationMemoryManager),
  216             URI_SUCCESS);
  217 }
  218 
  219 
  220 
  221 TEST(MemoryManagerTestingSuite, EmulateReallocarray) {
  222     UriMemoryManager partialEmulationMemoryManager;
  223     memcpy(&partialEmulationMemoryManager, &defaultMemoryManager,
  224             sizeof(UriMemoryManager));
  225     partialEmulationMemoryManager.reallocarray = uriEmulateReallocarray;
  226 
  227     ASSERT_EQ(uriTestMemoryManager(&partialEmulationMemoryManager),
  228             URI_SUCCESS);
  229 }
  230 
  231 
  232 
  233 TEST(MemoryManagerTestingOverflowDetectionSuite, EmulateCalloc) {
  234     EXPECT_GT(2 * sizeof(size_t), sizeof(void *));
  235 
  236     errno = 0;
  237     ASSERT_EQ(NULL, uriEmulateCalloc(
  238             &defaultMemoryManager, (size_t)-1, (size_t)-1));
  239     ASSERT_EQ(errno, ENOMEM);
  240 }
  241 
  242 
  243 
  244 TEST(MemoryManagerTestingOverflowDetectionSuite, EmulateReallocarray) {
  245     EXPECT_GT(2 * sizeof(size_t), sizeof(void *));
  246 
  247     errno = 0;
  248     ASSERT_EQ(NULL, uriEmulateReallocarray(
  249             &defaultMemoryManager, NULL, (size_t)-1, (size_t)-1));
  250     ASSERT_EQ(errno, ENOMEM);
  251 }
  252 
  253 
  254 
  255 TEST(MemoryManagerTestingSuite, EmulateCallocAndReallocarray) {
  256     UriMemoryManager partialEmulationMemoryManager;
  257     memcpy(&partialEmulationMemoryManager, &defaultMemoryManager,
  258             sizeof(UriMemoryManager));
  259     partialEmulationMemoryManager.calloc = uriEmulateCalloc;
  260     partialEmulationMemoryManager.reallocarray = uriEmulateReallocarray;
  261 
  262     ASSERT_EQ(uriTestMemoryManager(&partialEmulationMemoryManager),
  263             URI_SUCCESS);
  264 }
  265 
  266 
  267 
  268 TEST(FailingMemoryManagerSuite, AddBaseUriExMm) {
  269     UriUriA absoluteDest;
  270     UriUriA relativeSource = parse("foo");
  271     UriUriA absoluteBase = parse("http://example.org/bar");
  272     const UriResolutionOptions options = URI_RESOLVE_STRICTLY;
  273     FailingMemoryManager failingMemoryManager;
  274 
  275     ASSERT_EQ(uriAddBaseUriExMmA(&absoluteDest, &relativeSource,
  276             &absoluteBase, options, &failingMemoryManager),
  277             URI_ERROR_MALLOC);
  278 
  279     uriFreeUriMembersA(&relativeSource);
  280     uriFreeUriMembersA(&absoluteBase);
  281 }
  282 
  283 
  284 
  285 TEST(FailingMemoryManagerSuite, ComposeQueryMallocExMm) {
  286     char * dest = NULL;
  287     UriQueryListA * const queryList = parseQueryList("k1=v1");
  288     UriBool spaceToPlus = URI_TRUE;  // not of interest
  289     UriBool normalizeBreaks = URI_TRUE;  // not of interest
  290     FailingMemoryManager failingMemoryManager;
  291 
  292     ASSERT_EQ(uriComposeQueryMallocExMmA(&dest, queryList,
  293             spaceToPlus, normalizeBreaks, &failingMemoryManager),
  294             URI_ERROR_MALLOC);
  295 
  296     uriFreeQueryListA(queryList);
  297 }
  298 
  299 
  300 
  301 TEST(FailingMemoryManagerSuite, DissectQueryMallocExMm) {
  302     UriQueryListA * queryList;
  303     int itemCount;
  304     const char * const first = "k1=v1&k2=v2";
  305     const char * const afterLast = first + strlen(first);
  306     const UriBool plusToSpace = URI_TRUE;  // not of interest
  307     const UriBreakConversion breakConversion = URI_BR_DONT_TOUCH;  // not o. i.
  308     FailingMemoryManager failingMemoryManager;
  309 
  310     ASSERT_EQ(uriDissectQueryMallocExMmA(&queryList, &itemCount,
  311             first, afterLast, plusToSpace, breakConversion,
  312             &failingMemoryManager),
  313             URI_ERROR_MALLOC);
  314 }
  315 
  316 
  317 
  318 TEST(FailingMemoryManagerSuite, FreeQueryListMm) {
  319     UriQueryListA * const queryList = parseQueryList("k1=v1");
  320     FailingMemoryManager failingMemoryManager;
  321     ASSERT_EQ(failingMemoryManager.getCallCountFree(), 0);
  322 
  323     uriFreeQueryListMmA(queryList, &failingMemoryManager);
  324 
  325     ASSERT_GE(failingMemoryManager.getCallCountFree(), 1);
  326 }
  327 
  328 
  329 
  330 TEST(FailingMemoryManagerSuite, FreeUriMembersMm) {
  331     UriUriA uri = parse("http://example.org/");
  332     FailingMemoryManager failingMemoryManager;
  333     ASSERT_EQ(failingMemoryManager.getCallCountFree(), 0);
  334 
  335     uriFreeUriMembersMmA(&uri, &failingMemoryManager);
  336 
  337     ASSERT_GE(failingMemoryManager.getCallCountFree(), 1);
  338     uriFreeUriMembersA(&uri);
  339 }
  340 
  341 
  342 
  343 TEST(FailingMemoryManagerSuite, NormalizeSyntaxExMm) {
  344     UriUriA uri = parse("hTTp://example.org/path");
  345     const unsigned int mask = URI_NORMALIZE_SCHEME;  // anything but URI_NORMALIZED
  346     FailingMemoryManager failingMemoryManager;
  347 
  348     ASSERT_EQ(uriNormalizeSyntaxExMmA(&uri, mask, &failingMemoryManager),
  349             URI_ERROR_MALLOC);
  350 
  351     uriFreeUriMembersA(&uri);
  352 }
  353 
  354 
  355 
  356 TEST(FailingMemoryManagerSuite, ParseSingleUriExMm) {
  357     UriUriA uri;
  358     const char * const first = "k1=v1&k2=v2";
  359     const char * const afterLast = first + strlen(first);
  360     FailingMemoryManager failingMemoryManager;
  361 
  362     ASSERT_EQ(uriParseSingleUriExMmA(&uri, first, afterLast, NULL,
  363             &failingMemoryManager),
  364             URI_ERROR_MALLOC);
  365 }
  366 
  367 
  368 
  369 TEST(FailingMemoryManagerSuite, RemoveBaseUriMm) {
  370     UriUriA dest;
  371     UriUriA absoluteSource = parse("http://example.org/a/b/c/");
  372     UriUriA absoluteBase = parse("http://example.org/a/");
  373     const UriBool domainRootMode = URI_TRUE;  // not of interest
  374     FailingMemoryManager failingMemoryManager;
  375 
  376     ASSERT_EQ(uriRemoveBaseUriMmA(&dest, &absoluteSource, &absoluteBase,
  377             domainRootMode, &failingMemoryManager),
  378             URI_ERROR_MALLOC);
  379 
  380     uriFreeUriMembersA(&absoluteSource);
  381     uriFreeUriMembersA(&absoluteBase);
  382 }