Пример #1
0
static void
test_nvtable_realloc_leaves_original_intact_if_there_are_multiple_references(void)
{
  NVTable *tab_ref1, *tab_ref2;
  gboolean success;
  gsize old_size;

  tab_ref1 = nv_table_new(STATIC_VALUES, STATIC_VALUES, 1024);
  tab_ref2 = nv_table_ref(tab_ref1);

  success = nv_table_add_value(tab_ref1, STATIC_HANDLE, STATIC_NAME, strlen(STATIC_NAME), "value", 5, NULL);
  TEST_ASSERT(success == TRUE);
  TEST_NVTABLE_ASSERT(tab_ref1, STATIC_HANDLE, "value", 5);
  TEST_NVTABLE_ASSERT(tab_ref2, STATIC_HANDLE, "value", 5);

  old_size = tab_ref1->size;

  TEST_ASSERT(nv_table_realloc(tab_ref2, &tab_ref2));
  TEST_ASSERT(tab_ref1->size == old_size);
  TEST_ASSERT(tab_ref2->size >= old_size);
  TEST_NVTABLE_ASSERT(tab_ref1, STATIC_HANDLE, "value", 5);
  TEST_NVTABLE_ASSERT(tab_ref2, STATIC_HANDLE, "value", 5);

  nv_table_unref(tab_ref1);
  nv_table_unref(tab_ref2);
}
Пример #2
0
int
testcase_replace(const gchar *log, const gchar *re, gchar *replacement, const gchar *expected_result, const gint matcher_flags, LogMatcher *m)
{
  LogMessage *msg;
  LogTemplate *r;
  gchar *result;
  gssize length;
  gchar buf[1024];
  gssize msglen;
  NVHandle nonasciiz = log_msg_get_value_handle("NON-ASCIIZ");
  const gchar *value;
  GSockAddr *sa;

  sa = g_sockaddr_inet_new("10.10.10.10", 1010);
  msg = log_msg_new(log, strlen(log), sa, &parse_options);
  g_sockaddr_unref(sa);

  /* NOTE: we test how our matchers cope with non-zero terminated values. We don't change message_len, only the value */

  g_snprintf(buf, sizeof(buf), "%sAAAAAAAAAAAA", log_msg_get_value(msg, LM_V_MESSAGE, &msglen));
  log_msg_set_value(msg, log_msg_get_value_handle("MESSAGE2"), buf, -1);

  /* add a non-zero terminated indirect value which contains the whole message */
  log_msg_set_value_indirect(msg, nonasciiz, log_msg_get_value_handle("MESSAGE2"), 0, 0, msglen);

  log_matcher_set_flags(m, matcher_flags);

  log_matcher_compile(m, re);

  r = log_template_new(configuration, NULL);
  log_template_compile(r, replacement, NULL);

  NVTable *nv_table = nv_table_ref(msg->payload);
  value = log_msg_get_value(msg, nonasciiz, &msglen);
  result = log_matcher_replace(m, msg, nonasciiz, value, msglen, r, &length);
  value = log_msg_get_value(msg, nonasciiz, &msglen);
  nv_table_unref(nv_table);

  if (strncmp(result ? result : value, expected_result, result ? length : msglen) != 0)
    {
      fprintf(stderr, "Testcase failure. pattern=%s, result=%.*s, expected=%s\n", re, (gint) length, result ? result : value, expected_result);
      exit(1);
    }

  g_free(result);

  log_template_unref(r);
  log_matcher_unref(m);
  log_msg_unref(msg);
  return 0;
}
Пример #3
0
static void
testcase(gchar *msg, goffset offset, gchar *timezone, gchar *format, gchar *expected)
{
  LogTemplate *templ;
  LogMessage *logmsg;
  NVTable *nvtable;
  GlobalConfig *cfg = cfg_new (0x302);
  LogParser *parser = date_parser_new (cfg);
  gboolean success;
  const gchar *context_id = "test-context-id";
  GString *res = g_string_sized_new(128);

  date_parser_set_offset(parser, offset);
  if (format != NULL) date_parser_set_format(parser, format);
  if (timezone != NULL) date_parser_set_timezone(parser, timezone);

  parse_options.flags = 0;
  logmsg = log_msg_new_empty();
  logmsg->timestamps[LM_TS_RECVD].tv_sec = 1438793384; /* Wed Aug  5 2015 */
  log_msg_set_value(logmsg, log_msg_get_value_handle("MESSAGE"), msg, -1);
  nvtable = nv_table_ref(logmsg->payload);
  success = log_parser_process(parser, &logmsg, NULL, log_msg_get_value(logmsg, LM_V_MESSAGE, NULL), -1);
  nv_table_unref(nvtable);

  if (!success)
    {
      fprintf(stderr, "unable to parse offset=%d format=%s msg=%s\n", offset, format, msg);
      exit(1);
    }

  /* Convert to ISODATE */
  templ = compile_template("${ISODATE}", FALSE);
  log_template_format(templ, logmsg, NULL, LTZ_LOCAL, 999, context_id, res);
  assert_nstring(res->str, res->len, expected, strlen(expected),
                 "incorrect date parsed msg=%s offset=%d format=%s",
                 msg, offset, format);
  log_template_unref(templ);
  g_string_free(res, TRUE);

  log_pipe_unref(&parser->super);
  log_msg_unref(logmsg);
  return;
}
Пример #4
0
int
testcase(gchar *msg, guint parse_flags, gint max_columns, guint32 flags, gchar *delimiters, gchar *quotes, gchar *null_value, gchar *first_value, ...)
{
  LogMessage *logmsg;
  LogColumnParser *p;
  gchar *expected_value;
  gint i;
  va_list va;
  NVTable *nvtable;

  const gchar *column_array[] =
  {
    "C1",
    "C2",
    "C3",
    "C4",
    "C5",
    "C6",
    "C7",
    "C8",
    "C9",
    "C10",
    "C11",
    "C12",
    "C13",
    "C14",
    "C15",
    "C16",
    "C17",
    "C18",
    "C19",
    "C20",
    "C21",
    "C22",
    "C23",
    "C24",
    "C25",
    "C26",
    "C27",
    "C28",
    "C29",
    "C30",
    NULL
  };
  gboolean success;

  if (max_columns != -1)
    {
      g_assert(max_columns < (sizeof(column_array) / sizeof(column_array[0])));

      column_array[max_columns] = NULL;
    }

  parse_options.flags = parse_flags;
  logmsg = log_msg_new(msg, strlen(msg), NULL, &parse_options);

  p = log_csv_parser_new();
  log_csv_parser_set_flags(p, flags);
  log_column_parser_set_columns(p, string_array_to_list(column_array));
  if (delimiters)
    log_csv_parser_set_delimiters(p, delimiters);
  if (quotes)
    log_csv_parser_set_quote_pairs(p, quotes);
  if (null_value)
    log_csv_parser_set_null_value(p, null_value);

  nvtable = nv_table_ref(logmsg->payload);
  success = log_parser_process(&p->super, &logmsg, NULL, log_msg_get_value(logmsg, LM_V_MESSAGE, NULL), -1);
  nv_table_unref(nvtable);

  if (success && !first_value)
    {
      fprintf(stderr, "unexpected match; msg=%s\n", msg);
      exit(1);
    }
  if (!success && first_value)
    {
      fprintf(stderr, "unexpected non-match; msg=%s\n", msg);
      exit(1);
    }
  log_pipe_unref(&p->super.super);

  va_start(va, first_value);
  expected_value = first_value;
  i = 0;
  while (expected_value && column_array[i])
    {
      const gchar *value;
      gssize value_len;

      value = log_msg_get_value(logmsg, log_msg_get_value_handle(column_array[i]), &value_len);

      if (expected_value && expected_value[0])
        {
          TEST_ASSERT(value && value[0], "expected value set, but no actual value");
          TEST_ASSERT(strlen(expected_value) == value_len, "value length doesn't match actual length");
          TEST_ASSERT(strncmp(value, expected_value, value_len) == 0, "value does not match expected value");
        }
      else
        {
          TEST_ASSERT(!(value && value[0]), "expected unset, but actual value present");
        }

      expected_value = va_arg(va, char *);
      i++;
    }

  log_msg_unref(logmsg);
  return 1;
}