Debian Codegraph @

bindings/glib/tests/tests-chip.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPDX-FileCopyrightText: 2022-2024 Bartosz Golaszewski <[email protected]>
3
4 #include <glib.h>
5 #include <gpiod-glib.h>
6 #include <gpiod-test.h>
7 #include <gpiod-test-common.h>
8 #include <gpiosim-glib.h>
9
10 #include "helpers.h"
11
12 #define GPIOD_TEST_GROUP "glib/chip"
13
14 GPIOD_TEST_CASE(open_chip_good)
15 {
16 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new(NULL);
17 g_autoptr(GpiodglibChip) chip = NULL;
18 g_autoptr(GError) err = NULL;
19
20 chip = gpiodglib_chip_new(g_gpiosim_chip_get_dev_path(sim), &err);
21 g_assert_nonnull(chip);
22 g_assert_null(err);
23 }
24
25 GPIOD_TEST_CASE(open_chip_nonexistent)
26 {
27 g_autoptr(GpiodglibChip) chip = NULL;
28 g_autoptr(GError) err = NULL;
29
30 chip = gpiodglib_chip_new("/dev/nonexistent", &err);
31 g_assert_null(chip);
32 gpiodglib_test_check_error_or_fail(err, GPIODGLIB_ERROR,
33 GPIODGLIB_ERR_NOENT);
34 }
35
36 GPIOD_TEST_CASE(open_chip_not_a_character_device)
37 {
38 g_autoptr(GpiodglibChip) chip = NULL;
39 g_autoptr(GError) err = NULL;
40
41 chip = gpiodglib_chip_new("/tmp", &err);
42 g_assert_null(chip);
43 gpiodglib_test_check_error_or_fail(err, GPIODGLIB_ERROR,
44 GPIODGLIB_ERR_NOTTY);
45 }
46
47 GPIOD_TEST_CASE(open_chip_not_a_gpio_device)
48 {
49 g_autoptr(GpiodglibChip) chip = NULL;
50 g_autoptr(GError) err = NULL;
51
52 chip = gpiodglib_chip_new("/dev/null", &err);
53 g_assert_null(chip);
54 gpiodglib_test_check_error_or_fail(err, GPIODGLIB_ERROR,
55 GPIODGLIB_ERR_NODEV);
56 }
57
58 GPIOD_TEST_CASE(get_chip_path)
59 {
60 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new(NULL);
61 g_autoptr(GpiodglibChip) chip = NULL;
62 const gchar *path = g_gpiosim_chip_get_dev_path(sim);
63 g_autofree gchar *chip_path = NULL;
64
65 chip = gpiodglib_test_new_chip_or_fail(path);
66
67 chip_path = gpiodglib_chip_dup_path(chip);
68 g_assert_cmpstr(chip_path, ==, path);
69 }
70
71 GPIOD_TEST_CASE(closed_chip)
72 {
73 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new(NULL);
74 g_autoptr(GpiodglibChip) chip = NULL;
75 g_autoptr(GError) err = NULL;
76 g_autoptr(GpiodglibChipInfo) info = NULL;
77 const gchar *path = g_gpiosim_chip_get_dev_path(sim);
78 g_autofree gchar *chip_path = NULL;
79
80 chip = gpiodglib_test_new_chip_or_fail(path);
81
82 gpiodglib_chip_close(chip);
83
84 info = gpiodglib_chip_get_info(chip, &err);
85 g_assert_error(err, GPIODGLIB_ERROR, GPIODGLIB_ERR_CHIP_CLOSED);
86
87 /* Properties still work. */
88 chip_path = gpiodglib_chip_dup_path(chip);
89 g_assert_cmpstr(chip_path, ==, path);
90 }
91
92 GPIOD_TEST_CASE(find_line_bad)
93 {
94 static const GPIOSimLineName names[] = {
95 { .offset = 1, .name = "foo", },
96 { .offset = 2, .name = "bar", },
97 { .offset = 4, .name = "baz", },
98 { .offset = 5, .name = "xyz", },
99 { }
100 };
101
102 g_autoptr(GVariant) vnames = g_gpiosim_package_line_names(names);
103 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8,
104 "line-names", vnames,
105 NULL);
106 g_autoptr(GpiodglibChip) chip = NULL;
107 g_autoptr(GError) err = NULL;
108 guint offset;
109
110 chip = gpiodglib_test_new_chip_or_fail(
111 g_gpiosim_chip_get_dev_path(sim));
112
113 g_assert_false(gpiodglib_chip_get_line_offset_from_name(chip,
114 "nonexistent",
115 &offset, &err));
116 g_assert_no_error(err);
117 }
118
119 GPIOD_TEST_CASE(find_line_good)
120 {
121 static const GPIOSimLineName names[] = {
122 { .offset = 1, .name = "foo", },
123 { .offset = 2, .name = "bar", },
124 { .offset = 4, .name = "baz", },
125 { .offset = 5, .name = "xyz", },
126 { }
127 };
128
129 g_autoptr(GVariant) vnames = g_gpiosim_package_line_names(names);
130 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8,
131 "line-names", vnames,
132 NULL);
133 g_autoptr(GpiodglibChip) chip = NULL;
134 g_autoptr(GError) err = NULL;
135 guint offset;
136
137 chip = gpiodglib_test_new_chip_or_fail(
138 g_gpiosim_chip_get_dev_path(sim));
139
140 g_assert_true(gpiodglib_chip_get_line_offset_from_name(chip, "baz",
141 &offset, &err));
142 g_assert_no_error(err);
143 g_assert_cmpuint(offset, ==, 4);
144 }
145
146 /* Verify that for duplicated line names, the first one is returned. */
147 GPIOD_TEST_CASE(find_line_duplicate)
148 {
149 static const GPIOSimLineName names[] = {
150 { .offset = 1, .name = "foo", },
151 { .offset = 2, .name = "baz", },
152 { .offset = 4, .name = "baz", },
153 { .offset = 5, .name = "xyz", },
154 { }
155 };
156
157 g_autoptr(GVariant) vnames = g_gpiosim_package_line_names(names);
158 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8,
159 "line-names", vnames,
160 NULL);
161 g_autoptr(GpiodglibChip) chip = NULL;
162 g_autoptr(GError) err = NULL;
163 guint offset;
164
165 chip = gpiodglib_test_new_chip_or_fail(
166 g_gpiosim_chip_get_dev_path(sim));
167
168 g_assert_true(gpiodglib_chip_get_line_offset_from_name(chip, "baz",
169 &offset, &err));
170 g_assert_no_error(err);
171 g_assert_cmpuint(offset, ==, 2);
172 }
173
174 GPIOD_TEST_CASE(find_line_null_name)
175 {
176 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new(NULL);
177 g_autoptr(GpiodglibChip) chip = NULL;
178 g_autoptr(GError) err = NULL;
179 guint offset;
180
181 chip = gpiodglib_test_new_chip_or_fail(
182 g_gpiosim_chip_get_dev_path(sim));
183
184 g_assert_false(gpiodglib_chip_get_line_offset_from_name(chip, NULL,
185 &offset, &err));
186 g_assert_error(err, GPIODGLIB_ERROR, GPIODGLIB_ERR_INVAL);
187 }