Home | History | Annotate | Download | only in libtest
      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) 2014, Steve Holme, <steve_holme (at) 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.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 (at) example.com>"
     30 #define FROM "<sender (at) 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   curl = curl_easy_init();
     85   if(!curl) {
     86     fprintf(stderr, "curl_easy_init() failed\n");
     87     curl_global_cleanup();
     88     return TEST_ERR_MAJOR_BAD;
     89   }
     90 
     91   rcpt_list = curl_slist_append(rcpt_list, TO);
     92   /* more addresses can be added here
     93      rcpt_list = curl_slist_append(rcpt_list, "<others (at) example.com>");
     94   */
     95 
     96   test_setopt(curl, CURLOPT_URL, URL);
     97   test_setopt(curl, CURLOPT_UPLOAD, 1L);
     98   test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
     99   test_setopt(curl, CURLOPT_READDATA, &upload_ctx);
    100   test_setopt(curl, CURLOPT_MAIL_FROM, FROM);
    101   test_setopt(curl, CURLOPT_MAIL_RCPT, rcpt_list);
    102   test_setopt(curl, CURLOPT_VERBOSE, 1L);
    103 
    104   res = curl_easy_perform(curl);
    105 
    106 test_cleanup:
    107 
    108   curl_slist_free_all(rcpt_list);
    109   curl_easy_cleanup(curl);
    110   curl_global_cleanup();
    111 
    112   return (int)res;
    113 }
    114 
    115 
    116