Home | History | Annotate | Download | only in unit
      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) 1998 - 2016, 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 "curlcheck.h"
     23 #include "netrc.h"
     24 #include "memdebug.h" /* LAST include file */
     25 
     26 static char *login;
     27 static char *password;
     28 static char filename[64];
     29 
     30 static CURLcode unit_setup(void)
     31 {
     32   password = strdup("");
     33   login = strdup("");
     34   if(!password || !login) {
     35     Curl_safefree(password);
     36     Curl_safefree(login);
     37     return CURLE_OUT_OF_MEMORY;
     38   }
     39   return CURLE_OK;
     40 }
     41 
     42 static void unit_stop(void)
     43 {
     44   Curl_safefree(password);
     45   Curl_safefree(login);
     46 }
     47 
     48 UNITTEST_START
     49   int result;
     50 
     51   static const char * const filename1 = "log/netrc1304";
     52   memcpy(filename, filename1, strlen(filename1));
     53 
     54   /*
     55    * Test a non existent host in our netrc file.
     56    */
     57   result = Curl_parsenetrc("test.example.com", &login, &password, filename);
     58   fail_unless(result == 1, "Host not found should return 1");
     59   abort_unless(password != NULL, "returned NULL!");
     60   fail_unless(password[0] == 0, "password should not have been changed");
     61   abort_unless(login != NULL, "returned NULL!");
     62   fail_unless(login[0] == 0, "login should not have been changed");
     63 
     64   /*
     65    * Test a non existent login in our netrc file.
     66    */
     67   free(login);
     68   login = strdup("me");
     69   abort_unless(login != NULL, "returned NULL!");
     70   result = Curl_parsenetrc("example.com", &login, &password, filename);
     71   fail_unless(result == 0, "Host should be found");
     72   abort_unless(password != NULL, "returned NULL!");
     73   fail_unless(password[0] == 0, "password should not have been changed");
     74   abort_unless(login != NULL, "returned NULL!");
     75   fail_unless(strncmp(login, "me", 2) == 0,
     76               "login should not have been changed");
     77 
     78   /*
     79    * Test a non existent login and host in our netrc file.
     80    */
     81   free(login);
     82   login = strdup("me");
     83   abort_unless(login != NULL, "returned NULL!");
     84   result = Curl_parsenetrc("test.example.com", &login, &password, filename);
     85   fail_unless(result == 1, "Host should be found");
     86   abort_unless(password != NULL, "returned NULL!");
     87   fail_unless(password[0] == 0, "password should not have been changed");
     88   abort_unless(login != NULL, "returned NULL!");
     89   fail_unless(strncmp(login, "me", 2) == 0,
     90               "login should not have been changed");
     91 
     92   /*
     93    * Test a non existent login (substring of an existing one) in our
     94    * netrc file.
     95    */
     96   free(login);
     97   login = strdup("admi");
     98   abort_unless(login != NULL, "returned NULL!");
     99   result = Curl_parsenetrc("example.com", &login, &password, filename);
    100   fail_unless(result == 0, "Host should be found");
    101   abort_unless(password != NULL, "returned NULL!");
    102   fail_unless(password[0] == 0, "password should not have been changed");
    103   abort_unless(login != NULL, "returned NULL!");
    104   fail_unless(strncmp(login, "admi", 4) == 0,
    105               "login should not have been changed");
    106 
    107   /*
    108    * Test a non existent login (superstring of an existing one)
    109    * in our netrc file.
    110    */
    111   free(login);
    112   login = strdup("adminn");
    113   abort_unless(login != NULL, "returned NULL!");
    114   result = Curl_parsenetrc("example.com", &login, &password, filename);
    115   fail_unless(result == 0, "Host should be found");
    116   abort_unless(password != NULL, "returned NULL!");
    117   fail_unless(password[0] == 0, "password should not have been changed");
    118   abort_unless(login != NULL, "returned NULL!");
    119   fail_unless(strncmp(login, "adminn", 6) == 0,
    120               "login should not have been changed");
    121 
    122   /*
    123    * Test for the first existing host in our netrc file
    124    * with login[0] = 0.
    125    */
    126   free(login);
    127   login = strdup("");
    128   abort_unless(login != NULL, "returned NULL!");
    129   result = Curl_parsenetrc("example.com", &login, &password, filename);
    130   fail_unless(result == 0, "Host should have been found");
    131   abort_unless(password != NULL, "returned NULL!");
    132   fail_unless(strncmp(password, "passwd", 6) == 0,
    133               "password should be 'passwd'");
    134   abort_unless(login != NULL, "returned NULL!");
    135   fail_unless(strncmp(login, "admin", 5) == 0, "login should be 'admin'");
    136 
    137   /*
    138    * Test for the first existing host in our netrc file
    139    * with login[0] != 0.
    140    */
    141   free(password);
    142   password = strdup("");
    143   abort_unless(password != NULL, "returned NULL!");
    144   result = Curl_parsenetrc("example.com", &login, &password, filename);
    145   fail_unless(result == 0, "Host should have been found");
    146   abort_unless(password != NULL, "returned NULL!");
    147   fail_unless(strncmp(password, "passwd", 6) == 0,
    148               "password should be 'passwd'");
    149   abort_unless(login != NULL, "returned NULL!");
    150   fail_unless(strncmp(login, "admin", 5) == 0, "login should be 'admin'");
    151 
    152   /*
    153    * Test for the second existing host in our netrc file
    154    * with login[0] = 0.
    155    */
    156   free(password);
    157   password = strdup("");
    158   abort_unless(password != NULL, "returned NULL!");
    159   free(login);
    160   login = strdup("");
    161   abort_unless(login != NULL, "returned NULL!");
    162   result = Curl_parsenetrc("curl.example.com", &login, &password, filename);
    163   fail_unless(result == 0, "Host should have been found");
    164   abort_unless(password != NULL, "returned NULL!");
    165   fail_unless(strncmp(password, "none", 4) == 0,
    166               "password should be 'none'");
    167   abort_unless(login != NULL, "returned NULL!");
    168   fail_unless(strncmp(login, "none", 4) == 0, "login should be 'none'");
    169 
    170   /*
    171    * Test for the second existing host in our netrc file
    172    * with login[0] != 0.
    173    */
    174   free(password);
    175   password = strdup("");
    176   abort_unless(password != NULL, "returned NULL!");
    177   result = Curl_parsenetrc("curl.example.com", &login, &password, filename);
    178   fail_unless(result == 0, "Host should have been found");
    179   abort_unless(password != NULL, "returned NULL!");
    180   fail_unless(strncmp(password, "none", 4) == 0,
    181               "password should be 'none'");
    182   abort_unless(login != NULL, "returned NULL!");
    183   fail_unless(strncmp(login, "none", 4) == 0, "login should be 'none'");
    184 
    185   /* TODO:
    186    * Test over the size limit password / login!
    187    * Test files with a bad format
    188    */
    189 UNITTEST_STOP
    190