00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "config.h"
00019
00020 #if !defined(DEBUG)
00021 #define DEBUG 1
00022 #endif
00023
00024 #include <string.h>
00025 #include <errno.h>
00026 #include <assert.h>
00027 #include <assert_pp.h>
00028
00029 #include <rk.h>
00030
00031 #if defined(RK_STUB)
00032 #include <rk_stub.h>
00033 #endif
00034
00035 #include <timesFile.h>
00036
00037
00038
00039
00040
00041
00042
00043
00044 static int rbMakeBadFile(char *file_name, char *body)
00045 {
00046 int retval = 0;
00047 FILE *file;
00048
00049 require(file_name != NULL);
00050 require(body != NULL);
00051
00052 if( (file = fopen(file_name, "w")) != NULL )
00053 {
00054 fprintf(file, "%s", body);
00055 fclose(file);
00056
00057 retval = 1;
00058 }
00059 return( retval );
00060 }
00061
00062 int main(int argc, char *argv[])
00063 {
00064 struct cpu_reserve_attr cra, cra2;
00065 rk_resource_set_t rs;
00066 rk_reserve_t cr;
00067 int retval = 0;
00068 pid_t pid;
00069
00070 #if defined(RK_STUB)
00071 rk_stub_set_mode(RK_STUB_SIM);
00072 #endif
00073
00074
00075 #if defined(RK_STUB)
00076 pid = rk_stub_mk_pid("dummy", NULL, NULL, NULL);
00077 #else
00078 pid = getpid();
00079 #endif
00080 memset(&cra, 0, sizeof(cra));
00081
00082
00083 assert(tfMakeTimesFile("test_cpu_times",
00084 (const float[]){ 50.0, TIMES_FILE_TERMINATOR }));
00085 assert(rbMakeBadFile("bad1_cpu_times", "-1.0"));
00086 assert(rbMakeBadFile("bad2_cpu_times", "200.0"));
00087 assert(rbMakeBadFile("bad3_cpu_times", "foo"));
00088
00089
00090 assert(rk_resource_set_create(NULL) == NULL);
00091 assert(errno == EINVAL);
00092
00093 assert(rk_resource_set_create("") == NULL);
00094 assert(errno == EINVAL);
00095
00096 {
00097 char bad_name[RSET_NAME_LEN + 1];
00098
00099 memset(bad_name, 'a', sizeof(bad_name));
00100 bad_name[sizeof(bad_name) - 1] = '\0';
00101 assert(rk_resource_set_create(bad_name) == NULL);
00102 assert(errno == ENAMETOOLONG);
00103
00104 bad_name[0] = 200;
00105 bad_name[1] = '\0';
00106 assert(rk_resource_set_create(bad_name) == NULL);
00107 assert(errno == EINVAL);
00108 }
00109
00110 rs = rk_resource_set_create("test");
00111
00112
00113 rk_resource_set_destroy(rs);
00114 rk_resource_set_destroy(rs);
00115 rk_resource_set_destroy(NULL);
00116
00117
00118 rs = rk_resource_set_create("test");
00119
00120 assert(rk_resource_set_attach_process(NULL, -1) == -1 && errno == EINVAL);
00121 assert(rk_resource_set_attach_process(rs, -1) == -1 && errno == EINVAL);
00122 assert(rk_resource_set_attach_process(rs, 200000) == -1 && errno == ESRCH);
00123 #if defined(RK_STUB)
00124 assert(rk_resource_set_attach_process(rs, MAX_PCB) == -1 &&
00125 errno == ESRCH);
00126 #endif
00127 assert(rk_resource_set_attach_process(rs, pid + 1) == -1 &&
00128 errno == ESRCH);
00129 assert(rk_resource_set_attach_process(rs, pid) == 0);
00130
00131
00132 assert(rk_resource_set_detach_process(NULL, -1) == -1 && errno == EINVAL);
00133 assert(rk_resource_set_detach_process(rs, -1) == -1 && errno == EINVAL);
00134 assert(rk_resource_set_detach_process(rs, 200000) == -1 &&
00135 errno == EINVAL);
00136 assert(rk_resource_set_detach_process(rs, pid + 1) == -1 &&
00137 errno == ESRCH);
00138 assert(rk_resource_set_detach_process(rs, pid) == 0);
00139
00140
00141 assert(rk_resource_set_set_name(NULL, NULL) == -1 && errno == EINVAL);
00142 assert(rk_resource_set_set_name(rs, NULL) == -1 && errno == EINVAL);
00143 assert(rk_resource_set_set_name(rs, "") == -1 && errno == EINVAL);
00144 assert(rk_resource_set_set_name(rs, "test2") == 0);
00145
00146
00147 {
00148 char name[RSET_NAME_LEN];
00149
00150 assert(rk_resource_set_get_name(NULL, NULL) == -1 && errno == EINVAL);
00151 assert(rk_resource_set_get_name(rs, NULL) == -1 && errno == EINVAL);
00152 assert(rk_resource_set_get_name(rs, name) == 0);
00153 assert(strcmp(name, "test2") == 0);
00154
00155 assert(rk_resource_set_set_name(rs, "test") == 0);
00156 }
00157
00158
00159 rk_resource_set_destroy(rs);
00160 cra.compute_time.tv_sec = 1;
00161 cra.period.tv_sec = 2;
00162 cra.deadline.tv_sec = 2;
00163 cra.reserve_type.sch_mode =
00164 cra.reserve_type.enf_mode =
00165 cra.reserve_type.rep_mode = RSV_SOFT;
00166
00167
00168 assert((rs = rk_resource_set_create("nonexistent")) != NULL);
00169 assert(rk_cpu_reserve_create(rs, &cr, &cra) == -1 && errno == ENOENT);
00170 rk_resource_set_destroy(rs);
00171
00172 assert((rs = rk_resource_set_create("bad1")) != NULL);
00173 #if defined(EFTYPE)
00174 assert(rk_cpu_reserve_create(rs, &cr, &cra) == -1 && errno == EFTYPE);
00175 #else
00176 assert(rk_cpu_reserve_create(rs, &cr, &cra) == -1 && errno == EINVAL);
00177 #endif
00178 rk_resource_set_destroy(rs);
00179
00180 assert((rs = rk_resource_set_create("bad2")) != NULL);
00181 #if defined(EFTYPE)
00182 assert(rk_cpu_reserve_create(rs, &cr, &cra) == -1 && errno == EFTYPE);
00183 #else
00184 assert(rk_cpu_reserve_create(rs, &cr, &cra) == -1 && errno == EINVAL);
00185 #endif
00186 rk_resource_set_destroy(rs);
00187
00188 assert((rs = rk_resource_set_create("bad3")) != NULL);
00189 #if defined(EFTYPE)
00190 assert(rk_cpu_reserve_create(rs, &cr, &cra) == -1 && errno == EFTYPE);
00191 #else
00192 assert(rk_cpu_reserve_create(rs, &cr, &cra) == -1 && errno == EINVAL);
00193 #endif
00194 rk_resource_set_destroy(rs);
00195
00196 memset(&cra, 0, sizeof(cra));
00197
00198 assert((rs = rk_resource_set_create("test")) != NULL);
00199
00200
00201 assert(rk_cpu_reserve_create(NULL, NULL, NULL) == -1 && errno == EINVAL);
00202 assert(rk_cpu_reserve_create(rs, NULL, NULL) == -1 && errno == EINVAL);
00203 assert(rk_cpu_reserve_create(rs, &cr, NULL) == -1 && errno == EINVAL);
00204 assert(rk_cpu_reserve_create(rs, &cr, &cra) == -1 && errno == EINVAL);
00205 cra.compute_time.tv_sec = 1;
00206 assert(rk_cpu_reserve_create(rs, &cr, &cra) == -1 && errno == EINVAL);
00207 cra.deadline.tv_sec = 2;
00208 assert(rk_cpu_reserve_create(rs, &cr, &cra) == -1 && errno == EINVAL);
00209 cra.period.tv_sec = 2;
00210 assert(rk_cpu_reserve_create(rs, &cr, &cra) == -1 && errno == EINVAL);
00211 cra.reserve_type.sch_mode =
00212 cra.reserve_type.enf_mode =
00213 cra.reserve_type.rep_mode = 5000;
00214 assert(rk_cpu_reserve_create(rs, &cr, &cra) == -1 && errno == EINVAL);
00215 cra.reserve_type.sch_mode =
00216 cra.reserve_type.enf_mode =
00217 cra.reserve_type.rep_mode = RSV_SOFT;
00218 assert(rk_cpu_reserve_create(rs, &cr, &cra) == 0);
00219
00220
00221 rk_cpu_reserve_delete(NULL);
00222 rk_cpu_reserve_delete(rs);
00223 rk_cpu_reserve_delete(rs);
00224
00225
00226 assert(rk_cpu_reserve_create(rs, &cr, &cra) == 0);
00227
00228 assert(rk_cpu_reserve_get_attr(NULL, NULL) == -1 && errno == EINVAL);
00229 assert(rk_cpu_reserve_get_attr(cr, NULL) == -1 && errno == EINVAL);
00230 assert(rk_cpu_reserve_get_attr(cr, &cra2) == 0);
00231 assert(memcmp(&cra, &cra2, sizeof(cra)) == 0);
00232
00233
00234 memset(&cra, 0, sizeof(cra));
00235 assert(rk_cpu_reserve_ctl(rs, NULL) == -1 && errno == EINVAL);
00236 assert(rk_cpu_reserve_ctl(rs, &cra) == -1 && errno == ENOSPC);
00237 cra.compute_time.tv_sec = 1;
00238 assert(rk_cpu_reserve_ctl(rs, &cra) == -1 && errno == ENOSPC);
00239 cra.deadline.tv_sec = 2;
00240 assert(rk_cpu_reserve_ctl(rs, &cra) == -1 && errno == ENOSPC);
00241 cra.period.tv_sec = 2;
00242 assert(rk_cpu_reserve_ctl(rs, &cra) == -1 && errno == ENOSPC);
00243 cra.reserve_type.sch_mode =
00244 cra.reserve_type.enf_mode =
00245 cra.reserve_type.rep_mode = 5000;
00246 assert(rk_cpu_reserve_ctl(rs, &cra) == -1 && errno == ENOSPC);
00247 cra.reserve_type.sch_mode =
00248 cra.reserve_type.enf_mode =
00249 cra.reserve_type.rep_mode = RSV_SOFT;
00250 assert(rk_cpu_reserve_ctl(rs, &cra) == 0);
00251
00252 rk_cpu_reserve_delete(rs);
00253
00254 assert(rk_cpu_reserve_ctl(rs, &cra) == -1 && errno == ENOTCONN);
00255
00256 rk_resource_set_destroy(rs);
00257
00258 return( retval );
00259 }