1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
/* -*- C -*-
Copyright (C) 2010, 2011 Rocky Bernstein <rocky@gnu.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
Unit test for lib/driver/gnu_linux.c
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
# define __CDIO_CONFIG_H__ 1
#endif
#ifdef HAVE_STDIO_H
# include <stdio.h>
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_LIMITS_H
# include <limits.h>
#endif
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_ERRNO_H
# include <errno.h>
#endif
#include <cdio/util.h>
#define MY_DIR_SEPARATOR '/'
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
static char *
get_temporary_name(const char *dirname, const char *errmsg)
{
char *new_filename = tempnam(NULL, "syml");
if (NULL == new_filename) {
printf("Could not generate %s name\n", errmsg);
}
return new_filename;
}
static int check_rc(int rc, const char *psz_operation,
const char *psz_filename)
{
if (-1 == rc) {
printf("%s %s failed with error: %s\n",
psz_operation, psz_filename, strerror(errno));
} else if (0 != rc) {
printf("%s %s gives weird return %d\n",
psz_operation, psz_filename, rc);
}
return rc;
}
int
main(int argc, const char *argv[])
{
char *psz_tmp_subdir;
char *psz_orig_file;
char tmp_dir[PATH_MAX+1] = {0};
char tmp_subdir[PATH_MAX+1] = {0};
char psz_file_check[PATH_MAX+1];
char *psz_last_slash;
unsigned int i_last_slash;
char *psz_symlink_file = NULL;
psz_tmp_subdir = get_temporary_name(NULL, "temporary directory");
if (NULL == psz_tmp_subdir) {
exit(77);
}
if (NULL == psz_tmp_subdir) {
printf("extract parent directory from temporary directory name\n");
exit(77);
}
if (-1 == check_rc(mkdir(psz_tmp_subdir, 0755),
"mkdir", psz_tmp_subdir))
exit(77);
cdio_realpath(psz_tmp_subdir, tmp_subdir);
if (0 == strlen(tmp_subdir)) {
fprintf(stderr, "cdio_realpath on temp directory %s failed\n",
psz_tmp_subdir);
exit(1);
}
psz_last_slash = strrchr(tmp_subdir, MY_DIR_SEPARATOR);
i_last_slash = psz_last_slash - tmp_subdir + 1;
memcpy(tmp_dir, tmp_subdir, i_last_slash);
tmp_dir[i_last_slash] = '\0';
printf("Temp directory is %s\n", tmp_dir);
psz_orig_file = get_temporary_name(NULL, "file");
if (NULL != psz_orig_file) {
FILE *fp = fopen(psz_orig_file, "w");
char orig_file[PATH_MAX+1] = {0};
int rc;
char symlink_file[PATH_MAX+1] = {0};
fprintf(fp, "testing\n");
fclose(fp);
cdio_realpath(psz_orig_file, orig_file);
if (0 == strlen(orig_file)) {
fprintf(stderr, "cdio_realpath on temp file %s failed\n",
psz_orig_file);
exit(2);
}
psz_symlink_file = get_temporary_name(NULL, "symlink file");
rc = check_rc(symlink(psz_orig_file, psz_symlink_file),
"symlink", psz_symlink_file);
if (0 == rc) {
/* Just when you thought we'd forgotten, here is our first
test! */
cdio_realpath(psz_symlink_file, psz_file_check);
if (0 != strncmp(psz_file_check, orig_file, PATH_MAX)) {
fprintf(stderr, "simple cdio_realpath failed: %s vs %s\n",
psz_file_check, orig_file);
exit(3);
}
check_rc(unlink(psz_symlink_file), "unlink", psz_symlink_file);
}
/* Make sure we handle a cyclic symbolic name, e.g. xx -> xx */
cdio_realpath(psz_symlink_file, symlink_file);
rc = check_rc(symlink(psz_symlink_file, psz_symlink_file),
"symlink", psz_symlink_file);
if (0 == rc) {
cdio_realpath(psz_symlink_file, psz_file_check);
if (0 != strncmp(psz_file_check, symlink_file, PATH_MAX)) {
fprintf(stderr, "direct cdio_realpath cycle test failed. %s vs %s\n",
psz_file_check, symlink_file);
exit(4);
}
check_rc(unlink(psz_symlink_file), "unlink", psz_symlink_file);
}
}
check_rc(unlink(psz_orig_file), "unlink", psz_orig_file);
check_rc(rmdir(psz_tmp_subdir), "rmdir", psz_tmp_subdir);
return 0;
}
|