Commit 4cf474da authored by Hermann Mayer's avatar Hermann Mayer

[AVM][Tests] Removed all exit() calls from non-binary locations. (See #13)

parent b6afc6a2
......@@ -41,6 +41,14 @@ char* parse_start_session_res(struct response *res)
return xml_read_chars("/SessionInfo/SID", res);
}
char* session_invalid_start()
{
char *session = INVALID_SESSION_ID;
char *ret = (char*) malloc(sizeof(char) * 17);
strncpy(ret, session, 17);
return ret;
}
/* Start a new session by logging in on a given host
* with username and password response */
char* session_start(const char *hostname, const char *username,
......@@ -62,11 +70,24 @@ char* session_start(const char *hostname, const char *username,
char *ret;
char *url = build_url(hostname, path);
init_response(&res);
if (init_response(&res) > 0) {
free(challenge);
free(url);
free(path);
free(response);
return session_invalid_start();
}
if (req_get_wr(url, &res) > 0) {
utlog(LOG_ERR, "AVM: start_session::perform_get_req failed\n");
exit(EXIT_FAILURE);
if (NULL != res.ptr) {
free(res.ptr);
}
free(challenge);
free(url);
free(path);
free(response);
return session_invalid_start();
}
session_id = parse_start_session_res(&res);
......@@ -99,7 +120,6 @@ void session_end(const char *hostname, const char *session_id)
// Perform the request
if (req_get_wor(url) > 0) {
utlog(LOG_ERR, "AVM: end_session::perform_get_req failed\n");
exit(EXIT_FAILURE);
}
free(url);
......
......@@ -30,6 +30,14 @@ char* parse_challenge_res(struct response *res)
return xml_read_chars("/SessionInfo/Challenge", res);
}
char* passwd_invalid_challenge()
{
char *challenge = CHALLENGE_INVALID;
char *ret = (char*) malloc(sizeof(char) * 9);
strncpy(ret, challenge, 9);
return ret;
}
/* Retrieve a challenge */
char* passwd_challenge(const char *hostname)
{
......@@ -39,11 +47,18 @@ char* passwd_challenge(const char *hostname)
struct response res;
char *url = build_url(hostname, "/login_sid.lua");
init_response(&res);
if (init_response(&res) > 0) {
free(url);
return passwd_invalid_challenge();
}
if (req_get_wr(url, &res) > 0) {
utlog(LOG_ERR, "AVM: get_challenge::perform_get_req failed\n");
exit(EXIT_FAILURE);
free(url);
if (NULL != res.ptr) {
free(res.ptr);
}
return passwd_invalid_challenge();
}
challenge = parse_challenge_res(&res);
......
......@@ -24,6 +24,8 @@
#include "../utils/xml-response.h"
#define CHALLENGE_INVALID "00000000"
/* Retrieve a challenge */
char* passwd_challenge(const char*);
......
......@@ -63,6 +63,10 @@ char* build_sw_path(const char *command, const char *session_id, size_t size, ..
char response_char(struct response res)
{
if (-1 == res.len) {
return SWITCH_UNVRSL_UNKWN;
}
if (0 == strcmp("1", trimcrlf(res.ptr))) {
free(res.ptr);
return SWITCH_UNVRSL_ON;
......@@ -74,6 +78,10 @@ char response_char(struct response res)
char* response_str(struct response res)
{
if (-1 == res.len) {
return "";
}
size_t len = strlen(res.ptr) + 1;
char* str = (char*) malloc(sizeof(char) * len);
strncpy(str, res.ptr, len);
......@@ -89,12 +97,15 @@ struct response req_sw(const char *hostname, const char *command, const char *pa
struct response res;
char *url = build_url(hostname, path);
init_response(&res);
if (init_response(&res) > 0) {
free(url);
return res;
}
if (req_get_wr(url, &res) > 0) {
utlog(LOG_ERR, "AVM: %s::req_get_wr failed\n", command);
free(url);
exit(EXIT_FAILURE);
return res;
}
free(url);
......
......@@ -23,6 +23,7 @@
#define AVM_SWITCHES_OPERATIONS_H
#define SWITCH_PATH "/webservices/homeautoswitch.lua"
#define SWITCH_UNVRSL_UNKWN 2
#define SWITCH_UNVRSL_ON 1
#define SWITCH_UNVRSL_OFF 0
#define SWITCH_STATE_ON SWITCH_UNVRSL_ON
......
......@@ -39,18 +39,20 @@ char* build_url(const char *host, const char *path)
return url;
}
/* Initalize a empty response struct */
void init_response(struct response *res)
/* Initalize a empty response struct, >0 on errors. */
int init_response(struct response *res)
{
res->len = 0;
res->ptr = malloc(res->len + 1);
if (NULL == res->ptr) {
utlog(LOG_ERR, "AVM: init_response::malloc() failed\n");
exit(EXIT_FAILURE);
res->len = -1;
return 1;
}
res->ptr[0] = '\0';
return 0;
}
size_t append_response_chunk(void *contents, size_t size, size_t nmemb, void *userp)
......@@ -86,7 +88,7 @@ char* trimcrlf(char* str)
return str;
}
/* GET request WithOut Response */
/* GET request WithOut Response, >0 on errors. */
int req_get_wor(const char *url)
{
CURL *curl;
......@@ -96,7 +98,7 @@ int req_get_wor(const char *url)
if (NULL == curl) {
utlog(LOG_ERR, "AVM: req_get_wor::curl_easy_init() failed\n");
exit(EXIT_FAILURE);
return 1;
}
curl_easy_setopt(curl, CURLOPT_URL, url);
......@@ -116,7 +118,7 @@ int req_get_wor(const char *url)
return 0;
}
/* GET request With Response */
/* GET request With Response, >0 on errors. */
int req_get_wr(const char *url, struct response *res)
{
CURL *curl;
......@@ -126,7 +128,7 @@ int req_get_wr(const char *url, struct response *res)
if (NULL == curl) {
utlog(LOG_ERR, "AVM: req_get_wr::curl_easy_init() failed\n");
exit(EXIT_FAILURE);
return 1;
}
curl_easy_setopt(curl, CURLOPT_URL, url);
......
......@@ -29,8 +29,8 @@ struct response {
size_t len;
};
/* Initalize a empty response struct */
void init_response(struct response*);
/* Initalize a empty response struct, >0 on errors. */
int init_response(struct response*);
/* Build a URL from a given hostname and a given path */
char* build_url(const char*, const char*);
......@@ -38,10 +38,10 @@ char* build_url(const char*, const char*);
/* Trim carriage return's and line feed's from a given string */
char* trimcrlf(char*);
/* GET request WithOut Response */
/* GET request WithOut Response, >0 on errors. */
int req_get_wor(const char*);
/* GET request With Response */
/* GET request With Response, >0 on errors. */
int req_get_wr(const char*, struct response*);
#endif
......@@ -38,7 +38,7 @@ char* login(struct config *c)
if (SESSION_INVALID == session_id_chk(session_id)) {
utlog(LOG_ERR, "%s\n%s\n", "Failed to login while starting a session.",
"Wrong username or password.\n");
"Maybe wrong username or password, or something else.\n");
free(session_id);
exit(EXIT_FAILURE);
}
......
......@@ -6,7 +6,7 @@
#
# The hostname/ip[:port] of the fritzbox without protocol scheme or URI paths,
# by default "fritz.box"
hostname = "gateway.lan";
hostname = "gatew4y.lan";
# The username of the fritzbox which is able to perform smart home management
# According to the documentation from AVM this setting can be empty, by default empty
username = "";
......
......@@ -7,6 +7,7 @@
#define ANSI_COLOR_RED "\x1b[31;1m"
#define ANSI_COLOR_YELLOW "\x1b[33;1m"
#define ANSI_COLOR_GREY "\x1b[1;30m"
#define ANSI_COLOR_RESET "\x1b[0m"
#define STR(s) #s
......@@ -16,9 +17,11 @@ char* strbystate(char state)
{
if (SWITCH_STATE_ON == state) {
return COLOR(ANSI_COLOR_YELLOW, on);
} else if (SWITCH_STATE_OFF) {
return COLOR(ANSI_COLOR_RED, off);
}
return COLOR(ANSI_COLOR_RED, off);
return COLOR(ANSI_COLOR_GREY, unknown);
}
int main(void)
......
......@@ -7,6 +7,7 @@
#define ANSI_COLOR_RED "\x1b[31;1m"
#define ANSI_COLOR_YELLOW "\x1b[33;1m"
#define ANSI_COLOR_GREY "\x1b[1;30m"
#define ANSI_COLOR_RESET "\x1b[0m"
#define STR(s) #s
......@@ -16,11 +17,12 @@ char* strbystate(char state)
{
if (SWITCH_STATE_ON == state) {
return COLOR(ANSI_COLOR_YELLOW, on);
} else if (SWITCH_STATE_OFF) {
return COLOR(ANSI_COLOR_RED, off);
}
return COLOR(ANSI_COLOR_RED, off);
return COLOR(ANSI_COLOR_GREY, unknown);
}
int main(void)
{
setlocale(LC_CTYPE, "");
......
......@@ -7,6 +7,7 @@
#define ANSI_COLOR_RED "\x1b[31;1m"
#define ANSI_COLOR_YELLOW "\x1b[33;1m"
#define ANSI_COLOR_GREY "\x1b[1;30m"
#define ANSI_COLOR_RESET "\x1b[0m"
#define STR(s) #s
......@@ -16,9 +17,11 @@ char* strbystate(char state)
{
if (SWITCH_STATE_ON == state) {
return COLOR(ANSI_COLOR_YELLOW, on);
} else if (SWITCH_STATE_OFF) {
return COLOR(ANSI_COLOR_RED, off);
}
return COLOR(ANSI_COLOR_RED, off);
return COLOR(ANSI_COLOR_GREY, unknown);
}
int main(void)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment