113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci#include "curlcheck.h"
2513498266Sopenharmony_ci#include "netrc.h"
2613498266Sopenharmony_ci#include "memdebug.h" /* LAST include file */
2713498266Sopenharmony_ci
2813498266Sopenharmony_ci#ifndef CURL_DISABLE_NETRC
2913498266Sopenharmony_ci
3013498266Sopenharmony_cistatic char *login;
3113498266Sopenharmony_cistatic char *password;
3213498266Sopenharmony_ci
3313498266Sopenharmony_cistatic CURLcode unit_setup(void)
3413498266Sopenharmony_ci{
3513498266Sopenharmony_ci  password = strdup("");
3613498266Sopenharmony_ci  login = strdup("");
3713498266Sopenharmony_ci  if(!password || !login) {
3813498266Sopenharmony_ci    Curl_safefree(password);
3913498266Sopenharmony_ci    Curl_safefree(login);
4013498266Sopenharmony_ci    return CURLE_OUT_OF_MEMORY;
4113498266Sopenharmony_ci  }
4213498266Sopenharmony_ci  return CURLE_OK;
4313498266Sopenharmony_ci}
4413498266Sopenharmony_ci
4513498266Sopenharmony_cistatic void unit_stop(void)
4613498266Sopenharmony_ci{
4713498266Sopenharmony_ci  Curl_safefree(password);
4813498266Sopenharmony_ci  Curl_safefree(login);
4913498266Sopenharmony_ci}
5013498266Sopenharmony_ci
5113498266Sopenharmony_ciUNITTEST_START
5213498266Sopenharmony_ci  int result;
5313498266Sopenharmony_ci
5413498266Sopenharmony_ci  /*
5513498266Sopenharmony_ci   * Test a non existent host in our netrc file.
5613498266Sopenharmony_ci   */
5713498266Sopenharmony_ci  result = Curl_parsenetrc("test.example.com", &login, &password, arg);
5813498266Sopenharmony_ci  fail_unless(result == 1, "Host not found should return 1");
5913498266Sopenharmony_ci  abort_unless(password != NULL, "returned NULL!");
6013498266Sopenharmony_ci  fail_unless(password[0] == 0, "password should not have been changed");
6113498266Sopenharmony_ci  abort_unless(login != NULL, "returned NULL!");
6213498266Sopenharmony_ci  fail_unless(login[0] == 0, "login should not have been changed");
6313498266Sopenharmony_ci
6413498266Sopenharmony_ci  /*
6513498266Sopenharmony_ci   * Test a non existent login in our netrc file.
6613498266Sopenharmony_ci   */
6713498266Sopenharmony_ci  free(login);
6813498266Sopenharmony_ci  login = strdup("me");
6913498266Sopenharmony_ci  abort_unless(login != NULL, "returned NULL!");
7013498266Sopenharmony_ci  result = Curl_parsenetrc("example.com", &login, &password, arg);
7113498266Sopenharmony_ci  fail_unless(result == 0, "Host should have been found");
7213498266Sopenharmony_ci  abort_unless(password != NULL, "returned NULL!");
7313498266Sopenharmony_ci  fail_unless(password[0] == 0, "password should not have been changed");
7413498266Sopenharmony_ci  abort_unless(login != NULL, "returned NULL!");
7513498266Sopenharmony_ci  fail_unless(strncmp(login, "me", 2) == 0,
7613498266Sopenharmony_ci              "login should not have been changed");
7713498266Sopenharmony_ci
7813498266Sopenharmony_ci  /*
7913498266Sopenharmony_ci   * Test a non existent login and host in our netrc file.
8013498266Sopenharmony_ci   */
8113498266Sopenharmony_ci  free(login);
8213498266Sopenharmony_ci  login = strdup("me");
8313498266Sopenharmony_ci  abort_unless(login != NULL, "returned NULL!");
8413498266Sopenharmony_ci  result = Curl_parsenetrc("test.example.com", &login, &password, arg);
8513498266Sopenharmony_ci  fail_unless(result == 1, "Host not found should return 1");
8613498266Sopenharmony_ci  abort_unless(password != NULL, "returned NULL!");
8713498266Sopenharmony_ci  fail_unless(password[0] == 0, "password should not have been changed");
8813498266Sopenharmony_ci  abort_unless(login != NULL, "returned NULL!");
8913498266Sopenharmony_ci  fail_unless(strncmp(login, "me", 2) == 0,
9013498266Sopenharmony_ci              "login should not have been changed");
9113498266Sopenharmony_ci
9213498266Sopenharmony_ci  /*
9313498266Sopenharmony_ci   * Test a non existent login (substring of an existing one) in our
9413498266Sopenharmony_ci   * netrc file.
9513498266Sopenharmony_ci   */
9613498266Sopenharmony_ci  free(login);
9713498266Sopenharmony_ci  login = strdup("admi");
9813498266Sopenharmony_ci  abort_unless(login != NULL, "returned NULL!");
9913498266Sopenharmony_ci  result = Curl_parsenetrc("example.com", &login, &password, arg);
10013498266Sopenharmony_ci  fail_unless(result == 0, "Host should have been found");
10113498266Sopenharmony_ci  abort_unless(password != NULL, "returned NULL!");
10213498266Sopenharmony_ci  fail_unless(password[0] == 0, "password should not have been changed");
10313498266Sopenharmony_ci  abort_unless(login != NULL, "returned NULL!");
10413498266Sopenharmony_ci  fail_unless(strncmp(login, "admi", 4) == 0,
10513498266Sopenharmony_ci              "login should not have been changed");
10613498266Sopenharmony_ci
10713498266Sopenharmony_ci  /*
10813498266Sopenharmony_ci   * Test a non existent login (superstring of an existing one)
10913498266Sopenharmony_ci   * in our netrc file.
11013498266Sopenharmony_ci   */
11113498266Sopenharmony_ci  free(login);
11213498266Sopenharmony_ci  login = strdup("adminn");
11313498266Sopenharmony_ci  abort_unless(login != NULL, "returned NULL!");
11413498266Sopenharmony_ci  result = Curl_parsenetrc("example.com", &login, &password, arg);
11513498266Sopenharmony_ci  fail_unless(result == 0, "Host should have been found");
11613498266Sopenharmony_ci  abort_unless(password != NULL, "returned NULL!");
11713498266Sopenharmony_ci  fail_unless(password[0] == 0, "password should not have been changed");
11813498266Sopenharmony_ci  abort_unless(login != NULL, "returned NULL!");
11913498266Sopenharmony_ci  fail_unless(strncmp(login, "adminn", 6) == 0,
12013498266Sopenharmony_ci              "login should not have been changed");
12113498266Sopenharmony_ci
12213498266Sopenharmony_ci  /*
12313498266Sopenharmony_ci   * Test for the first existing host in our netrc file
12413498266Sopenharmony_ci   * with login[0] = 0.
12513498266Sopenharmony_ci   */
12613498266Sopenharmony_ci  free(login);
12713498266Sopenharmony_ci  login = strdup("");
12813498266Sopenharmony_ci  abort_unless(login != NULL, "returned NULL!");
12913498266Sopenharmony_ci  result = Curl_parsenetrc("example.com", &login, &password, arg);
13013498266Sopenharmony_ci  fail_unless(result == 0, "Host should have been found");
13113498266Sopenharmony_ci  abort_unless(password != NULL, "returned NULL!");
13213498266Sopenharmony_ci  fail_unless(strncmp(password, "passwd", 6) == 0,
13313498266Sopenharmony_ci              "password should be 'passwd'");
13413498266Sopenharmony_ci  abort_unless(login != NULL, "returned NULL!");
13513498266Sopenharmony_ci  fail_unless(strncmp(login, "admin", 5) == 0, "login should be 'admin'");
13613498266Sopenharmony_ci
13713498266Sopenharmony_ci  /*
13813498266Sopenharmony_ci   * Test for the first existing host in our netrc file
13913498266Sopenharmony_ci   * with login[0] != 0.
14013498266Sopenharmony_ci   */
14113498266Sopenharmony_ci  free(password);
14213498266Sopenharmony_ci  password = strdup("");
14313498266Sopenharmony_ci  abort_unless(password != NULL, "returned NULL!");
14413498266Sopenharmony_ci  result = Curl_parsenetrc("example.com", &login, &password, arg);
14513498266Sopenharmony_ci  fail_unless(result == 0, "Host should have been found");
14613498266Sopenharmony_ci  abort_unless(password != NULL, "returned NULL!");
14713498266Sopenharmony_ci  fail_unless(strncmp(password, "passwd", 6) == 0,
14813498266Sopenharmony_ci              "password should be 'passwd'");
14913498266Sopenharmony_ci  abort_unless(login != NULL, "returned NULL!");
15013498266Sopenharmony_ci  fail_unless(strncmp(login, "admin", 5) == 0, "login should be 'admin'");
15113498266Sopenharmony_ci
15213498266Sopenharmony_ci  /*
15313498266Sopenharmony_ci   * Test for the second existing host in our netrc file
15413498266Sopenharmony_ci   * with login[0] = 0.
15513498266Sopenharmony_ci   */
15613498266Sopenharmony_ci  free(password);
15713498266Sopenharmony_ci  password = strdup("");
15813498266Sopenharmony_ci  abort_unless(password != NULL, "returned NULL!");
15913498266Sopenharmony_ci  free(login);
16013498266Sopenharmony_ci  login = strdup("");
16113498266Sopenharmony_ci  abort_unless(login != NULL, "returned NULL!");
16213498266Sopenharmony_ci  result = Curl_parsenetrc("curl.example.com", &login, &password, arg);
16313498266Sopenharmony_ci  fail_unless(result == 0, "Host should have been found");
16413498266Sopenharmony_ci  abort_unless(password != NULL, "returned NULL!");
16513498266Sopenharmony_ci  fail_unless(strncmp(password, "none", 4) == 0,
16613498266Sopenharmony_ci              "password should be 'none'");
16713498266Sopenharmony_ci  abort_unless(login != NULL, "returned NULL!");
16813498266Sopenharmony_ci  fail_unless(strncmp(login, "none", 4) == 0, "login should be 'none'");
16913498266Sopenharmony_ci
17013498266Sopenharmony_ci  /*
17113498266Sopenharmony_ci   * Test for the second existing host in our netrc file
17213498266Sopenharmony_ci   * with login[0] != 0.
17313498266Sopenharmony_ci   */
17413498266Sopenharmony_ci  free(password);
17513498266Sopenharmony_ci  password = strdup("");
17613498266Sopenharmony_ci  abort_unless(password != NULL, "returned NULL!");
17713498266Sopenharmony_ci  result = Curl_parsenetrc("curl.example.com", &login, &password, arg);
17813498266Sopenharmony_ci  fail_unless(result == 0, "Host should have been found");
17913498266Sopenharmony_ci  abort_unless(password != NULL, "returned NULL!");
18013498266Sopenharmony_ci  fail_unless(strncmp(password, "none", 4) == 0,
18113498266Sopenharmony_ci              "password should be 'none'");
18213498266Sopenharmony_ci  abort_unless(login != NULL, "returned NULL!");
18313498266Sopenharmony_ci  fail_unless(strncmp(login, "none", 4) == 0, "login should be 'none'");
18413498266Sopenharmony_ci
18513498266Sopenharmony_ciUNITTEST_STOP
18613498266Sopenharmony_ci
18713498266Sopenharmony_ci#else
18813498266Sopenharmony_cistatic CURLcode unit_setup(void)
18913498266Sopenharmony_ci{
19013498266Sopenharmony_ci  return CURLE_OK;
19113498266Sopenharmony_ci}
19213498266Sopenharmony_cistatic void unit_stop(void)
19313498266Sopenharmony_ci{
19413498266Sopenharmony_ci}
19513498266Sopenharmony_ciUNITTEST_START
19613498266Sopenharmony_ciUNITTEST_STOP
19713498266Sopenharmony_ci
19813498266Sopenharmony_ci#endif
199