Commit b5c3feda authored by Kamil Dudka's avatar Kamil Dudka Committed by Daniel Stenberg
Browse files

NTLM tests: boost coverage by forcing the hostname

A shared library tests/libtest/.libs/lihostname.so is preloaded in NTLM
test-cases to override the system implementation of gethostname().  It
makes it possible to test the NTLM authentication for exact match, and
this way test the implementation of MD4 and DES.

If LD_PRELOAD doesn't work, a debug build willl also workk as debug
builds are now made to prefer a specific environment variable and will
then return that content as host name instead of the actual one.

Kamil wrote the bulk of this, Daniel Stenberg polished it.
parent 01c309e1
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ CSOURCES = file.c timeval.c base64.c hostip.c progress.c formdata.c \
  strdup.c socks.c ssh.c nss.c qssl.c rawstr.c curl_addrinfo.c		\
  socks_gssapi.c socks_sspi.c curl_sspi.c slist.c nonblock.c		\
  curl_memrchr.c imap.c pop3.c smtp.c pingpong.c rtsp.c curl_threads.c	\
  warnless.c hmac.c polarssl.c curl_rtmp.c openldap.c
  warnless.c hmac.c polarssl.c curl_rtmp.c openldap.c curl_gethostname.c

HHEADERS = arpa_telnet.h netrc.h file.h timeval.h qssl.h hostip.h	\
  progress.h formdata.h cookie.h http.h sendf.h ftp.h url.h dict.h	\
@@ -34,5 +34,5 @@ HHEADERS = arpa_telnet.h netrc.h file.h timeval.h qssl.h hostip.h \
  tftp.h sockaddr.h splay.h strdup.h setup_once.h socks.h ssh.h nssg.h	\
  curl_base64.h rawstr.h curl_addrinfo.h curl_sspi.h slist.h nonblock.h	\
  curl_memrchr.h imap.h pop3.h smtp.h pingpong.h rtsp.h curl_threads.h	\
  warnless.h curl_hmac.h polarssl.h curl_rtmp.h
  warnless.h curl_hmac.h polarssl.h curl_rtmp.h curl_gethostname.h

lib/curl_gethostname.c

0 → 100644
+52 −0
Original line number Diff line number Diff line
/***************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
 * are also available at http://curl.haxx.se/docs/copyright.html.
 *
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 * copies of the Software, and permit persons to whom the Software is
 * furnished to do so, under the terms of the COPYING file.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ***************************************************************************/

#include "setup.h"
#include "curl_gethostname.h"

#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define GETHOSTNAME_ENV_VAR "CURL_GETHOSTNAME"

int Curl_gethostname(char *name, size_t namelen) {
#ifdef HAVE_GETHOSTNAME

#ifdef CURLDEBUG
  /* we check the environment variable only in case of debug build */
  const char *force_hostname = getenv(GETHOSTNAME_ENV_VAR);
  if(force_hostname) {
    strncpy(name, force_hostname, namelen);
    return 0;
  }
#endif
  /* no override requested */
  return gethostname(name, namelen);

#else
  /* no gethostname() available on system, we should always fail */
  (void) name;
  (void) namelen;
  return -1;
#endif
}

lib/curl_gethostname.h

0 → 100644
+34 −0
Original line number Diff line number Diff line
#ifndef HEADER_CURL_GETHOSTNAME_H
#define HEADER_CURL_GETHOSTNAME_H
/***************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
 * are also available at http://curl.haxx.se/docs/copyright.html.
 *
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 * copies of the Software, and permit persons to whom the Software is
 * furnished to do so, under the terms of the COPYING file.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ***************************************************************************/

#include "setup.h"

/* wrapper around gethostname(), which makes it possible to override the
 * returned value during testing.  It reads the value of CURL_GETHOSTNAME
 * environment variable when built with --enable-curldebug.  The function always
 * returns -1, if gethostname() is not available on system.
 */
int Curl_gethostname(char *name, size_t namelen);

#endif /* HEADER_CURL_GETHOSTNAME_H */
+2 −1
Original line number Diff line number Diff line
@@ -58,6 +58,7 @@
#include "curl_base64.h"
#include "http_ntlm.h"
#include "url.h"
#include "curl_gethostname.h"
#include "curl_memory.h"

#define _MPRINTF_REPLACE /* use our functions only */
@@ -994,7 +995,7 @@ CURLcode Curl_output_ntlm(struct connectdata *conn,
      user = userp;
    userlen = strlen(user);

    if(gethostname(host, HOSTNAME_MAX)) {
    if(Curl_gethostname(host, HOSTNAME_MAX)) {
      infof(conn->data, "gethostname() failed, continuing without!");
      hostlen = 0;
    }
+3 −4
Original line number Diff line number Diff line
@@ -91,6 +91,7 @@
#include "curl_base64.h"
#include "curl_md5.h"
#include "curl_hmac.h"
#include "curl_gethostname.h"

#define _MPRINTF_REPLACE /* use our functions only */
#include <curl/mprintf.h>
@@ -1110,11 +1111,9 @@ static CURLcode smtp_connect(struct connectdata *conn,
  pp->conn = conn;

  if(!*path) {
#ifdef HAVE_GETHOSTNAME
    if(!gethostname(localhost, sizeof localhost))
    if(!Curl_gethostname(localhost, sizeof localhost))
      path = localhost;
    else
#endif
      path = "localhost";
  }

Loading