chiark / gitweb /
Make test-login and test-sleep output debugging
[elogind.git] / src / test / test-utf8.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Dave Reisner
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22
23 #include "utf8.h"
24 #include "util.h"
25
26 /* helpers for test_udev_encode_string */
27 static char *do_encode_string(const char *in) {
28         size_t out_len = strlen(in) * 4;
29         char *out = malloc(out_len);
30
31         assert_se(out);
32         assert_se(udev_encode_string(in, out, out_len) >= 0);
33         puts(out);
34
35         return out;
36 }
37
38 static bool expect_encoded_as(const char *in, const char *expected) {
39         _cleanup_free_ char *encoded = do_encode_string(in);
40         return streq(encoded, expected);
41 }
42
43 static void test_udev_encode_string(void) {
44         assert_se(expect_encoded_as("systemd sucks", "systemd\\x20sucks"));
45         assert_se(expect_encoded_as("pinkiepie", "pinkiepie"));
46         assert_se(expect_encoded_as("valíd\\ųtf8", "valíd\\x5cųtf8"));
47         assert_se(expect_encoded_as("s/ash/ng", "s\\x2fash\\x2fng"));
48 }
49
50 static void test_utf8_is_valid(void) {
51         assert_se(utf8_is_valid("ascii is valid unicode"));
52         assert_se(utf8_is_valid("\341\204\242"));
53         assert_se(!utf8_is_valid("\341\204"));
54 }
55
56 int main(int argc, char *argv[]) {
57         test_utf8_is_valid();
58         test_udev_encode_string();
59 }