xref: /third_party/curl/tests/libtest/lib1156.c (revision 13498266)
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 "test.h"
2513498266Sopenharmony_ci
2613498266Sopenharmony_ci/*
2713498266Sopenharmony_ci  Check range/resume returned error codes and data presence.
2813498266Sopenharmony_ci
2913498266Sopenharmony_ci  The input parameters are:
3013498266Sopenharmony_ci  - CURLOPT_RANGE/CURLOPT_RESUME_FROM
3113498266Sopenharmony_ci  - CURLOPT_FAILONERROR
3213498266Sopenharmony_ci  - Returned http code (2xx/416)
3313498266Sopenharmony_ci  - Content-Range header present in reply.
3413498266Sopenharmony_ci
3513498266Sopenharmony_ci*/
3613498266Sopenharmony_ci
3713498266Sopenharmony_ci#include "memdebug.h"
3813498266Sopenharmony_ci
3913498266Sopenharmony_ci#define F_RESUME        (1 << 0)        /* resume/range. */
4013498266Sopenharmony_ci#define F_HTTP416       (1 << 1)        /* Server returns http code 416. */
4113498266Sopenharmony_ci#define F_FAIL          (1 << 2)        /* Fail on error. */
4213498266Sopenharmony_ci#define F_CONTENTRANGE  (1 << 3)        /* Server sends content-range hdr. */
4313498266Sopenharmony_ci#define F_IGNOREBODY    (1 << 4)        /* Body should be ignored. */
4413498266Sopenharmony_ci
4513498266Sopenharmony_cistruct testparams {
4613498266Sopenharmony_ci  unsigned int flags; /* ORed flags as above. */
4713498266Sopenharmony_ci  CURLcode result; /* Code that should be returned by curl_easy_perform(). */
4813498266Sopenharmony_ci};
4913498266Sopenharmony_ci
5013498266Sopenharmony_cistatic const struct testparams params[] = {
5113498266Sopenharmony_ci  { 0,                                                             CURLE_OK },
5213498266Sopenharmony_ci  {                                 F_CONTENTRANGE,                CURLE_OK },
5313498266Sopenharmony_ci  {                        F_FAIL,                                 CURLE_OK },
5413498266Sopenharmony_ci  {                        F_FAIL | F_CONTENTRANGE,                CURLE_OK },
5513498266Sopenharmony_ci  {            F_HTTP416,                                          CURLE_OK },
5613498266Sopenharmony_ci  {            F_HTTP416 |          F_CONTENTRANGE,                CURLE_OK },
5713498266Sopenharmony_ci  {            F_HTTP416 | F_FAIL |                  F_IGNOREBODY,
5813498266Sopenharmony_ci                                                  CURLE_HTTP_RETURNED_ERROR },
5913498266Sopenharmony_ci  {            F_HTTP416 | F_FAIL | F_CONTENTRANGE | F_IGNOREBODY,
6013498266Sopenharmony_ci                                                  CURLE_HTTP_RETURNED_ERROR },
6113498266Sopenharmony_ci  { F_RESUME |                                       F_IGNOREBODY,
6213498266Sopenharmony_ci                                                          CURLE_RANGE_ERROR },
6313498266Sopenharmony_ci  { F_RESUME |                      F_CONTENTRANGE,                CURLE_OK },
6413498266Sopenharmony_ci  { F_RESUME |             F_FAIL |                  F_IGNOREBODY,
6513498266Sopenharmony_ci                                                          CURLE_RANGE_ERROR },
6613498266Sopenharmony_ci  { F_RESUME |             F_FAIL | F_CONTENTRANGE,                CURLE_OK },
6713498266Sopenharmony_ci  { F_RESUME | F_HTTP416 |                           F_IGNOREBODY, CURLE_OK },
6813498266Sopenharmony_ci  { F_RESUME | F_HTTP416 |          F_CONTENTRANGE | F_IGNOREBODY, CURLE_OK },
6913498266Sopenharmony_ci  { F_RESUME | F_HTTP416 | F_FAIL |                  F_IGNOREBODY, CURLE_OK },
7013498266Sopenharmony_ci  { F_RESUME | F_HTTP416 | F_FAIL | F_CONTENTRANGE | F_IGNOREBODY,
7113498266Sopenharmony_ci                                                                   CURLE_OK }
7213498266Sopenharmony_ci};
7313498266Sopenharmony_ci
7413498266Sopenharmony_cistatic int      hasbody;
7513498266Sopenharmony_ci
7613498266Sopenharmony_cistatic size_t writedata(char *data, size_t size, size_t nmemb, void *userdata)
7713498266Sopenharmony_ci{
7813498266Sopenharmony_ci  (void) data;
7913498266Sopenharmony_ci  (void) userdata;
8013498266Sopenharmony_ci
8113498266Sopenharmony_ci  if(size && nmemb)
8213498266Sopenharmony_ci    hasbody = 1;
8313498266Sopenharmony_ci  return size * nmemb;
8413498266Sopenharmony_ci}
8513498266Sopenharmony_ci
8613498266Sopenharmony_cistatic int onetest(CURL *curl, const char *url, const struct testparams *p,
8713498266Sopenharmony_ci                   size_t num)
8813498266Sopenharmony_ci{
8913498266Sopenharmony_ci  CURLcode res;
9013498266Sopenharmony_ci  unsigned int replyselector;
9113498266Sopenharmony_ci  char urlbuf[256];
9213498266Sopenharmony_ci
9313498266Sopenharmony_ci  replyselector = (p->flags & F_CONTENTRANGE)? 1: 0;
9413498266Sopenharmony_ci  if(p->flags & F_HTTP416)
9513498266Sopenharmony_ci    replyselector += 2;
9613498266Sopenharmony_ci  msnprintf(urlbuf, sizeof(urlbuf), "%s%04u", url, replyselector);
9713498266Sopenharmony_ci  test_setopt(curl, CURLOPT_URL, urlbuf);
9813498266Sopenharmony_ci  test_setopt(curl, CURLOPT_VERBOSE, 1L);
9913498266Sopenharmony_ci  test_setopt(curl, CURLOPT_RESUME_FROM, (p->flags & F_RESUME)? 3: 0);
10013498266Sopenharmony_ci  test_setopt(curl, CURLOPT_RANGE, !(p->flags & F_RESUME)?
10113498266Sopenharmony_ci                                   "3-1000000": (char *) NULL);
10213498266Sopenharmony_ci  test_setopt(curl, CURLOPT_FAILONERROR, (p->flags & F_FAIL)? 1: 0);
10313498266Sopenharmony_ci  hasbody = 0;
10413498266Sopenharmony_ci  res = curl_easy_perform(curl);
10513498266Sopenharmony_ci  if(res != p->result) {
10613498266Sopenharmony_ci    printf("%zd: bad error code (%d): resume=%s, fail=%s, http416=%s, "
10713498266Sopenharmony_ci           "content-range=%s, expected=%d\n", num, res,
10813498266Sopenharmony_ci           (p->flags & F_RESUME)? "yes": "no",
10913498266Sopenharmony_ci           (p->flags & F_FAIL)? "yes": "no",
11013498266Sopenharmony_ci           (p->flags & F_HTTP416)? "yes": "no",
11113498266Sopenharmony_ci           (p->flags & F_CONTENTRANGE)? "yes": "no",
11213498266Sopenharmony_ci           p->result);
11313498266Sopenharmony_ci    return 1;
11413498266Sopenharmony_ci  }
11513498266Sopenharmony_ci  if(hasbody && (p->flags & F_IGNOREBODY)) {
11613498266Sopenharmony_ci    printf("body should be ignored and is not: resume=%s, fail=%s, "
11713498266Sopenharmony_ci           "http416=%s, content-range=%s\n",
11813498266Sopenharmony_ci           (p->flags & F_RESUME)? "yes": "no",
11913498266Sopenharmony_ci           (p->flags & F_FAIL)? "yes": "no",
12013498266Sopenharmony_ci           (p->flags & F_HTTP416)? "yes": "no",
12113498266Sopenharmony_ci           (p->flags & F_CONTENTRANGE)? "yes": "no");
12213498266Sopenharmony_ci    return 1;
12313498266Sopenharmony_ci  }
12413498266Sopenharmony_ci  return 0;
12513498266Sopenharmony_ci
12613498266Sopenharmony_citest_cleanup:
12713498266Sopenharmony_ci
12813498266Sopenharmony_ci  return 1;
12913498266Sopenharmony_ci}
13013498266Sopenharmony_ci
13113498266Sopenharmony_ci/* for debugging: */
13213498266Sopenharmony_ci/* #define SINGLETEST 9 */
13313498266Sopenharmony_ci
13413498266Sopenharmony_ciint test(char *URL)
13513498266Sopenharmony_ci{
13613498266Sopenharmony_ci  CURLcode res;
13713498266Sopenharmony_ci  CURL *curl;
13813498266Sopenharmony_ci  size_t i;
13913498266Sopenharmony_ci  int status = 0;
14013498266Sopenharmony_ci
14113498266Sopenharmony_ci  if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
14213498266Sopenharmony_ci    fprintf(stderr, "curl_global_init() failed\n");
14313498266Sopenharmony_ci    return TEST_ERR_MAJOR_BAD;
14413498266Sopenharmony_ci  }
14513498266Sopenharmony_ci
14613498266Sopenharmony_ci  for(i = 0; i < sizeof(params) / sizeof(params[0]); i++) {
14713498266Sopenharmony_ci    curl = curl_easy_init();
14813498266Sopenharmony_ci    if(!curl) {
14913498266Sopenharmony_ci      fprintf(stderr, "curl_easy_init() failed\n");
15013498266Sopenharmony_ci      curl_global_cleanup();
15113498266Sopenharmony_ci      return TEST_ERR_MAJOR_BAD;
15213498266Sopenharmony_ci    }
15313498266Sopenharmony_ci
15413498266Sopenharmony_ci    test_setopt(curl, CURLOPT_WRITEFUNCTION, writedata);
15513498266Sopenharmony_ci
15613498266Sopenharmony_ci#ifdef SINGLETEST
15713498266Sopenharmony_ci    if(SINGLETEST == i)
15813498266Sopenharmony_ci#endif
15913498266Sopenharmony_ci      status |= onetest(curl, URL, params + i, i);
16013498266Sopenharmony_ci    curl_easy_cleanup(curl);
16113498266Sopenharmony_ci  }
16213498266Sopenharmony_ci
16313498266Sopenharmony_ci  curl_global_cleanup();
16413498266Sopenharmony_ci  printf("%d\n", status);
16513498266Sopenharmony_ci  return status;
16613498266Sopenharmony_ci
16713498266Sopenharmony_citest_cleanup:
16813498266Sopenharmony_ci
16913498266Sopenharmony_ci  curl_easy_cleanup(curl);
17013498266Sopenharmony_ci  curl_global_cleanup();
17113498266Sopenharmony_ci
17213498266Sopenharmony_ci  return (int)res;
17313498266Sopenharmony_ci}
174