File: test-otcore.c

package info (click to toggle)
ostree 2025.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,996 kB
  • sloc: ansic: 70,831; sh: 15,652; xml: 5,007; yacc: 1,236; javascript: 531; makefile: 243; python: 155
file content (141 lines) | stat: -rw-r--r-- 4,897 bytes parent folder | download | duplicates (2)
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
/*
 * SPDX-License-Identifier: LGPL-2.0+
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "otcore.h"

static void
test_ed25519 (void)
{
  g_autoptr (GBytes) empty = g_bytes_new_static ("", 0);
  bool valid = false;
  g_autoptr (GError) error = NULL;
  if (otcore_validate_ed25519_signature (empty, empty, empty, &valid, &error))
    g_assert_not_reached ();
  g_assert (error != NULL);
  g_clear_error (&error);
}

static void
test_prepare_root_cmdline (void)
{
  g_autoptr (GError) error = NULL;
  g_autofree char *target = NULL;
  static const char *notfound_cases[]
      = { "", "foo", "foo=bar baz  sometest", "xostree foo", "xostree=blah bar", NULL };
  for (const char **iter = notfound_cases; iter && *iter; iter++)
    {
      const char *tcase = *iter;
      g_assert (otcore_get_ostree_target (tcase, NULL, &target, &error));
      g_assert_no_error (error);
      g_assert (target == NULL);
    }

  // Test the default ostree=
  g_assert (
      otcore_get_ostree_target ("blah baz=blah ostree=/foo/bar somearg", NULL, &target, &error));
  g_assert_no_error (error);
  g_assert_cmpstr (target, ==, "/foo/bar");
  free (g_steal_pointer (&target));

  // Test android boot
  g_assert (otcore_get_ostree_target ("blah baz=blah androidboot.slot_suffix=_b somearg", NULL,
                                      &target, &error));
  g_assert_no_error (error);
  g_assert_cmpstr (target, ==, "/ostree/root.b");
  free (g_steal_pointer (&target));

  g_assert (otcore_get_ostree_target ("blah baz=blah androidboot.slot_suffix=_a somearg", NULL,
                                      &target, &error));
  g_assert_no_error (error);
  g_assert_cmpstr (target, ==, "/ostree/root.a");
  free (g_steal_pointer (&target));

  // And an expected failure to parse a "c" suffix
  g_assert (!otcore_get_ostree_target ("blah baz=blah androidboot.slot_suffix=_c somearg", NULL,
                                       &target, &error));
  g_assert (error);
  g_assert (target == NULL);
  g_clear_error (&error);

  // And non-A/B androidboot
  g_assert (otcore_get_ostree_target ("blah baz=blah androidboot.somethingelse somearg", NULL,
                                      &target, &error));
  g_assert_no_error (error);
  g_assert_cmpstr (target, ==, "/ostree/root.a");
  free (g_steal_pointer (&target));
}

static void
test_prepare_root_config (void)
{
  g_autoptr (GError) error = NULL;
  g_auto (GLnxTmpDir) tmpdir = {
    0,
  };
  g_assert (glnx_mkdtempat (AT_FDCWD, "/tmp/test-XXXXXX", 0777, &tmpdir, &error));
  g_assert_no_error (error);

  {
    g_autoptr (GKeyFile) config = NULL;
    g_auto (GStrv) keys = NULL;
    config = otcore_load_config (tmpdir.fd, "ostree/someconfig.conf", &error);
    g_assert (config);
    keys = g_key_file_get_groups (config, NULL);
    g_assert (keys && *keys == NULL);
  }

  g_assert (glnx_shutil_mkdir_p_at (tmpdir.fd, "usr/lib/ostree", 0755, NULL, NULL));
  g_assert (glnx_file_replace_contents_at (tmpdir.fd, "usr/lib/ostree/someconfig.conf",
                                           (guint8 *)"[foo]\nbar=baz", -1, 0, NULL, NULL));

  {
    g_autoptr (GKeyFile) config = NULL;
    g_auto (GStrv) keys = NULL;
    config = otcore_load_config (tmpdir.fd, "ostree/someconfig.conf", &error);
    g_assert (config);
    keys = g_key_file_get_groups (config, NULL);
    g_assert (keys);
    g_assert_cmpstr (*keys, ==, "foo");
  }

  g_assert (glnx_shutil_mkdir_p_at (tmpdir.fd, "etc/ostree", 0755, NULL, NULL));
  g_assert (glnx_file_replace_contents_at (tmpdir.fd, "usr/lib/ostree/someconfig.conf",
                                           (guint8 *)"[test]\nbar=baz", -1, 0, NULL, NULL));

  {
    g_autoptr (GKeyFile) config = NULL;
    g_auto (GStrv) keys = NULL;
    config = otcore_load_config (tmpdir.fd, "ostree/someconfig.conf", &error);
    g_assert (config);
    keys = g_key_file_get_groups (config, NULL);
    g_assert (keys);
    g_assert_cmpstr (*keys, ==, "test");
  }
}

int
main (int argc, char **argv)
{
  g_test_init (&argc, &argv, NULL);
  otcore_ed25519_init ();
  g_test_add_func ("/ed25519", test_ed25519);
  g_test_add_func ("/prepare-root-cmdline", test_prepare_root_cmdline);
  g_test_add_func ("/prepare-root-config", test_prepare_root_config);
  return g_test_run ();
}