5325N/A/***
5325N/A This file is part of systemd.
5325N/A
5325N/A Copyright 2013 Dave Reisner
5325N/A
5325N/A systemd is free software; you can redistribute it and/or modify it
5325N/A under the terms of the GNU Lesser General Public License as published by
5325N/A the Free Software Foundation; either version 2.1 of the License, or
5325N/A (at your option) any later version.
5325N/A
5325N/A systemd is distributed in the hope that it will be useful, but
5325N/A WITHOUT ANY WARRANTY; without even the implied warranty of
5325N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5325N/A Lesser General Public License for more details.
5325N/A
5325N/A You should have received a copy of the GNU Lesser General Public License
5325N/A along with systemd; If not, see <http://www.gnu.org/licenses/>.
5325N/A***/
5325N/A
5325N/A#include <sys/types.h>
5325N/A
5325N/A#include "alloc-util.h"
5325N/A#include "device-nodes.h"
5325N/A#include "string-util.h"
6406N/A#include "util.h"
5325N/A
5325N/A/* helpers for test_encode_devnode_name */
5325N/Astatic char *do_encode_string(const char *in) {
6556N/A size_t out_len = strlen(in) * 4 + 1;
5325N/A char *out = malloc(out_len);
6406N/A
5325N/A assert_se(out);
6556N/A assert_se(encode_devnode_name(in, out, out_len) >= 0);
6556N/A puts(out);
5375N/A
6556N/A return out;
5325N/A}
5680N/A
6406N/Astatic bool expect_encoded_as(const char *in, const char *expected) {
6406N/A _cleanup_free_ char *encoded = do_encode_string(in);
6406N/A return streq(encoded, expected);
6406N/A}
6406N/A
6406N/Astatic void test_encode_devnode_name(void) {
6406N/A assert_se(expect_encoded_as("systemd sucks", "systemd\\x20sucks"));
6406N/A assert_se(expect_encoded_as("pinkiepie", "pinkiepie"));
5680N/A assert_se(expect_encoded_as("valíd\\ųtf8", "valíd\\x5cųtf8"));
5325N/A assert_se(expect_encoded_as("s/ash/ng", "s\\x2fash\\x2fng"));
5325N/A assert_se(expect_encoded_as("/", "\\x2f"));
5680N/A assert_se(expect_encoded_as("!", "\\x21"));
5325N/A}
5325N/A
5325N/Aint main(int argc, char *argv[]) {
5680N/A test_encode_devnode_name();
5325N/A
5325N/A return 0;
5325N/A}
5325N/A