root/test/util/opal_os_create_dirpath.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main
  2. test1
  3. test2
  4. test3

   1 /*
   2  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
   3  *                         University Research and Technology
   4  *                         Corporation.  All rights reserved.
   5  * Copyright (c) 2004-2005 The University of Tennessee and The University
   6  *                         of Tennessee Research Foundation.  All rights
   7  *                         reserved.
   8  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
   9  *                         University of Stuttgart.  All rights reserved.
  10  * Copyright (c) 2004-2005 The Regents of the University of California.
  11  *                         All rights reserved.
  12  * $COPYRIGHT$
  13  *
  14  * Additional copyrights may follow
  15  *
  16  * $HEADER$
  17  */
  18 
  19 #include "orte_config.h"
  20 
  21 #include <stdio.h>
  22 #include <string.h>
  23 #include <stdlib.h>
  24 #ifdef HAVE_UNISTD_H
  25 #include <unistd.h>
  26 #endif
  27 #ifdef HAVE_SYS_PARAM_H
  28 #include <sys/param.h>
  29 #endif
  30 #include <sys/stat.h>
  31 
  32 #include "support.h"
  33 #include "opal/runtime/opal.h"
  34 #include "opal/constants.h"
  35 #include "opal/util/os_path.h"
  36 #include "opal/util/os_dirpath.h"
  37 
  38 #define PATH_SEP "/"
  39 
  40 static const char *path_sep = PATH_SEP;
  41 
  42 static bool test1(void);   /* trivial test */
  43 static bool test2(void);   /* test existing path, both with and without correct mode */
  44 static bool test3(void);   /* test making a directory tree */
  45 
  46 
  47 int main(int argc, char* argv[])
  48 {
  49     opal_init(&argc, &argv);
  50 
  51     test_init("opal_os_create_dirpath_t");
  52 
  53     /* All done */
  54 
  55     if (test1()) {
  56         test_success();
  57     }
  58     else {
  59       test_failure("opal_os_create_dirpath test1 failed");
  60     }
  61 
  62     if (test2()) {
  63         test_success();
  64     }
  65     else {
  66       test_failure("opal_os_create_dirpath test2 failed");
  67     }
  68 
  69     if (test3()) {
  70         test_success();
  71     }
  72     else {
  73       test_failure("opal_os_create_dirpath test3 failed");
  74     }
  75 
  76     test_finalize();
  77     opal_finalize();
  78 
  79     return 0;
  80 }
  81 
  82 
  83 static bool test1(void)
  84 {
  85 
  86     /* Test trivial functionality. Program should return OPAL_ERROR when called with NULL path. */
  87 
  88     if (OPAL_SUCCESS == opal_os_dirpath_create(NULL, S_IRWXU))
  89             return(false);
  90 
  91     return true;
  92 }
  93 
  94 
  95 static bool test2(void)
  96 {
  97     char *tmp;
  98     struct stat buf;
  99 
 100     if (NULL == path_sep) {
 101         printf("test2 cannot be run\n");
 102         return(false);
 103     }
 104     tmp = opal_os_path(true, "tmp", NULL);
 105     if (0 != mkdir(tmp, S_IRWXU)) {
 106         printf("test2 could not be run - directory could not be made\n");
 107         return(false);
 108     }
 109 
 110     if (OPAL_SUCCESS != opal_os_dirpath_create(tmp, S_IRWXU)) {
 111         rmdir(tmp);
 112         return(false);
 113     }
 114 
 115     chmod(tmp, S_IRUSR);
 116 
 117     if (OPAL_SUCCESS != opal_os_dirpath_create(tmp, S_IRWXU)) {
 118         rmdir(tmp);
 119         return(false);
 120     }
 121 
 122     stat(tmp, &buf);
 123     if (S_IRWXU != (S_IRWXU & buf.st_mode)) {
 124         rmdir(tmp);
 125         return(false);
 126     }
 127 
 128     rmdir(tmp);
 129 
 130     free(tmp);
 131     return true;
 132 }
 133 
 134 
 135 static bool test3(void)
 136 {
 137     char *out;
 138     struct stat buf;
 139     char *a[] = { "aaa", "bbb", "ccc", NULL };
 140 
 141     if (NULL == path_sep) {
 142         printf("test3 cannot be run\n");
 143         return(false);
 144     }
 145 
 146     out = opal_os_path(true, a[0], a[1], a[2], NULL);
 147     if (OPAL_SUCCESS != opal_os_dirpath_create(out, S_IRWXU)) {
 148         out = opal_os_path(true, a[0], a[1], a[2], NULL);
 149         if (0 == stat(out, &buf))
 150             rmdir(out);
 151         out = opal_os_path(true, a[0], a[1], NULL);
 152         if (0 == stat(out, &buf))
 153             rmdir(out);
 154         out = opal_os_path(true, a[0], NULL);
 155         if (0 == stat(out, &buf))
 156             rmdir(out);
 157         return(false);
 158     }
 159 
 160     free(out);
 161 
 162     return(true);
 163 }

/* [<][>][^][v][top][bottom][index][help] */