Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/curl/tests/libtest/lib1308.c
2654 views
1
/***************************************************************************
2
* _ _ ____ _
3
* Project ___| | | | _ \| |
4
* / __| | | | |_) | |
5
* | (__| |_| | _ <| |___
6
* \___|\___/|_| \_\_____|
7
*
8
* Copyright (C) Daniel Stenberg, <[email protected]>, 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 "first.h"
25
26
static size_t print_httppost_callback(void *arg, const char *buf, size_t len)
27
{
28
fwrite(buf, len, 1, stdout);
29
(*(size_t *) arg) += len;
30
return len;
31
}
32
33
#define t1308_fail_unless(expr, msg) \
34
do { \
35
if(!(expr)) { \
36
curl_mfprintf(stderr, "%s:%d Assertion '%s' FAILED: %s\n", \
37
__FILE__, __LINE__, #expr, msg); \
38
errorcount++; \
39
} \
40
} while(0)
41
42
static CURLcode test_lib1308(const char *URL)
43
{
44
int errorcount = 0;
45
CURLFORMcode rc;
46
CURLcode res = CURLE_OK;
47
int formres = 0;
48
struct curl_httppost *post = NULL;
49
struct curl_httppost *last = NULL;
50
size_t total_size = 0;
51
char buffer[] = "test buffer";
52
53
global_init(CURL_GLOBAL_ALL);
54
55
rc = curl_formadd(&post, &last, CURLFORM_COPYNAME, "name",
56
CURLFORM_COPYCONTENTS, "content", CURLFORM_END);
57
t1308_fail_unless(rc == 0, "curl_formadd returned error");
58
59
/* after the first curl_formadd when there's a single entry, both pointers
60
should point to the same struct */
61
t1308_fail_unless(post == last, "post and last weren't the same");
62
63
rc = curl_formadd(&post, &last, CURLFORM_COPYNAME, "htmlcode",
64
CURLFORM_COPYCONTENTS, "<HTML></HTML>",
65
CURLFORM_CONTENTTYPE, "text/html", CURLFORM_END);
66
t1308_fail_unless(rc == 0, "curl_formadd returned error");
67
68
rc = curl_formadd(&post, &last, CURLFORM_COPYNAME, "name_for_ptrcontent",
69
CURLFORM_PTRCONTENTS, buffer, CURLFORM_END);
70
t1308_fail_unless(rc == 0, "curl_formadd returned error");
71
72
formres = curl_formget(post, &total_size, print_httppost_callback);
73
t1308_fail_unless(formres == 0, "curl_formget returned error");
74
75
t1308_fail_unless(total_size == 518, "curl_formget got wrong size back");
76
77
curl_formfree(post);
78
79
/* start a new formpost with a file upload and formget */
80
post = last = NULL;
81
82
rc = curl_formadd(&post, &last,
83
CURLFORM_PTRNAME, "name of file field",
84
CURLFORM_FILE, URL,
85
CURLFORM_FILENAME, "custom named file",
86
CURLFORM_END);
87
t1308_fail_unless(rc == 0, "curl_formadd returned error");
88
89
formres = curl_formget(post, &total_size, print_httppost_callback);
90
91
t1308_fail_unless(formres == 0, "curl_formget returned error");
92
t1308_fail_unless(total_size == 899, "curl_formget got wrong size back");
93
94
curl_formfree(post);
95
96
curl_global_cleanup();
97
98
return errorcount ? TEST_ERR_FAILURE : CURLE_OK;
99
}
100
101