Beispiel #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);
}
Beispiel #2
0
static void
test_nvtable_clone_grows_the_cloned_structure(void)
{
  NVTable *tab, *tab_clone;
  gboolean success;

  tab = nv_table_new(STATIC_VALUES, STATIC_VALUES, 64);
  success = nv_table_add_value(tab, STATIC_HANDLE, STATIC_NAME, strlen(STATIC_NAME), "value", 5, NULL);
  TEST_ASSERT(success == TRUE);
  TEST_NVTABLE_ASSERT(tab, STATIC_HANDLE, "value", 5);

  tab_clone = nv_table_clone(tab, 64);
  TEST_NVTABLE_ASSERT(tab_clone, STATIC_HANDLE, "value", 5);
  TEST_ASSERT(tab->size < tab_clone->size);
  nv_table_unref(tab_clone);
  nv_table_unref(tab);
}
Beispiel #3
0
static void
test_nvtable_clone_cannot_grow_nvtable_larger_than_nvtable_max_bytes(void)
{
  NVTable *tab, *tab_clone;
  gboolean success;

  tab = nv_table_new(STATIC_VALUES, STATIC_VALUES, NV_TABLE_MAX_BYTES - 1024);
  success = nv_table_add_value(tab, STATIC_HANDLE, STATIC_NAME, strlen(STATIC_NAME), "value", 5, NULL);
  TEST_ASSERT(success == TRUE);
  TEST_NVTABLE_ASSERT(tab, STATIC_HANDLE, "value", 5);

  tab_clone = nv_table_clone(tab, 2048);
  TEST_NVTABLE_ASSERT(tab_clone, STATIC_HANDLE, "value", 5);
  TEST_ASSERT(tab->size < tab_clone->size);
  TEST_ASSERT(tab_clone->size <= NV_TABLE_MAX_BYTES);
  nv_table_unref(tab_clone);
  nv_table_unref(tab);
}
Beispiel #4
0
/*
 *
 *  - other corner cases
 *    - set zero length value in direct value
 *      - add a zero-length value to non-existing entry
 *      - add a zero-length value to an existing entry that is not zero-length
 *      - add a zero-length value to an existing entry that is zero-length
 *    - set zero length value in an indirect value
 *
 * - change an entry that is referenced by other entries
 */
