]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-device-nodes.c
tree-wide: drop 'This file is part of systemd' blurb
[thirdparty/systemd.git] / src / test / test-device-nodes.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
8f6ce71f 2/***
8f6ce71f 3 Copyright 2013 Dave Reisner
8f6ce71f
DR
4***/
5
6#include <sys/types.h>
7
b5efdb8a 8#include "alloc-util.h"
8f6ce71f 9#include "device-nodes.h"
07630cea 10#include "string-util.h"
8f6ce71f
DR
11#include "util.h"
12
13/* helpers for test_encode_devnode_name */
14static char *do_encode_string(const char *in) {
1d5989fd 15 size_t out_len = strlen(in) * 4 + 1;
8f6ce71f
DR
16 char *out = malloc(out_len);
17
18 assert_se(out);
19 assert_se(encode_devnode_name(in, out, out_len) >= 0);
20 puts(out);
21
22 return out;
23}
24
25static bool expect_encoded_as(const char *in, const char *expected) {
26 _cleanup_free_ char *encoded = do_encode_string(in);
27 return streq(encoded, expected);
28}
29
30static void test_encode_devnode_name(void) {
31 assert_se(expect_encoded_as("systemd sucks", "systemd\\x20sucks"));
32 assert_se(expect_encoded_as("pinkiepie", "pinkiepie"));
33 assert_se(expect_encoded_as("valíd\\ųtf8", "valíd\\x5cųtf8"));
34 assert_se(expect_encoded_as("s/ash/ng", "s\\x2fash\\x2fng"));
1d5989fd
DR
35 assert_se(expect_encoded_as("/", "\\x2f"));
36 assert_se(expect_encoded_as("!", "\\x21"));
8f6ce71f
DR
37}
38
39int main(int argc, char *argv[]) {
40 test_encode_devnode_name();
41
42 return 0;
43}