/
githubmirror
/
libreport
Обзор
Документация
Войти
/
githubmirror
/
libreport
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
tests/ureport.at
1 064 строки
34 KB
Michal Fabik
Use g_free for GLib-alloc'd memory
17 июн 2021, 17:49
17 июн 2021, 17:49
865db9e
Код
Авторство
О чём код?
# -*- Autotest -*- AT_BANNER([ureport]) ## -------------------------------------- ## ## libreport_ureport_server_config_init ## ## -------------------------------------- ## AT_TESTFUN([libreport_ureport_server_config_init], [[ #include "internal_libreport.h" #include "ureport.h" #include <assert.h> #define DESTROYED_POINTER (void *)0xdeadbeef int main(void) { libreport_g_verbose=3; struct ureport_server_config config; libreport_ureport_server_config_init(&config); assert(config.ur_url == NULL); assert(config.ur_ssl_verify == true); assert(config.ur_client_cert == NULL); assert(config.ur_client_key == NULL); assert(config.ur_username == NULL); assert(config.ur_password == NULL); assert(config.ur_prefs.urp_auth_items == NULL); config.ur_url = (char *)"url"; config.ur_ssl_verify = false; config.ur_client_cert = (char *)"cert"; config.ur_client_key = (char *)"key"; config.ur_username = (char *)"username"; config.ur_password = (char *)"password"; config.ur_prefs.urp_auth_items = DESTROYED_POINTER; libreport_ureport_server_config_init(&config); assert(config.ur_url == NULL); assert(config.ur_ssl_verify == true); assert(config.ur_client_cert == NULL); assert(config.ur_client_key == NULL); assert(config.ur_username == NULL); assert(config.ur_password == NULL); assert(config.ur_prefs.urp_auth_items == NULL); return 0; } ]]) ## ----------------------------------------- ## ## libreport_ureport_server_config_destroy ## ## ----------------------------------------- ## AT_TESTFUN([libreport_ureport_server_config_destroy], [[ #include "internal_libreport.h" #include "ureport.h" #include <assert.h> #define DESTROYED_POINTER (void *)0xdeadbeef int main(void) { libreport_g_verbose=3; struct ureport_server_config config; libreport_ureport_server_config_init(&config); config.ur_url = g_strdup("url"); config.ur_client_cert = g_strdup("cert"); config.ur_client_key = g_strdup("key"); config.ur_username = g_strdup("username"); config.ur_password = g_strdup("password"); assert(strcmp(config.ur_url, "url") == 0); assert(strcmp(config.ur_client_cert, "cert") == 0); assert(strcmp(config.ur_client_key, "key") == 0); assert(strcmp(config.ur_username, "username") == 0); assert(strcmp(config.ur_password , "password") == 0); libreport_ureport_server_config_destroy(&config); assert(config.ur_url == DESTROYED_POINTER); assert(config.ur_client_cert == DESTROYED_POINTER); assert(config.ur_client_key == DESTROYED_POINTER); assert(config.ur_username == DESTROYED_POINTER); assert(config.ur_password == DESTROYED_POINTER); assert(config.ur_prefs.urp_auth_items == DESTROYED_POINTER); return 0; } ]]) ## -------------------------------------- ## ## libreport_ureport_server_config_load ## ## -------------------------------------- ## AT_TESTFUN([libreport_ureport_server_config_load], [[ #include "internal_libreport.h" #include "ureport.h" #include <assert.h> #define TESTING_CERTS_CORRECT_DIR_PATH "../../ureport/certs/correct" int main(void) { libreport_g_verbose=3; /* value from env */ /* IncludeAuthData set to 'no' */ struct ureport_server_config config; libreport_ureport_server_config_init(&config); setenv("uReport_URL", "env_url", 1); setenv("uReport_SSLVerify", "yes", 1); setenv("SSLClientAuth", "", 1); setenv("uReport_IncludeAuthData", "no", 1); setenv("uReport_AuthDataItems", "hostname", 1); GHashTable *settings = g_hash_table_new_full(g_str_hash, g_str_equal, free, free); libreport_ureport_server_config_load(&config, settings); assert(strcmp(config.ur_url, "env_url") == 0); assert(config.ur_ssl_verify == true); GList *l = config.ur_prefs.urp_auth_items; assert(l == NULL); libreport_ureport_server_config_destroy(&config); /* value from env */ /* IncludeAuthData set to 'yes' but AuthDataItems is empty. */ libreport_ureport_server_config_init(&config); setenv("uReport_URL", "env_url", 1); setenv("uReport_SSLVerify", "yes", 1); setenv("SSLClientAuth", "", 1); setenv("uReport_IncludeAuthData", "yes", 1); setenv("uReport_AuthDataItems", "", 1); libreport_ureport_server_config_load(&config, settings); assert(strcmp(config.ur_url, "env_url") == 0); assert(config.ur_ssl_verify == true); l = config.ur_prefs.urp_auth_items; assert(l == NULL); libreport_ureport_server_config_destroy(&config); /* value from env */ /* IncludeAuthData set to 'yes' */ libreport_ureport_server_config_init(&config); setenv("uReport_URL", "env_url", 1); setenv("uReport_SSLVerify", "no", 1); setenv("SSLClientAuth", "", 1); setenv("uReport_IncludeAuthData", "yes", 1); setenv("uReport_AuthDataItems", "hostname, time", 1); libreport_ureport_server_config_load(&config, settings); assert(strcmp(config.ur_url, "env_url") == 0); assert(config.ur_ssl_verify == false); l = config.ur_prefs.urp_auth_items; assert(strcmp(l->data, "hostname") == 0); assert(strcmp(l->next->data, "time") == 0); libreport_ureport_server_config_destroy(&config); /* value from settings */ /* IncludeAuthData set to 'no' */ libreport_ureport_server_config_init(&config); unsetenv("uReport_URL"); unsetenv("uReport_SSLVerify"); unsetenv("uReport_IncludeAuthData"); unsetenv("uReport_AuthDataItems"); g_hash_table_insert(settings, g_strdup("URL"), g_strdup("settings_url")); g_hash_table_insert(settings, g_strdup("SSLVerify"), g_strdup("yes")); g_hash_table_insert(settings, g_strdup("SSLClientAuth"), g_strdup("")); g_hash_table_insert(settings, g_strdup("IncludeAuthData"), g_strdup("no")); g_hash_table_insert(settings, g_strdup("AuthDataItems"), g_strdup("hostname")); libreport_ureport_server_config_load(&config, settings); assert(strcmp(config.ur_url, "settings_url") == 0); assert(config.ur_ssl_verify == true); l = config.ur_prefs.urp_auth_items; assert(l == NULL); libreport_ureport_server_config_destroy(&config); if (settings) g_hash_table_destroy(settings); /* value from settings */ /* IncludeAuthData set to 'yes' but AuthDataItems is empty. */ libreport_ureport_server_config_init(&config); settings = g_hash_table_new_full(g_str_hash, g_str_equal, free, free); g_hash_table_insert(settings, g_strdup("URL"), g_strdup("settings_url")); g_hash_table_insert(settings, g_strdup("SSLVerify"), g_strdup("yes")); g_hash_table_insert(settings, g_strdup("SSLClientAuth"), g_strdup("")); g_hash_table_insert(settings, g_strdup("IncludeAuthData"), g_strdup("yes")); g_hash_table_insert(settings, g_strdup("AuthDataItems"), g_strdup("")); libreport_ureport_server_config_load(&config, settings); assert(strcmp(config.ur_url, "settings_url") == 0); assert(config.ur_ssl_verify == true); l = config.ur_prefs.urp_auth_items; assert(l == NULL); libreport_ureport_server_config_destroy(&config); if (settings) g_hash_table_destroy(settings); /* value from settings */ /* IncludeAuthData set to 'yes' */ libreport_ureport_server_config_init(&config); settings = g_hash_table_new_full(g_str_hash, g_str_equal, free, free); g_hash_table_insert(settings, g_strdup("URL"), g_strdup("settings_url")); g_hash_table_insert(settings, g_strdup("SSLVerify"), g_strdup("no")); g_hash_table_insert(settings, g_strdup("SSLClientAuth"), g_strdup("")); g_hash_table_insert(settings, g_strdup("IncludeAuthData"), g_strdup("yes")); g_hash_table_insert(settings, g_strdup("AuthDataItems"), g_strdup("hostname, type")); libreport_ureport_server_config_load(&config, settings); assert(strcmp(config.ur_url, "settings_url") == 0); assert(config.ur_ssl_verify == false); l = config.ur_prefs.urp_auth_items; assert(strcmp(l->data, "hostname") == 0); assert(strcmp(l->next->data, "type") == 0); libreport_ureport_server_config_destroy(&config); if (settings) g_hash_table_destroy(settings); return 0; } ]]) ## ----------------------------------------- ## ## libreport_ureport_server_config_set_url ## ## ----------------------------------------- ## AT_TESTFUN([libreport_ureport_server_config_set_url], [[ #include "internal_libreport.h" #include "ureport.h" #include <assert.h> #define DESTROYED_POINTER (void *)0xdeadbeef int main(void) { libreport_g_verbose=3; struct ureport_server_config config; libreport_ureport_server_config_init(&config); libreport_ureport_server_config_set_url(&config, g_strdup("url")); assert(strcmp(config.ur_url, "url") == 0); libreport_ureport_server_config_set_url(&config, g_strdup("next.url")); assert(strcmp(config.ur_url, "next.url") == 0); libreport_ureport_server_config_destroy(&config); return 0; } ]]) ## ------------------------------------------------- ## ## libreport_ureport_server_config_set_client_auth ## ## ------------------------------------------------- ## AT_TESTFUN([libreport_ureport_server_config_set_client_auth], [[ #include "internal_libreport.h" #include "ureport.h" #include <assert.h> #define DESTROYED_POINTER (void *)0xdeadbeef #define TESTING_CERTS_CORRECT_DIR_PATH "../../ureport/certs/correct" #define TESTING_CERTS_INCORRECT_ONLY_CERT_DIR_PATH "../../ureport/certs/only_cert" #define TESTING_CERTS_INCORRECT_ONLY_KEY_DIR_PATH "../../ureport/certs/only_key" #define TESTING_PYTHONPATH "../../ureport/" #define WRONG_TESTING_PYTHONPATH "../../ureportxxxxxx/" void set_ureport_server_config(struct ureport_server_config *config, const char *url, bool ver, const char *cert, const char *key, const char *uname, const char *passwd) { config->ur_url = g_strdup(url); config->ur_ssl_verify = ver; config->ur_client_cert = g_strdup(cert); config->ur_client_key = g_strdup(key); config->ur_username = g_strdup(uname); config->ur_password = g_strdup(passwd); return; } void my_assert(const char *str1, const char *str2) { if (str1 == str2) return; if (str1 == NULL && str2 != NULL) { fprintf(stderr, "Assertion failed: NULL == '%s'\n", str2); abort(); } else if (str1 != NULL && str2 == NULL) { fprintf(stderr, "Assertion failed: '%s' == NULL\n", str1); abort(); } else if (strcmp(str1, str2) != 0) { fprintf(stderr, "Assertion failed: '%s' == '%s'\n", str1, str2); abort(); } } void assert_ureport_server_config(struct ureport_server_config *config, const char *url, bool ver, const char *cert, const char *key, const char *username, const char *password) { my_assert(config->ur_url, url); assert(config->ur_ssl_verify == ver); my_assert(config->ur_client_cert, cert); my_assert(config->ur_client_key, key); my_assert(config->ur_username, username); my_assert(config->ur_password , password); return; } int test_ureport_server_config_set_client_auth_exit_code_ext(struct ureport_server_config *config, const char *client_auth, const char *cert_file, const char *key_file) { libreport_ureport_server_config_init(config); pid_t pid = fork(); if (pid < 0) { perror_msg("fork"); return -1; } if (pid == 0) { libreport_ureport_server_config_set_client_auth(config, client_auth); assert((cert_file == (void *)-1) || (cert_file == NULL && config->ur_client_cert == NULL) || (strcmp(cert_file, config->ur_client_cert) == 0)); assert((key_file == (void *)-1) || (key_file == NULL && config->ur_client_cert == NULL) || (strcmp(key_file, config->ur_client_key) == 0)); exit(0); } int status; wait(&status); libreport_ureport_server_config_destroy(config); return status; } int test_ureport_server_config_set_client_auth_exit_code(struct ureport_server_config *config, const char *client_auth) { return test_ureport_server_config_set_client_auth_exit_code_ext(config, client_auth, (void *)-1, (void *)-1); } int main(void) { libreport_g_verbose=3; struct ureport_server_config config; libreport_ureport_server_config_init(&config); libreport_ureport_server_config_set_client_auth(&config, NULL); set_ureport_server_config(&config, "url", true, "cert", "key", "username", "passwd"); /* client_auth == NULL */ libreport_ureport_server_config_set_client_auth(&config, NULL); assert_ureport_server_config(&config, "url", true, "cert", "key", "username", "passwd"); /* client_auth == "" */ libreport_ureport_server_config_set_client_auth(&config, ""); assert_ureport_server_config(&config, "url", true, NULL, NULL, "username", "passwd"); libreport_ureport_server_config_destroy(&config); /* client_auth == cert:key */ /* ur_url == NULL */ libreport_ureport_server_config_init(&config); set_ureport_server_config(&config, NULL, true, NULL, NULL, "username", "passwd"); libreport_ureport_server_config_set_client_auth(&config, "cert:key"); assert_ureport_server_config(&config, NULL, true, "cert", "key", NULL, NULL); libreport_ureport_server_config_destroy(&config); /* client_auth == cert:key */ /* ur_url != NULL */ libreport_ureport_server_config_init(&config); set_ureport_server_config(&config, "url", true, NULL, NULL, "username", "passwd"); libreport_ureport_server_config_set_client_auth(&config, "cert:key"); assert_ureport_server_config(&config, "url", true, "cert", "key", NULL, NULL); libreport_ureport_server_config_destroy(&config); /* wrong client_auth */ int ret_val = test_ureport_server_config_set_client_auth_exit_code(&config, "cert:"); assert(ret_val != 0 && ret_val != -1); ret_val = test_ureport_server_config_set_client_auth_exit_code(&config, ":key"); assert(ret_val != 0 && ret_val != -1); ret_val = test_ureport_server_config_set_client_auth_exit_code(&config, "cert"); assert(ret_val != 0 && ret_val != -1); return 0; } ]]) ## ------------------------------------------------ ## ## libreport_ureport_server_config_set_basic_auth ## ## ------------------------------------------------ ## AT_TESTFUN([libreport_ureport_server_config_set_basic_auth], [[ #include "internal_libreport.h" #include "ureport.h" #include <assert.h> void my_assert(const char *str1, const char *str2) { if (str1 == str2) return; if (str1 == NULL && str2 != NULL) { fprintf(stderr, "Assertion failed: NULL == '%s'\n", str2); abort(); } else if (str1 != NULL && str2 == NULL) { fprintf(stderr, "Assertion failed: '%s' == NULL\n", str1); abort(); } else if (strcmp(str1, str2) != 0) { fprintf(stderr, "Assertion failed: '%s' == '%s'\n", str1, str2); abort(); } } void assert_ureport_server_config(struct ureport_server_config *config, const char *url, bool ver, const char *cert, const char *key, const char *username, const char *password) { my_assert(config->ur_url, url); assert(config->ur_ssl_verify == ver); my_assert(config->ur_client_cert, cert); my_assert(config->ur_client_key, key); my_assert(config->ur_username, username); my_assert(config->ur_password , password); return; } int main(void) { libreport_g_verbose=3; struct ureport_server_config config; libreport_ureport_server_config_init(&config); libreport_ureport_server_config_set_basic_auth(&config, NULL, NULL); assert_ureport_server_config(&config, NULL, true, NULL, NULL, NULL, NULL); libreport_ureport_server_config_set_basic_auth(&config, "usr", NULL); assert_ureport_server_config(&config, NULL, true, NULL, NULL, "usr", NULL); libreport_ureport_server_config_set_basic_auth(&config, NULL, "passwd"); assert_ureport_server_config(&config, NULL, true, NULL, NULL, NULL, "passwd"); libreport_ureport_server_config_set_basic_auth(&config, "usr", "passwd"); assert_ureport_server_config(&config, NULL, true, NULL, NULL, "usr", "passwd"); libreport_ureport_server_config_destroy(&config); return 0; } ]]) ## ---------------------------------------------- ## ## libreport_ureport_server_response_from_reply ## ## ---------------------------------------------- ## AT_TESTFUN([libreport_ureport_server_response_from_reply], [[ #include "internal_libreport.h" #include "ureport.h" #include <assert.h> #include "libreport_curl.h" int main(void) { /* curl_resul is not CURL_OK */ struct post_state ps; memset((void *)&ps, 0, sizeof(ps)); ps.curl_result = 1; strcpy(ps.errmsg, "Artificial Error for the purpose of testing ability to recover from errors"); ps.body = (char *)"body"; struct ureport_server_config config; libreport_ureport_server_config_init(&config); g_autofree char *url = g_strdup("url"); libreport_ureport_server_config_set_url(&config, url); assert(libreport_ureport_server_response_from_reply(&ps, &config) == NULL); /* curl_resul is CURLE_OK */ /* http_resp_code == 404 */ ps.curl_result = CURLE_OK; ps.http_resp_code = 404; assert(libreport_ureport_server_response_from_reply(&ps, &config) == NULL); ps.http_resp_code = 500; assert(libreport_ureport_server_response_from_reply(&ps, &config) == NULL); ps.http_resp_code = 503; assert(libreport_ureport_server_response_from_reply(&ps, &config) == NULL); ps.http_resp_code = 404; assert(libreport_ureport_server_response_from_reply(&ps, &config) == NULL); ps.http_resp_code = 201; assert(libreport_ureport_server_response_from_reply(&ps, &config) == NULL); /* unable parse json */ ps.http_resp_code = 202; assert(libreport_ureport_server_response_from_reply(&ps, &config) == NULL); /* correct json && invalid format */ ps.body = (char *)"{ \"resultxxxxxxxx\" : true }"; assert(libreport_ureport_server_response_from_reply(&ps, &config) == NULL); /* correct json && valid format */ ps.body = (char *)"{ 'result' : true, \ 'message': 'message', \ 'bthash': '691cf824e3e07457156125636e86c50279e29496', \ 'reported_to': [ { 'type': 'url', \ 'value': 'value', \ 'reporter': 'ABRT Server' } ] }"; struct ureport_server_response *response = libreport_ureport_server_response_from_reply(&ps, &config); assert(strcmp(response->urr_value, "true") == 0); assert(strcmp(response->urr_message, "message") == 0); assert(strcmp(response->urr_bthash, "691cf824e3e07457156125636e86c50279e29496") == 0); GList *urr_reported_to_list = response->urr_reported_to_list; assert(strcmp(urr_reported_to_list->data, "ABRT Server: URL=value") == 0); libreport_ureport_server_response_free(response); return 0; } ]]) ## --------------------------------------------------- ## ## libreport_ureport_server_response_save_in_dump_dir ## ## --------------------------------------------------- ## AT_TESTFUN([libreport_ureport_server_response_save_in_dump_dir], [[ #include "internal_libreport.h" #include "ureport.h" #include <assert.h> #include "libreport_curl.h" #include "dump_dir.h" #include "problem_data.h" int main(void) { libreport_g_verbose=3; /* reported to*/ struct post_state ps; ps.curl_result = CURLE_OK; ps.http_resp_code = 202; ps.body = (char *)"{ 'result' : true, \ 'message': 'message', \ 'bthash': '691cf824e3e07457156125636e86c50279e29496', \ 'reported_to': [ { 'type': 'url', \ 'value': 'value', \ 'reporter': 'ABRT Server' } ] }"; struct ureport_server_config config; libreport_ureport_server_config_init(&config); libreport_ureport_server_config_set_url(&config, g_strdup("url")); struct dump_dir *dd = dd_create("./test", (uid_t)-1L, DEFAULT_DUMP_DIR_MODE); assert(dd != NULL); dd_create_basic_files(dd, (uid_t)-1L, NULL); dd_save_text(dd, FILENAME_TYPE, "CCpp"); dd_save_text(dd, FILENAME_COUNT, "1"); dd_close(dd); struct ureport_server_response *response = libreport_ureport_server_response_from_reply(&ps, &config); assert(libreport_ureport_server_response_save_in_dump_dir(response, "./test", &config)); /* dump dir do not exist */ assert(false == libreport_ureport_server_response_save_in_dump_dir(response, "not_existing_dir", &config)); dd = dd_opendir("./test", 0); char *reported_to = dd_load_text_ext(dd, FILENAME_REPORTED_TO, DD_LOAD_TEXT_RETURN_NULL_ON_FAILURE); assert(strstr(reported_to, "uReport: BTHASH=691cf824e3e07457156125636e86c50279e29496") != NULL); assert(strstr(reported_to, "url/reports/bthash/691cf824e3e07457156125636e86c50279e29496") != NULL); assert(strstr(reported_to, "ABRT Server: URL=value") != NULL); g_free(reported_to); /* not-reportable must not exist */ assert(!dd_exist(dd, FILENAME_NOT_REPORTABLE)); g_free(config.ur_url); libreport_ureport_server_response_free(response); dd_close(dd); delete_dump_dir("./test"); /* not-reportable*/ ps.curl_result = CURLE_OK; ps.http_resp_code = 202; ps.body = (char *)"{ 'result' : true, \ 'message': 'message', \ 'solutions': [ { 'cause': 'solution_cause', \ 'url': 'solution_url', \ 'note': 'solution_note' } ], \ 'bthash': '691cf824e3e07457156125636e86c50279e29496', \ 'reported_to': [ { 'type': 'url', \ 'value': 'value', \ 'reporter': 'ABRT Server' } ] }"; libreport_ureport_server_config_init(&config); libreport_ureport_server_config_set_url(&config, g_strdup("url")); dd = dd_create("./test", (uid_t)-1L, DEFAULT_DUMP_DIR_MODE); assert(dd != NULL); dd_create_basic_files(dd, (uid_t)-1L, NULL); dd_save_text(dd, FILENAME_TYPE, "CCpp"); dd_save_text(dd, FILENAME_COUNT, "1"); dd_close(dd); response = libreport_ureport_server_response_from_reply(&ps, &config); assert(libreport_ureport_server_response_save_in_dump_dir(response, "./test", &config)); dd = dd_opendir("./test", 0); reported_to = dd_load_text_ext(dd, FILENAME_REPORTED_TO, DD_LOAD_TEXT_RETURN_NULL_ON_FAILURE); assert(strstr(reported_to, "uReport: BTHASH=691cf824e3e07457156125636e86c50279e29496") != NULL); assert(strstr(reported_to, "url/reports/bthash/691cf824e3e07457156125636e86c50279e29496") != NULL); assert(strstr(reported_to, "ABRT Server: URL=value") != NULL); g_free(reported_to); /* not-reportable must exist */ g_autofree char *not_reportable = dd_load_text_ext(dd, FILENAME_NOT_REPORTABLE, DD_LOAD_TEXT_RETURN_NULL_ON_FAILURE); assert(strstr(not_reportable, "Your problem seems to be caused by solution_cause") != NULL); g_free(config.ur_url); libreport_ureport_server_response_free(response); dd_close(dd); delete_dump_dir("./test"); return 0; } ]]) ## ------------------------------------------------- ## ## libreport_ureport_server_response_get_report_url ## ## ------------------------------------------------- ## AT_TESTFUN([libreport_ureport_server_response_get_report_url], [[ #include "internal_libreport.h" #include "ureport.h" #include <assert.h> #include "libreport_curl.h" #include "problem_data.h" #define BTHASH_URL_SFX "reports/bthash/" int main(void) { libreport_g_verbose=3; /* reported to*/ struct post_state ps; ps.curl_result = CURLE_OK; ps.http_resp_code = 202; ps.body = (char *)"{ 'result' : true, \ 'message': 'message', \ 'bthash': '691cf824e3e07457156125636e86c50279e29496', \ 'reported_to': [ { 'type': 'url', \ 'value': 'value', \ 'reporter': 'ABRT Server' } ] }"; struct ureport_server_config config; libreport_ureport_server_config_init(&config); libreport_ureport_server_config_set_url(&config, g_strdup("url")); struct ureport_server_response *response = libreport_ureport_server_response_from_reply(&ps, &config); g_autofree char *report_url = libreport_ureport_server_response_get_report_url(response, &config); g_autofree char *expect_bthash_url = g_build_filename(config.ur_url ? config.ur_url : "", BTHASH_URL_SFX, NULL); g_autofree char *expect_report_url = g_build_filename(expect_bthash_url ? expect_bthash_url : "", response->urr_bthash, NULL); assert(strcmp(report_url, expect_report_url) == 0); g_free(config.ur_url); libreport_ureport_server_response_free(response); return 0; } ]]) ## -------------------------- ## ## libreport_ureport_do_post ## ## -------------------------- ## AT_TESTFUN([libreport_ureport_do_post], [[ #include "internal_libreport.h" #include "ureport.h" #include <assert.h> #include "libreport_curl.h" #include "problem_data.h" int main(void) { libreport_g_verbose=3; struct dump_dir *dd = dd_create("./test", (uid_t)-1L, DEFAULT_DUMP_DIR_MODE); assert(dd != NULL); dd_create_basic_files(dd, (uid_t)-1L, NULL); dd_save_text(dd, FILENAME_TYPE, "CCpp"); dd_save_text(dd, FILENAME_ANALYZER, "CCpp"); dd_save_text(dd, FILENAME_PKG_EPOCH, "pkg_epoch"); dd_save_text(dd, FILENAME_PKG_ARCH, "pkg_arch"); dd_save_text(dd, FILENAME_PKG_RELEASE, "pkg_release"); dd_save_text(dd, FILENAME_PKG_VERSION, "pkg_version"); dd_save_text(dd, FILENAME_PKG_NAME, "pkg_name"); const char *bt = "{ \"signal\": 6, \"executable\": \"/usr/bin/will_abort\" }"; dd_save_text(dd, FILENAME_CORE_BACKTRACE, bt); dd_save_text(dd, FILENAME_COUNT, "1"); dd_close(dd); g_autofree char *json = libreport_ureport_from_dump_dir_ext("./test", NULL); /* wrong url */ struct ureport_server_config config; libreport_ureport_server_config_init(&config); struct post_state *post_state = libreport_ureport_do_post(json, &config, "not_exist"); assert(post_state->curl_result == CURLE_COULDNT_RESOLVE_HOST); free_post_state(post_state); libreport_ureport_server_config_destroy(&config); delete_dump_dir("./test"); return 0; } ]]) ## ------------------------- ## ## libreport_ureport_submit ## ## ------------------------- ## AT_TESTFUN([libreport_ureport_submit], [[ #include "internal_libreport.h" #include "ureport.h" #include <assert.h> #include "libreport_curl.h" #include "problem_data.h" int main(void) { libreport_g_verbose=3; struct dump_dir *dd = dd_create("./test", (uid_t)-1L, DEFAULT_DUMP_DIR_MODE); assert(dd != NULL); dd_create_basic_files(dd, (uid_t)-1L, NULL); dd_save_text(dd, FILENAME_TYPE, "CCpp"); dd_save_text(dd, FILENAME_ANALYZER, "CCpp"); dd_save_text(dd, FILENAME_PKG_EPOCH, "pkg_epoch"); dd_save_text(dd, FILENAME_PKG_ARCH, "pkg_arch"); dd_save_text(dd, FILENAME_PKG_RELEASE, "pkg_release"); dd_save_text(dd, FILENAME_PKG_VERSION, "pkg_version"); dd_save_text(dd, FILENAME_PKG_NAME, "pkg_name"); const char *bt = "{ \"signal\": 6, \"executable\": \"/usr/bin/will_abort\" }"; dd_save_text(dd, FILENAME_CORE_BACKTRACE, bt); dd_save_text(dd, FILENAME_COUNT, "1"); dd_close(dd); g_autofree char *json = libreport_ureport_from_dump_dir_ext("./test", NULL); /* wrong url */ struct ureport_server_config config; libreport_ureport_server_config_init(&config); struct ureport_server_response *response = libreport_ureport_submit(json, &config); assert(response == NULL); libreport_ureport_server_response_free(response); libreport_ureport_server_config_destroy(&config); delete_dump_dir("./test"); return 0; } ]]) ## --------------------------- ## ## ureport_json_attachment_new ## ## --------------------------- ## AT_TESTFUN([ureport_json_attachment_new], [[ #include "internal_libreport.h" #include "ureport.h" #include <assert.h> #include "libreport_curl.h" #include "problem_data.h" int main(void) { libreport_g_verbose=3; char *json = ureport_json_attachment_new("data_bthash", "data_type", "data_data"); assert(strcmp(json, "{ \"bthash\": \"data_bthash\", \"type\": \"data_type\", \"data\": \"data_data\" }") == 0); g_free(json); json = ureport_json_attachment_new("", "", ""); assert(strcmp(json, "{ \"bthash\": \"\", \"type\": \"\", \"data\": \"\" }") == 0); g_free(json); return 0; } ]]) ## --------------------- ## ## ureport_attach_string ## ## --------------------- ## AT_TESTFUN([ureport_attach_string], [[ #include "internal_libreport.h" #include "ureport.h" #include <assert.h> #include "libreport_curl.h" #include "problem_data.h" int main(void) { libreport_g_verbose=3; struct ureport_server_config config; libreport_ureport_server_config_init(&config); bool res = ureport_attach(&config, "691cf824e3e07457156125636e86c50279e29496", "email", "abrt@email.com"); assert(res == true); res = ureport_attach(&config, "691cf824e3e07457156125636e86c50279e29496", "count", "%d", 5); assert(res); libreport_ureport_server_config_destroy(&config); return 0; } ]]) ## ----------------------------------- ## ## libreport_ureport_from_dump_dir_ext ## ## ----------------------------------- ## AT_TESTFUN([libreport_ureport_from_dump_dir_ext], [[ #include "internal_libreport.h" #include "ureport.h" #include <assert.h> #include "libreport_curl.h" #include "problem_data.h" int main(void) { libreport_g_verbose=3; struct dump_dir *dd = dd_create("./test", (uid_t)-1L, DEFAULT_DUMP_DIR_MODE); assert(dd != NULL); dd_create_basic_files(dd, (uid_t)-1L, NULL); dd_save_text(dd, FILENAME_TYPE, "CCpp"); dd_save_text(dd, FILENAME_ANALYZER, "CCpp"); dd_save_text(dd, FILENAME_PKG_EPOCH, "pkg_epoch"); dd_save_text(dd, FILENAME_PKG_ARCH, "pkg_arch"); dd_save_text(dd, FILENAME_PKG_RELEASE, "pkg_release"); dd_save_text(dd, FILENAME_PKG_VERSION, "pkg_version"); dd_save_text(dd, FILENAME_PKG_NAME, "pkg_name"); const char *bt = "{ \"signal\": 6, \"executable\": \"/usr/bin/will_abort\" }"; dd_save_text(dd, FILENAME_CORE_BACKTRACE, bt); dd_save_text(dd, FILENAME_COUNT, "1"); dd_close(dd); /* no auth */ char *ureport = libreport_ureport_from_dump_dir_ext("./test", NULL); assert(strstr(ureport, "auth") == NULL); g_free(ureport); /* auth */ dd = dd_opendir("./test", 0); dd_save_text(dd, FILENAME_HOSTNAME, "env_hostname"); dd_close(dd); struct ureport_server_config config; libreport_ureport_server_config_init(&config); GHashTable *settings = g_hash_table_new_full(g_str_hash, g_str_equal, free, free); setenv("uReport_IncludeAuthData", "yes", 1); setenv("uReport_AuthDataItems", "hostname", 1); libreport_ureport_server_config_load(&config, settings); ureport = libreport_ureport_from_dump_dir_ext("./test", &config.ur_prefs); assert(strstr(ureport, "auth") != NULL); assert(strstr(ureport, "\"hostname\": \"env_hostname\"") != NULL); g_free(ureport); libreport_ureport_server_config_destroy(&config); if (settings) g_hash_table_destroy(settings); /* auth with unknown uReport_AuthDataItems */ libreport_ureport_server_config_init(&config); settings = g_hash_table_new_full(g_str_hash, g_str_equal, free, free); setenv("uReport_AuthDataItems", "hostname, unknown", 1); libreport_ureport_server_config_load(&config, settings); ureport = libreport_ureport_from_dump_dir_ext("./test", &config.ur_prefs); assert(strstr(ureport, "auth") != NULL); assert(strstr(ureport, "\"hostname\": \"env_hostname\"") != NULL); assert(strstr(ureport, "unknown") == NULL); g_free(ureport); libreport_ureport_server_config_destroy(&config); if (settings) g_hash_table_destroy(settings); delete_dump_dir("./test"); return 0; } ]]) ## ------------------------------------- ## ## ureport_server_config_load_basic_auth ## ## ------------------------------------- ## AT_TESTFUN([ureport_server_config_load_basic_auth], [[ #include "internal_libreport.h" #include "ureport.h" #include <assert.h> #include "libreport_curl.h" #include "problem_data.h" int main(void) { libreport_g_verbose=3; { struct ureport_server_config config; libreport_ureport_server_config_init(&config); ureport_server_config_load_basic_auth(&config, "username:password"); assert(strcmp(config.ur_username, "username") == 0); assert(strcmp(config.ur_password, "password") == 0); libreport_ureport_server_config_destroy(&config); } { pid_t pid = fork(); if (pid < 0) { perror_msg("fork"); return -1; } if (pid == 0) { struct ureport_server_config config; libreport_ureport_server_config_init(&config); setenv("REPORT_CLIENT_NONINTERACTIVE", "1", 1); ureport_server_config_load_basic_auth(&config, "username"); libreport_ureport_server_config_destroy(&config); exit(0); } int status; wait(&status); assert(WIFEXITED(status)); assert(WEXITSTATUS(status) != 0); } return 0; } ]])