static void
test_nvtable_others(void)
{
  NVTable *tab;
  NVHandle handle;
  gchar value[1024], name[16];
  gboolean success;
  gint i;

  for (i = 0; i < sizeof(value); i++)
    value[i] = 'A' + (i % 26);

  handle = DYN_HANDLE+1;
  g_snprintf(name, sizeof(name), "VAL%d", handle);
  fprintf(stderr, "Testing other cases, name: %s, handle: %d\n", name, handle);

  /* one that fits */
  tab = nv_table_new(STATIC_VALUES, STATIC_VALUES, 256);
  success = nv_table_add_value(tab, STATIC_HANDLE, STATIC_NAME, 4, value, 128, NULL);
  TEST_ASSERT(success == TRUE);

  success = nv_table_add_value_indirect(tab, handle, name, strlen(name), STATIC_HANDLE, 0, 1, 126, NULL);
  TEST_ASSERT(success == TRUE);
  success = nv_table_add_value(tab, STATIC_HANDLE, STATIC_NAME, 4, value + 32, 32, NULL);
  TEST_ASSERT(success == TRUE);

  TEST_NVTABLE_ASSERT(tab, STATIC_HANDLE, value + 32, 32);
  TEST_NVTABLE_ASSERT(tab, handle, value + 1, 126);
  nv_table_unref(tab);

  /* one that doesn't fit */
  tab = nv_table_new(STATIC_VALUES, STATIC_VALUES, 192);
  success = nv_table_add_value(tab, STATIC_HANDLE, STATIC_NAME, 4, value, 128, NULL);
  TEST_ASSERT(success == TRUE);

  success = nv_table_add_value_indirect(tab, handle, name, strlen(name), STATIC_HANDLE, 0, 1, 126, NULL);
  TEST_ASSERT(success == TRUE);
  success = nv_table_add_value(tab, STATIC_HANDLE, STATIC_NAME, 4, value + 32, 32, NULL);
  TEST_ASSERT(success == FALSE);

  TEST_NVTABLE_ASSERT(tab, STATIC_HANDLE, value, 128);
  TEST_NVTABLE_ASSERT(tab, handle, value + 1, 126);
  nv_table_unref(tab);
}
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;
}
Beispiel #6
0
static void
test_nvtable_realloc_fails_if_size_is_at_maximum(void)
{
  NVTable *tab;
  gboolean success;

  tab = nv_table_new(STATIC_VALUES, STATIC_VALUES, NV_TABLE_MAX_BYTES);
  success = nv_table_add_value(tab, STATIC_HANDLE, STATIC_NAME, strlen(STATIC_NAME), "value", 5, NULL);
  TEST_ASSERT(success == TRUE);
  TEST_NVTABLE_ASSERT(tab, STATIC_HANDLE, "value", 5);

  TEST_ASSERT(nv_table_realloc(tab, &tab) == FALSE);
  TEST_ASSERT(tab->size == NV_TABLE_MAX_BYTES);
  TEST_NVTABLE_ASSERT(tab, STATIC_HANDLE, "value", 5);

  nv_table_unref(tab);
}
Beispiel #7
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;
}
Beispiel #8
0
static void
test_nvtable_lookup()
{
  NVTable *tab;
  NVHandle handle;
  gchar name[16];
  gboolean success;
  gint i;
  NVHandle handles[100];
  gint x;

  srand(time(NULL));
  for (x = 0; x < 100; x++)
    {
      /* test dynamic lookup */
      tab = nv_table_new(STATIC_VALUES, STATIC_VALUES, 4096);

      for (i = 0; i < 100; i++)
        {
          do
            {
              handle = rand() & 0x8FFF;
            }
          while (handle == 0);
          g_snprintf(name, sizeof(name), "VAL%d", handle);
          success = nv_table_add_value(tab, handle, name, strlen(name), name, strlen(name), NULL);
          TEST_ASSERT(success == TRUE);
          handles[i] = handle;
        }

      for (i = 99; i >= 0; i--)
        {
          handle = handles[i];
          g_snprintf(name, sizeof(name), "VAL%d", handle);
          TEST_NVTABLE_ASSERT(tab, handles[i], name, strlen(name));
          TEST_ASSERT(nv_table_is_value_set(tab, handles[i]));

        }
      TEST_ASSERT(nv_table_is_value_set(tab, 0xFE00) == FALSE);
      nv_table_unref(tab);
    }
}
Beispiel #9
0
static void
test_nvtable_realloc_sets_size_to_nv_table_max_bytes_at_most(void)
{
  NVTable *tab;
  gboolean success;
  gsize old_size;

  tab = nv_table_new(STATIC_VALUES, STATIC_VALUES, NV_TABLE_MAX_BYTES - 1024);
  success = nv_table_add_value(tab, STATIC_HANDLE, STATIC_NAME, strlen(STATIC_NAME), "value", 5, NULL);
  TEST_ASSERT(success == TRUE);
  TEST_NVTABLE_ASSERT(tab, STATIC_HANDLE, "value", 5);

  old_size = tab->size;

  TEST_ASSERT(nv_table_realloc(tab, &tab));
  TEST_ASSERT(tab->size > old_size);
  TEST_ASSERT(tab->size <= NV_TABLE_MAX_BYTES);
  TEST_NVTABLE_ASSERT(tab, STATIC_HANDLE, "value", 5);

  nv_table_unref(tab);
}
Beispiel #10
0
static void
test_nvtable_realloc_doubles_nvtable_size(void)
{
  NVTable *tab;
  gboolean success;
  gsize old_size;

  tab = nv_table_new(STATIC_VALUES, STATIC_VALUES, 1024);
  success = nv_table_add_value(tab, STATIC_HANDLE, STATIC_NAME, strlen(STATIC_NAME), "value", 5, NULL);
  TEST_ASSERT(success == TRUE);
  TEST_NVTABLE_ASSERT(tab, STATIC_HANDLE, "value", 5);

  old_size = tab->size;

  TEST_ASSERT(nv_table_realloc(tab, &tab));
  TEST_ASSERT(tab->size >= old_size * 2);
  TEST_NVTABLE_ASSERT(tab, STATIC_HANDLE, "value", 5);

  nv_table_unref(tab);

}
Beispiel #11
0
/*
 *  - indirect values
 *    - indirect static values are not possible
 *    - set/get dynamic NV entries that refer to direct entries
 *      - new NV entry
 *        - entries that fit into the current NVTable
 *        - entries that do not fit into the current NVTable
 *      - overwrite indirect NV entry
 *        - value always fits to a current NV entry, no other cases
 *      - overwrite direct NV entry
 *        - value that fits into the current entry
 *        - value that doesn't fit into the current entry, but fits into NVTable
 *        - value that doesn't fit into the current entry and neither to NVTable
 *    - set/get dynamic NV entries that refer to indirect entry, they become direct entries
 *      - new NV entry
 *        - entries that fit into the current NVTable
 *        - entries that do not fit into the current NVTable
 *      - overwrite indirect NV entry
 *        - value that fits into the current entry
 *        - value that doesn't fit into the current entry, but fits into NVTable
 *        - value that doesn't fit into the current entry and neither to NVTable
 *      - overwrite direct NV entry
 *        - value that fits into the current entry
 *        - value that doesn't fit into the current entry, but fits into NVTable
 *        - value that doesn't fit into the current entry and neither to NVTable
 *    - set/get dynamic NV entries that refer to a non-existant entry
 *        -
 */
