[PATCH 2/2] unit: Add unit test for dnsproxy cache cleanup check
Unit test for checking that a nonexistent dnsproxy cache is not cleaned up. --- Makefile.am | 9 ++- unit/test-dnsproxy.c | 160 +++ 2 files changed, 167 insertions(+), 2 deletions(-) create mode 100644 unit/test-dnsproxy.c diff --git a/Makefile.am b/Makefile.am index a7f3ed3..b651d92 100644 --- a/Makefile.am +++ b/Makefile.am @@ -254,7 +254,8 @@ client_connmanctl_LDADD = gdbus/libgdbus-internal.la @DBUS_LIBS@ @GLIB_LIBS@ \ -lreadline -ldl endif -noinst_PROGRAMS += unit/test-pbkdf2-sha1 unit/test-prf-sha1 unit/test-ippool +noinst_PROGRAMS += unit/test-pbkdf2-sha1 unit/test-prf-sha1 unit/test-ippool \ + unit/test-dnsproxy unit_test_pbkdf2_sha1_SOURCES = unit/test-pbkdf2-sha1.c \ src/shared/sha1.h src/shared/sha1.c @@ -269,7 +270,11 @@ unit_test_ippool_SOURCES = src/log.c src/dbus.c src/error.c \ unit_test_ippool_LDADD = gdbus/libgdbus-internal.la \ @GLIB_LIBS@ @DBUS_LIBS@ -ldl -TESTS = unit/test-pbkdf2-sha1 unit/test-prf-sha1 unit/test-ippool +unit_test_dnsproxy_SOURCES = unit/test-dnsproxy.c src/log.c +unit_test_dnsproxy_LDADD = @GLIB_LIBS@ -ldl + +TESTS = unit/test-pbkdf2-sha1 unit/test-prf-sha1 unit/test-ippool \ + unit/test-dnsproxy if WISPR noinst_PROGRAMS += tools/wispr diff --git a/unit/test-dnsproxy.c b/unit/test-dnsproxy.c new file mode 100644 index 000..6435c2b --- /dev/null +++ b/unit/test-dnsproxy.c @@ -0,0 +1,160 @@ +/* + * + * Connection Manager + * + * Copyright (C) 2014 Jolla Ltd. All rights reserved. + * Contact: Hannu Mallat hannu.mal...@jollamobile.com + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +/* Include source file to access static variables easily */ +#include src/dnsproxy.c + +static GMainLoop *main_loop = NULL; + +/* Stub getaddrinfo() to return test data */ +int getaddrinfo(const char *node, const char *service, + const struct addrinfo *hints, + struct addrinfo **res) +{ + struct addrinfo *ai = g_new0(struct addrinfo, 1); + ai-ai_socktype = hints-ai_socktype; + ai-ai_protocol = hints-ai_protocol; + if (hints-ai_family == AF_INET6) { + struct sockaddr_in6 *in6 = g_new0(struct sockaddr_in6, 1); + in6-sin6_family = AF_INET6; + in6-sin6_port = htons(53); + memcpy(in6-sin6_addr.s6_addr, 0123456789abcdef, 16); + + ai-ai_family = AF_INET6; + ai-ai_addrlen = sizeof(struct sockaddr_in6); + ai-ai_addr = (struct sockaddr *)in6; + } else { + struct sockaddr_in *in = g_new0(struct sockaddr_in, 1); + in-sin_family = AF_INET; + in-sin_port = htons(53); + in-sin_addr.s_addr = htonl(0x12345678); + + ai-ai_family = AF_INET6; + ai-ai_addrlen = sizeof(struct sockaddr_in); + ai-ai_addr = (struct sockaddr *)in; + } + ai-ai_canonname = g_strdup(node); + ai-ai_next = NULL; + *res = ai; + + return 0; +} + +void freeaddrinfo(struct addrinfo *res) +{ + if (res) { + if (res-ai_addr) { + g_free(res-ai_addr); + } + if (res-ai_canonname) { + g_free(res-ai_canonname); + } + g_free(res); + } +} + +/* Stub socket() that always fails */ +int socket(int domain, int type, int protocol) +{ + return -1; +} + +int connman_inet_ifindex(const char *name) +{ + return -1; +} + +int __connman_service_get_index(struct connman_service *service) +{ + return -1; +} + +bool __connman_service_index_is_default(int index) +{ + return FALSE; +} + +bool __connman_service_index_is_split_routing(int index) +{ + return FALSE; +} + +int __connman_resolvfile_append(int index, const char *domain, const char *server) +{ + return -1; +} + +int __connman_resolvfile_remove(int index, const char *domain, const char *server) +{ + return -1; +} + +int connman_notifier_register(struct connman_notifier *notifier) +{ + return 0; +} + +void connman_notifier_unregister(struct connman_notifier *notifier) +{ +} + +static gboolean
[PATCH 2/2] unit: Add unit test for dnsproxy cache cleanup check
Unit test for checking that a nonexistent dnsproxy cache is not cleaned up. --- Makefile.am | 9 ++- unit/test-dnsproxy.c | 192 +++ 2 files changed, 199 insertions(+), 2 deletions(-) create mode 100644 unit/test-dnsproxy.c diff --git a/Makefile.am b/Makefile.am index a7f3ed3..e0b44b9 100644 --- a/Makefile.am +++ b/Makefile.am @@ -254,7 +254,8 @@ client_connmanctl_LDADD = gdbus/libgdbus-internal.la @DBUS_LIBS@ @GLIB_LIBS@ \ -lreadline -ldl endif -noinst_PROGRAMS += unit/test-pbkdf2-sha1 unit/test-prf-sha1 unit/test-ippool +noinst_PROGRAMS += unit/test-pbkdf2-sha1 unit/test-prf-sha1 unit/test-ippool \ + unit/test-dnsproxy unit_test_pbkdf2_sha1_SOURCES = unit/test-pbkdf2-sha1.c \ src/shared/sha1.h src/shared/sha1.c @@ -269,7 +270,11 @@ unit_test_ippool_SOURCES = src/log.c src/dbus.c src/error.c \ unit_test_ippool_LDADD = gdbus/libgdbus-internal.la \ @GLIB_LIBS@ @DBUS_LIBS@ -ldl -TESTS = unit/test-pbkdf2-sha1 unit/test-prf-sha1 unit/test-ippool +unit_test_dnsproxy_SOURCES = unit/test-dnsproxy.c src/log.c +unit_test_dnsproxy_LDADD = @GLIB_LIBS@ -lresolv -ldl + +TESTS = unit/test-pbkdf2-sha1 unit/test-prf-sha1 unit/test-ippool \ + unit/test-dnsproxy if WISPR noinst_PROGRAMS += tools/wispr diff --git a/unit/test-dnsproxy.c b/unit/test-dnsproxy.c new file mode 100644 index 000..ebe9587 --- /dev/null +++ b/unit/test-dnsproxy.c @@ -0,0 +1,192 @@ +/* + * + * Connection Manager + * + * Copyright (C) 2014 Jolla Ltd. All rights reserved. + * Contact: Hannu Mallat hannu.mal...@jollamobile.com + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +/* Include source file to access static variables easily */ +#include src/dnsproxy.c + +static GMainLoop *main_loop = NULL; + +/* Stub getaddrinfo() to return test data */ +int getaddrinfo(const char *node, const char *service, + const struct addrinfo *hints, + struct addrinfo **res) +{ + struct addrinfo *ai = g_new0(struct addrinfo, 1); + ai-ai_socktype = hints-ai_socktype; + ai-ai_protocol = hints-ai_protocol; + if (hints-ai_family == AF_INET6) { + struct sockaddr_in6 *in6 = g_new0(struct sockaddr_in6, 1); + in6-sin6_family = AF_INET6; + in6-sin6_port = htons(53); + memcpy(in6-sin6_addr.s6_addr, 0123456789abcdef, 16); + + ai-ai_family = AF_INET6; + ai-ai_addrlen = sizeof(struct sockaddr_in6); + ai-ai_addr = (struct sockaddr *)in6; + } else { + struct sockaddr_in *in = g_new0(struct sockaddr_in, 1); + in-sin_family = AF_INET; + in-sin_port = htons(53); + in-sin_addr.s_addr = htonl(0x12345678); + + ai-ai_family = AF_INET6; + ai-ai_addrlen = sizeof(struct sockaddr_in); + ai-ai_addr = (struct sockaddr *)in; + } + ai-ai_canonname = g_strdup(node); + ai-ai_next = NULL; + *res = ai; + + return 0; +} + +void freeaddrinfo(struct addrinfo *res) +{ + if (res) { + if (res-ai_addr) { + g_free(res-ai_addr); + } + if (res-ai_canonname) { + g_free(res-ai_canonname); + } + g_free(res); + } +} + +/* Stub socket() that always fails */ +int socket(int domain, int type, int protocol) +{ + return -1; +} + +GResolv *g_resolv_new(int index) +{ + return NULL; +} + +bool g_resolv_set_address_family(GResolv *resolv, int family) +{ + return FALSE; +} + +bool g_resolv_add_nameserver(GResolv *resolv, const char *address, + uint16_t port, unsigned long flags) +{ + return FALSE; +} + +guint g_resolv_lookup_hostname(GResolv *resolv, const char *hostname, + GResolvResultFunc func, gpointer user_data) +{ + return 0; +} + +char *connman_inet_ifname(int index) +{ + return NULL; +} + +int connman_inet_ifindex(const char *name) +{ + return -1; +} + +int __connman_inet_get_interface_address(int index, int family, void *address) +{ + return -1; +} + +int