quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

lib1520.c (3031B)


      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 "first.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 
     34 struct upload_status {
     35   int lines_read;
     36 };
     37 
     38 static size_t t1520_read_cb(char *ptr, size_t size, size_t nmemb, void *userp)
     39 {
     40   static const char *payload_text[] = {
     41     "From: different\r\n",
     42     "To: another\r\n",
     43     "\r\n",
     44     "\r\n",
     45     ".\r\n",
     46     ".\r\n",
     47     "\r\n",
     48     ".\r\n",
     49     "\r\n",
     50     "body",
     51     NULL
     52   };
     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 
     74 static CURLcode test_lib1520(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     curl_mfprintf(stderr, "curl_global_init() failed\n");
     83     return TEST_ERR_MAJOR_BAD;
     84   }
     85 
     86   curl = curl_easy_init();
     87   if(!curl) {
     88     curl_mfprintf(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, t1520_read_cb);
    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 
    108 test_cleanup:
    109 
    110   curl_slist_free_all(rcpt_list);
    111   curl_easy_cleanup(curl);
    112   curl_global_cleanup();
    113 
    114   return res;
    115 }