"Fossies" - the Fresh Open Source Software Archive

Member "vnstat-2.9/tests/id_tests.c" (7 Jul 2019, 2617 Bytes) of package /linux/misc/vnstat-2.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #include "common.h"
    2 #include "vnstat_tests.h"
    3 #include "id_tests.h"
    4 #include "id.h"
    5 
    6 START_TEST(getuser_root_string)
    7 {
    8     ck_assert_int_eq((int)getuser("root"), 0);
    9 }
   10 END_TEST
   11 
   12 START_TEST(getuser_root_numeric)
   13 {
   14     ck_assert_int_eq((int)getuser("0"), 0);
   15 }
   16 END_TEST
   17 
   18 START_TEST(getuser_no_such_user_string)
   19 {
   20     suppress_output();
   21     getuser("reallynosuchuser");
   22 }
   23 END_TEST
   24 
   25 START_TEST(getuser_no_such_user_numeric)
   26 {
   27     suppress_output();
   28     getuser("99999999");
   29 }
   30 END_TEST
   31 
   32 START_TEST(getgroup_root_string)
   33 {
   34 #if defined(__linux__) || defined(__GNU__) || defined(__GLIBC__)
   35     ck_assert_int_eq((int)getgroup("root"), 0);
   36 #else
   37     ck_assert_int_eq((int)getgroup("wheel"), 0);
   38 #endif
   39 }
   40 END_TEST
   41 
   42 START_TEST(getgroup_root_numeric)
   43 {
   44     ck_assert_int_eq((int)getgroup("0"), 0);
   45 }
   46 END_TEST
   47 
   48 START_TEST(getgroup_no_such_user_string)
   49 {
   50     suppress_output();
   51     getgroup("reallynosuchgroup");
   52 }
   53 END_TEST
   54 
   55 START_TEST(getgroup_no_such_user_numeric)
   56 {
   57     suppress_output();
   58     getgroup("99999999");
   59 }
   60 END_TEST
   61 
   62 START_TEST(setuser_with_empty_user)
   63 {
   64     suppress_output();
   65     setuser("");
   66 }
   67 END_TEST
   68 
   69 START_TEST(setuser_with_current_user)
   70 {
   71     char temp[16];
   72     struct passwd *pw;
   73 
   74     pw = getpwuid(getuid());
   75 
   76     if (pw != NULL) {
   77         suppress_output();
   78         snprintf(temp, 16, "%d", getuser(""));
   79         setuser(temp);
   80     } else {
   81         printf("Note: skipping test setuser_with_current_user due to non-existing user\n");
   82     }
   83 }
   84 END_TEST
   85 
   86 START_TEST(setgroup_with_empty_group)
   87 {
   88     suppress_output();
   89     setgroup("");
   90 }
   91 END_TEST
   92 
   93 START_TEST(setgroup_with_current_group)
   94 {
   95     char temp[16];
   96     struct group *gr;
   97 
   98     gr = getgrgid(getgid());
   99 
  100     if (gr != NULL) {
  101         suppress_output();
  102         snprintf(temp, 16, "%d", getgroup(""));
  103         setgroup(temp);
  104     } else {
  105         printf("Note: skipping test setgroup_with_current_group due to non-existing group\n");
  106     }
  107 }
  108 END_TEST
  109 
  110 void add_id_tests(Suite *s)
  111 {
  112     TCase *tc_id = tcase_create("ID");
  113     tcase_add_checked_fixture(tc_id, setup, teardown);
  114     tcase_add_unchecked_fixture(tc_id, setup, teardown);
  115     tcase_add_test(tc_id, getuser_root_string);
  116     tcase_add_test(tc_id, getuser_root_numeric);
  117     tcase_add_exit_test(tc_id, getuser_no_such_user_string, 1);
  118     tcase_add_exit_test(tc_id, getuser_no_such_user_numeric, 1);
  119     tcase_add_test(tc_id, getgroup_root_string);
  120     tcase_add_test(tc_id, getgroup_root_numeric);
  121     tcase_add_exit_test(tc_id, getgroup_no_such_user_string, 1);
  122     tcase_add_exit_test(tc_id, getgroup_no_such_user_numeric, 1);
  123     tcase_add_test(tc_id, setuser_with_empty_user);
  124     tcase_add_test(tc_id, setuser_with_current_user);
  125     tcase_add_test(tc_id, setgroup_with_empty_group);
  126     tcase_add_test(tc_id, setgroup_with_current_group);
  127     suite_add_tcase(s, tc_id);
  128 }