Home | History | Annotate | Line # | Download | only in kernel
gen_t_subr_prf revision 1.7
      1 #!/bin/sh
      2 
      3 cat << _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 <stdlib.h>
     10 #include <string.h>
     11 #include <sha2.h>
     12 
     13 #include <atf-c.h>
     14 
     15 /* Avoid SSP re-definitions */
     16 #undef snprintf
     17 #undef vsnprintf
     18 #undef sprintf
     19 #undef vsprintf
     20 #undef vasprintf
     21 
     22 #define KPRINTF_BUFSIZE 1024
     23 #undef putchar
     24 #define putchar xputchar
     25 
     26 #define	kmem_alloc(n, f)	malloc(n)
     27 
     28 static int putchar(char c, int foo, void *b)
     29 {
     30 	return fputc(c, stderr);
     31 }
     32 
     33 #define TOBUFONLY 1
     34 static const char HEXDIGITS[] = "0123456789ABCDEF";
     35 static const char hexdigits[] = "0123456789abcdef";
     36 
     37 typedef int device_t;
     38 
     39 #if 0
     40 static SHA512_CTX kprnd_sha;
     41 #endif
     42 
     43 #define timespec timeval
     44 #define nanotime(ts) gettimeofday(ts, NULL)
     45 
     46 #define device_xname(a) ""
     47 int kprintf(const char *, int, void *, char *, va_list) __printflike(1, 0);
     48 void device_printf(device_t, const char *, ...) __printflike(2, 3);
     49 
     50 static void
     51 empty(void)
     52 {
     53 }
     54 
     55 static void (*v_flush)(void) = empty;
     56 
     57 ATF_TC(snprintf_print);
     58 ATF_TC_HEAD(snprintf_print, tc)
     59 {
     60         atf_tc_set_md_var(tc, "descr", "checks snprintf print");
     61 }
     62  
     63 ATF_TC_BODY(snprintf_print, tc)
     64 {
     65 	char buf[13];
     66 	int i;
     67 
     68 	memset(buf, 'x', sizeof(buf));
     69 	i = snprintf(buf, sizeof(buf), "number %d", 10);
     70 	ATF_CHECK_EQ(i, 9);
     71 	ATF_CHECK_STREQ(buf, "number 10");
     72 }
     73 
     74 ATF_TC(snprintf_print_overflow);
     75 ATF_TC_HEAD(snprintf_print_overflow, tc)
     76 {
     77         atf_tc_set_md_var(tc, "descr", "checks snprintf print with overflow");
     78 }
     79  
     80 ATF_TC_BODY(snprintf_print_overflow, tc)
     81 {
     82 	char buf[10];
     83 	int i;
     84 
     85 	memset(buf, 'x', sizeof(buf));
     86 	i = snprintf(buf, sizeof(buf), "fjsdfsdjfsdf %d\n", 10);
     87 	ATF_CHECK_EQ(i, 16);
     88 	ATF_CHECK_STREQ(buf, "fjsdfsdjf");
     89 }
     90 
     91 ATF_TC(snprintf_count);
     92 ATF_TC_HEAD(snprintf_count, tc)
     93 {
     94         atf_tc_set_md_var(tc, "descr", "checks snprintf count");
     95 }
     96  
     97 ATF_TC_BODY(snprintf_count, tc)
     98 {
     99 	int i;
    100 	
    101 	i = snprintf(NULL, 20, "number %d", 10);
    102 	ATF_CHECK_EQ(i, 9);
    103 }
    104 
    105 ATF_TC(snprintf_count_overflow);
    106 ATF_TC_HEAD(snprintf_count_overflow, tc)
    107 {
    108         atf_tc_set_md_var(tc, "descr", "checks snprintf count with overflow");
    109 }
    110  
    111 ATF_TC_BODY(snprintf_count_overflow, tc)
    112 {
    113 	int i;
    114 
    115 	i = snprintf(NULL, 10, "fjsdfsdjfsdf %d\n", 10);
    116 	ATF_CHECK_EQ(i, 16);
    117 }
    118 
    119 ATF_TC(vasprintf_print);
    120 ATF_TC_HEAD(vasprintf_print, tc)
    121 {
    122         atf_tc_set_md_var(tc, "descr", "checks vasprintf works");
    123 }
    124 
    125 static int __printflike(2, 3)
    126 vasp_helper(char **buf, const char *fmt, ...)
    127 {
    128 	va_list ap;
    129 	int ret;
    130 
    131 	va_start(ap, fmt);
    132 	ret = vasprintf(buf, fmt, ap);
    133 	va_end(ap);
    134 	return ret;
    135 }
    136 
    137 ATF_TC_BODY(vasprintf_print, tc)
    138 {
    139 	int i;
    140 	char *buf;
    141 
    142 	buf = NULL;
    143 	i =  vasp_helper(&buf, "N=%d C=%c S=%s", 7, 'x', "abc");
    144 	ATF_CHECK_EQ(i, 13);
    145 	ATF_CHECK(buf != NULL);
    146 	ATF_CHECK_STREQ(buf, "N=7 C=x S=abc");
    147 	free(buf);
    148 }
    149 
    150 ATF_TP_ADD_TCS(tp)
    151 {
    152         ATF_TP_ADD_TC(tp, snprintf_print);
    153         ATF_TP_ADD_TC(tp, snprintf_print_overflow);
    154         ATF_TP_ADD_TC(tp, snprintf_count);
    155         ATF_TP_ADD_TC(tp, snprintf_count_overflow);
    156 	ATF_TP_ADD_TC(tp, vasprintf_print);
    157 
    158         return atf_no_error();
    159 }
    160 _EOF
    161 
    162 awk '
    163 /^snprintf\(/ {
    164 	print prevline
    165 	out = 1
    166 }
    167 {
    168 	if (out) print
    169 	else prevline = $0
    170 }' $1 >>$2
    171