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