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