aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2014-06-30 21:13:06 +0100
committerRon Yorston <rmy@pobox.com>2014-06-30 21:13:06 +0100
commit099e8b6438345baae560a629d548af07a8c3125c (patch)
tree71b5600b22b0019af675e4a991394ce32c8207c5 /libbb
parente19594cc6e49e78fa50a654f15cf9a04e77d054a (diff)
parent184b2669175e562d58894e22f6320cebf3316c25 (diff)
downloadbusybox-w32-099e8b6438345baae560a629d548af07a8c3125c.tar.gz
busybox-w32-099e8b6438345baae560a629d548af07a8c3125c.tar.bz2
busybox-w32-099e8b6438345baae560a629d548af07a8c3125c.zip
Merge branch 'busybox' into merge
Diffstat (limited to 'libbb')
-rw-r--r--libbb/Kbuild.src1
-rw-r--r--libbb/bbunit.c90
-rw-r--r--libbb/copy_file.c4
-rw-r--r--libbb/make_directory.c4
-rw-r--r--libbb/obscure.c38
-rw-r--r--libbb/remove_file.c8
-rw-r--r--libbb/strrstr.c19
-rw-r--r--libbb/sysconf.c22
8 files changed, 174 insertions, 12 deletions
diff --git a/libbb/Kbuild.src b/libbb/Kbuild.src
index 107c80689..d7ab1b129 100644
--- a/libbb/Kbuild.src
+++ b/libbb/Kbuild.src
@@ -84,6 +84,7 @@ lib-y += skip_whitespace.o
84lib-y += speed_table.o 84lib-y += speed_table.o
85lib-y += str_tolower.o 85lib-y += str_tolower.o
86lib-y += strrstr.o 86lib-y += strrstr.o
87lib-y += sysconf.o
87lib-y += time.o 88lib-y += time.o
88lib-y += trim.o 89lib-y += trim.o
89lib-y += u_signal_names.o 90lib-y += u_signal_names.o
diff --git a/libbb/bbunit.c b/libbb/bbunit.c
new file mode 100644
index 000000000..256014441
--- /dev/null
+++ b/libbb/bbunit.c
@@ -0,0 +1,90 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * bbunit: Simple unit-testing framework for Busybox.
4 *
5 * Copyright (C) 2014 by Bartosz Golaszewski <bartekgola@gmail.com>
6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 */
9
10//kbuild:lib-$(CONFIG_UNIT_TEST) += bbunit.o
11//applet:IF_UNIT_TEST(APPLET(unit, BB_DIR_USR_BIN, BB_SUID_DROP))
12
13//usage:#define unit_trivial_usage
14//usage: ""
15//usage:#define unit_full_usage "\n\n"
16//usage: "Run the unit-test suite"
17
18#include "libbb.h"
19
20#define WANT_TIMING 0
21
22static llist_t *tests = NULL;
23static unsigned tests_registered = 0;
24static int test_retval;
25
26void bbunit_registertest(struct bbunit_listelem *test)
27{
28 llist_add_to_end(&tests, test);
29 tests_registered++;
30}
31
32void bbunit_settestfailed(void)
33{
34 test_retval = -1;
35}
36
37#if WANT_TIMING
38static void timeval_diff(struct timeval* res,
39 const struct timeval* x,
40 const struct timeval* y)
41{
42 long udiff = x->tv_usec - y->tv_usec;
43
44 res->tv_sec = x->tv_sec - y->tv_sec - (udiff < 0);
45 res->tv_usec = (udiff >= 0 ? udiff : udiff + 1000000);
46}
47#endif
48
49int unit_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) MAIN_EXTERNALLY_VISIBLE;
50int unit_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
51{
52 unsigned tests_run = 0;
53 unsigned tests_failed = 0;
54#if WANT_TIMING
55 struct timeval begin;
56 struct timeval end;
57 struct timeval time_spent;
58 gettimeofday(&begin, NULL);
59#endif
60
61 bb_error_msg("Running %d test(s)...", tests_registered);
62 for (;;) {
63 struct bbunit_listelem* el = llist_pop(&tests);
64 if (!el)
65 break;
66 bb_error_msg("Case: [%s]", el->name);
67 test_retval = 0;
68 el->testfunc();
69 if (test_retval < 0) {
70 bb_error_msg("[ERROR] [%s]: TEST FAILED", el->name);
71 tests_failed++;
72 }
73 tests_run++;
74 el = el->next;
75 }
76
77#if WANT_TIMING
78 gettimeofday(&end, NULL);
79 timeval_diff(&time_spent, &end, &begin);
80 bb_error_msg("Elapsed time %u.%06u seconds"
81 (int)time_spent.tv_sec,
82 (int)time_spent.tv_usec);
83#endif
84 if (tests_failed > 0) {
85 bb_error_msg("[ERROR] %u test(s) FAILED", tests_failed);
86 return EXIT_FAILURE;
87 }
88 bb_error_msg("All tests passed");
89 return EXIT_SUCCESS;
90}
diff --git a/libbb/copy_file.c b/libbb/copy_file.c
index be65c4b47..935a9a4c4 100644
--- a/libbb/copy_file.c
+++ b/libbb/copy_file.c
@@ -398,5 +398,9 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags)
398 bb_perror_msg("can't preserve %s of '%s'", "permissions", dest); 398 bb_perror_msg("can't preserve %s of '%s'", "permissions", dest);
399 } 399 }
400 400
401 if (flags & FILEUTILS_VERBOSE) {
402 printf("'%s' -> '%s'\n", source, dest);
403 }
404
401 return retval; 405 return retval;
402} 406}
diff --git a/libbb/make_directory.c b/libbb/make_directory.c
index 1f874f1f2..3980376ec 100644
--- a/libbb/make_directory.c
+++ b/libbb/make_directory.c
@@ -124,6 +124,10 @@ int FAST_FUNC bb_make_directory(char *path, long mode, int flags)
124 if (!c) { 124 if (!c) {
125 goto ret0; 125 goto ret0;
126 } 126 }
127 } else {
128 if (flags & FILEUTILS_VERBOSE) {
129 printf("created directory: '%s'\n", path);
130 }
127 } 131 }
128 132
129 if (!c) { 133 if (!c) {
diff --git a/libbb/obscure.c b/libbb/obscure.c
index 24c4ac917..ad17d1ff1 100644
--- a/libbb/obscure.c
+++ b/libbb/obscure.c
@@ -182,3 +182,41 @@ int FAST_FUNC obscure(const char *old, const char *newval, const struct passwd *
182 } 182 }
183 return 0; 183 return 0;
184} 184}
185
186#if ENABLE_UNIT_TEST
187
188/* Test obscure_msg() instead of obscure() in order not to print anything. */
189
190static const struct passwd pw = {
191 .pw_name = (char *)"johndoe",
192 .pw_gecos = (char *)"John Doe",
193};
194
195BBUNIT_DEFINE_TEST(obscure_weak_pass)
196{
197 /* Empty password */
198 BBUNIT_ASSERT_NOTNULL(obscure_msg("Ad4#21?'S|", "", &pw));
199 /* Pure numbers */
200 BBUNIT_ASSERT_NOTNULL(obscure_msg("Ad4#21?'S|", "23577315", &pw));
201 /* Similar to pw_name */
202 BBUNIT_ASSERT_NOTNULL(obscure_msg("Ad4#21?'S|", "johndoe123%", &pw));
203 /* Similar to pw_gecos, reversed */
204 BBUNIT_ASSERT_NOTNULL(obscure_msg("Ad4#21?'S|", "eoD nhoJ^44@", &pw));
205 /* Similar to the old password */
206 BBUNIT_ASSERT_NOTNULL(obscure_msg("Ad4#21?'S|", "d4#21?'S", &pw));
207 /* adjacent letters */
208 BBUNIT_ASSERT_NOTNULL(obscure_msg("Ad4#21?'S|", "qwerty123", &pw));
209 /* Many similar chars */
210 BBUNIT_ASSERT_NOTNULL(obscure_msg("Ad4#21?'S|", "^33Daaaaaa1", &pw));
211
212 BBUNIT_ENDTEST;
213}
214
215BBUNIT_DEFINE_TEST(obscure_strong_pass)
216{
217 BBUNIT_ASSERT_NULL(obscure_msg("Rt4##2&:'|", "}(^#rrSX3S*22", &pw));
218
219 BBUNIT_ENDTEST;
220}
221
222#endif /* ENABLE_UNIT_TEST */
diff --git a/libbb/remove_file.c b/libbb/remove_file.c
index 5b75f7f30..eaca293d9 100644
--- a/libbb/remove_file.c
+++ b/libbb/remove_file.c
@@ -78,6 +78,10 @@ int FAST_FUNC remove_file(const char *path, int flags)
78 return -1; 78 return -1;
79 } 79 }
80 80
81 if (flags & FILEUTILS_VERBOSE) {
82 printf("removed directory: '%s'\n", path);
83 }
84
81 return status; 85 return status;
82 } 86 }
83 87
@@ -98,5 +102,9 @@ int FAST_FUNC remove_file(const char *path, int flags)
98 return -1; 102 return -1;
99 } 103 }
100 104
105 if (flags & FILEUTILS_VERBOSE) {
106 printf("removed '%s'\n", path);
107 }
108
101 return 0; 109 return 0;
102} 110}
diff --git a/libbb/strrstr.c b/libbb/strrstr.c
index d8823fc51..93d970a1b 100644
--- a/libbb/strrstr.c
+++ b/libbb/strrstr.c
@@ -7,13 +7,7 @@
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree. 7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 */ 8 */
9 9
10#ifdef __DO_STRRSTR_TEST
11#include <stdlib.h>
12#include <string.h>
13#include <stdio.h>
14#else
15#include "libbb.h" 10#include "libbb.h"
16#endif
17 11
18/* 12/*
19 * The strrstr() function finds the last occurrence of the substring needle 13 * The strrstr() function finds the last occurrence of the substring needle
@@ -34,8 +28,9 @@ char* FAST_FUNC strrstr(const char *haystack, const char *needle)
34 } 28 }
35} 29}
36 30
37#ifdef __DO_STRRSTR_TEST 31#if ENABLE_UNIT_TEST
38int main(int argc, char **argv) 32
33BBUNIT_DEFINE_TEST(strrstr)
39{ 34{
40 static const struct { 35 static const struct {
41 const char *h, *n; 36 const char *h, *n;
@@ -59,13 +54,13 @@ int main(int argc, char **argv)
59 i = 0; 54 i = 0;
60 while (i < sizeof(test_array) / sizeof(test_array[0])) { 55 while (i < sizeof(test_array) / sizeof(test_array[0])) {
61 const char *r = strrstr(test_array[i].h, test_array[i].n); 56 const char *r = strrstr(test_array[i].h, test_array[i].n);
62 printf("'%s' vs. '%s': '%s' - ", test_array[i].h, test_array[i].n, r);
63 if (r == NULL) 57 if (r == NULL)
64 r = test_array[i].h - 1; 58 r = test_array[i].h - 1;
65 printf("%s\n", r == test_array[i].h + test_array[i].pos ? "PASSED" : "FAILED"); 59 BBUNIT_ASSERT_EQ(r, test_array[i].h + test_array[i].pos);
66 i++; 60 i++;
67 } 61 }
68 62
69 return 0; 63 BBUNIT_ENDTEST;
70} 64}
71#endif 65
66#endif /* ENABLE_UNIT_TEST */
diff --git a/libbb/sysconf.c b/libbb/sysconf.c
new file mode 100644
index 000000000..031901980
--- /dev/null
+++ b/libbb/sysconf.c
@@ -0,0 +1,22 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Various system configuration helpers.
4 *
5 * Copyright (C) 2014 Bartosz Golaszewski <bartekgola@gmail.com>
6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 */
9#include "libbb.h"
10
11#if !defined(ARG_MAX) && defined(_SC_ARG_MAX)
12unsigned FAST_FUNC bb_arg_max(void)
13{
14 return sysconf(_SC_ARG_MAX);
15}
16#endif
17
18/* Return the number of clock ticks per second. */
19unsigned FAST_FUNC bb_clk_tck(void)
20{
21 return sysconf(_SC_CLK_TCK);
22}