|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <pty.h> |
| 4 | +#include <unistd.h> |
| 5 | +#include <string.h> |
| 6 | +#include <sys/types.h> |
| 7 | +#include <sys/stat.h> |
| 8 | +#include <fcntl.h> |
| 9 | +#include "../utempter.h" |
| 10 | +#include "CUnit.h" |
| 11 | +#include "Automated.h" |
| 12 | + |
| 13 | +int check_update_utempter_success(const char * command) |
| 14 | +{ |
| 15 | + FILE* wtmp_fd; |
| 16 | + char buff[255]; |
| 17 | + |
| 18 | + system("utmpdump /var/log/wtmp > wtmp.txt"); |
| 19 | + wtmp_fd = fopen("wtmp.txt", "r"); |
| 20 | + if (wtmp_fd == NULL) { |
| 21 | + printf("open wtmp failed, err = %d\n", errno); |
| 22 | + return -1; |
| 23 | + } |
| 24 | + |
| 25 | + while((fgets(buff, 255, wtmp_fd)) != NULL) { |
| 26 | + continue; |
| 27 | + } |
| 28 | + fclose(wtmp_fd); |
| 29 | + |
| 30 | + if (strstr(buff, "test_libutempter") != NULL) { |
| 31 | + // find test hostname, print success |
| 32 | + if (strcmp(command, "add") == 0) { |
| 33 | + printf("utempter add success\n"); |
| 34 | + return 0; |
| 35 | + } else { |
| 36 | + printf("utempter add failed\n"); |
| 37 | + return -1; |
| 38 | + } |
| 39 | + } else { |
| 40 | + if (strcmp(command, "del") == 0) { |
| 41 | + printf("utempter remove success\n"); |
| 42 | + return 0; |
| 43 | + } else { |
| 44 | + printf("utempter remove failed\n"); |
| 45 | + return -1; |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +void utcase_test_add_remove() |
| 51 | +{ |
| 52 | + int master; |
| 53 | + int slave; |
| 54 | + int err; |
| 55 | + int SUCCESS = 1; |
| 56 | + char pid[32]; |
| 57 | + |
| 58 | + err = openpty(&master, &slave, NULL, NULL, NULL); |
| 59 | + if(0 > err) { |
| 60 | + printf("Error: %s\n", strerror(err)); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + err = utempter_add_record(master, "test_libutempter"); |
| 65 | + printf("add_record result = %d\n", err); |
| 66 | + CU_ASSERT(err == SUCCESS) |
| 67 | + |
| 68 | + err = check_update_utempter_success("add"); |
| 69 | + CU_ASSERT(err == 0); |
| 70 | + |
| 71 | + err = utempter_remove_added_record(); |
| 72 | + printf("remove_record result = %d\n", err); |
| 73 | + |
| 74 | + CU_ASSERT(err == SUCCESS); |
| 75 | + err = check_update_utempter_success("del"); |
| 76 | + CU_ASSERT(err == 0); |
| 77 | + |
| 78 | + close(slave); |
| 79 | + close(master); |
| 80 | +} |
| 81 | + |
| 82 | +void utcase_test_set_path() |
| 83 | +{ |
| 84 | + utempter_set_helper("/usr/lib/utempter/utempter"); |
| 85 | + return; |
| 86 | +} |
| 87 | + |
| 88 | +static CU_TestInfo ut_cases[] = |
| 89 | +{ |
| 90 | + {"case:test_set_path", utcase_test_set_path}, |
| 91 | + {"case:test_add_remove", utcase_test_add_remove}, |
| 92 | + CU_TEST_INFO_NULL, |
| 93 | +}; |
| 94 | + |
| 95 | +int suite_init(void) |
| 96 | +{ |
| 97 | + return 0; |
| 98 | +} |
| 99 | + |
| 100 | +int suite_clean(void) |
| 101 | +{ |
| 102 | + return 0; |
| 103 | +} |
| 104 | + |
| 105 | +static CU_SuiteInfo ut_suites[] = |
| 106 | +{ |
| 107 | + {"my_first_suite", suite_init, suite_clean, NULL, NULL, ut_cases}, |
| 108 | + CU_SUITE_INFO_NULL, |
| 109 | +}; |
| 110 | + |
| 111 | +int main() { |
| 112 | + int rc = 0; |
| 113 | + CU_ErrorCode err = CUE_SUCCESS; |
| 114 | + |
| 115 | + err = CU_initialize_registry(); |
| 116 | + if (err != CUE_SUCCESS) { |
| 117 | + fprintf(stderr, "failed to initialize registry, error %d", err); |
| 118 | + rc = 1; |
| 119 | + goto l_out; |
| 120 | + } |
| 121 | + |
| 122 | + err = CU_register_suites(ut_suites); |
| 123 | + if (err != CUE_SUCCESS) { |
| 124 | + fprintf(stderr, "failed to register suites, error %d, %s", err, CU_get_error_msg()); |
| 125 | + rc = 1; |
| 126 | + goto l_clean_register; |
| 127 | + } |
| 128 | + |
| 129 | + CU_set_output_filename("cunit_sample"); |
| 130 | + |
| 131 | + err = CU_list_tests_to_file(); |
| 132 | + if (err != CUE_SUCCESS) { |
| 133 | + fprintf(stderr, "failed to list tests to file, error %d, %s", err, CU_get_error_msg()); |
| 134 | + rc = 1; |
| 135 | + goto l_clean_register; |
| 136 | + } |
| 137 | + |
| 138 | + CU_automated_run_tests(); |
| 139 | + |
| 140 | +l_clean_register: |
| 141 | + CU_cleanup_registry(); |
| 142 | + |
| 143 | +l_out: |
| 144 | + return rc; |
| 145 | +} |
0 commit comments