Debian Codegraph @

tests/tests-misc.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski <[email protected]>
3
4 #include <errno.h>
5 #include <glib.h>
6 #include <gpiod.h>
7 #include <gpiod-test.h>
8 #include <gpiod-test-common.h>
9 #include <gpiosim-glib.h>
10 #include <unistd.h>
11
12 #include "helpers.h"
13
14 #define GPIOD_TEST_GROUP "misc"
15
16 GPIOD_TEST_CASE(is_gpiochip_bad)
17 {
18 g_assert_false(gpiod_is_gpiochip_device("/dev/null"));
19 g_assert_cmpint(errno, ==, 0);
20 g_assert_false(gpiod_is_gpiochip_device("/dev/nonexistent"));
21 g_assert_cmpint(errno, ==, 0);
22 }
23
24 GPIOD_TEST_CASE(is_gpiochip_good)
25 {
26 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new(NULL);
27
28 g_assert_true(gpiod_is_gpiochip_device(
29 g_gpiosim_chip_get_dev_path(sim)));
30 }
31
32 GPIOD_TEST_CASE(is_gpiochip_link_bad)
33 {
34 g_autofree gchar *link = NULL;
35 gint ret;
36
37 link = g_strdup_printf("/tmp/gpiod-test-link.%u", getpid());
38 ret = symlink("/dev/null", link);
39 g_assert_cmpint(ret, ==, 0);
40 gpiod_test_return_if_failed();
41
42 g_assert_false(gpiod_is_gpiochip_device(link));
43 ret = unlink(link);
44 g_assert_cmpint(ret, ==, 0);
45 }
46
47 GPIOD_TEST_CASE(is_gpiochip_link_good)
48 {
49 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new(NULL);
50 g_autofree gchar *link = NULL;
51 gint ret;
52
53 link = g_strdup_printf("/tmp/gpiod-test-link.%u", getpid());
54 ret = symlink(g_gpiosim_chip_get_dev_path(sim), link);
55 g_assert_cmpint(ret, ==, 0);
56 gpiod_test_return_if_failed();
57
58 g_assert_true(gpiod_is_gpiochip_device(link));
59 ret = unlink(link);
60 g_assert_cmpint(ret, ==, 0);
61 }
62
63 GPIOD_TEST_CASE(is_gpiochip_null_path)
64 {
65 g_assert_false(gpiod_is_gpiochip_device(NULL));
66 gpiod_test_expect_errno(0);
67 }
68
69 GPIOD_TEST_CASE(version_string)
70 {
71 static const gchar *const pattern = "^\\d+\\.\\d+(\\.\\d+|\\-devel|\\-rc\\d+)?$";
72
73 g_autoptr(GError) err = NULL;
74 g_autoptr(GRegex) regex = NULL;
75 g_autoptr(GMatchInfo) match = NULL;
76 g_autofree gchar *res = NULL;
77 const gchar *ver;
78 gboolean ret;
79
80 ver = gpiod_api_version();
81 g_assert_nonnull(ver);
82 gpiod_test_return_if_failed();
83
84 regex = g_regex_new(pattern, 0, 0, &err);
85 g_assert_nonnull(regex);
86 g_assert_no_error(err);
87 gpiod_test_return_if_failed();
88
89 ret = g_regex_match(regex, ver, 0, &match);
90 g_assert_true(ret);
91 gpiod_test_return_if_failed();
92
93 g_assert_true(g_match_info_matches(match));
94 res = g_match_info_fetch(match, 0);
95 g_assert_nonnull(res);
96 g_assert_cmpstr(res, ==, ver);
97 g_match_info_next(match, &err);
98 g_assert_no_error(err);
99 g_assert_false(g_match_info_matches(match));
100 }