00001 #include <string.h>
00002 #include <syslog.h>
00003 #include <stdlib.h>
00004 #include <stdio.h>
00005 #include <sys/stat.h>
00006 #include <sys/time.h>
00007 #include <sys/types.h>
00008 #include <fcntl.h>
00009 #include <signal.h>
00010 #include <u/libu.h>
00011
00012 struct itimerval itv;
00013 size_t file_size, buf_size;
00014
00015 U_TEST_MODULE(misc);
00016
00017 static void setitv(struct itimerval *pitv)
00018 {
00019 memset(pitv, 0, sizeof(struct itimerval));
00020
00021 pitv->it_value.tv_sec = 0;
00022 pitv->it_value.tv_usec = 1;
00023 }
00024
00025 static void onsigalrm(int s)
00026 {
00027 u_unused_args(s);
00028 setitv(&itv);
00029 con_if(setitimer(ITIMER_REAL, &itv, NULL) < 0);
00030 }
00031
00032 static int cat_file(const char *fn, unsigned int *phash)
00033 {
00034 ssize_t tot = 0, c = 0;
00035 unsigned int hash = 0;
00036 int fd, i;
00037 struct stat st;
00038 char *buf = NULL;
00039
00040 con_err_sif(stat(fn, &st) < 0);
00041
00042 buf = u_malloc(buf_size);
00043 con_err_if(buf == NULL);
00044
00045 memset(buf, 0, buf_size);
00046
00047 fd = open(fn, O_NONBLOCK | O_RDONLY);
00048 con_err_if(fd < 0);
00049
00050 for(tot = 0; ; tot += c)
00051 {
00052 c = u_read(fd, buf, buf_size);
00053
00054 con_err_sif(c < 0);
00055
00056 for(i = 0; i < c; ++i)
00057 hash += buf[i];
00058
00059 dbg_ifb(c == 0)
00060 break;
00061 }
00062
00063 close(fd);
00064
00065 *phash = hash;
00066
00067 con_err_ifm(st.st_size != tot, "file size differs (%d != %d)",
00068 st.st_size, tot);
00069
00070 u_free(buf);
00071
00072 return 0;
00073 err:
00074 if(buf)
00075 u_free(buf);
00076 return 1;
00077 }
00078
00079 static int gen_file(const char *fn, unsigned int *phash)
00080 {
00081 int fd = -1;
00082 char *buf = NULL;
00083 unsigned int hash = 0;
00084 size_t i, c, size = file_size;
00085
00086 buf = u_malloc(buf_size);
00087 con_err_if(buf == NULL);
00088
00089 memset(buf, 0, buf_size);
00090
00091 fd = open(fn, O_CREAT | O_WRONLY | O_NONBLOCK, 0600);
00092 con_err_sif(fd < 0);
00093
00094 while(size)
00095 {
00096 c = (size < buf_size ? size : buf_size);
00097
00098 for(i = 0; i < c; ++i)
00099 buf[i] = i;
00100
00101 con_err_if(u_write(fd, buf, c) < 0);
00102
00103 for(i = 0; i < c; ++i)
00104 hash += buf[i];
00105
00106 size -= c;
00107 }
00108
00109 close(fd);
00110 u_free(buf);
00111
00112 *phash = hash;
00113
00114 return 0;
00115 err:
00116 if(fd >= 0)
00117 close(fd);
00118 if(buf)
00119 u_free(buf);
00120 return ~0;
00121 }
00122
00123 static int tmp_u_rdwr(int rd, const char *fn, unsigned int *phash)
00124 {
00125 int read_rc, write_rc;
00126
00127
00128 signal(SIGALRM, onsigalrm);
00129 setitv(&itv);
00130 con_err_if(setitimer(ITIMER_REAL, &itv, NULL) < 0);
00131
00132 if(rd)
00133 read_rc = cat_file(fn, phash);
00134 else
00135 write_rc = gen_file(fn, phash);
00136
00137
00138 signal(SIGALRM, SIG_IGN);
00139 memset(&itv, 0, sizeof(itv));
00140 setitimer(ITIMER_REAL, &itv, &itv);
00141 signal(SIGALRM, SIG_DFL);
00142
00143 if(rd)
00144 con_err_if(read_rc);
00145 else
00146 con_err_if(write_rc);
00147
00148 return 0;
00149 err:
00150 return ~0;
00151 }
00152
00153 static int test_u_rdwr(void)
00154 {
00155 char fn[U_FILENAME_MAX + 1] = { 0 };
00156 unsigned int hash_read, hash_write;
00157 int i;
00158
00159 buf_size = 1;
00160 file_size = 1013;
00161
00162
00163 for(i = 0; i < 10; ++i)
00164 {
00165
00166 buf_size = (buf_size << 1) + 1;
00167 file_size = (file_size << 1) + 1;
00168
00169 con_err_if(tmpnam(fn) == NULL);
00170
00171 con_err_if(tmp_u_rdwr(0 , fn, &hash_write));
00172
00173 con_err_if(tmp_u_rdwr(1 , fn, &hash_read));
00174
00175 con_err_if(hash_read != hash_write);
00176
00177 unlink(fn);
00178 }
00179
00180 return 0;
00181 err:
00182 con("failed. file: %s file_size: %d, buf_size: %d", fn,
00183 file_size, buf_size);
00184 return 1;
00185 }
00186
00187 static int test_u_str(void)
00188 {
00189 u_string_t *s = NULL;
00190
00191 con_err_if(u_string_create("0", 1, &s));
00192
00193 con_err_if(strcmp(u_string_c(s), "0"));
00194
00195 con_err_if(u_string_sprintf(s, "%s", "1"));
00196 con_err_if(strcmp(u_string_c(s), "1"));
00197
00198 con_err_if(u_string_aprintf(s, "%s", "23"));
00199 con_err_if(strcmp(u_string_c(s), "123"));
00200
00201 con_err_if(u_string_cat(s, "45"));
00202 con_err_if(strcmp(u_string_c(s), "12345"));
00203
00204 con_err_if(u_string_ncat(s, "6777", 2));
00205 con_err_if(strcmp(u_string_c(s), "1234567"));
00206
00207 con_err_if(u_string_sprintf(s, "reset"));
00208 con_err_if(strcmp(u_string_c(s), "reset"));
00209
00210 u_string_free(s);
00211
00212 return 0;
00213 err:
00214 return ~0;
00215 }
00216
00217 U_TEST_MODULE(misc)
00218 {
00219 U_TEST_RUN( test_u_rdwr );
00220 U_TEST_RUN( test_u_str );
00221
00222 return 0;
00223 }
00224
00225
00226