xref: /freebsd/contrib/netbsd-tests/kernel/gen_t_subr_prf (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1#!/bin/sh
2
3cat << _EOF > $2
4#include <sys/types.h>
5#include <sys/time.h>
6#include <stdio.h>
7#include <stdarg.h>
8#include <stdint.h>
9#include <string.h>
10#include <sha2.h>
11
12#include <atf-c.h>
13
14/* Avoid SSP re-definitions */
15#undef snprintf
16#undef vsnprintf
17#undef sprintf
18#undef vsprintf
19
20#define KPRINTF_BUFSIZE 1024
21#undef putchar
22#define putchar xputchar
23
24static int putchar(char c, int foo, void *b)
25{
26	return fputc(c, stderr);
27}
28
29#define TOBUFONLY 1
30static const char HEXDIGITS[] = "0123456789ABCDEF";
31static const char hexdigits[] = "0123456789abcdef";
32
33typedef int device_t;
34
35#if 0
36static SHA512_CTX kprnd_sha;
37#endif
38
39#define timespec timeval
40#define nanotime(ts) gettimeofday(ts, NULL)
41
42#define device_xname(a) ""
43int kprintf(const char *, int, void *, char *, va_list) __printflike(1, 0);
44void device_printf(device_t, const char *, ...) __printflike(2, 3);
45
46static void
47empty(void)
48{
49}
50
51static void (*v_flush)(void) = empty;
52
53ATF_TC(snprintf_print);
54ATF_TC_HEAD(snprintf_print, tc)
55{
56        atf_tc_set_md_var(tc, "descr", "checks snprintf print");
57}
58 
59ATF_TC_BODY(snprintf_print, tc)
60{
61	char buf[10];
62	int i;
63
64	memset(buf, 'x', sizeof(buf));
65	i = snprintf(buf, sizeof(buf), "number %d", 10);
66	ATF_CHECK_EQ(i, 9);
67	ATF_CHECK_STREQ(buf, "number 10");
68}
69
70ATF_TC(snprintf_print_overflow);
71ATF_TC_HEAD(snprintf_print_overflow, tc)
72{
73        atf_tc_set_md_var(tc, "descr", "checks snprintf print with overflow");
74}
75 
76ATF_TC_BODY(snprintf_print_overflow, tc)
77{
78	char buf[10];
79	int i;
80
81	memset(buf, 'x', sizeof(buf));
82	i = snprintf(buf, sizeof(buf), "fjsdfsdjfsdf %d\n", 10);
83	ATF_CHECK_EQ(i, 16);
84	ATF_CHECK_STREQ(buf, "fjsdfsdjf");
85}
86
87ATF_TC(snprintf_count);
88ATF_TC_HEAD(snprintf_count, tc)
89{
90        atf_tc_set_md_var(tc, "descr", "checks snprintf count");
91}
92 
93ATF_TC_BODY(snprintf_count, tc)
94{
95	int i;
96	
97	i = snprintf(NULL, 20, "number %d", 10);
98	ATF_CHECK_EQ(i, 9);
99}
100
101ATF_TC(snprintf_count_overflow);
102ATF_TC_HEAD(snprintf_count_overflow, tc)
103{
104        atf_tc_set_md_var(tc, "descr", "checks snprintf count with overflow");
105}
106 
107ATF_TC_BODY(snprintf_count_overflow, tc)
108{
109	int i;
110
111	i = snprintf(NULL, 10, "fjsdfsdjfsdf %d\n", 10);
112	ATF_CHECK_EQ(i, 16);
113}
114
115ATF_TP_ADD_TCS(tp)
116{
117        ATF_TP_ADD_TC(tp, snprintf_print);
118        ATF_TP_ADD_TC(tp, snprintf_print_overflow);
119        ATF_TP_ADD_TC(tp, snprintf_count);
120        ATF_TP_ADD_TC(tp, snprintf_count_overflow);
121
122        return atf_no_error();
123}
124_EOF
125
126awk '
127/^snprintf\(/ {
128	print prevline
129	out = 1
130}
131{
132	if (out) print
133	else prevline = $0
134}' $1 >>$2
135