return saved_logs;
}
+int
+mock_saved_log_n_entries(void)
+{
+ return saved_logs ? smartlist_len(saved_logs) : 0;
+}
+
/**
* Return true iff there is a message recorded by log capture
* that is exactly equal to <b>msg</b>
int mock_saved_log_has_message_containing(const char *msg);
int mock_saved_log_has_severity(int severity);
int mock_saved_log_has_entry(void);
+int mock_saved_log_n_entries(void);
void mock_dump_saved_logs(void);
#define assert_log_predicate(predicate, failure_msg) \
mock_saved_log_has_message_containing(str2), \
"expected log to contain " # str1 " or " # str2);
+#define expect_single_log_msg(str) \
+ do { \
+ \
+ assert_log_predicate(mock_saved_log_has_message_containing(str) && \
+ mock_saved_log_n_entries() == 1, \
+ "expected log to contain exactly 1 message: " # str); \
+ } while (0);
+
#define expect_single_log_msg_containing(str) \
- do { \
- assert_log_predicate(mock_saved_log_has_message_containing(str), \
- "expected log to contain " # str); \
- tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1); \
+ do { \
+ assert_log_predicate(mock_saved_log_has_message_containing(str)&& \
+ mock_saved_log_n_entries() == 1 , \
+ "expected log to contain 1 message, containing" # str); \
} while (0);
#define expect_no_log_msg(str) \
#include "test_dir_common.h"
#include "torcert.h"
#include "relay.h"
+#include "log_test_helpers.h"
#define NS_MODULE dir
test_dir_fetch_type(void *arg)
{
(void)arg;
+ int log_level = 0;
+
tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_EXTRAINFO, ROUTER_PURPOSE_BRIDGE,
NULL), OP_EQ, EXTRAINFO_DIRINFO | BRIDGE_DIRINFO);
tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_EXTRAINFO, ROUTER_PURPOSE_GENERAL,
tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_MICRODESC, ROUTER_PURPOSE_GENERAL,
NULL), OP_EQ, MICRODESC_DIRINFO);
+ /* This will give a warning, because this function isn't supposed to be
+ * used for HS descriptors. */
+ log_level = setup_full_capture_of_logs(LOG_WARN);
tt_int_op(dir_fetch_type(DIR_PURPOSE_FETCH_RENDDESC_V2,
ROUTER_PURPOSE_GENERAL, NULL), OP_EQ, NO_DIRINFO);
- done: ;
+ expect_single_log_msg_containing("Unexpected purpose");
+ done:
+ if (log_level)
+ teardown_capture_of_logs(log_level);
}
static void
test_dir_conn_purpose_to_string(void *data)
{
(void)data;
+ int log_level = 0;
#define EXPECT_CONN_PURPOSE(purpose, expected) \
tt_str_op(dir_conn_purpose_to_string(purpose), OP_EQ, expected);
EXPECT_CONN_PURPOSE(DIR_PURPOSE_UPLOAD_RENDDESC_V2,
"hidden-service v2 descriptor upload");
EXPECT_CONN_PURPOSE(DIR_PURPOSE_FETCH_MICRODESC, "microdescriptor fetch");
+
+ /* This will give a warning, because there is no purpose 1024. */
+ log_level = setup_full_capture_of_logs(LOG_WARN);
EXPECT_CONN_PURPOSE(1024, "(unknown)");
+ expect_single_log_msg_containing("Called with unknown purpose 1024");
- done: ;
+ done:
+ if (log_level)
+ teardown_capture_of_logs(log_level);
}
NS_DECL(int,
#include "test.h"
#include "test_helpers.h"
+#include "log_test_helpers.h"
/** Generate a vote_routerstatus_t for a router with identity digest
* <b>digest_in_hex</b>. */
const char *guardfraction_str_bad2 = "GuardFraction=166"; /* no percentage */
routerstatus_t rs_bad2;
+ int log_level = 0;
(void) arg;
/* GuardFraction use is currently disabled by default. So we need to
memset(&rs_no_guard, 0, sizeof(routerstatus_t));
tt_assert(!rs_no_guard.is_possible_guard);
+ log_level = setup_full_capture_of_logs(LOG_WARN);
retval = routerstatus_parse_guardfraction(guardfraction_str_good,
NULL, NULL,
&rs_no_guard);
tt_int_op(retval, ==, 0);
tt_assert(!rs_no_guard.has_guardfraction);
+ expect_single_log_msg_containing("Got GuardFraction for non-guard . "
+ "This is not supposed to happen.");
+ teardown_capture_of_logs(log_level);
+ log_level = 0;
}
{ /* Bad GuardFraction. Function should fail and not apply. */
}
done:
- ;
+ if (log_level)
+ teardown_capture_of_logs(log_level);
}
/** Make sure that we use GuardFraction information when we should,
#include "config.h"
#include <openssl/rsa.h>
#include "rend_test_helpers.h"
+#include "log_test_helpers.h"
#define NS_MODULE rend_cache
static void
test_rend_cache_decrement_allocation(void *data)
{
+ int log_level = 0;
(void)data;
// Test when the cache has enough allocations
// Test when there are not enough allocations
rend_cache_total_allocation = 1;
+ log_level = setup_full_capture_of_logs(LOG_WARN);
rend_cache_decrement_allocation(2);
tt_int_op(rend_cache_total_allocation, OP_EQ, 0);
+ expect_single_log_msg_containing(
+ "Underflow in rend_cache_decrement_allocation");
+ teardown_capture_of_logs(log_level);
+ log_level = 0;
// And again
rend_cache_decrement_allocation(2);
tt_int_op(rend_cache_total_allocation, OP_EQ, 0);
done:
- (void)0;
+ if (log_level)
+ teardown_capture_of_logs(log_level);
}
static void
test_rend_cache_increment_allocation(void *data)
{
+ int log_level = 0;
(void)data;
// Test when the cache is not overflowing
// Test when there are too many allocations
rend_cache_total_allocation = SIZE_MAX-1;
+ log_level = setup_full_capture_of_logs(LOG_WARN);
rend_cache_increment_allocation(2);
tt_u64_op(rend_cache_total_allocation, OP_EQ, SIZE_MAX);
+ expect_single_log_msg_containing(
+ "Overflow in rend_cache_increment_allocation");
+ teardown_capture_of_logs(log_level);
+ log_level = 0;
// And again
rend_cache_increment_allocation(2);
tt_u64_op(rend_cache_total_allocation, OP_EQ, SIZE_MAX);
done:
- (void)0;
+ if (log_level)
+ teardown_capture_of_logs(log_level);
}
static void
HEX_DIGEST_LEN);
tt_mem_op(digest, ==, our_commit->rsa_identity, sizeof(digest));
tt_str_op(smartlist_get(tokens, 4), OP_EQ, our_commit->encoded_commit);
- tt_str_op(smartlist_get(tokens, 5), OP_EQ, our_commit->encoded_reveal);
-
+ tt_str_op(smartlist_get(tokens, 5), OP_EQ, our_commit->encoded_reveal)
+;
/* Finally, does this vote line creates a valid commit object? */
smartlist_t *args = smartlist_new();
smartlist_add(args, smartlist_get(tokens, 1));
tor_tls_t *tls;
SSL_CTX *ctx;
SSL *ssl;
- int previous_log = setup_capture_of_logs(LOG_WARN);
+ int previous_log = 0;
SSL_library_init();
SSL_load_error_strings();
tls->magic = TOR_TLS_MAGIC;
tls->ssl = ssl;
+ previous_log = setup_full_capture_of_logs(LOG_WARN);
SSL_set_state(ssl, SSL3_ST_SW_SRVR_HELLO_A);
mock_clean_saved_logs();
tor_tls_server_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 0);
- expect_log_msg("Couldn't look up the tls for an SSL*. How odd!\n");
+ expect_single_log_msg("Couldn't look up the tls for an SSL*. How odd!\n");
SSL_set_state(ssl, SSL3_ST_SW_SRVR_HELLO_B);
mock_clean_saved_logs();
tor_tls_server_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 0);
- expect_log_msg("Couldn't look up the tls for an SSL*. How odd!\n");
+ expect_single_log_msg("Couldn't look up the tls for an SSL*. How odd!\n");
SSL_set_state(ssl, 99);
mock_clean_saved_logs();
tor_tls_server_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 0);
expect_no_log_entry();
+ teardown_capture_of_logs(previous_log);
+ previous_log = 0;
SSL_set_ex_data(tls->ssl, tor_tls_object_ex_data_index, tls);
SSL_set_state(ssl, SSL3_ST_SW_SRVR_HELLO_B);
tt_int_op(tls->wasV2Handshake, OP_EQ, 0);
done:
- teardown_capture_of_logs(previous_log);
+ if (previous_log)
+ teardown_capture_of_logs(previous_log);
SSL_free(ssl);
SSL_CTX_free(ctx);
tor_free(tls);
X509 *c1 = read_cert_from(validCertString);
SESS_CERT_local *sess = NULL;
+ int log_level = 0;
ctx = SSL_CTX_new(method);
tls->isServer = 1;
tls->wasV2Handshake = 0;
+ log_level = setup_full_capture_of_logs(LOG_WARN);
ret = tor_tls_finish_handshake(tls);
tt_int_op(ret, OP_EQ, 0);
tt_int_op(tls->wasV2Handshake, OP_EQ, 1);
+ expect_single_log_msg_containing("For some reason, wasV2Handshake didn't "
+ "get set.");
+ teardown_capture_of_logs(log_level);
+ log_level = 0;
tls->wasV2Handshake = 1;
ret = tor_tls_finish_handshake(tls);
tor_free(tls);
SSL_CTX_free(ctx);
tor_free(method);
+ if (log_level)
+ teardown_capture_of_logs(log_level);
}
#endif
{
(void)ignored;
waitpid_callback_t *res1 = NULL, *res2 = NULL;
- int previous_log = setup_capture_of_logs(LOG_WARN);
+ int previous_log = setup_full_capture_of_logs(LOG_WARN);
pid_t pid = (pid_t)42;
res1 = set_waitpid_callback(pid, temp_callback, NULL);
res2 = set_waitpid_callback(pid, temp_callback, NULL);
tt_assert(res2);
- expect_log_msg("Replaced a waitpid monitor on pid 42. That should be "
+ expect_single_log_msg(
+ "Replaced a waitpid monitor on pid 42. That should be "
"impossible.\n");
done: