]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-strxcpyx.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / test / test-strxcpyx.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
c62c294f
TA
2/***
3 This file is part of systemd.
4
5 Copyright 2013 Thomas H.P. Andersen
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 <string.h>
22
07630cea 23#include "string-util.h"
c62c294f 24#include "strxcpyx.h"
07630cea 25#include "util.h"
c62c294f
TA
26
27static void test_strpcpy(void) {
28 char target[25];
29 char *s = target;
30 size_t space_left;
31
32 space_left = sizeof(target);
33 space_left = strpcpy(&s, space_left, "12345");
34 space_left = strpcpy(&s, space_left, "hey hey hey");
35 space_left = strpcpy(&s, space_left, "waldo");
36 space_left = strpcpy(&s, space_left, "ba");
37 space_left = strpcpy(&s, space_left, "r");
38 space_left = strpcpy(&s, space_left, "foo");
39
bdf7026e
TA
40 assert_se(streq(target, "12345hey hey heywaldobar"));
41 assert_se(space_left == 0);
c62c294f
TA
42}
43
44static void test_strpcpyf(void) {
45 char target[25];
46 char *s = target;
47 size_t space_left;
48
49 space_left = sizeof(target);
1fa2f38f 50 space_left = strpcpyf(&s, space_left, "space left: %zu. ", space_left);
c62c294f
TA
51 space_left = strpcpyf(&s, space_left, "foo%s", "bar");
52
bdf7026e
TA
53 assert_se(streq(target, "space left: 25. foobar"));
54 assert_se(space_left == 3);
54d46a78
ZJS
55
56 /* test overflow */
57 s = target;
58 space_left = strpcpyf(&s, 12, "00 left: %i. ", 999);
59 assert_se(streq(target, "00 left: 99"));
60 assert_se(space_left == 0);
61 assert_se(target[12] == '2');
c62c294f
TA
62}
63
64static void test_strpcpyl(void) {
65 char target[25];
66 char *s = target;
67 size_t space_left;
68
69 space_left = sizeof(target);
70 space_left = strpcpyl(&s, space_left, "waldo", " test", " waldo. ", NULL);
71 space_left = strpcpyl(&s, space_left, "Banana", NULL);
72
bdf7026e
TA
73 assert_se(streq(target, "waldo test waldo. Banana"));
74 assert_se(space_left == 1);
c62c294f
TA
75}
76
77static void test_strscpy(void) {
78 char target[25];
79 size_t space_left;
80
81 space_left = sizeof(target);
82 space_left = strscpy(target, space_left, "12345");
83
bdf7026e
TA
84 assert_se(streq(target, "12345"));
85 assert_se(space_left == 20);
c62c294f
TA
86}
87
88static void test_strscpyl(void) {
89 char target[25];
90 size_t space_left;
91
92 space_left = sizeof(target);
93 space_left = strscpyl(target, space_left, "12345", "waldo", "waldo", NULL);
94
bdf7026e
TA
95 assert_se(streq(target, "12345waldowaldo"));
96 assert_se(space_left == 10);
c62c294f
TA
97}
98
99int main(int argc, char *argv[]) {
100 test_strpcpy();
101 test_strpcpyf();
102 test_strpcpyl();
103 test_strscpy();
104 test_strscpyl();
105
106 return 0;
107}