2020-11-09 07:23:58 +03:00
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2013-02-11 00:56:03 +04:00
2019-07-03 17:27:27 +03:00
# include <stdio.h>
2013-02-11 00:56:03 +04:00
2015-10-24 23:58:24 +03:00
# include "string-util.h"
2013-02-11 00:56:03 +04:00
# include "strxcpyx.h"
2021-11-24 14:00:02 +03:00
# include "tests.h"
2015-10-24 23:58:24 +03:00
# include "util.h"
2013-02-11 00:56:03 +04:00
2021-11-24 14:00:02 +03:00
TEST ( strpcpy ) {
2013-02-11 00:56:03 +04:00
char target [ 25 ] ;
char * s = target ;
size_t space_left ;
2021-12-16 00:19:28 +03:00
bool truncated ;
2013-02-11 00:56:03 +04:00
space_left = sizeof ( target ) ;
2021-12-16 00:19:28 +03:00
space_left = strpcpy_full ( & s , space_left , " 12345 " , & truncated ) ;
assert_se ( ! truncated ) ;
space_left = strpcpy_full ( & s , space_left , " hey hey hey " , & truncated ) ;
assert_se ( ! truncated ) ;
space_left = strpcpy_full ( & s , space_left , " waldo " , & truncated ) ;
assert_se ( ! truncated ) ;
space_left = strpcpy_full ( & s , space_left , " ba " , & truncated ) ;
assert_se ( ! truncated ) ;
space_left = strpcpy_full ( & s , space_left , " r " , & truncated ) ;
assert_se ( ! truncated ) ;
assert_se ( space_left = = 1 ) ;
assert_se ( streq ( target , " 12345hey hey heywaldobar " ) ) ;
2013-02-11 00:56:03 +04:00
2021-12-16 00:19:28 +03:00
space_left = strpcpy_full ( & s , space_left , " " , & truncated ) ;
assert_se ( ! truncated ) ;
assert_se ( space_left = = 1 ) ;
2014-10-05 01:51:45 +04:00
assert_se ( streq ( target , " 12345hey hey heywaldobar " ) ) ;
2021-12-16 00:19:28 +03:00
space_left = strpcpy_full ( & s , space_left , " f " , & truncated ) ;
assert_se ( truncated ) ;
2014-10-05 01:51:45 +04:00
assert_se ( space_left = = 0 ) ;
2021-12-16 00:19:28 +03:00
assert_se ( streq ( target , " 12345hey hey heywaldobar " ) ) ;
space_left = strpcpy_full ( & s , space_left , " " , & truncated ) ;
assert_se ( ! truncated ) ;
assert_se ( space_left = = 0 ) ;
assert_se ( streq ( target , " 12345hey hey heywaldobar " ) ) ;
space_left = strpcpy_full ( & s , space_left , " foo " , & truncated ) ;
assert_se ( truncated ) ;
assert_se ( space_left = = 0 ) ;
assert_se ( streq ( target , " 12345hey hey heywaldobar " ) ) ;
2013-02-11 00:56:03 +04:00
}
2021-11-24 14:00:02 +03:00
TEST ( strpcpyf ) {
2013-02-11 00:56:03 +04:00
char target [ 25 ] ;
char * s = target ;
size_t space_left ;
2021-12-16 00:19:28 +03:00
bool truncated ;
2013-02-11 00:56:03 +04:00
space_left = sizeof ( target ) ;
2021-12-16 00:19:28 +03:00
space_left = strpcpyf_full ( & s , space_left , & truncated , " space left: %zu. " , space_left ) ;
assert_se ( ! truncated ) ;
space_left = strpcpyf_full ( & s , space_left , & truncated , " foo%s " , " bar " ) ;
assert_se ( ! truncated ) ;
2014-10-05 01:51:45 +04:00
assert_se ( space_left = = 3 ) ;
2021-12-16 00:19:28 +03:00
assert_se ( streq ( target , " space left: 25. foobar " ) ) ;
space_left = strpcpyf_full ( & s , space_left , & truncated , " %i " , 42 ) ;
assert_se ( ! truncated ) ;
assert_se ( space_left = = 1 ) ;
assert_se ( streq ( target , " space left: 25. foobar42 " ) ) ;
space_left = strpcpyf_full ( & s , space_left , & truncated , " %s " , " " ) ;
assert_se ( ! truncated ) ;
assert_se ( space_left = = 1 ) ;
assert_se ( streq ( target , " space left: 25. foobar42 " ) ) ;
space_left = strpcpyf_full ( & s , space_left , & truncated , " %c " , ' x ' ) ;
assert_se ( truncated ) ;
assert_se ( space_left = = 0 ) ;
assert_se ( streq ( target , " space left: 25. foobar42 " ) ) ;
space_left = strpcpyf_full ( & s , space_left , & truncated , " %s " , " " ) ;
assert_se ( ! truncated ) ;
assert_se ( space_left = = 0 ) ;
assert_se ( streq ( target , " space left: 25. foobar42 " ) ) ;
space_left = strpcpyf_full ( & s , space_left , & truncated , " abc%s " , " hoge " ) ;
assert_se ( truncated ) ;
assert_se ( space_left = = 0 ) ;
assert_se ( streq ( target , " space left: 25. foobar42 " ) ) ;
2017-07-02 19:37:42 +03:00
/* test overflow */
s = target ;
2021-12-16 00:19:28 +03:00
space_left = strpcpyf_full ( & s , 12 , & truncated , " 00 left: %i. " , 999 ) ;
assert_se ( truncated ) ;
2017-07-02 19:37:42 +03:00
assert_se ( streq ( target , " 00 left: 99 " ) ) ;
assert_se ( space_left = = 0 ) ;
assert_se ( target [ 12 ] = = ' 2 ' ) ;
2013-02-11 00:56:03 +04:00
}
2021-11-24 14:00:02 +03:00
TEST ( strpcpyl ) {
2013-02-11 00:56:03 +04:00
char target [ 25 ] ;
char * s = target ;
size_t space_left ;
2021-12-16 00:19:28 +03:00
bool truncated ;
2013-02-11 00:56:03 +04:00
space_left = sizeof ( target ) ;
2021-12-16 00:19:28 +03:00
space_left = strpcpyl_full ( & s , space_left , & truncated , " waldo " , " test " , " waldo. " , NULL ) ;
assert_se ( ! truncated ) ;
space_left = strpcpyl_full ( & s , space_left , & truncated , " Banana " , NULL ) ;
assert_se ( ! truncated ) ;
assert_se ( space_left = = 1 ) ;
2014-10-05 01:51:45 +04:00
assert_se ( streq ( target , " waldo test waldo. Banana " ) ) ;
2021-12-16 00:19:28 +03:00
space_left = strpcpyl_full ( & s , space_left , & truncated , " " , " " , " " , NULL ) ;
assert_se ( ! truncated ) ;
2014-10-05 01:51:45 +04:00
assert_se ( space_left = = 1 ) ;
2021-12-16 00:19:28 +03:00
assert_se ( streq ( target , " waldo test waldo. Banana " ) ) ;
space_left = strpcpyl_full ( & s , space_left , & truncated , " " , " x " , " " , NULL ) ;
assert_se ( truncated ) ;
assert_se ( space_left = = 0 ) ;
assert_se ( streq ( target , " waldo test waldo. Banana " ) ) ;
space_left = strpcpyl_full ( & s , space_left , & truncated , " hoge " , NULL ) ;
assert_se ( truncated ) ;
assert_se ( space_left = = 0 ) ;
assert_se ( streq ( target , " waldo test waldo. Banana " ) ) ;
2013-02-11 00:56:03 +04:00
}
2021-11-24 14:00:02 +03:00
TEST ( strscpy ) {
2013-02-11 00:56:03 +04:00
char target [ 25 ] ;
size_t space_left ;
2021-12-16 00:19:28 +03:00
bool truncated ;
2013-02-11 00:56:03 +04:00
space_left = sizeof ( target ) ;
2021-12-16 00:19:28 +03:00
space_left = strscpy_full ( target , space_left , " 12345 " , & truncated ) ;
assert_se ( ! truncated ) ;
2013-02-11 00:56:03 +04:00
2014-10-05 01:51:45 +04:00
assert_se ( streq ( target , " 12345 " ) ) ;
assert_se ( space_left = = 20 ) ;
2013-02-11 00:56:03 +04:00
}
2021-11-24 14:00:02 +03:00
TEST ( strscpyl ) {
2013-02-11 00:56:03 +04:00
char target [ 25 ] ;
size_t space_left ;
2021-12-16 00:19:28 +03:00
bool truncated ;
2013-02-11 00:56:03 +04:00
space_left = sizeof ( target ) ;
2021-12-16 00:19:28 +03:00
space_left = strscpyl_full ( target , space_left , & truncated , " 12345 " , " waldo " , " waldo " , NULL ) ;
assert_se ( ! truncated ) ;
2013-02-11 00:56:03 +04:00
2014-10-05 01:51:45 +04:00
assert_se ( streq ( target , " 12345waldowaldo " ) ) ;
assert_se ( space_left = = 10 ) ;
2013-02-11 00:56:03 +04:00
}
2021-11-24 14:00:02 +03:00
TEST ( sd_event_code_migration ) {
2019-07-03 17:27:27 +03:00
char b [ 100 * DECIMAL_STR_MAX ( unsigned ) + 1 ] ;
char c [ 100 * DECIMAL_STR_MAX ( unsigned ) + 1 ] , * p ;
unsigned i ;
size_t l ;
2021-11-14 04:28:42 +03:00
int o , r ;
2019-07-03 17:27:27 +03:00
2021-11-14 04:28:42 +03:00
for ( i = o = 0 ; i < 100 ; i + + ) {
r = snprintf ( & b [ o ] , sizeof ( b ) - o , " %u " , i ) ;
assert_se ( r > = 0 & & r < ( int ) sizeof ( b ) - o ) ;
o + = r ;
}
2019-07-03 17:27:27 +03:00
p = c ;
l = sizeof ( c ) ;
for ( i = 0 ; i < 100 ; i + + )
l = strpcpyf ( & p , l , " %u " , i ) ;
assert_se ( streq ( b , c ) ) ;
}
2021-11-24 14:00:02 +03:00
DEFINE_TEST_MAIN ( LOG_INFO ) ;