}
END_TEST
+START_TEST(getiflist_no_source)
+{
+ char *ifacelist;
+
+ ck_assert_int_eq(remove_directory(TESTDIR), 1);
+ ck_assert_int_eq(getiflist(&ifacelist), 0);
+ ck_assert_str_eq(ifacelist, "");
+
+ free(ifacelist);
+}
+END_TEST
+
+START_TEST(getiflist_one_interface)
+{
+ char *ifacelist;
+
+ ck_assert_int_eq(remove_directory(TESTDIR), 1);
+ fake_proc_net_dev("w", "ethunusual", 0, 0, 0, 0);
+
+ ck_assert_int_eq(getiflist(&ifacelist), 1);
+ ck_assert_str_eq(ifacelist, "lo ethunusual ");
+
+ free(ifacelist);
+}
+END_TEST
+
+START_TEST(getiflist_multiple_interfaces)
+{
+ char *ifacelist;
+
+ ck_assert_int_eq(remove_directory(TESTDIR), 1);
+ fake_proc_net_dev("w", "random", 0, 0, 0, 0);
+ fake_proc_net_dev("a", "interfaces", 0, 0, 0, 0);
+ fake_proc_net_dev("a", "having", 0, 0, 0, 0);
+ fake_proc_net_dev("a", "fun", 0, 0, 0, 0);
+ fake_proc_net_dev("a", "i", 0, 0, 0, 0);
+
+ ck_assert_int_eq(getiflist(&ifacelist), 1);
+ ck_assert_str_eq(ifacelist, "lo random interfaces having fun i ");
+
+ free(ifacelist);
+}
+END_TEST
+
+START_TEST(readproc_no_file)
+{
+ ck_assert_int_eq(remove_directory(TESTDIR), 1);
+ ck_assert_int_eq(readproc("ethunusual"), 0);
+}
+END_TEST
+
+START_TEST(readproc_not_found)
+{
+ ck_assert_int_eq(remove_directory(TESTDIR), 1);
+ fake_proc_net_dev("w", "ethwrong", 10, 20, 30, 40);
+
+ ck_assert_int_eq(readproc("ethunusual"), 0);
+}
+END_TEST
+
+START_TEST(readproc_success)
+{
+ ck_assert_int_eq(remove_directory(TESTDIR), 1);
+ fake_proc_net_dev("w", "ethwrong", 10, 20, 30, 40);
+ fake_proc_net_dev("a", "ethunusual", 1, 2, 3, 4);
+
+ ck_assert_int_eq(readproc("ethunusual"), 1);
+ ck_assert_str_eq(ifinfo.name, "ethunusual");
+ ck_assert_int_eq(ifinfo.filled, 1);
+ ck_assert_int_eq(ifinfo.rx, 1);
+ ck_assert_int_eq(ifinfo.tx, 2);
+ ck_assert_int_eq(ifinfo.rxp, 3);
+ ck_assert_int_eq(ifinfo.txp, 4);
+}
+END_TEST
+
+START_TEST(getifinfo_not_found)
+{
+ suppress_output();
+ ck_assert_int_eq(remove_directory(TESTDIR), 1);
+ fake_proc_net_dev("w", "ethwrong", 10, 20, 30, 40);
+
+ ck_assert_int_eq(getifinfo("ethunusual"), 0);
+}
+END_TEST
+
+START_TEST(getifinfo_success)
+{
+ suppress_output();
+
+ ck_assert_int_eq(remove_directory(TESTDIR), 1);
+ fake_proc_net_dev("w", "ethwrong", 10, 20, 30, 40);
+ fake_proc_net_dev("a", "ethunusual", 1, 2, 3, 4);
+
+ ck_assert_int_eq(getifinfo("ethunusual"), 1);
+ ck_assert_str_eq(ifinfo.name, "ethunusual");
+ ck_assert_int_eq(ifinfo.filled, 1);
+ ck_assert_int_eq(ifinfo.rx, 1);
+ ck_assert_int_eq(ifinfo.tx, 2);
+ ck_assert_int_eq(ifinfo.rxp, 3);
+ ck_assert_int_eq(ifinfo.txp, 4);
+}
+END_TEST
+
void add_ifinfo_tests(Suite *s)
{
/* Ifinfo test cases */
tcase_add_test(tc_ifinfo, parseifinfo_long_update_interval_causes_sync);
tcase_add_test(tc_ifinfo, parseifinfo_hitting_maxbw_limit_causes_sync);
tcase_add_test(tc_ifinfo, parseifinfo_multiple_parses);
+ tcase_add_test(tc_ifinfo, getiflist_no_source);
+ tcase_add_test(tc_ifinfo, getiflist_one_interface);
+ tcase_add_test(tc_ifinfo, getiflist_multiple_interfaces);
+ tcase_add_test(tc_ifinfo, readproc_no_file);
+ tcase_add_test(tc_ifinfo, readproc_not_found);
+ tcase_add_test(tc_ifinfo, readproc_success);
+ tcase_add_test(tc_ifinfo, getifinfo_not_found);
+ tcase_add_test(tc_ifinfo, getifinfo_success);
suite_add_tcase(s, tc_ifinfo);
}
int main(void)
{
int number_failed = 0;
+ debug = 0;
Suite *s = test_suite();
SRunner *sr = srunner_create(s);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
- remove_directory(TESTDBDIR);
+ if (number_failed == 0) {
+ remove_directory(TESTDIR);
+ }
return number_failed;
}
{
struct stat statbuf;
+ create_testdir();
+
if (stat(TESTDBDIR, &statbuf)!=0) {
if (errno==ENOENT) {
if (mkdir(TESTDBDIR, 0755)==0) {
return 1;
}
- ck_abort_msg("error \"%s\" while creating directory \"%s\", please remove it manually", strerror(errno), TESTDBDIR);
}
+ ck_abort_msg("error \"%s\" while creating directory \"%s\"", strerror(errno), TESTDBDIR);
}
if (!remove_directory(TESTDBDIR)) {
}
if (mkdir(TESTDBDIR, 0755)!=0) {
- ck_abort_msg("error \"%s\" while creating directory \"%s\", please remove it manually", strerror(errno), TESTDBDIR);
+ ck_abort_msg("error \"%s\" while creating directory \"%s\"", strerror(errno), TESTDBDIR);
+ }
+
+ return 1;
+}
+
+int create_testdir(void)
+{
+ struct stat statbuf;
+
+ if (stat(TESTDIR, &statbuf)!=0) {
+ if (errno==ENOENT) {
+ if (mkdir(TESTDIR, 0755)==0) {
+ return 1;
+ }
+ }
+ ck_abort_msg("error \"%s\" while creating directory \"%s\"", strerror(errno), TESTDIR);
}
return 1;
return 1;
}
+
+int fake_proc_net_dev(const char *mode, const char *iface, const int rx, const int tx, const int rxp, const int txp)
+{
+ FILE *devfp;
+ char filename[512];
+ struct stat statbuf;
+
+ if (strcmp(mode, "w") != 0 && strcmp(mode, "a") != 0) {
+ ck_abort_msg("error: only w and a modes are supported");
+ }
+
+ create_testdir();
+
+ if (stat(TESTPROCDIR, &statbuf)!=0) {
+ if (errno==ENOENT) {
+ if (mkdir(TESTPROCDIR, 0755) != 0) {
+ ck_abort_msg("error \"%s\" while creating directory \"%s\"", strerror(errno), TESTPROCDIR);
+ }
+ } else {
+ ck_abort_msg("error \"%s\" while creating directory \"%s\"", strerror(errno), TESTPROCDIR);
+ }
+ }
+
+ snprintf(filename, 512, "%s/dev", TESTPROCDIR);
+ if ((devfp=fopen(filename, mode))==NULL) {
+ ck_abort_msg("error \"%s\" while opening file \"%s\" for writing", strerror(errno), filename);
+ }
+
+ if (strcmp(mode, "w") == 0) {
+ fprintf(devfp, "Inter-| Receive | Transmit\n");
+ fprintf(devfp, " face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed\n");
+ fprintf(devfp, " lo: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n");
+ }
+ fprintf(devfp, "%6s: %7d %7d 0 0 0 0 0 0 %7d %7d 0 0 0 0 0 0\n", iface, rx, rxp, tx, txp);
+
+ fclose(devfp);
+
+ return 1;
+}