Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

rk_bad.c

Go to the documentation of this file.
00001 /*
00002  * rk_bad.c
00003  *
00004  * Copyright (c) 2003 The University of Utah and the Flux Group.
00005  * All rights reserved.
00006  *
00007  * This file is licensed under the terms of the GNU Public License.  
00008  * See the file "license.terms" for restrictions on redistribution 
00009  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
00010  */
00011 
00012 /**
00013  * @file rk_bad.c
00014  *
00015  * Test the rk functions to make sure they detect bad inputs.
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  * Make a bad input file.
00039  *
00040  * @param file_name The name of the file to create.
00041  * @param body The body of the newly created file.
00042  * @return True if the file was created, false otherwise.
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     /* ... prime some variables for use later on */
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     /* Make some good/bad input files. */
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     /* BEGIN rk_resource_set_create */
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     /* BEGIN rk_resource_set_destroy */
00113     rk_resource_set_destroy(rs);
00114     rk_resource_set_destroy(rs);
00115     rk_resource_set_destroy(NULL);
00116     
00117     /* BEGIN rk_resource_set_attach_process */
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     /* BEGIN rk_resource_set_detach_process */
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     /* BEGIN rk_resource_set_set_name */
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     /* BEGIN rk_resource_set_get_name */
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         /* Need to reset it to 'test' since thats what the files are called. */
00155         assert(rk_resource_set_set_name(rs, "test") == 0);
00156     }
00157     
00158     /* BEGIN rk_cpu_reserve_create */
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     /* ... test bad input files */
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     /* ... test regular inputs */
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     /* BEGIN rk_cpu_reserve_delete */
00221     rk_cpu_reserve_delete(NULL);
00222     rk_cpu_reserve_delete(rs);
00223     rk_cpu_reserve_delete(rs);
00224     
00225     /* BEGIN rk_cpu_reserve_get_attr */
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     /* BEGIN rk_cpu_reserve_ctl */
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 }

Generated on Mon Dec 1 16:29:07 2003 for CPU Broker by doxygen 1.3.4