Blame tests/test-ot-tool-util.c

Packit Service 2a3f3d
/*
Packit Service 2a3f3d
 * Copyright (C) 2015 Red Hat, Inc.
Packit Service 2a3f3d
 *
Packit Service 2a3f3d
 * SPDX-License-Identifier: LGPL-2.0+
Packit Service 2a3f3d
 *
Packit Service 2a3f3d
 * This library is free software; you can redistribute it and/or
Packit Service 2a3f3d
 * modify it under the terms of the GNU Lesser General Public
Packit Service 2a3f3d
 * License as published by the Free Software Foundation; either
Packit Service 2a3f3d
 * version 2 of the License, or (at your option) any later version.
Packit Service 2a3f3d
 *
Packit Service 2a3f3d
 * This library is distributed in the hope that it will be useful,
Packit Service 2a3f3d
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 2a3f3d
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 2a3f3d
 * Lesser General Public License for more details.
Packit Service 2a3f3d
 *
Packit Service 2a3f3d
 * You should have received a copy of the GNU Lesser General Public
Packit Service 2a3f3d
 * License along with this library; if not, write to the
Packit Service 2a3f3d
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit Service 2a3f3d
 * Boston, MA 02111-1307, USA.
Packit Service 2a3f3d
 */
Packit Service 2a3f3d
Packit Service 2a3f3d
#include "config.h"
Packit Service 2a3f3d
#include "libglnx.h"
Packit Service 2a3f3d
#include "ostree-mutable-tree.h"
Packit Service 2a3f3d
#include <glib.h>
Packit Service 2a3f3d
#include <stdlib.h>
Packit Service 2a3f3d
#include <gio/gio.h>
Packit Service 2a3f3d
#include <string.h>
Packit Service 2a3f3d
#include "ot-tool-util.h"
Packit Service 2a3f3d
Packit Service 2a3f3d
/*
Packit Service 2a3f3d
Packit Service 2a3f3d
Packit Service 2a3f3d
gboolean
Packit Service 2a3f3d
ot_parse_boolean (const char  *value,
Packit Service 2a3f3d
                  gboolean    *out_parsed,
Packit Service 2a3f3d
                  GError     **error);
Packit Service 2a3f3d
gboolean
Packit Service 2a3f3d
ot_parse_keyvalue (const char  *keyvalue,
Packit Service 2a3f3d
                   char       **out_key,
Packit Service 2a3f3d
                   char       **out_value,
Packit Service 2a3f3d
                   GError     **error);
Packit Service 2a3f3d
*/
Packit Service 2a3f3d
static void
Packit Service 2a3f3d
test_ot_parse_boolean (void)
Packit Service 2a3f3d
{
Packit Service 2a3f3d
  g_autoptr(GError) error = NULL;
Packit Service 2a3f3d
  gboolean out = FALSE;
Packit Service 2a3f3d
  g_assert_true (ot_parse_boolean ("yes", &out, &error));
Packit Service 2a3f3d
  g_assert_true (out);
Packit Service 2a3f3d
Packit Service 2a3f3d
  out = FALSE;
Packit Service 2a3f3d
  g_assert_true (ot_parse_boolean ("1", &out, &error));
Packit Service 2a3f3d
  g_assert_true (out);
Packit Service 2a3f3d
Packit Service 2a3f3d
  out = FALSE;
Packit Service 2a3f3d
  g_assert_true (ot_parse_boolean ("true", &out, &error));
Packit Service 2a3f3d
  g_assert_true (out);
Packit Service 2a3f3d
Packit Service 2a3f3d
  g_assert_true (ot_parse_boolean ("false", &out, &error));
Packit Service 2a3f3d
  g_assert_false (out);
Packit Service 2a3f3d
Packit Service 2a3f3d
  out = TRUE;
Packit Service 2a3f3d
  g_assert_true (ot_parse_boolean ("no", &out, &error));
Packit Service 2a3f3d
  g_assert_false (out);
Packit Service 2a3f3d
Packit Service 2a3f3d
  out = TRUE;
Packit Service 2a3f3d
  g_assert_true (ot_parse_boolean ("0", &out, &error));
Packit Service 2a3f3d
  g_assert_false (out);
Packit Service 2a3f3d
Packit Service 2a3f3d
  out = TRUE;
Packit Service 2a3f3d
  g_assert_true (ot_parse_boolean ("none", &out, &error));
Packit Service 2a3f3d
  g_assert_false (out);
Packit Service 2a3f3d
  g_clear_error (&error);
Packit Service 2a3f3d
Packit Service 2a3f3d
  g_assert_false (ot_parse_boolean ("FOO", &out, &error));
Packit Service 2a3f3d
  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
Packit Service 2a3f3d
  g_clear_error (&error);
Packit Service 2a3f3d
}
Packit Service 2a3f3d
Packit Service 2a3f3d
static void
Packit Service 2a3f3d
test_ot_parse_keyvalue (void)
Packit Service 2a3f3d
{
Packit Service 2a3f3d
  g_autoptr(GError) error = NULL;
Packit Service 2a3f3d
  char *keyvalue[] = {"foo=bar", "a=", "b=1231231"};
Packit Service 2a3f3d
  char *key[] = {"foo", "a", "b"};
Packit Service 2a3f3d
  char *value[] = {"bar", "", "1231231"};
Packit Service 2a3f3d
  guint i;
Packit Service 2a3f3d
Packit Service 2a3f3d
  for (i = 0; i < G_N_ELEMENTS (keyvalue); i++)
Packit Service 2a3f3d
    {
Packit Service 2a3f3d
      g_autofree char *out_key = NULL;
Packit Service 2a3f3d
      g_autofree char *out_value = NULL;
Packit Service 2a3f3d
      g_assert_true (ot_parse_keyvalue (keyvalue[i],
Packit Service 2a3f3d
                                        &out_key,
Packit Service 2a3f3d
                                        &out_value,
Packit Service 2a3f3d
                                        &error));
Packit Service 2a3f3d
      g_assert_cmpstr (out_key, ==, key[i]);
Packit Service 2a3f3d
      g_assert_cmpstr (out_value, ==, value[i]);
Packit Service 2a3f3d
    }
Packit Service 2a3f3d
Packit Service 2a3f3d
  {
Packit Service 2a3f3d
    g_autofree char *out_key = NULL;
Packit Service 2a3f3d
    g_autofree char *out_value = NULL;
Packit Service 2a3f3d
    g_assert_false (ot_parse_keyvalue ("blabla",
Packit Service 2a3f3d
                                       &out_key,
Packit Service 2a3f3d
                                       &out_value,
Packit Service 2a3f3d
                                       &error));
Packit Service 2a3f3d
    g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
Packit Service 2a3f3d
    g_clear_error (&error);
Packit Service 2a3f3d
  }
Packit Service 2a3f3d
}
Packit Service 2a3f3d
Packit Service 2a3f3d
int main (int argc, char **argv)
Packit Service 2a3f3d
{
Packit Service 2a3f3d
  g_test_init (&argc, &argv, NULL);
Packit Service 2a3f3d
  g_test_add_func ("/ot-tool-util/parse-boolean", test_ot_parse_boolean);
Packit Service 2a3f3d
  g_test_add_func ("/ot-tool-util/parse-keyvalue", test_ot_parse_keyvalue);
Packit Service 2a3f3d
  return g_test_run();
Packit Service 2a3f3d
}