summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorjvoisin2023-07-09 18:03:34 +0200
committerjvoisin2023-07-09 18:03:34 +0200
commitf9f83da31803062d1fb665ad612ccede5e445757 (patch)
tree13216a6e43b1d17e17ef8b26c80fcc30c6abf7df /tests
parent33acec595b47ec97ae57a98ec396a26f6d8e309e (diff)
Add tests for stdio.h
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile6
-rw-r--r--tests/test_fread_int_overflow.c14
-rw-r--r--tests/test_fread_overwrite.c18
-rw-r--r--tests/test_fwrite_int_overflow.c14
-rw-r--r--tests/test_fwrite_overwrite.c18
-rw-r--r--tests/test_vsnprintf.c28
-rw-r--r--tests/test_vsprintf.c28
7 files changed, 126 insertions, 0 deletions
diff --git a/tests/Makefile b/tests/Makefile
index d240a97..49ae984 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -51,6 +51,12 @@ TARGETS= \
51 test_readlink \ 51 test_readlink \
52 test_ttyname_r \ 52 test_ttyname_r \
53 test_write \ 53 test_write \
54 test_fread_int_overflow \
55 test_fread_overwrite \
56 test_fwrite_int_overflow \
57 test_fwrite_overwrite \
58 test_vsnprintf \
59 test_vsprintf \
54 60
55.SILENT: 61.SILENT:
56 62
diff --git a/tests/test_fread_int_overflow.c b/tests/test_fread_int_overflow.c
new file mode 100644
index 0000000..2f0c43f
--- /dev/null
+++ b/tests/test_fread_int_overflow.c
@@ -0,0 +1,14 @@
1#include "common.h"
2
3#include <stdio.h>
4
5int main(int argc, char** argv) {
6 char buffer[12] = {0};
7
8 CHK_FAIL_START
9 fread(buffer, (size_t)-1, (size_t)-1, NULL);
10 CHK_FAIL_END
11
12 puts(buffer);
13 return ret;
14}
diff --git a/tests/test_fread_overwrite.c b/tests/test_fread_overwrite.c
new file mode 100644
index 0000000..2710d24
--- /dev/null
+++ b/tests/test_fread_overwrite.c
@@ -0,0 +1,18 @@
1#include <assert.h>
2
3#include "common.h"
4
5#include <stdio.h>
6
7int main(int argc, char** argv) {
8 char buffer[12] = {0};
9
10 assert(sizeof(buffer - 2) * 10 > sizeof(buffer));
11
12 CHK_FAIL_START
13 fread(buffer, sizeof(buffer) - 2, 10, NULL);
14 CHK_FAIL_END
15
16 puts(buffer);
17 return ret;
18}
diff --git a/tests/test_fwrite_int_overflow.c b/tests/test_fwrite_int_overflow.c
new file mode 100644
index 0000000..1655498
--- /dev/null
+++ b/tests/test_fwrite_int_overflow.c
@@ -0,0 +1,14 @@
1#include "common.h"
2
3#include <stdio.h>
4
5int main(int argc, char** argv) {
6 char buffer[12] = {0};
7
8 CHK_FAIL_START
9 fwrite(buffer, (size_t)-1, (size_t)-1, NULL);
10 CHK_FAIL_END
11
12 puts(buffer);
13 return ret;
14}
diff --git a/tests/test_fwrite_overwrite.c b/tests/test_fwrite_overwrite.c
new file mode 100644
index 0000000..b3767f8
--- /dev/null
+++ b/tests/test_fwrite_overwrite.c
@@ -0,0 +1,18 @@
1#include <assert.h>
2
3#include "common.h"
4
5#include <stdio.h>
6
7int main(int argc, char** argv) {
8 char buffer[12] = {0};
9
10 assert(sizeof(buffer - 2) * 10 > sizeof(buffer));
11
12 CHK_FAIL_START
13 fwrite(buffer, sizeof(buffer) - 2, 10, NULL);
14 CHK_FAIL_END
15
16 puts(buffer);
17 return ret;
18}
diff --git a/tests/test_vsnprintf.c b/tests/test_vsnprintf.c
new file mode 100644
index 0000000..f1263a8
--- /dev/null
+++ b/tests/test_vsnprintf.c
@@ -0,0 +1,28 @@
1#include "common.h"
2
3#include <stdarg.h>
4#include <unistd.h>
5
6char buffer[12] = {0};
7
8int msg_valid(int n, const char * format, ... ) {
9 va_list args;
10 va_start (args, format);
11 vsnprintf(buffer, n, format, args);
12 va_end (args);
13}
14
15int msg(int n, const char * format, ... ) {
16 va_list args;
17 va_start (args, format);
18 CHK_FAIL_START
19 vsnprintf(buffer, n, format, args);
20 CHK_FAIL_END
21 va_end (args);
22 return ret;
23}
24
25int main(int argc, char** argv) {
26 msg_valid(sizeof(buffer), "%s", "1234567890ABCDEF");
27 return msg(sizeof(buffer)+1, "%s", "1234567890ABCDEF");
28}
diff --git a/tests/test_vsprintf.c b/tests/test_vsprintf.c
new file mode 100644
index 0000000..740816f
--- /dev/null
+++ b/tests/test_vsprintf.c
@@ -0,0 +1,28 @@
1#include "common.h"
2
3#include <stdarg.h>
4#include <unistd.h>
5
6char buffer[12] = {0};
7
8int msg_valid(const char * format, ... ) {
9 va_list args;
10 va_start (args, format);
11 vsprintf(buffer, format, args);
12 va_end (args);
13}
14
15int msg(int n, const char * format, ... ) {
16 va_list args;
17 va_start (args, format);
18 CHK_FAIL_START
19 vsprintf(buffer, format, args);
20 CHK_FAIL_END
21 va_end (args);
22 return ret;
23}
24
25int main(int argc, char** argv) {
26 msg_valid("%s", "1234567");
27 return msg("%s", "1234567890ABCDEF");
28}