From: Adam Stokes <astokes(a)fedoraproject.org>
- test utility library added in order to do some pattern matching on
our api results. this can of course be extended beyond regex use
during unittests
---
src/tests/CMakeLists.txt | 5 +++-
src/tests/mh_api_network.h | 45 ++++++++++++++++++++++++-------
src/tests/mh_test_utilities.c | 60 +++++++++++++++++++++++++++++++++++++++++
src/tests/mh_test_utilities.h | 28 +++++++++++++++++++
4 files changed, 127 insertions(+), 11 deletions(-)
create mode 100644 src/tests/mh_test_utilities.c
create mode 100644 src/tests/mh_test_utilities.h
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt
index 99ae968..3cbd8cc 100644
--- a/src/tests/CMakeLists.txt
+++ b/src/tests/CMakeLists.txt
@@ -4,7 +4,10 @@ if(CXXTEST_FOUND)
include_directories(${CXXTEST_INCLUDE_DIR})
ENABLE_TESTING()
CXXTEST_ADD_TEST(mh_api_network_unittest mh_api_network_unittest.cpp
${CMAKE_CURRENT_SOURCE_DIR}/mh_api_network.h)
- target_link_libraries(mh_api_network_unittest mnetwork)
+ CXXTEST_ADD_TEST(mh_api_host_unittest mh_api_host_unittest.cpp
${CMAKE_CURRENT_SOURCE_DIR}/mh_api_host.h)
+ add_library(mh_tester SHARED mh_test_utilities.c)
+ target_link_libraries(mh_api_network_unittest mnetwork mh_tester)
+ target_link_libraries(mh_api_host_unittest mhost pcre mh_tester)
message(STATUS "Testing Found.")
endif(CXXTEST_FOUND)
diff --git a/src/tests/mh_api_network.h b/src/tests/mh_api_network.h
index 30b9888..1b2b629 100644
--- a/src/tests/mh_api_network.h
+++ b/src/tests/mh_api_network.h
@@ -16,6 +16,7 @@ extern "C" {
#include <sigar.h>
#include <sigar_format.h>
#include <glib.h>
+#include "mh_test_utilities.h"
};
using namespace std;
@@ -27,30 +28,54 @@ class MhApiNetworkSuite : public CxxTest::TestSuite
GList *interface_list;
sigar_net_interface_config_t *ifconfig;
vector<char*> iface_names;
-
- void setUp (void)
+
+ void init()
{
interface_list = mh_network_get_interfaces();
for (plist = g_list_first(interface_list); plist;
- plist = g_list_next(plist)) {
+ plist = g_list_next(plist)) {
ifconfig = (sigar_net_interface_config_t *)plist->data;
iface_names.push_back(ifconfig->name);
}
+ g_list_free(interface_list);
}
-
- void tearDown (void)
- {
- g_list_free(interface_list);
- }
-
+
void testListNetworkDevices(void)
{
unsigned int i;
-
+
+ /* only initialize on first test in suite */
+ init();
+
for(i=0; i<iface_names.size(); i++) {
+ TS_TRACE(iface_names[i]);
TS_ASSERT(iface_names[i]);
}
}
+
+ void testGetNetworkIP(void)
+ {
+ unsigned int i;
+ const char *ip = NULL;
+ for(i=0; i<iface_names.size(); i++) {
+ ip = mh_network_get_ip_address(iface_names[i]);
+ TS_TRACE(ip);
+ TS_ASSERT((is_match("^\\d+\\.\\d+\\.\\d+\\.\\d+",
+ ip)) >= 0);
+ }
+ }
+
+ void testGetNetworkMAC(void)
+ {
+ unsigned int i;
+ const char *mac = NULL;
+ for(i=0; i<iface_names.size(); i++) {
+ mac = mh_network_get_mac_address(iface_names[i]);
+ TS_TRACE(mac);
+ TS_ASSERT((is_match("^([0-9a-fA-F]{2}([:-]|$)){6}$",
+ mac)) >= 0);
+ }
+ }
};
#endif
diff --git a/src/tests/mh_test_utilities.c b/src/tests/mh_test_utilities.c
new file mode 100644
index 0000000..1c3456d
--- /dev/null
+++ b/src/tests/mh_test_utilities.c
@@ -0,0 +1,60 @@
+/*
+ * mh_test_utilities.c: util functions for testing
+ *
+ * Copyright (C) 2011 Red Hat Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Author: Adam Stokes <astokes(a)fedoraproject.org>
+ */
+
+#include "mh_test_utilities.h"
+#include <pcre.h>
+#include <string.h>
+
+#define OVECCOUNT 30
+
+int
+is_match(const char *pattern, const char *subject)
+{
+ int erroroffset;
+ int ovector[OVECCOUNT];
+ const char *error;
+ pcre *re;
+ int rc;
+
+ re = pcre_compile(pattern,
+ 0,
+ &error,
+ &erroroffset,
+ NULL);
+ if (re == NULL) {
+ return PCRE_ERROR_NULL;
+ }
+
+ rc = pcre_exec(
+ re,
+ NULL,
+ subject,
+ strlen(subject),
+ 0,
+ 0,
+ ovector,
+ OVECCOUNT);
+
+ /* Since we only care about return code for now free the regex */
+ pcre_free(re);
+ return rc;
+}
diff --git a/src/tests/mh_test_utilities.h b/src/tests/mh_test_utilities.h
new file mode 100644
index 0000000..f75df05
--- /dev/null
+++ b/src/tests/mh_test_utilities.h
@@ -0,0 +1,28 @@
+/*
+ * mh_test_utilities.h: util functions for unittest
+ *
+ * Copyright (C) 2011 Red Hat Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Author: Adam Stokes <astokes(a)fedoraproject.org>
+ */
+
+#ifndef __MH_TEST_UTILITIES_H
+#define __MH_TEST_UTILITIES_H
+
+extern int is_match(const char *pattern, const char *subject);
+
+#endif /* __MH_TEST_UTILITIES */
--
1.7.4.4