]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
8f6ce71f DR |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2013 Dave Reisner | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
20 | ||
21 | #include <sys/types.h> | |
22 | ||
b5efdb8a | 23 | #include "alloc-util.h" |
8f6ce71f | 24 | #include "device-nodes.h" |
07630cea | 25 | #include "string-util.h" |
8f6ce71f DR |
26 | #include "util.h" |
27 | ||
28 | /* helpers for test_encode_devnode_name */ | |
29 | static char *do_encode_string(const char *in) { | |
1d5989fd | 30 | size_t out_len = strlen(in) * 4 + 1; |
8f6ce71f DR |
31 | char *out = malloc(out_len); |
32 | ||
33 | assert_se(out); | |
34 | assert_se(encode_devnode_name(in, out, out_len) >= 0); | |
35 | puts(out); | |
36 | ||
37 | return out; | |
38 | } | |
39 | ||
40 | static bool expect_encoded_as(const char *in, const char *expected) { | |
41 | _cleanup_free_ char *encoded = do_encode_string(in); | |
42 | return streq(encoded, expected); | |
43 | } | |
44 | ||
45 | static void test_encode_devnode_name(void) { | |
46 | assert_se(expect_encoded_as("systemd sucks", "systemd\\x20sucks")); | |
47 | assert_se(expect_encoded_as("pinkiepie", "pinkiepie")); | |
48 | assert_se(expect_encoded_as("valíd\\ųtf8", "valíd\\x5cųtf8")); | |
49 | assert_se(expect_encoded_as("s/ash/ng", "s\\x2fash\\x2fng")); | |
1d5989fd DR |
50 | assert_se(expect_encoded_as("/", "\\x2f")); |
51 | assert_se(expect_encoded_as("!", "\\x21")); | |
8f6ce71f DR |
52 | } |
53 | ||
54 | int main(int argc, char *argv[]) { | |
55 | test_encode_devnode_name(); | |
56 | ||
57 | return 0; | |
58 | } |