aboutsummaryrefslogtreecommitdiff
path: root/src/test/log_test_helpers.c
blob: 88d28e1cc0f748b3c8a3ae02ca7c52e7e6a1a5f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* Copyright (c) 2015, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#define LOG_PRIVATE
#include "torlog.h"
#include "log_test_helpers.h"

static smartlist_t *saved_logs = NULL;

int
setup_capture_of_logs(int new_level)
{
  int previous_log = log_global_min_severity_;
  log_global_min_severity_ = new_level;
  mock_clean_saved_logs();
  MOCK(logv, mock_saving_logv);
  return previous_log;
}

void
teardown_capture_of_logs(int prev)
{
  UNMOCK(logv);
  log_global_min_severity_ = prev;
  mock_clean_saved_logs();
}

void
mock_clean_saved_logs(void)
{
  if (!saved_logs)
    return;
  SMARTLIST_FOREACH(saved_logs, mock_saved_log_entry_t *, m,
                    { tor_free(m->generated_msg); tor_free(m); });
  smartlist_free(saved_logs);
  saved_logs = NULL;
}

static mock_saved_log_entry_t *
mock_get_log_entry(int ix)
{
  int saved_log_count = mock_saved_log_number();
  if (ix < 0) {
    ix = saved_log_count + ix;
  }

  if (saved_log_count <= ix)
    return NULL;

  return smartlist_get(saved_logs, ix);
}

const char *
mock_saved_log_at(int ix)
{
  mock_saved_log_entry_t *ent = mock_get_log_entry(ix);
  if (ent)
    return ent->generated_msg;
  else
    return "";
}

int
mock_saved_severity_at(int ix)
{
  mock_saved_log_entry_t *ent = mock_get_log_entry(ix);
  if (ent)
    return ent->severity;
  else
    return -1;
}

int
mock_saved_log_number(void)
{
  if (!saved_logs)
    return 0;
  return smartlist_len(saved_logs);
}

const smartlist_t *
mock_saved_logs(void)
{
  return saved_logs;
}

int
mock_saved_log_has_message(const char *msg)
{
  int has_msg = 0;
  if (saved_logs) {
    SMARTLIST_FOREACH(saved_logs, mock_saved_log_entry_t *, m,
                      {
                        if (msg && m->generated_msg &&
                            !strcmp(msg, m->generated_msg)) {
                          has_msg = 1;
                        }
                      });
  }

  return has_msg;
}

void
mock_saving_logv(int severity, log_domain_mask_t domain,
                 const char *funcname, const char *suffix,
                 const char *format, va_list ap)
{
  (void)domain;
  char *buf = tor_malloc_zero(10240);
  int n;
  n = tor_vsnprintf(buf,10240,format,ap);
  tor_assert(n < 10240-1);
  buf[n]='\n';
  buf[n+1]='\0';

  mock_saved_log_entry_t *e = tor_malloc_zero(sizeof(mock_saved_log_entry_t));
  e->severity = severity;
  e->funcname = funcname;
  e->suffix = suffix;
  e->format = format;
  e->generated_msg = tor_strdup(buf);
  tor_free(buf);

  if (!saved_logs)
    saved_logs = smartlist_new();
  smartlist_add(saved_logs, e);
}