Example #1
0
void
test_log_messages_can_be_parsed(struct msgparse_params *param)
{
  LogMessage *parsed_message;
  LogStamp *parsed_timestamp;
  time_t now;
  GString *sd_str;

  parsed_message = _parse_log_message(param->msg, param->parse_flags, param->bad_hostname_re);
  parsed_timestamp = &(parsed_message->timestamps[LM_TS_STAMP]);

  if (param->expected_stamp_sec)
    {
      if (param->expected_stamp_sec != 1)
        cr_assert_eq(parsed_timestamp->tv_sec, param->expected_stamp_sec,
                     "Unexpected timestamp, value=%ld, expected=%ld, msg=%s",
                     parsed_timestamp->tv_sec, param->expected_stamp_sec, param->msg);

      cr_assert_eq(parsed_timestamp->tv_usec, param->expected_stamp_usec, "Unexpected microseconds");
      cr_assert_eq(parsed_timestamp->zone_offset, param->expected_stamp_ofs, "Unexpected timezone offset");
    }
  else
    {
      time(&now);
      cr_assert(_absolute_value(parsed_timestamp->tv_sec - now) <= 5,
                "Expected parsed message timestamp to be set to now; now='%d', timestamp->tv_sec='%d'",
                (gint)now, (gint)parsed_timestamp->tv_sec);
    }

  cr_assert_eq(parsed_message->pri, param->expected_pri, "Unexpected message priority");
  assert_log_message_value(parsed_message, LM_V_HOST, param->expected_host);
  assert_log_message_value(parsed_message, LM_V_PROGRAM, param->expected_program);
  assert_log_message_value(parsed_message, LM_V_MESSAGE, param->expected_msg);
  if (param->expected_pid)
    assert_log_message_value(parsed_message, LM_V_PID, param->expected_pid);
  if (param->expected_msgid)
    assert_log_message_value(parsed_message, LM_V_MSGID, param->expected_msgid);
  if (param->expected_sd_str)
    {
      sd_str = g_string_sized_new(0);
      log_msg_format_sdata(parsed_message, sd_str, 0);
      cr_assert_str_eq(sd_str->str, param->expected_sd_str, "Unexpected formatted SData");
      g_string_free(sd_str, TRUE);
    }

  assert_log_message_sdata_pairs(parsed_message, param->expected_sd_pairs);

  log_msg_unref(parsed_message);
}
Example #2
0
void
testcase(gchar *msg,
         gint parse_flags,
         gchar *bad_hostname_re,
         gint expected_pri,
         unsigned long expected_stamp_sec,
         unsigned long expected_stamp_usec,
         unsigned long expected_stamp_ofs,
         const gchar *expected_host,
         const gchar *expected_program,
         const gchar *expected_msg,
         const gchar *expected_sd_str,
         const gchar *expected_pid,
         const gchar *expected_msgid,
         const gchar *expected_sd_pairs[][2])
{
  LogMessage *parsed_message;
  LogStamp *parsed_timestamp;
  time_t now;
  GString *sd_str;

  testcase_begin("Testing log message parsing; parse_flags='%x', bad_hostname_re='%s', msg='%s'",
                 parse_flags, bad_hostname_re ? : "(null)", msg);

  parsed_message = parse_log_message(msg, parse_flags, bad_hostname_re);
  parsed_timestamp = &(parsed_message->timestamps[LM_TS_STAMP]);

  if (expected_stamp_sec)
    {
      if (expected_stamp_sec != 1)
        assert_guint(parsed_timestamp->tv_sec, expected_stamp_sec, "Unexpected timestamp");
      assert_guint32(parsed_timestamp->tv_usec, expected_stamp_usec, "Unexpected microseconds");
      assert_guint32(parsed_timestamp->zone_offset, expected_stamp_ofs, "Unexpected timezone offset");
    }
  else
    {
      time(&now);
      assert_true(absolute_value(parsed_timestamp->tv_sec - now) <= 5,
                  "Expected parsed message timestamp to be set to now; now='%d', timestamp->tv_sec='%d'",
                  (gint)now, (gint)parsed_timestamp->tv_sec, NULL);
    }
  assert_guint16(parsed_message->pri, expected_pri, "Unexpected message priority");
  assert_log_message_value(parsed_message, LM_V_HOST, expected_host);
  assert_log_message_value(parsed_message, LM_V_PROGRAM, expected_program);
  assert_log_message_value(parsed_message, LM_V_MESSAGE, expected_msg);
  if (expected_pid)
    assert_log_message_value(parsed_message, LM_V_PID, expected_pid);
  if (expected_msgid)
    assert_log_message_value(parsed_message, LM_V_MSGID, expected_msgid);
  if (expected_sd_str)
    {
      sd_str = g_string_sized_new(0);
      log_msg_format_sdata(parsed_message, sd_str, 0);
      assert_string(sd_str->str, expected_sd_str, "Unexpected formatted SData");
      g_string_free(sd_str, TRUE);
    }

  assert_log_message_sdata_pairs(parsed_message, expected_sd_pairs);

  log_msg_unref(parsed_message);

  testcase_end();
}