quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

unit1661.c (3522B)


      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
      9  *
     10  * This software is licensed as described in the file COPYING, which
     11  * you should have received as part of this distribution. The terms
     12  * are also available at https://curl.se/docs/copyright.html.
     13  *
     14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
     15  * copies of the Software, and permit persons to whom the Software is
     16  * furnished to do so, under the terms of the COPYING file.
     17  *
     18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
     19  * KIND, either express or implied.
     20  *
     21  * SPDX-License-Identifier: curl
     22  *
     23  ***************************************************************************/
     24 #include "unitcheck.h"
     25 #include "bufref.h"
     26 #include "memdebug.h"
     27 
     28 static int freecount = 0;
     29 
     30 static void test_free(void *p)
     31 {
     32   fail_unless(p, "pointer to free may not be NULL");
     33   freecount++;
     34   free(p);
     35 }
     36 
     37 static CURLcode t1661_setup(struct bufref *bufref)
     38 {
     39   Curl_bufref_init(bufref);
     40   return CURLE_OK;
     41 }
     42 
     43 static void t1661_stop(struct bufref *bufref)
     44 {
     45   Curl_bufref_free(bufref);
     46 }
     47 
     48 static CURLcode test_unit1661(char *arg)
     49 {
     50   struct bufref bufref;
     51 
     52   UNITTEST_BEGIN(t1661_setup(&bufref))
     53 
     54   const char *buffer = NULL;
     55   CURLcode result = CURLE_OK;
     56 
     57   /**
     58    * testing Curl_bufref_init.
     59    * @assumptions:
     60    * 1: data size will be 0
     61    * 2: reference will be NULL
     62    * 3: destructor will be NULL
     63    */
     64   fail_unless(!bufref.ptr, "Initial reference must be NULL");
     65   fail_unless(!bufref.len, "Initial length must be NULL");
     66   fail_unless(!bufref.dtor, "Destructor must be NULL");
     67 
     68   /**
     69    * testing Curl_bufref_set
     70    */
     71   buffer = malloc(13);
     72   abort_unless(buffer, "Out of memory");
     73   Curl_bufref_set(&bufref, buffer, 13, test_free);
     74 
     75   fail_unless((const char *)bufref.ptr == buffer, "Referenced data badly set");
     76   fail_unless(bufref.len == 13, "Data size badly set");
     77   fail_unless(bufref.dtor == test_free, "Destructor badly set");
     78 
     79   /**
     80    * testing Curl_bufref_ptr
     81    */
     82   fail_unless((const char *) Curl_bufref_ptr(&bufref) == buffer,
     83               "Wrong pointer value returned");
     84 
     85   /**
     86    * testing Curl_bufref_len
     87    */
     88   fail_unless(Curl_bufref_len(&bufref) == 13, "Wrong data size returned");
     89 
     90   /**
     91    * testing Curl_bufref_memdup
     92    */
     93   result = Curl_bufref_memdup(&bufref, "1661", 3);
     94   abort_unless(result == CURLE_OK, curl_easy_strerror(result));
     95   fail_unless(freecount == 1, "Destructor not called");
     96   fail_unless((const char *)bufref.ptr != buffer, "Returned pointer not set");
     97   buffer = (const char *)Curl_bufref_ptr(&bufref);
     98   fail_unless(buffer, "Allocated pointer is NULL");
     99   fail_unless(bufref.len == 3, "Wrong data size stored");
    100   if(buffer) {
    101     fail_unless(!buffer[3], "Duplicated data should have been truncated");
    102     fail_unless(!strcmp(buffer, "166"), "Bad duplicated data");
    103   }
    104 
    105   /**
    106    * testing Curl_bufref_free
    107    */
    108   Curl_bufref_free(&bufref);
    109   fail_unless(freecount == 1, "Wrong destructor called");
    110   fail_unless(!bufref.ptr, "Initial reference must be NULL");
    111   fail_unless(!bufref.len, "Initial length must be NULL");
    112   fail_unless(!bufref.dtor, "Destructor must be NULL");
    113 
    114   UNITTEST_END(t1661_stop(&bufref))
    115 }