From e3779987e89be3ca0213288ce4469426cd2a33a0 Mon Sep 17 00:00:00 2001 From: Toni Gundogdu Date: Mon, 6 Aug 2012 13:01:35 +0300 Subject: [PATCH] tests: Add regexp (capture, match), and fetch Add capture, match and fetch functions to the tests convenience library. --- tests/lib/Makefile.am | 2 +- tests/lib/fetch.c | 168 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/lib/re.c | 88 ++++++++++++++++++++++++++ tests/lib/tests.h | 6 ++ 4 files changed, 263 insertions(+), 1 deletion(-) create mode 100644 tests/lib/fetch.c create mode 100644 tests/lib/re.c diff --git a/tests/lib/Makefile.am b/tests/lib/Makefile.am index 4eaf33c..c2b595a 100644 --- a/tests/lib/Makefile.am +++ b/tests/lib/Makefile.am @@ -1,4 +1,4 @@ -src=env.c qerr.c qm_test.c +src=env.c fetch.c re.c qerr.c qm_test.c hdr=tests.h noinst_LTLIBRARIES=libconvenience_test.la diff --git a/tests/lib/fetch.c b/tests/lib/fetch.c new file mode 100644 index 0000000..1782a62 --- /dev/null +++ b/tests/lib/fetch.c @@ -0,0 +1,168 @@ +/* libquvi-scripts + * Copyright (C) 2012 Toni Gundogdu + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * 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 Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +#include "config.h" + +#include +#include +#include +#include + +struct temp_s +{ + gsize size; + gchar *p; +}; +typedef struct temp_s *temp_t; + +static gpointer temp_new() +{ + return (g_new0(struct temp_s, 1)); +} + +static void temp_free(temp_t t) +{ + if (t == NULL) + return; + + g_free(t->p); + t->p = NULL; + + g_free(t); + t = NULL; +} + +/* cURL write callback. */ +static gsize temp_wrcb(gpointer p, gsize sz, gsize nmemb, gpointer d) +{ + const gsize rsize = sz*nmemb; + gpointer *np; + temp_t t; + + t = (temp_t) d; + np = g_realloc(t->p, t->size+rsize+1); + + if (np != NULL) + { + t->p = (gchar*) np; + memcpy(&(t->p[t->size]), p, rsize); + t->size += rsize; + t->p[t->size] = '\0'; + } + return (rsize); +} + +static void set_opts(CURL *c, temp_t t, const gchar *url) +{ + typedef curl_write_callback cwc; + + curl_easy_setopt(c, CURLOPT_USERAGENT, "Mozilla/5.0"); + curl_easy_setopt(c, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(c, CURLOPT_MAXREDIRS, 5L); /* http://is.gd/kFsvE4 */ + curl_easy_setopt(c, CURLOPT_NOBODY, 0L); + + curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, (cwc) temp_wrcb); + curl_easy_setopt(c, CURLOPT_URL, url); + curl_easy_setopt(c, CURLOPT_WRITEDATA, t); + /* CURLOPT_ENCODING -> CURLOPT_ACCEPT_ENCODING 7.21.6+ */ + curl_easy_setopt(c, CURLOPT_ENCODING, ""); + + if (chk_env("TEST_VERBOSE", NULL) == TRUE) + curl_easy_setopt(c, CURLOPT_VERBOSE, 1L); +} + +static void reset_opts(CURL *c) +{ + curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, NULL); + curl_easy_setopt(c, CURLOPT_WRITEDATA, NULL); +} + +static gint _fetch(CURL *c) +{ + CURLcode curlcode; + glong respcode; + gint rc; + + curlcode = curl_easy_perform(c); + curl_easy_getinfo(c, CURLINFO_RESPONSE_CODE, &respcode); + + rc = 0; + + if (curlcode == CURLE_OK && respcode == 200) + ; + else + { + if (curlcode == CURLE_OK) + { +#define _EOK "server responded with code %03ld" + g_test_message(_EOK, respcode); +#undef _EOK + } + else + { + const gchar *s = curl_easy_strerror(curlcode); + const glong c = respcode; + const gint cc = curlcode; +#define _ENO "%s (HTTP/%03ld, cURL=0x%03x)" + g_test_message(_ENO, s, c, cc); +#undef _ENO + } + rc = 1; + } + return (rc); +} + +gchar *fetch(const gchar *url) +{ + gchar *r; + temp_t t; + CURL *c; + gint rc; + + curl_global_init(CURL_GLOBAL_ALL); + + c = curl_easy_init(); + if (c == NULL) + { + g_message("curl_easy_init returned NULL"); + return (NULL); + } + + t = temp_new(); + r = NULL; + + set_opts(c, t, url); + rc = _fetch(c); + reset_opts(c); + + if (rc == 0) + r = g_strdup(t->p); + + temp_free(t); + t = NULL; + + curl_easy_cleanup(c); + c = NULL; + + curl_global_cleanup(); + + return (r); +} + +/* vim: set ts=2 sw=2 tw=72 expandtab: */ diff --git a/tests/lib/re.c b/tests/lib/re.c new file mode 100644 index 0000000..89025a3 --- /dev/null +++ b/tests/lib/re.c @@ -0,0 +1,88 @@ +/* libquvi-scripts + * Copyright (C) 2012 Toni Gundogdu + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * 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 Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +#include "config.h" + +#include + +#define _W "%s: %s" + +gboolean match(const gchar *s, const gchar *p) +{ + GMatchInfo *m; + GError *err; + GRegex *re; + gboolean r; + + err = NULL; + re = g_regex_new(p, G_REGEX_MULTILINE, 0, &err); + + if (err != NULL) + { + g_warning(_W, __func__, err->message); + g_error_free(err); + return (FALSE); + } + + m = NULL; + r = g_regex_match(re, s, 0, &m); + + g_match_info_free(m); + m = NULL; + + g_regex_unref(re); + re = NULL; + + return (r); +} + +gchar *capture(const gchar *s, const gchar *p) +{ + GMatchInfo *m; + GError *err; + GRegex *re; + gchar *r; + + err = NULL; + + re = g_regex_new(p, G_REGEX_MULTILINE, 0, &err); + if (err != NULL) + { + g_warning(_W, __func__, err->message); + g_error_free(err); + err = NULL; + return (NULL); + } + + m = NULL; + r = NULL; + + if (g_regex_match(re, s, 0, &m) == TRUE) + r = g_match_info_fetch(m, 1); + + g_match_info_free(m); + m = NULL; + + g_regex_unref(re); + re = NULL; + + return (r); +} + +/* vim: set ts=2 sw=2 tw=72 expandtab: */ diff --git a/tests/lib/tests.h b/tests/lib/tests.h index be81fcb..9b88fde 100644 --- a/tests/lib/tests.h +++ b/tests/lib/tests.h @@ -115,6 +115,12 @@ typedef struct qm_test_opts_s *qm_test_opts_t; void qm_test(const gchar*, const gchar*, const qm_test_exact_t, const qm_test_opts_t); +/* Other. */ + +gboolean match(const gchar*, const gchar*); +gchar *capture(const gchar*, const gchar*); +gchar *fetch(const gchar*); + #endif /* tests_h */ /* vim: set ts=2 sw=2 tw=72 expandtab: */ -- 2.11.4.GIT