Imported Upstream version 7.40.0
[platform/upstream/curl.git] / tests / libtest / lib1520.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 2014, Steve Holme, <steve_holme@hotmail.com>.
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 http://curl.haxx.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  ***************************************************************************/
22 #include "test.h"
23
24 #include "memdebug.h"
25
26 /*
27  * This is the list of basic details you need to tweak to get things right.
28  */
29 #define TO "<recipient@example.com>"
30 #define FROM "<sender@example.com>"
31
32 static const char *payload_text[] = {
33   "From: different\r\n",
34   "To: another\r\n",
35   "\r\n",
36   "\r\n",
37   ".\r\n",
38   ".\r\n",
39   "\r\n",
40   ".\r\n",
41   "\r\n",
42   "body",
43   NULL
44 };
45
46 struct upload_status {
47   int lines_read;
48 };
49
50 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
51 {
52   struct upload_status *upload_ctx = (struct upload_status *)userp;
53   const char *data;
54
55   if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
56     return 0;
57   }
58
59   data = payload_text[upload_ctx->lines_read];
60
61   if(data) {
62     size_t len = strlen(data);
63     memcpy(ptr, data, len);
64     upload_ctx->lines_read++;
65
66     return len;
67   }
68
69   return 0;
70 }
71
72 int test(char *URL)
73 {
74   CURLcode res;
75   CURL *curl;
76   struct curl_slist *rcpt_list = NULL;
77   struct upload_status upload_ctx = {0};
78
79   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
80     fprintf(stderr, "curl_global_init() failed\n");
81     return TEST_ERR_MAJOR_BAD;
82   }
83
84   if((curl = curl_easy_init()) == NULL) {
85     fprintf(stderr, "curl_easy_init() failed\n");
86     curl_global_cleanup();
87     return TEST_ERR_MAJOR_BAD;
88   }
89
90   rcpt_list = curl_slist_append(rcpt_list, TO);
91   /* more addresses can be added here
92      rcpt_list = curl_slist_append(rcpt_list, "<others@example.com>");
93   */
94
95   test_setopt(curl, CURLOPT_URL, URL);
96   test_setopt(curl, CURLOPT_UPLOAD, 1L);
97   test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
98   test_setopt(curl, CURLOPT_READDATA, &upload_ctx);
99   test_setopt(curl, CURLOPT_MAIL_FROM, FROM);
100   test_setopt(curl, CURLOPT_MAIL_RCPT, rcpt_list);
101   test_setopt(curl, CURLOPT_VERBOSE, 1L);
102
103   res = curl_easy_perform(curl);
104
105 test_cleanup:
106
107   curl_slist_free_all(rcpt_list);
108   curl_easy_cleanup(curl);
109   curl_global_cleanup();
110
111   return (int)res;
112 }
113
114