xref: /curl/tests/libtest/lib652.c (revision 71cf0d1f)
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 "memdebug.h"
27 
28 static char testbuf[17000]; /* more than 16K */
29 
test(char * URL)30 CURLcode test(char *URL)
31 {
32   CURL *curl = NULL;
33   CURLcode res = CURLE_OK;
34   curl_mime *mime = NULL;
35   curl_mimepart *part;
36   struct curl_slist *recipients = NULL;
37 
38   /* create a testbuf with AAAA...BBBBB...CCCC...etc */
39   int i;
40   int size = (int)sizeof(testbuf) / 10;
41 
42   for(i = 0; i < size ; i++)
43     memset(&testbuf[i * 10], 65 + (i % 26), 10);
44 
45   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
46     fprintf(stderr, "curl_global_init() failed\n");
47     return TEST_ERR_MAJOR_BAD;
48   }
49 
50   curl = curl_easy_init();
51   if(!curl) {
52     fprintf(stderr, "curl_easy_init() failed\n");
53     res = TEST_ERR_MAJOR_BAD;
54     goto test_cleanup;
55   }
56 
57   /* Build mime structure. */
58   mime = curl_mime_init(curl);
59   if(!mime) {
60     fprintf(stderr, "curl_mime_init() failed\n");
61     res = TEST_ERR_MAJOR_BAD;
62     goto test_cleanup;
63   }
64   part = curl_mime_addpart(mime);
65   if(!part) {
66     fprintf(stderr, "curl_mime_addpart() failed\n");
67     res = TEST_ERR_MAJOR_BAD;
68     goto test_cleanup;
69   }
70   res = curl_mime_filename(part, "myfile.jpg");
71   if(res) {
72     fprintf(stderr, "curl_mime_filename() failed\n");
73     goto test_cleanup;
74   }
75   res = curl_mime_type(part, "image/jpeg");
76   if(res) {
77     fprintf(stderr, "curl_mime_type() failed\n");
78     goto test_cleanup;
79   }
80   res = curl_mime_data(part, testbuf, sizeof(testbuf));
81   if(res) {
82     fprintf(stderr, "curl_mime_data() failed\n");
83     goto test_cleanup;
84   }
85   res = curl_mime_encoder(part, "base64");
86   if(res) {
87     fprintf(stderr, "curl_mime_encoder() failed\n");
88     goto test_cleanup;
89   }
90 
91   /* Prepare recipients. */
92   recipients = curl_slist_append(NULL, "someone@example.com");
93   if(!recipients) {
94     fprintf(stderr, "curl_slist_append() failed\n");
95     goto test_cleanup;
96   }
97 
98   /* First set the URL that is about to receive our mime mail. */
99   test_setopt(curl, CURLOPT_URL, URL);
100 
101   /* Set sender. */
102   test_setopt(curl, CURLOPT_MAIL_FROM, "somebody@example.com");
103 
104   /* Set recipients. */
105   test_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
106 
107   /* send a multi-part mail */
108   test_setopt(curl, CURLOPT_MIMEPOST, mime);
109 
110   /* Shorten upload buffer. */
111   test_setopt(curl, CURLOPT_UPLOAD_BUFFERSIZE, 16411L);
112 
113   /* get verbose debug output please */
114   test_setopt(curl, CURLOPT_VERBOSE, 1L);
115 
116   /* Perform the request, res will get the return code */
117   res = curl_easy_perform(curl);
118 
119 test_cleanup:
120 
121   /* always cleanup */
122   curl_easy_cleanup(curl);
123 
124   /* now cleanup the mime structure */
125   curl_mime_free(mime);
126 
127   /* cleanup the recipients. */
128   curl_slist_free_all(recipients);
129 
130   curl_global_cleanup();
131 
132   return res;
133 }
134