113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci#include "curlcheck.h"
2513498266Sopenharmony_ci
2613498266Sopenharmony_ci#include "bufref.h"
2713498266Sopenharmony_ci
2813498266Sopenharmony_cistatic struct bufref bufref;
2913498266Sopenharmony_ci
3013498266Sopenharmony_cistatic int freecount = 0;
3113498266Sopenharmony_ci
3213498266Sopenharmony_cistatic void test_free(void *p)
3313498266Sopenharmony_ci{
3413498266Sopenharmony_ci  fail_unless(p, "pointer to free may not be NULL");
3513498266Sopenharmony_ci  freecount++;
3613498266Sopenharmony_ci  free(p);
3713498266Sopenharmony_ci}
3813498266Sopenharmony_ci
3913498266Sopenharmony_cistatic CURLcode unit_setup(void)
4013498266Sopenharmony_ci{
4113498266Sopenharmony_ci  Curl_bufref_init(&bufref);
4213498266Sopenharmony_ci  return CURLE_OK;
4313498266Sopenharmony_ci}
4413498266Sopenharmony_ci
4513498266Sopenharmony_cistatic void unit_stop(void)
4613498266Sopenharmony_ci{
4713498266Sopenharmony_ci}
4813498266Sopenharmony_ci
4913498266Sopenharmony_ciUNITTEST_START
5013498266Sopenharmony_ci{
5113498266Sopenharmony_ci  char *buffer = NULL;
5213498266Sopenharmony_ci  CURLcode result = CURLE_OK;
5313498266Sopenharmony_ci
5413498266Sopenharmony_ci  /**
5513498266Sopenharmony_ci   * testing Curl_bufref_init.
5613498266Sopenharmony_ci   * @assumptions:
5713498266Sopenharmony_ci   * 1: data size will be 0
5813498266Sopenharmony_ci   * 2: reference will be NULL
5913498266Sopenharmony_ci   * 3: destructor will be NULL
6013498266Sopenharmony_ci   */
6113498266Sopenharmony_ci
6213498266Sopenharmony_ci  fail_unless(!bufref.ptr, "Initial reference must be NULL");
6313498266Sopenharmony_ci  fail_unless(!bufref.len, "Initial length must be NULL");
6413498266Sopenharmony_ci  fail_unless(!bufref.dtor, "Destructor must be NULL");
6513498266Sopenharmony_ci
6613498266Sopenharmony_ci  /**
6713498266Sopenharmony_ci   * testing Curl_bufref_set
6813498266Sopenharmony_ci   */
6913498266Sopenharmony_ci
7013498266Sopenharmony_ci  buffer = malloc(13);
7113498266Sopenharmony_ci  abort_unless(buffer, "Out of memory");
7213498266Sopenharmony_ci  Curl_bufref_set(&bufref, buffer, 13, test_free);
7313498266Sopenharmony_ci
7413498266Sopenharmony_ci  fail_unless((char *) bufref.ptr == buffer, "Referenced data badly set");
7513498266Sopenharmony_ci  fail_unless(bufref.len == 13, "Data size badly set");
7613498266Sopenharmony_ci  fail_unless(bufref.dtor == test_free, "Destructor badly set");
7713498266Sopenharmony_ci
7813498266Sopenharmony_ci  /**
7913498266Sopenharmony_ci   * testing Curl_bufref_ptr
8013498266Sopenharmony_ci   */
8113498266Sopenharmony_ci
8213498266Sopenharmony_ci  fail_unless((char *) Curl_bufref_ptr(&bufref) == buffer,
8313498266Sopenharmony_ci              "Wrong pointer value returned");
8413498266Sopenharmony_ci
8513498266Sopenharmony_ci  /**
8613498266Sopenharmony_ci   * testing Curl_bufref_len
8713498266Sopenharmony_ci   */
8813498266Sopenharmony_ci
8913498266Sopenharmony_ci  fail_unless(Curl_bufref_len(&bufref) == 13, "Wrong data size returned");
9013498266Sopenharmony_ci
9113498266Sopenharmony_ci  /**
9213498266Sopenharmony_ci   * testing Curl_bufref_memdup
9313498266Sopenharmony_ci   */
9413498266Sopenharmony_ci
9513498266Sopenharmony_ci  result = Curl_bufref_memdup(&bufref, "1661", 3);
9613498266Sopenharmony_ci  abort_unless(result == CURLE_OK, curl_easy_strerror(result));
9713498266Sopenharmony_ci  fail_unless(freecount == 1, "Destructor not called");
9813498266Sopenharmony_ci  fail_unless((char *) bufref.ptr != buffer, "Returned pointer not set");
9913498266Sopenharmony_ci  buffer = (char *) Curl_bufref_ptr(&bufref);
10013498266Sopenharmony_ci  fail_unless(buffer, "Allocated pointer is NULL");
10113498266Sopenharmony_ci  fail_unless(bufref.len == 3, "Wrong data size stored");
10213498266Sopenharmony_ci  fail_unless(!buffer[3], "Duplicated data should have been truncated");
10313498266Sopenharmony_ci  fail_unless(!strcmp(buffer, "166"), "Bad duplicated data");
10413498266Sopenharmony_ci
10513498266Sopenharmony_ci  /**
10613498266Sopenharmony_ci   * testing Curl_bufref_free
10713498266Sopenharmony_ci   */
10813498266Sopenharmony_ci
10913498266Sopenharmony_ci  Curl_bufref_free(&bufref);
11013498266Sopenharmony_ci  fail_unless(freecount == 1, "Wrong destructor called");
11113498266Sopenharmony_ci  fail_unless(!bufref.ptr, "Initial reference must be NULL");
11213498266Sopenharmony_ci  fail_unless(!bufref.len, "Initial length must be NULL");
11313498266Sopenharmony_ci  fail_unless(!bufref.dtor, "Destructor must be NULL");
11413498266Sopenharmony_ci}
11513498266Sopenharmony_ciUNITTEST_STOP
116