static void
test_nvtable_indirect()
{
  NVTable *tab;
  NVHandle handle;
  gchar value[1024], name[16];
  gboolean success;
  gint i;
  guint16 used;

  for (i = 0; i < sizeof(value); i++)
    value[i] = 'A' + (i % 26);

  handle = DYN_HANDLE+1;
  g_snprintf(name, sizeof(name), "VAL%d", handle);
  fprintf(stderr, "Testing indirect values, name: %s, handle: %d\n", name, handle);

  /*************************************************************/
  /*************************************************************/
  /* indirect entries that refer to direct entries */
  /*************************************************************/
  /*************************************************************/

  /*************************************************************/
  /* new NV entry */
  /*************************************************************/

  /* one that fits */
  tab = nv_table_new(STATIC_VALUES, STATIC_VALUES, 192);
  success = nv_table_add_value(tab, STATIC_HANDLE, STATIC_NAME, 4, value, 128, NULL);
  TEST_ASSERT(success == TRUE);

  success = nv_table_add_value_indirect(tab, handle, name, strlen(name), STATIC_HANDLE, 0, 1, 126, NULL);
  TEST_ASSERT(success == TRUE);
  TEST_NVTABLE_ASSERT(tab, STATIC_HANDLE, value, 128);
  TEST_NVTABLE_ASSERT(tab, handle, value + 1, 126);
  nv_table_unref(tab);

  /* one that is too large */

  /* NOTE: the sizing of the NVTable can easily be broken, it is sized
     to make it possible to store one direct entry */

  tab = nv_table_new(STATIC_VALUES, 0, 138+3);  // direct: +3
  success = nv_table_add_value(tab, STATIC_HANDLE, STATIC_NAME, 4, value, 128, NULL);
  TEST_ASSERT(success == TRUE);

  success = nv_table_add_value_indirect(tab, handle, name, strlen(name), STATIC_HANDLE, 0, 1, 126, NULL);
  TEST_ASSERT(success == FALSE);

  nv_table_unref(tab);

  /*************************************************************/
  /* overwrite NV entry */
  /*************************************************************/

  tab = nv_table_new(STATIC_VALUES, STATIC_VALUES, 192);
  success = nv_table_add_value(tab, STATIC_HANDLE, STATIC_NAME, 4, value, 128, NULL);
  TEST_ASSERT(success == TRUE);
  success = nv_table_add_value_indirect(tab, handle, name, strlen(name), STATIC_HANDLE, 0, 1, 126, NULL);
  TEST_ASSERT(success == TRUE);
  used = tab->used;

  success = nv_table_add_value_indirect(tab, handle, name, strlen(name), STATIC_HANDLE, 0, 1, 62, NULL);

  TEST_ASSERT(success == TRUE);
  TEST_ASSERT(used == tab->used);
  TEST_NVTABLE_ASSERT(tab, STATIC_HANDLE, value, 128);
  TEST_NVTABLE_ASSERT(tab, handle, value + 1, 62);
  nv_table_unref(tab);


  /*************************************************************/
  /* overwrite direct entry */
  /*************************************************************/

  /* the new entry fits to the space allocated to the old */

  /* setup code: add static and a dynamic-direct entry */
  tab = nv_table_new(STATIC_VALUES, STATIC_VALUES, 256);
  success = nv_table_add_value(tab, STATIC_HANDLE, STATIC_NAME, 4, value, 128, NULL);
  TEST_ASSERT(success == TRUE);
  success = nv_table_add_value(tab, handle, name, strlen(name), value, 64, NULL);
  TEST_ASSERT(success == TRUE);
  used = tab->used;

  success = nv_table_add_value_indirect(tab, handle, name, strlen(name), STATIC_HANDLE, 0, 1, 126, NULL);
  TEST_ASSERT(success == TRUE);
  TEST_ASSERT(tab->used == used);
  TEST_NVTABLE_ASSERT(tab, STATIC_HANDLE, value, 128);
  TEST_NVTABLE_ASSERT(tab, handle, value + 1, 126);

  nv_table_unref(tab);

  /* the new entry will not fit to the space allocated to the old */

  /* setup code: add static and a dynamic-direct entry */
  tab = nv_table_new(STATIC_VALUES, STATIC_VALUES, 256);
  success = nv_table_add_value(tab, STATIC_HANDLE, STATIC_NAME, 4, value, 128, NULL);
  TEST_ASSERT(success == TRUE);
  success = nv_table_add_value(tab, handle, name, strlen(name), value, 1, NULL);
  TEST_ASSERT(success == TRUE);
  used = tab->used;

  success = nv_table_add_value_indirect(tab, handle, name, strlen(name), STATIC_HANDLE, 0, 1, 126, NULL);
  TEST_ASSERT(success == TRUE);
  TEST_ASSERT(tab->used > used);
  TEST_NVTABLE_ASSERT(tab, STATIC_HANDLE, value, 128);
  TEST_NVTABLE_ASSERT(tab, handle, value + 1, 126);

  nv_table_unref(tab);

  /* the new entry will not fit to the space allocated to the old and neither to the NVTable */

  /* setup code: add static and a dynamic-direct entry */
  tab = nv_table_new(STATIC_VALUES, 1, 154+3+4);  // direct: +3, indirect: +4
  success = nv_table_add_value(tab, STATIC_HANDLE, STATIC_NAME, 4, value, 128, NULL);
  TEST_ASSERT(success == TRUE);
  success = nv_table_add_value(tab, handle, name, strlen(name), value, 1, NULL);
  TEST_ASSERT(success == TRUE);

  success = nv_table_add_value_indirect(tab, handle, name, strlen(name), STATIC_HANDLE, 0, 1, 126, NULL);
  TEST_ASSERT(success == FALSE);
  TEST_NVTABLE_ASSERT(tab, STATIC_HANDLE, value, 128);
  TEST_NVTABLE_ASSERT(tab, handle, value, 1);

  nv_table_unref(tab);

  /*************************************************************/
  /*************************************************************/
  /* indirect entries that refer to indirect entries */
  /*************************************************************/
  /*************************************************************/


  /*************************************************************/
  /* new NV entry */
  /*************************************************************/

  /* one that fits */
  tab = nv_table_new(STATIC_VALUES, STATIC_VALUES, 256);
  success = nv_table_add_value(tab, STATIC_HANDLE, STATIC_NAME, 4, value, 128, NULL);
  TEST_ASSERT(success == TRUE);
  success = nv_table_add_value_indirect(tab, DYN_HANDLE, DYN_NAME, strlen(DYN_NAME), STATIC_HANDLE, 0, 1, 126, NULL);
  TEST_ASSERT(success == TRUE);

  success = nv_table_add_value_indirect(tab, handle, name, strlen(name), DYN_HANDLE, 0, 1, 122, NULL);
  TEST_ASSERT(success == TRUE);
  TEST_NVTABLE_ASSERT(tab, STATIC_HANDLE, value, 128);
  TEST_NVTABLE_ASSERT(tab, DYN_HANDLE, value + 1, 126);
  TEST_NVTABLE_ASSERT(tab, handle, value + 2, 122);
  nv_table_unref(tab);

  /* one that is too large */
  tab = nv_table_new(STATIC_VALUES, STATIC_VALUES, 192);
  success = nv_table_add_value(tab, STATIC_HANDLE, STATIC_NAME, 4, value, 128, NULL);
  TEST_ASSERT(success == TRUE);
  success = nv_table_add_value_indirect(tab, DYN_HANDLE, DYN_NAME, strlen(DYN_NAME), STATIC_HANDLE, 0, 1, 126, NULL);
  TEST_ASSERT(success == TRUE);

  success = nv_table_add_value_indirect(tab, handle, name, strlen(name), DYN_HANDLE, 0, 1, 122, NULL);
  TEST_ASSERT(success == FALSE);
  TEST_NVTABLE_ASSERT(tab, STATIC_HANDLE, value, 128);
  TEST_NVTABLE_ASSERT(tab, DYN_HANDLE, value + 1, 126);
  TEST_NVTABLE_ASSERT(tab, handle, "", 0);
  nv_table_unref(tab);

  /*************************************************************/
  /* overwrite indirect NV entry */
  /*************************************************************/


  /* we fit to the space of the old */
  tab = nv_table_new(STATIC_VALUES, STATIC_VALUES, 256);
  success = nv_table_add_value(tab, STATIC_HANDLE, STATIC_NAME, 4, value, 128, NULL);
  TEST_ASSERT(success == TRUE);
  success = nv_table_add_value_indirect(tab, DYN_HANDLE, DYN_NAME, strlen(DYN_NAME), STATIC_HANDLE, 0, 1, 126, NULL);
  TEST_ASSERT(success == TRUE);
  success = nv_table_add_value_indirect(tab, handle, name, strlen(name), STATIC_HANDLE, 0, 1, 126, NULL);
  TEST_ASSERT(success == TRUE);
  used = tab->used;

  success = nv_table_add_value_indirect(tab, handle, name, strlen(name), DYN_HANDLE, 0, 1, 1, NULL);
  TEST_ASSERT(success == TRUE);

  TEST_ASSERT(tab->used == used);
  TEST_NVTABLE_ASSERT(tab, STATIC_HANDLE, value, 128);
  TEST_NVTABLE_ASSERT(tab, DYN_HANDLE, value + 1, 126);
  TEST_NVTABLE_ASSERT(tab, handle, value + 2, 1);
  nv_table_unref(tab);

  /* the new entry will not fit to the space allocated to the old */

  tab = nv_table_new(STATIC_VALUES, STATIC_VALUES, 256);
  success = nv_table_add_value(tab, STATIC_HANDLE, STATIC_NAME, 4, value, 128, NULL);
  TEST_ASSERT(success == TRUE);
  success = nv_table_add_value_indirect(tab, DYN_HANDLE, DYN_NAME, strlen(DYN_NAME), STATIC_HANDLE, 0, 1, 126, NULL);
  TEST_ASSERT(success == TRUE);
  success = nv_table_add_value_indirect(tab, handle, name, strlen(name), STATIC_HANDLE, 0, 1, 126, NULL);
  TEST_ASSERT(success == TRUE);
  used = tab->used;

  success = nv_table_add_value_indirect(tab, handle, name, strlen(name), DYN_HANDLE, 0, 1, 16, NULL);
  TEST_ASSERT(success == TRUE);

  TEST_ASSERT(tab->used > used);
  TEST_NVTABLE_ASSERT(tab, STATIC_HANDLE, value, 128);
  TEST_NVTABLE_ASSERT(tab, DYN_HANDLE, value + 1, 126);
  TEST_NVTABLE_ASSERT(tab, handle, value + 2, 16);
  nv_table_unref(tab);

  /* one that is too large */

  tab = nv_table_new(STATIC_VALUES, 4, 256);
  success = nv_table_add_value(tab, STATIC_HANDLE, STATIC_NAME, 4, value, 128, NULL);
  TEST_ASSERT(success == TRUE);
  success = nv_table_add_value_indirect(tab, DYN_HANDLE, DYN_NAME, strlen(DYN_NAME), STATIC_HANDLE, 0, 1, 126, NULL);
  TEST_ASSERT(success == TRUE);
  success = nv_table_add_value_indirect(tab, handle, name, strlen(name), STATIC_HANDLE, 0, 1, 126, NULL);
  TEST_ASSERT(success == TRUE);
  used = tab->used;

  success = nv_table_add_value_indirect(tab, handle, name, strlen(name), DYN_HANDLE, 0, 1, 124, NULL);
  TEST_ASSERT(success == FALSE);

  TEST_ASSERT(tab->used == used);
  TEST_NVTABLE_ASSERT(tab, STATIC_HANDLE, value, 128);
  TEST_NVTABLE_ASSERT(tab, DYN_HANDLE, value + 1, 126);
  TEST_NVTABLE_ASSERT(tab, handle, value + 1, 126);
  nv_table_unref(tab);

  /*************************************************************/
  /* overwrite direct NV entry */
  /*************************************************************/


  /* we fit to the space of the old */
  tab = nv_table_new(STATIC_VALUES, STATIC_VALUES, 256);
  success = nv_table_add_value(tab, STATIC_HANDLE, STATIC_NAME, 4, value, 128, NULL);
  TEST_ASSERT(success == TRUE);
  success = nv_table_add_value_indirect(tab, DYN_HANDLE, DYN_NAME, strlen(DYN_NAME), STATIC_HANDLE, 0, 1, 126, NULL);
  TEST_ASSERT(success == TRUE);
  success = nv_table_add_value(tab, handle, name, strlen(name), value, 64, NULL);
  TEST_ASSERT(success == TRUE);
  used = tab->used;

  success = nv_table_add_value_indirect(tab, handle, name, strlen(name), DYN_HANDLE, 0, 1, 16, NULL);
  TEST_ASSERT(success == TRUE);

  TEST_ASSERT(tab->used == used);
  TEST_NVTABLE_ASSERT(tab, STATIC_HANDLE, value, 128);
  TEST_NVTABLE_ASSERT(tab, DYN_HANDLE, value + 1, 126);
  TEST_NVTABLE_ASSERT(tab, handle, value + 2, 16);
  nv_table_unref(tab);


  /* the new entry will not fit to the space allocated to the old */

  tab = nv_table_new(STATIC_VALUES, STATIC_VALUES, 256);
  success = nv_table_add_value(tab, STATIC_HANDLE, STATIC_NAME, 4, value, 128, NULL);
  TEST_ASSERT(success == TRUE);
  success = nv_table_add_value_indirect(tab, DYN_HANDLE, DYN_NAME, strlen(DYN_NAME), STATIC_HANDLE, 0, 1, 126, NULL);
  TEST_ASSERT(success == TRUE);
  success = nv_table_add_value(tab, handle, name, strlen(name), value, 16, NULL);
  TEST_ASSERT(success == TRUE);
  used = tab->used;

  success = nv_table_add_value_indirect(tab, handle, name, strlen(name), DYN_HANDLE, 0, 1, 32, NULL);
  TEST_ASSERT(success == TRUE);

  TEST_ASSERT(tab->used > used);
  TEST_NVTABLE_ASSERT(tab, STATIC_HANDLE, value, 128);
  TEST_NVTABLE_ASSERT(tab, DYN_HANDLE, value + 1, 126);
  TEST_NVTABLE_ASSERT(tab, handle, value + 2, 32);
  nv_table_unref(tab);

  /* one that is too large */

  tab = nv_table_new(STATIC_VALUES, 4, 256);
  success = nv_table_add_value(tab, STATIC_HANDLE, STATIC_NAME, 4, value, 128, NULL);
  TEST_ASSERT(success == TRUE);
  success = nv_table_add_value_indirect(tab, DYN_HANDLE, DYN_NAME, strlen(DYN_NAME), STATIC_HANDLE, 0, 1, 126, NULL);
  TEST_ASSERT(success == TRUE);
  success = nv_table_add_value(tab, handle, name, strlen(name), value, 16, NULL);
  TEST_ASSERT(success == TRUE);
  used = tab->used;

  success = nv_table_add_value_indirect(tab, handle, name, strlen(name), DYN_HANDLE, 0, 1, 124, NULL);
  TEST_ASSERT(success == FALSE);

  TEST_ASSERT(tab->used == used);
  TEST_NVTABLE_ASSERT(tab, STATIC_HANDLE, value, 128);
  TEST_NVTABLE_ASSERT(tab, DYN_HANDLE, value + 1, 126);
  TEST_NVTABLE_ASSERT(tab, handle, value, 16);
  nv_table_unref(tab);

  /*************************************************************/
  /* indirect that refers to non-existant entry */
  /*************************************************************/
  /* one that fits */
  tab = nv_table_new(STATIC_VALUES, STATIC_VALUES, 192);
  used = tab->used;

  success = nv_table_add_value_indirect(tab, handle, name, strlen(name), STATIC_HANDLE, 0, 1, 126, NULL);
  TEST_ASSERT(success == TRUE);
  TEST_ASSERT(used == tab->used);
  TEST_NVTABLE_ASSERT(tab, STATIC_HANDLE, "", 0);
  TEST_NVTABLE_ASSERT(tab, handle, "", 0);
  nv_table_unref(tab);
}
Beispiel #12
0
static void
test_nvtable_direct()
{
  NVTable *tab;
  NVHandle handle;
  gchar value[1024], name[16];
  gboolean success;
  gint i;
  guint16 used;

  for (i = 0; i < sizeof(value); i++)
    value[i] = 'A' + (i % 26);

  handle = STATIC_HANDLE;
  do
    {
      g_snprintf(name, sizeof(name), "VAL%d", handle);
      fprintf(stderr, "Testing direct values, name: %s, handle: %d\n", name, handle);

      /*************************************************************/
      /* new NV entry */
      /*************************************************************/

      /* one that fits */
      tab = nv_table_new(STATIC_VALUES, STATIC_VALUES, 256);
      success = nv_table_add_value(tab, handle, name, strlen(name), value, 128, NULL);
      TEST_ASSERT(success == TRUE);
      TEST_NVTABLE_ASSERT(tab, handle, value, 128);
      nv_table_unref(tab);

      /* one that is too large */
      tab = nv_table_new(STATIC_VALUES, STATIC_VALUES, 256);
      success = nv_table_add_value(tab, handle, name, strlen(name), value, 512, NULL);
      TEST_ASSERT(success == FALSE);
      nv_table_unref(tab);

      /*************************************************************/
      /* overwrite NV entry */
      /*************************************************************/

      /* one that fits, but realloced size wouldn't fit */
      tab = nv_table_new(STATIC_VALUES, STATIC_VALUES, 192);
      success = nv_table_add_value(tab, handle, name, strlen(name), value, 128, NULL);
      TEST_ASSERT(success == TRUE);
      used = tab->used;

      success = nv_table_add_value(tab, handle, name, strlen(name), value, 64, NULL);
      TEST_ASSERT(success == TRUE);
      TEST_ASSERT(tab->used == used);
      TEST_NVTABLE_ASSERT(tab, handle, value, 64);
      nv_table_unref(tab);

      /* one that is too large for the given entry, but still fits in the NVTable */
      tab = nv_table_new(STATIC_VALUES, STATIC_VALUES, 256);
      success = nv_table_add_value(tab, handle, name, strlen(name), value, 64, NULL);
      TEST_ASSERT(success == TRUE);
      used = tab->used;

      success = nv_table_add_value(tab, handle, name, strlen(name), value, 128, NULL);
      TEST_ASSERT(success == TRUE);
      TEST_ASSERT(tab->used > used);
      TEST_NVTABLE_ASSERT(tab, handle, value, 128);
      nv_table_unref(tab);

      /* one that is too large for the given entry, and also for the NVTable */
      tab = nv_table_new(STATIC_VALUES, STATIC_VALUES, 256);
      success = nv_table_add_value(tab, handle, name, strlen(name), value, 64, NULL);
      TEST_ASSERT(success == TRUE);

      success = nv_table_add_value(tab, handle, name, strlen(name), value, 512, NULL);
      TEST_ASSERT(success == FALSE);
      TEST_NVTABLE_ASSERT(tab, handle, value, 64);
      nv_table_unref(tab);

      /*************************************************************/
      /* overwrite indirect entry */
      /*************************************************************/

      if (handle > STATIC_VALUES)
        {
          /* we can only test this with dynamic entries */

          /* setup code: add static and a dynamic-indirect entry */
          tab = nv_table_new(STATIC_VALUES, STATIC_VALUES, 192);
          success = nv_table_add_value(tab, STATIC_HANDLE, STATIC_NAME, 4, value, 128, NULL);
          TEST_ASSERT(success == TRUE);
          success = nv_table_add_value_indirect(tab, handle, name, strlen(name), STATIC_HANDLE, 0, 1, 126, NULL);
          TEST_ASSERT(success == TRUE);
          used = tab->used;

          /* store a direct entry over the indirect one */
          success = nv_table_add_value(tab, handle, name, strlen(name), value, 1, NULL);
          TEST_ASSERT(success == TRUE);
          TEST_ASSERT(tab->used == used);
          TEST_NVTABLE_ASSERT(tab, STATIC_HANDLE, value, 128);
          TEST_NVTABLE_ASSERT(tab, handle, value, 1);

          nv_table_unref(tab);

          /* setup code: add static and a dynamic-indirect entry */
          tab = nv_table_new(STATIC_VALUES, STATIC_VALUES, 256);
          success = nv_table_add_value(tab, STATIC_HANDLE, STATIC_NAME, 4, value, 64, NULL);
          TEST_ASSERT(success == TRUE);
          success = nv_table_add_value_indirect(tab, handle, name, strlen(name), STATIC_HANDLE, 0, 1, 63, NULL);
          TEST_ASSERT(success == TRUE);

          used = tab->used;

          /* store a direct entry over the indirect one, we don't fit in the allocated space */
          success = nv_table_add_value(tab, handle, name, strlen(name), value, 128, NULL);
          TEST_ASSERT(success == TRUE);
          TEST_ASSERT(tab->used > used);
          TEST_NVTABLE_ASSERT(tab, STATIC_HANDLE, value, 64);
          TEST_NVTABLE_ASSERT(tab, handle, value, 128);

          nv_table_unref(tab);

          /* setup code: add static and a dynamic-indirect entry */
          tab = nv_table_new(STATIC_VALUES, STATIC_VALUES, 256);
          success = nv_table_add_value(tab, STATIC_HANDLE, STATIC_NAME, 4, value, 64, NULL);
          TEST_ASSERT(success == TRUE);
          success = nv_table_add_value_indirect(tab, handle, name, strlen(name), STATIC_HANDLE, 0, 1, 62, NULL);
          TEST_ASSERT(success == TRUE);
          used = tab->used;

          /* store a direct entry over the indirect one, we don't fit in the allocated space */
          success = nv_table_add_value(tab, handle, name, strlen(name), value, 256, NULL);
          TEST_ASSERT(success == FALSE);
          TEST_NVTABLE_ASSERT(tab, STATIC_HANDLE, value, 64);
          TEST_NVTABLE_ASSERT(tab, handle, value + 1, 62);

          nv_table_unref(tab);

        }

      handle += STATIC_VALUES;
    }
  while (handle < 2 * STATIC_VALUES);
}
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;
}