Browse Source

* Implemented libcurl mock interface

master
bergmann 5 years ago
parent
commit
4f0d5d5e2b
5 changed files with 460 additions and 8 deletions
  1. +4
    -1
      include/cppcurl/easy.inl
  2. +14
    -6
      src/CMakeLists.txt
  3. +17
    -1
      test/CMakeLists.txt
  4. +242
    -0
      test/helper/libcurl_mock.cpp
  5. +183
    -0
      test/helper/libcurl_mock.h

+ 4
- 1
include/cppcurl/easy.inl View File

@@ -156,7 +156,10 @@ namespace cppcurl
{ return _handle.get(); }

void easy::reset()
{ curl_easy_reset(_handle.get()); }
{
_header.reset();
curl_easy_reset(_handle.get());
}

void easy::perform()
{


+ 14
- 6
src/CMakeLists.txt View File

@@ -16,21 +16,26 @@ Set ( CPPCURL_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/..
Set ( CPPCURL_GENERATED_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated/include )
Configure_File ( ${CMAKE_CURRENT_SOURCE_DIR}/../cmake/config.h.in
${CPPCURL_GENERATED_INCLUDE_DIR}/cppcurl/config.h )
Add_Library ( cppcurl INTERFACE )
Target_Include_Directories ( cppcurl
Add_Library ( cppcurl-mock INTERFACE )
Get_Target_Property ( CURL_INCLUDE CURL::libcurl INTERFACE_INCLUDE_DIRECTORIES )
Target_Include_Directories ( cppcurl-mock
INTERFACE
${CURL_INCLUDE}
$<BUILD_INTERFACE:${CPPCURL_INCLUDE_DIR}>
$<BUILD_INTERFACE:${CPPCURL_GENERATED_INCLUDE_DIR}>
$<INSTALL_INTERFACE:${CPPCURL_INSTALL_DIR_INCLUDE}> )
Target_Link_Libraries ( cppcurl
INTERFACE
CURL::libcurl )
If ( CPPCURL_HAS_CPPCORE )
Target_Link_Libraries ( cppcurl
Target_Link_Libraries ( cppcurl-mock
INTERFACE
cppcore::cppcore )
EndIf ( )

Add_Library ( cppcurl INTERFACE )
Target_Link_Libraries ( cppcurl
INTERFACE
CURL::libcurl
cppcurl-mock )

# Install #########################################################################################

Set ( CPPCURL_HAS_EXPORT False PARENT_SCOPE )
@@ -44,6 +49,9 @@ If ( CPPCURL_INSTALL_HEADER )
DESTINATION ${CPPCURL_INSTALL_DIR_INCLUDE} )
Install ( DIRECTORY ${CPPCURL_GENERATED_INCLUDE_DIR}/cppcurl
DESTINATION ${CPPCURL_INSTALL_DIR_INCLUDE} )
Install ( TARGETS cppcurl-mock
EXPORT cppcurl
DESTINATION ${CPPCURL_INSTALL_DIR_INCLUDE} )
Install ( TARGETS cppcurl
EXPORT cppcurl
DESTINATION ${CPPCURL_INSTALL_DIR_INCLUDE} )


+ 17
- 1
test/CMakeLists.txt View File

@@ -4,6 +4,22 @@ Include ( cotire OPTIONAL RESULT_VARIABLE HAS_COTIRE
Include ( pedantic OPTIONAL RESULT_VARIABLE HAS_PEDANTIC )
Include ( cmake_tests OPTIONAL RESULT_VARIABLE HAS_CMAKE_TESTS )

# Test Helper #####################################################################################

File ( GLOB_RECURSE SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/helper/*.cpp )
Add_Library ( cppcurl-test-helper STATIC ${SOURCE_FILES} )
Target_Include_Directories ( cppcurl-test-helper
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/helper>
$<INSTALL_INTERFACE:${CPPCURL_INSTALL_DIR_INCLUDE}> )
Target_Include_Directories ( cppcurl-test-helper
SYSTEM PUBLIC
${MICROHTTPD_INCLUDE_DIRS} )
Target_Link_Libraries ( cppcurl-test-helper
PUBLIC
cppcurl-mock
GMock::GMock )

# Test ############################################################################################

Find_Package ( GTest )
@@ -17,7 +33,7 @@ File ( GLOB_RECURSE CPPCURL_TEST_INLINE_FILES
${CMAKE_CURRENT_SOURCE_DIR}/*.inl )
File ( GLOB_RECURSE CPPCURL_TEST_SOURCE_FILES
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp )
${CMAKE_CURRENT_SOURCE_DIR}/cppcurl/*.cpp )

ForEach ( FILE IN LISTS CPPCURL_TEST_SOURCE_FILES )
# add test


+ 242
- 0
test/helper/libcurl_mock.cpp View File

@@ -0,0 +1,242 @@
#include "libcurl_mock.h"

#pragma GCC diagnostic ignored "-Wvarargs"

libcurl_mock * libcurl_mock::instance = nullptr;

extern "C"
{

CURLcode curl_global_init ( long flags )
{
return libcurl_mock::instance
? libcurl_mock::instance->curl_global_init(flags)
: CURLE_OK;
}

void curl_global_cleanup ( void )
{
if (libcurl_mock::instance)
libcurl_mock::instance->curl_global_cleanup();
}

struct curl_slist * curl_slist_append ( struct curl_slist * list,
const char * elem)
{
return libcurl_mock::instance
? libcurl_mock::instance->curl_slist_append(list, elem)
: nullptr;
}

void curl_slist_free_all ( struct curl_slist * list )
{
if (libcurl_mock::instance)
libcurl_mock::instance->curl_slist_free_all(list);
}

CURL * curl_easy_init(void)
{
return libcurl_mock::instance
? libcurl_mock::instance->curl_easy_init()
: nullptr;
}

void curl_easy_cleanup(CURL * curl)
{
if (libcurl_mock::instance)
libcurl_mock::instance->curl_easy_cleanup(curl);
}

CURLcode curl_easy_setopt(CURL * curl,
CURLoption option,
...)
{
if (!libcurl_mock::instance)
return CURLE_OK;

va_list args;
va_start(args, option);

if (option >= CURLOPTTYPE_OFF_T)
return libcurl_mock::instance->curl_easy_setopt_off(curl, option, va_arg(args, curl_off_t));

else if (option >= CURLOPTTYPE_FUNCTIONPOINT)
return libcurl_mock::instance->curl_easy_setopt_ptr(curl, option, va_arg(args, void *));

else if (option >= CURLOPTTYPE_STRINGPOINT)
return libcurl_mock::instance->curl_easy_setopt_ptr(curl, option, va_arg(args, void *));

else if (option >= CURLOPTTYPE_OBJECTPOINT)
return libcurl_mock::instance->curl_easy_setopt_ptr(curl, option, va_arg(args, void *));

else if (option >= CURLOPTTYPE_LONG)
return libcurl_mock::instance->curl_easy_setopt_long(curl, option, va_arg(args, long));

return CURLE_OK;
}

CURLcode curl_easy_perform(CURL * curl)
{
return libcurl_mock::instance
? libcurl_mock::instance->curl_easy_perform(curl)
: CURLE_OK;
}

CURLcode curl_easy_getinfo(CURL * curl,
CURLINFO info,
...)
{
if (!libcurl_mock::instance)
return CURLE_OK;

va_list args;
va_start(args, info);

if (info >= CURLINFO_OFF_T)
return libcurl_mock::instance->curl_easy_getinfo_off(curl, info, va_arg(args, curl_off_t *));

else if (info >= CURLINFO_SOCKET)
return libcurl_mock::instance->curl_easy_getinfo_socket(curl, info, va_arg(args, curl_socket_t *));

else if (info >= CURLINFO_PTR)
return libcurl_mock::instance->curl_easy_getinfo_ptr(curl, info, va_arg(args, void *));

else if (info >= CURLINFO_SLIST)
return libcurl_mock::instance->curl_easy_getinfo_ptr(curl, info, va_arg(args, void *));

else if (info >= CURLINFO_DOUBLE)
return libcurl_mock::instance->curl_easy_getinfo_double(curl, info, va_arg(args, double *));

else if (info >= CURLINFO_LONG)
return libcurl_mock::instance->curl_easy_getinfo_long(curl, info, va_arg(args, long *));

else if (info >= CURLINFO_STRING)
return libcurl_mock::instance->curl_easy_getinfo_str(curl, info, va_arg(args, char **));

return CURLE_OK;
}

CURL * curl_easy_duphandle(CURL *curl)
{
return libcurl_mock::instance
? libcurl_mock::instance->curl_easy_duphandle(curl)
: nullptr;
}

void curl_easy_reset(CURL *curl)
{
if (libcurl_mock::instance)
libcurl_mock::instance->curl_easy_reset(curl);
}

CURLcode curl_easy_recv(CURL * curl,
void * buffer,
size_t buflen,
size_t * n)
{
return libcurl_mock::instance
? libcurl_mock::instance->curl_easy_recv(curl, buffer, buflen, n)
: CURLE_OK;
}

CURLcode curl_easy_send(CURL * curl,
const void * buffer,
size_t buflen,
size_t * n)
{
return libcurl_mock::instance
? libcurl_mock::instance->curl_easy_send(curl, buffer, buflen, n)
: CURLE_OK;
}

const char * curl_easy_strerror ( CURLcode code )
{
return libcurl_mock::instance
? libcurl_mock::instance->curl_easy_strerror(code)
: nullptr;
}

const char * curl_multi_strerror ( CURLMcode code )
{
return libcurl_mock::instance
? libcurl_mock::instance->curl_multi_strerror(code)
: nullptr;
}

CURLM * curl_multi_init ( void )
{
return libcurl_mock::instance
? libcurl_mock::instance->curl_multi_init()
: nullptr;
}

CURLMcode curl_multi_cleanup ( CURLM *multi_handle )
{
return libcurl_mock::instance
? libcurl_mock::instance->curl_multi_cleanup(multi_handle)
: CURLM_OK;
}

CURLMcode curl_multi_add_handle ( CURLM * multi_handle,
CURL * curl_handle )
{
return libcurl_mock::instance
? libcurl_mock::instance->curl_multi_add_handle(multi_handle, curl_handle)
: CURLM_OK;
}

CURLMcode curl_multi_remove_handle ( CURLM * multi_handle,
CURL * curl_handle )
{
return libcurl_mock::instance
? libcurl_mock::instance->curl_multi_remove_handle(multi_handle, curl_handle)
: CURLM_OK;
}

CURLMcode curl_multi_fdset ( CURLM * multi_handle,
fd_set * read_fd_set,
fd_set * write_fd_set,
fd_set * exc_fd_set,
int * max_fd)
{
return libcurl_mock::instance
? libcurl_mock::instance->curl_multi_fdset(multi_handle, read_fd_set, write_fd_set, exc_fd_set, max_fd)
: CURLM_OK;
}

CURLMcode curl_multi_wait ( CURLM * multi_handle,
struct curl_waitfd extra_fds[],
unsigned int extra_nfds,
int timeout_ms,
int * ret)
{
return libcurl_mock::instance
? libcurl_mock::instance->curl_multi_wait(multi_handle, extra_fds, extra_nfds, timeout_ms, ret)
: CURLM_OK;
}

CURLMcode curl_multi_perform ( CURLM * multi_handle,
int * running_handles)
{
return libcurl_mock::instance
? libcurl_mock::instance->curl_multi_perform(multi_handle, running_handles)
: CURLM_OK;
}

CURLMsg * curl_multi_info_read ( CURLM * multi_handle,
int * msgs_in_queue )
{
return libcurl_mock::instance
? libcurl_mock::instance->curl_multi_info_read(multi_handle, msgs_in_queue)
: nullptr;
}

CURLMcode curl_multi_timeout ( CURLM * multi_handle,
long * milliseconds )
{
return libcurl_mock::instance
? libcurl_mock::instance->curl_multi_timeout(multi_handle, milliseconds)
: CURLM_OK;
}

}

+ 183
- 0
test/helper/libcurl_mock.h View File

@@ -0,0 +1,183 @@
#pragma once

#include <curl/curl.h>
#include <gmock/gmock.h>

struct libcurl_mock
{
public:
MOCK_METHOD1(
curl_global_init,
CURLcode ( long flags ));

MOCK_METHOD0(
curl_global_cleanup,
void ( void ) );

MOCK_METHOD2 (
curl_slist_append,
struct curl_slist * ( struct curl_slist *,
const char *));

MOCK_METHOD1(curl_slist_free_all,
void ( struct curl_slist * list ));

MOCK_METHOD0(
curl_easy_init,
CURL * ( void ));

MOCK_METHOD1(
curl_easy_cleanup,
void ( CURL * curl ));

MOCK_METHOD1(
curl_easy_perform,
CURLcode ( CURL * curl ));

MOCK_METHOD3(
curl_easy_setopt_off,
CURLcode( CURL * curl,
CURLoption option,
curl_off_t value ));

MOCK_METHOD3(
curl_easy_setopt_ptr,
CURLcode ( CURL * curl,
CURLoption option,
void * value ));

MOCK_METHOD3(
curl_easy_setopt_long,
CURLcode ( CURL * curl,
CURLoption option,
long value ));

MOCK_METHOD3(
curl_easy_getinfo_off,
CURLcode ( CURL * curl,
CURLINFO info,
curl_off_t * value));

MOCK_METHOD3(
curl_easy_getinfo_socket,
CURLcode ( CURL * curl,
CURLINFO info,
curl_socket_t * value));

MOCK_METHOD3(
curl_easy_getinfo_ptr,
CURLcode ( CURL * curl,
CURLINFO info,
void * value));

MOCK_METHOD3(
curl_easy_getinfo_double,
CURLcode ( CURL * curl,
CURLINFO info,
double * value));

MOCK_METHOD3(
curl_easy_getinfo_long,
CURLcode ( CURL * curl,
CURLINFO info,
long * value));

MOCK_METHOD3(
curl_easy_getinfo_str,
CURLcode ( CURL * curl,
CURLINFO info,
char ** value));

MOCK_METHOD1(
curl_easy_duphandle,
CURL * ( CURL *curl ));

MOCK_METHOD1(
curl_easy_reset,
void ( CURL *curl ));

MOCK_METHOD4(
curl_easy_recv,
CURLcode ( CURL * curl,
void * buffer,
size_t buflen,
size_t * n));

MOCK_METHOD4(
curl_easy_send,
CURLcode ( CURL * curl,
const void * buffer,
size_t buflen,
size_t * n));
MOCK_METHOD1(
curl_easy_strerror,
const char * ( CURLcode mode ));

MOCK_METHOD1(
curl_multi_strerror,
const char * ( CURLMcode mode ));

MOCK_METHOD0(
curl_multi_init,
CURLM * ( void ));

MOCK_METHOD1(
curl_multi_cleanup,
CURLMcode ( CURLM *multi_handle ));

MOCK_METHOD2(
curl_multi_add_handle,
CURLMcode ( CURLM * multi_handle,
CURL * curl_handle ));

MOCK_METHOD2(
curl_multi_remove_handle,
CURLMcode ( CURLM * multi_handle,
CURL * curl_handle ));

MOCK_METHOD5(
curl_multi_fdset,
CURLMcode ( CURLM * multi_handle,
fd_set * read_fd_set,
fd_set * write_fd_set,
fd_set * exc_fd_set,
int * max_fd));

MOCK_METHOD5(
curl_multi_wait,
CURLMcode ( CURLM * multi_handle,
struct curl_waitfd extra_fds[],
unsigned int extra_nfds,
int timeout_ms,
int * ret));

MOCK_METHOD2(
curl_multi_perform,
CURLMcode ( CURLM * multi_handle,
int * running_handles));

MOCK_METHOD2(
curl_multi_info_read,
CURLMsg * ( CURLM *multi_handle,
int *msgs_in_queue));

MOCK_METHOD2(
curl_multi_timeout,
CURLMcode ( CURLM * multi_handle,
long * milliseconds ));

public:
inline libcurl_mock()
{
instance = this;
}

inline ~libcurl_mock()
{
if (instance == this)
instance = nullptr;
}

public:
static libcurl_mock * instance;
};

Loading…
Cancel
Save