test_network.c revision da770c2324ca473ff5cf479d2960b05a284493d4
5a3bd0ef762b8a3238869ac9963194555e39e6a7jpikus/**
5a3bd0ef762b8a3238869ac9963194555e39e6a7jpikus * The contents of this file are subject to the terms of the Common Development and
5a3bd0ef762b8a3238869ac9963194555e39e6a7jpikus * Distribution License (the License). You may not use this file except in compliance with the
5a3bd0ef762b8a3238869ac9963194555e39e6a7jpikus * License.
5a3bd0ef762b8a3238869ac9963194555e39e6a7jpikus *
5a3bd0ef762b8a3238869ac9963194555e39e6a7jpikus * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
* specific language governing permission and limitations under the License.
*
* When distributing Covered Software, include this CDDL Header Notice in each file and include
* the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
* Header, with the fields enclosed by brackets [] replaced by your own identifying
* information: "Portions copyright [year] [name of copyright owner]".
*
* Copyright 2015 ForgeRock AS.
*/
#include <stdio.h>
#include <string.h>
#include <setjmp.h>
#include "platform.h"
#include "am.h"
#include "utility.h"
#include "net_client.h"
#include "thread.h"
#include "cmocka.h"
void am_net_init_ssl_reset();
static void install_log(const char *format, ...) {
char ts[64];
struct tm now;
#ifdef _WIN32
FILE *f = fopen("c:\\windows\\temp\\test.log", "a+");
time_t tv;
time(&tv);
localtime_s(&now, &tv);
#else
FILE *f = fopen("/tmp/test.log", "a+");
struct timeval tv;
gettimeofday(&tv, NULL);
localtime_r(&tv.tv_sec, &now);
#endif
strftime(ts, sizeof (ts) - 1, "%Y-%m-%d %H:%M:%S", &now);
if (f != NULL) {
va_list args;
fprintf(f, "%s ", ts);
va_start(args, format);
vfprintf(f, format, args);
va_end(args);
fprintf(f, "\n");
fclose(f);
}
}
void test_single_request(void **state) {
int rv;
const char *openam_url = "https://am.example.com:443/am";
int httpcode = 0;
am_net_init();
rv = am_url_validate(0, openam_url, NULL, &httpcode, install_log);
assert_int_equal(rv, AM_SUCCESS);
am_net_shutdown();
am_net_init_ssl_reset();
fprintf(stderr, "STATUS: %s\n", am_strerror(rv));
fprintf(stderr, "HTTP STATUS: %d\n", httpcode);
}
void test_multiple_requests(void **state) {
int rv;
const char *openam_url = "https://am.example.com:443/am";
const char *agent_user = "agent";
const char *agent_password = "password";
const char *agent_realm = "/";
char *agent_token = NULL;
am_net_init();
rv = am_agent_login(0, openam_url, NULL,
agent_user, agent_password, agent_realm, AM_TRUE, 0, NULL,
&agent_token, NULL, NULL, NULL, install_log);
fprintf(stderr, "LOGIN STATUS: %s\n", am_strerror(rv));
assert_int_equal(rv, AM_SUCCESS);
if (agent_token != NULL) {
rv = am_agent_logout(0, openam_url, agent_token,
NULL, NULL, install_log);
fprintf(stderr, "LOGOUT STATUS: %s\n", am_strerror(rv));
assert_int_equal(rv, AM_SUCCESS);
}
am_net_shutdown();
am_net_init_ssl_reset();
fprintf(stderr, "TOKEN: %s\n", LOGEMPTY(agent_token));
am_free(agent_token);
}