File: test-backend-settings.c

package info (click to toggle)
libguestfs 1%3A1.44.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 118,932 kB
  • sloc: ansic: 458,017; ml: 51,424; sh: 13,191; java: 9,578; makefile: 7,931; cs: 6,328; haskell: 5,674; python: 3,871; perl: 3,528; erlang: 2,446; xml: 1,347; ruby: 350; pascal: 257; javascript: 157; lex: 135; yacc: 128; cpp: 10
file content (146 lines) | stat: -rw-r--r-- 4,224 bytes parent folder | download | duplicates (4)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* libguestfs
 * Copyright (C) 2014 Red Hat Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/* Test backend settings API. */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <error.h>
#include <assert.h>

#include "guestfs.h"
#include "guestfs-utils.h"

int
main (int argc, char *argv[])
{
  guestfs_h *g;
  char **strs;
  char *str;
  int r, pass;

  /* Make sure that LIBGUESTFS_BACKEND_SETTINGS in the test
   * environment doesn't affect the handle.
   */
  unsetenv ("LIBGUESTFS_BACKEND_SETTINGS");

  g = guestfs_create ();
  if (g == NULL)
    error (EXIT_FAILURE, errno, "guestfs_create");

  /* There should be no backend settings initially. */
  strs = guestfs_get_backend_settings (g);
  assert (strs != NULL);
  assert (strs[0] == NULL);
  guestfs_int_free_string_list (strs);

  guestfs_push_error_handler (g, NULL, NULL);
  str = guestfs_get_backend_setting (g, "foo");
  guestfs_pop_error_handler (g);
  assert (str == NULL);
  assert (guestfs_last_errno (g) == ESRCH);

  r = guestfs_clear_backend_setting (g, "bar");
  assert (r == 0);

  /* Create some settings in the handle, either using
   * guestfs_set_backend_settings or using the environment variable.
   */
  for (pass = 0; pass <= 1; ++pass) {
    if (pass == 0) {
      const char *initial_settings[] = {
        "foo", "foo=1", "foo=bar", "bar", "baz=value", NULL
      };
      r = guestfs_set_backend_settings (g, (char **) initial_settings);
      assert (r == 0);
    }
    else /* pass == 1 */ {
      const char *initial_settings = "foo:foo=1:foo=bar:bar:baz=value";

      guestfs_close (g);

      setenv ("LIBGUESTFS_BACKEND_SETTINGS", initial_settings, 1);
      g = guestfs_create ();
      if (g == NULL)
        error (EXIT_FAILURE, errno, "guestfs_create");
    }

    /* Check the settings are correct. */
    strs = guestfs_get_backend_settings (g);
    assert (strs != NULL);
    assert (STREQ (strs[0], "foo"));
    assert (STREQ (strs[1], "foo=1"));
    assert (STREQ (strs[2], "foo=bar"));
    assert (STREQ (strs[3], "bar"));
    assert (STREQ (strs[4], "baz=value"));
    assert (strs[5] == NULL);
    guestfs_int_free_string_list (strs);

    str = guestfs_get_backend_setting (g, "bar");
    assert (str != NULL);
    assert (STREQ (str, "1"));
    free (str);

    str = guestfs_get_backend_setting (g, "baz");
    assert (str != NULL);
    assert (STREQ (str, "value"));
    free (str);

    str = guestfs_get_backend_setting (g, "foo");
    assert (str != NULL);
    /* An implementation could return any of the values. */
    free (str);

    guestfs_push_error_handler (g, NULL, NULL);
    str = guestfs_get_backend_setting (g, "nothere");
    guestfs_pop_error_handler (g);
    assert (str == NULL);
    assert (guestfs_last_errno (g) == ESRCH);

    r = guestfs_set_backend_setting (g, "foo", "");
    assert (r == 0);
    r = guestfs_set_backend_setting (g, "foo", "1");
    assert (r == 0);
    r = guestfs_set_backend_setting (g, "foo", "2");
    assert (r == 0);
    r = guestfs_set_backend_setting (g, "foo", "3");
    assert (r == 0);
    r = guestfs_clear_backend_setting (g, "foo");
    assert (r == 1);

    r = guestfs_clear_backend_setting (g, "bar");
    assert (r == 1);

    r = guestfs_clear_backend_setting (g, "baz");
    assert (r == 1);

    strs = guestfs_get_backend_settings (g);
    assert (strs != NULL);
    assert (strs[0] == NULL);
    guestfs_int_free_string_list (strs);
  }

  guestfs_close (g);

  exit (EXIT_SUCCESS);
}