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