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#define CURL_NO_FMT_CHECKS
2513498266Sopenharmony_ci
2613498266Sopenharmony_ci#include "curlcheck.h"
2713498266Sopenharmony_ci
2813498266Sopenharmony_ci#include "urldata.h"
2913498266Sopenharmony_ci#include "sendf.h"
3013498266Sopenharmony_ci
3113498266Sopenharmony_ci/*
3213498266Sopenharmony_ci * This test hardcodes the knowledge of the buffer size which is internal to
3313498266Sopenharmony_ci * Curl_infof(). If that buffer is changed in size, this tests needs to be
3413498266Sopenharmony_ci * updated to still be valid.
3513498266Sopenharmony_ci */
3613498266Sopenharmony_ci
3713498266Sopenharmony_cistatic struct Curl_easy *data;
3813498266Sopenharmony_ci
3913498266Sopenharmony_cistatic char input[4096];
4013498266Sopenharmony_cistatic char result[4096];
4113498266Sopenharmony_ci
4213498266Sopenharmony_ciint debugf_cb(CURL *handle, curl_infotype type, char *buf, size_t size,
4313498266Sopenharmony_ci              void *userptr);
4413498266Sopenharmony_ci
4513498266Sopenharmony_ci/*
4613498266Sopenharmony_ci * This debugf callback is simply dumping the string into the static buffer
4713498266Sopenharmony_ci * for the unit test to inspect. Since we know that we're only dealing with
4813498266Sopenharmony_ci * text we can afford the luxury of skipping the type check here.
4913498266Sopenharmony_ci */
5013498266Sopenharmony_ciint
5113498266Sopenharmony_cidebugf_cb(CURL *handle, curl_infotype type, char *buf, size_t size,
5213498266Sopenharmony_ci                void *userptr)
5313498266Sopenharmony_ci{
5413498266Sopenharmony_ci  (void)handle;
5513498266Sopenharmony_ci  (void)type;
5613498266Sopenharmony_ci  (void)userptr;
5713498266Sopenharmony_ci
5813498266Sopenharmony_ci  memset(result, '\0', sizeof(result));
5913498266Sopenharmony_ci  memcpy(result, buf, size);
6013498266Sopenharmony_ci  return 0;
6113498266Sopenharmony_ci}
6213498266Sopenharmony_ci
6313498266Sopenharmony_cistatic CURLcode
6413498266Sopenharmony_ciunit_setup(void)
6513498266Sopenharmony_ci{
6613498266Sopenharmony_ci  CURLcode res = CURLE_OK;
6713498266Sopenharmony_ci
6813498266Sopenharmony_ci  global_init(CURL_GLOBAL_ALL);
6913498266Sopenharmony_ci  data = curl_easy_init();
7013498266Sopenharmony_ci  if(!data) {
7113498266Sopenharmony_ci    curl_global_cleanup();
7213498266Sopenharmony_ci    return CURLE_OUT_OF_MEMORY;
7313498266Sopenharmony_ci  }
7413498266Sopenharmony_ci  curl_easy_setopt(data, CURLOPT_DEBUGFUNCTION, debugf_cb);
7513498266Sopenharmony_ci  curl_easy_setopt(data, CURLOPT_VERBOSE, 1L);
7613498266Sopenharmony_ci  return res;
7713498266Sopenharmony_ci}
7813498266Sopenharmony_ci
7913498266Sopenharmony_cistatic void
8013498266Sopenharmony_ciunit_stop(void)
8113498266Sopenharmony_ci{
8213498266Sopenharmony_ci  curl_easy_cleanup(data);
8313498266Sopenharmony_ci  curl_global_cleanup();
8413498266Sopenharmony_ci}
8513498266Sopenharmony_ci
8613498266Sopenharmony_cistatic int verify(const char *info, const char *two)
8713498266Sopenharmony_ci{
8813498266Sopenharmony_ci  /* the 'info' one has a newline appended */
8913498266Sopenharmony_ci  char *nl = strchr(info, '\n');
9013498266Sopenharmony_ci  if(!nl)
9113498266Sopenharmony_ci    return 1; /* nope */
9213498266Sopenharmony_ci  return strncmp(info, two, nl - info);
9313498266Sopenharmony_ci}
9413498266Sopenharmony_ci
9513498266Sopenharmony_ciUNITTEST_START
9613498266Sopenharmony_ci
9713498266Sopenharmony_ci/* Injecting a simple short string via a format */
9813498266Sopenharmony_cimsnprintf(input, sizeof(input), "Simple Test");
9913498266Sopenharmony_ciCurl_infof(data, "%s", input);
10013498266Sopenharmony_cifail_unless(verify(result, input) == 0, "Simple string test");
10113498266Sopenharmony_ci
10213498266Sopenharmony_ci/* Injecting a few different variables with a format */
10313498266Sopenharmony_ciCurl_infof(data, "%s %u testing %lu", input, 42, 43L);
10413498266Sopenharmony_cifail_unless(verify(result, "Simple Test 42 testing 43\n") == 0,
10513498266Sopenharmony_ci            "Format string");
10613498266Sopenharmony_ci
10713498266Sopenharmony_ci/* Variations of empty strings */
10813498266Sopenharmony_ciCurl_infof(data, "");
10913498266Sopenharmony_cifail_unless(strlen(result) == 1, "Empty string");
11013498266Sopenharmony_ciCurl_infof(data, "%s", (char *)NULL);
11113498266Sopenharmony_cifail_unless(verify(result, "(nil)") == 0, "Passing NULL as string");
11213498266Sopenharmony_ci
11313498266Sopenharmony_ci/* A string just long enough to not be truncated */
11413498266Sopenharmony_cimemset(input, '\0', sizeof(input));
11513498266Sopenharmony_cimemset(input, 'A', 2047);
11613498266Sopenharmony_ciCurl_infof(data, "%s", input);
11713498266Sopenharmony_cifail_unless(strlen(result) == 2048, "No truncation of infof input");
11813498266Sopenharmony_cifail_unless(verify(result, input) == 0, "No truncation of infof input");
11913498266Sopenharmony_cifail_unless(result[sizeof(result) - 1] == '\0',
12013498266Sopenharmony_ci            "No truncation of infof input");
12113498266Sopenharmony_ci
12213498266Sopenharmony_ci/* Just over the limit for truncation without newline */
12313498266Sopenharmony_cimemset(input + 2047, 'A', 4);
12413498266Sopenharmony_ciCurl_infof(data, "%s", input);
12513498266Sopenharmony_cifail_unless(strlen(result) == 2048, "Truncation of infof input 1");
12613498266Sopenharmony_cifail_unless(result[sizeof(result) - 1] == '\0', "Truncation of infof input 1");
12713498266Sopenharmony_ci
12813498266Sopenharmony_ci/* Just over the limit for truncation with newline */
12913498266Sopenharmony_cimemset(input + 2047, 'A', 4);
13013498266Sopenharmony_cimemset(input + 2047 + 4, '\n', 1);
13113498266Sopenharmony_ciCurl_infof(data, "%s", input);
13213498266Sopenharmony_cifail_unless(strlen(result) == 2048, "Truncation of infof input 2");
13313498266Sopenharmony_cifail_unless(result[sizeof(result) - 1] == '\0', "Truncation of infof input 2");
13413498266Sopenharmony_ci
13513498266Sopenharmony_ci/* Way over the limit for truncation with newline */
13613498266Sopenharmony_cimemset(input, '\0', sizeof(input));
13713498266Sopenharmony_cimemset(input, 'A', sizeof(input) - 1);
13813498266Sopenharmony_ciCurl_infof(data, "%s", input);
13913498266Sopenharmony_cifail_unless(strlen(result) == 2048, "Truncation of infof input 3");
14013498266Sopenharmony_cifail_unless(result[sizeof(result) - 1] == '\0', "Truncation of infof input 3");
14113498266Sopenharmony_ci
14213498266Sopenharmony_ci
14313498266Sopenharmony_ciUNITTEST_STOP
144