xref: /third_party/curl/tests/libtest/lib576.c (revision 13498266)
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 "test.h"
25
26#include "testutil.h"
27#include "memdebug.h"
28
29struct chunk_data {
30  int remains;
31  int print_content;
32};
33
34static
35long chunk_bgn(const struct curl_fileinfo *finfo, void *ptr, int remains)
36{
37  struct chunk_data *ch_d = ptr;
38  ch_d->remains = remains;
39
40  printf("=============================================================\n");
41  printf("Remains:      %d\n", remains);
42  printf("Filename:     %s\n", finfo->filename);
43  if(finfo->strings.perm) {
44    printf("Permissions:  %s", finfo->strings.perm);
45    if(finfo->flags & CURLFINFOFLAG_KNOWN_PERM)
46      printf(" (parsed => %o)", finfo->perm);
47    printf("\n");
48  }
49  printf("Size:         %ldB\n", (long)finfo->size);
50  if(finfo->strings.user)
51    printf("User:         %s\n", finfo->strings.user);
52  if(finfo->strings.group)
53    printf("Group:        %s\n", finfo->strings.group);
54  if(finfo->strings.time)
55    printf("Time:         %s\n", finfo->strings.time);
56  printf("Filetype:     ");
57  switch(finfo->filetype) {
58  case CURLFILETYPE_FILE:
59    printf("regular file\n");
60    break;
61  case CURLFILETYPE_DIRECTORY:
62    printf("directory\n");
63    break;
64  case CURLFILETYPE_SYMLINK:
65    printf("symlink\n");
66    printf("Target:       %s\n", finfo->strings.target);
67    break;
68  default:
69    printf("other type\n");
70    break;
71  }
72  if(finfo->filetype == CURLFILETYPE_FILE) {
73    ch_d->print_content = 1;
74    printf("Content:\n"
75      "-------------------------------------------------------------\n");
76  }
77  if(strcmp(finfo->filename, "someothertext.txt") == 0) {
78    printf("# THIS CONTENT WAS SKIPPED IN CHUNK_BGN CALLBACK #\n");
79    return CURL_CHUNK_BGN_FUNC_SKIP;
80  }
81  return CURL_CHUNK_BGN_FUNC_OK;
82}
83
84static
85long chunk_end(void *ptr)
86{
87  struct chunk_data *ch_d = ptr;
88  if(ch_d->print_content) {
89    ch_d->print_content = 0;
90    printf("-------------------------------------------------------------\n");
91  }
92  if(ch_d->remains == 1)
93    printf("=============================================================\n");
94  return CURL_CHUNK_END_FUNC_OK;
95}
96
97int test(char *URL)
98{
99  CURL *handle = NULL;
100  CURLcode res = CURLE_OK;
101  struct chunk_data chunk_data = {0, 0};
102  curl_global_init(CURL_GLOBAL_ALL);
103  handle = curl_easy_init();
104  if(!handle) {
105    res = CURLE_OUT_OF_MEMORY;
106    goto test_cleanup;
107  }
108
109  test_setopt(handle, CURLOPT_URL, URL);
110  test_setopt(handle, CURLOPT_WILDCARDMATCH, 1L);
111  test_setopt(handle, CURLOPT_CHUNK_BGN_FUNCTION, chunk_bgn);
112  test_setopt(handle, CURLOPT_CHUNK_END_FUNCTION, chunk_end);
113  test_setopt(handle, CURLOPT_CHUNK_DATA, &chunk_data);
114
115  res = curl_easy_perform(handle);
116
117test_cleanup:
118  if(handle)
119    curl_easy_cleanup(handle);
120  curl_global_cleanup();
121  return res;
122}
123