1f08c3bdfSopenharmony_ci/* 2f08c3bdfSopenharmony_ci * 3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2002 4f08c3bdfSopenharmony_ci * 5f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify 6f08c3bdfSopenharmony_ci * it under the terms of the GNU General Public License as published by 7f08c3bdfSopenharmony_ci * the Free Software Foundation; either version 2 of the License, or 8f08c3bdfSopenharmony_ci * (at your option) any later version. 9f08c3bdfSopenharmony_ci * 10f08c3bdfSopenharmony_ci * This program is distributed in the hope that it will be useful, 11f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 12f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 13f08c3bdfSopenharmony_ci * the GNU General Public License for more details. 14f08c3bdfSopenharmony_ci * 15f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License 16f08c3bdfSopenharmony_ci * along with this program; if not, write to the Free Software 17f08c3bdfSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18f08c3bdfSopenharmony_ci */ 19f08c3bdfSopenharmony_ci 20f08c3bdfSopenharmony_ci/* 10/31/2002 Port to LTP robbiew@us.ibm.com */ 21f08c3bdfSopenharmony_ci/* 06/30/2001 Port to Linux nsharoff@us.ibm.com */ 22f08c3bdfSopenharmony_ci 23f08c3bdfSopenharmony_ci/* 24f08c3bdfSopenharmony_ci * NAME 25f08c3bdfSopenharmony_ci * lib64.c - This file contains code for common failure conditions 26f08c3bdfSopenharmony_ci */ 27f08c3bdfSopenharmony_ci 28f08c3bdfSopenharmony_ci#include "nftw64.h" 29f08c3bdfSopenharmony_ci 30f08c3bdfSopenharmony_ciextern int s2; 31f08c3bdfSopenharmony_ci 32f08c3bdfSopenharmony_cistatic char *tmp_path = "./tmp"; 33f08c3bdfSopenharmony_cistatic const char *no_file = "./tmp/no_such_file"; 34f08c3bdfSopenharmony_cistatic const char *is_a_file = "./tmp/is_a_file"; 35f08c3bdfSopenharmony_ci 36f08c3bdfSopenharmony_ciextern FILE *temp; 37f08c3bdfSopenharmony_ci/* 38f08c3bdfSopenharmony_ci * Cleanup the ./tmp 39f08c3bdfSopenharmony_ci */ 40f08c3bdfSopenharmony_civoid remove_test_ENOTDIR_files(void) 41f08c3bdfSopenharmony_ci{ 42f08c3bdfSopenharmony_ci (void)unlink(is_a_file); 43f08c3bdfSopenharmony_ci} 44f08c3bdfSopenharmony_ci 45f08c3bdfSopenharmony_ci/* 46f08c3bdfSopenharmony_ci * Cleanup the ./tmp 47f08c3bdfSopenharmony_ci */ 48f08c3bdfSopenharmony_civoid remove_test_ENOENT_files(void) 49f08c3bdfSopenharmony_ci{ 50f08c3bdfSopenharmony_ci (void)unlink(no_file); 51f08c3bdfSopenharmony_ci} 52f08c3bdfSopenharmony_ci 53f08c3bdfSopenharmony_ci/* 54f08c3bdfSopenharmony_ci * get_long_name_buffer() 55f08c3bdfSopenharmony_ci * Create a path containing a component of length pathconf(NAME_MAX) + 1 56f08c3bdfSopenharmony_ci * characters. 57f08c3bdfSopenharmony_ci */ 58f08c3bdfSopenharmony_ci 59f08c3bdfSopenharmony_cistatic char *get_long_name_buffer(size_t * length, size_t extra) 60f08c3bdfSopenharmony_ci{ 61f08c3bdfSopenharmony_ci char *buffer; 62f08c3bdfSopenharmony_ci size_t path_length, name_length; 63f08c3bdfSopenharmony_ci 64f08c3bdfSopenharmony_ci temp = stderr; 65f08c3bdfSopenharmony_ci if ((path_length = pathconf(tmp_path, _PC_PATH_MAX)) == -1) { 66f08c3bdfSopenharmony_ci tst_resm(TFAIL, "pathconf(_PC_PATH_MAX) failed: %s", 67f08c3bdfSopenharmony_ci strerror(errno)); 68f08c3bdfSopenharmony_ci cleanup_function(); 69f08c3bdfSopenharmony_ci fail_exit(); 70f08c3bdfSopenharmony_ci } 71f08c3bdfSopenharmony_ci 72f08c3bdfSopenharmony_ci if ((name_length = pathconf(tmp_path, _PC_NAME_MAX)) == -1) { 73f08c3bdfSopenharmony_ci tst_resm(TFAIL, "pathconf(_PC_NAME_MAX) failed: %s", 74f08c3bdfSopenharmony_ci strerror(errno)); 75f08c3bdfSopenharmony_ci cleanup_function(); 76f08c3bdfSopenharmony_ci fail_exit(); 77f08c3bdfSopenharmony_ci } 78f08c3bdfSopenharmony_ci 79f08c3bdfSopenharmony_ci if ((strlen(tmp_path) + name_length + extra) > path_length) { 80f08c3bdfSopenharmony_ci tst_resm(TFAIL, 81f08c3bdfSopenharmony_ci "pathconf(_PC_NAME_MAX)[=%zi] too large relative to pathconf(_PC_PATH_MAX)[=%zi]", 82f08c3bdfSopenharmony_ci name_length, path_length); 83f08c3bdfSopenharmony_ci cleanup_function(); 84f08c3bdfSopenharmony_ci fail_exit(); 85f08c3bdfSopenharmony_ci } 86f08c3bdfSopenharmony_ci 87f08c3bdfSopenharmony_ci if ((buffer = malloc(path_length + extra)) == NULL) { 88f08c3bdfSopenharmony_ci tst_resm(TFAIL, "malloc(%zi) failed: %s", path_length + extra, 89f08c3bdfSopenharmony_ci strerror(errno)); 90f08c3bdfSopenharmony_ci cleanup_function(); 91f08c3bdfSopenharmony_ci fail_exit(); 92f08c3bdfSopenharmony_ci } 93f08c3bdfSopenharmony_ci *length = name_length; 94f08c3bdfSopenharmony_ci return buffer; 95f08c3bdfSopenharmony_ci} 96f08c3bdfSopenharmony_ci 97f08c3bdfSopenharmony_cistatic void 98f08c3bdfSopenharmony_ciexecute_function(char *name, int (*callback) (const char *), char *buffer, 99f08c3bdfSopenharmony_ci int expected) 100f08c3bdfSopenharmony_ci{ 101f08c3bdfSopenharmony_ci int result; 102f08c3bdfSopenharmony_ci 103f08c3bdfSopenharmony_ci temp = stderr; 104f08c3bdfSopenharmony_ci#ifdef DEBUG 105f08c3bdfSopenharmony_ci fprintf(temp, "TEST: %s fails with ENAMETOOLONG\n", name); 106f08c3bdfSopenharmony_ci#endif 107f08c3bdfSopenharmony_ci 108f08c3bdfSopenharmony_ci errno = 0; 109f08c3bdfSopenharmony_ci result = (*callback) (buffer); 110f08c3bdfSopenharmony_ci 111f08c3bdfSopenharmony_ci /*callback found an error, fail */ 112f08c3bdfSopenharmony_ci if (result == -752) { 113f08c3bdfSopenharmony_ci tst_resm(TFAIL, "%s callback did not work correctly", name); 114f08c3bdfSopenharmony_ci cleanup_function(); 115f08c3bdfSopenharmony_ci fail_exit(); 116f08c3bdfSopenharmony_ci } 117f08c3bdfSopenharmony_ci if (result != expected) { 118f08c3bdfSopenharmony_ci tst_resm(TFAIL, 119f08c3bdfSopenharmony_ci "%s did not return value as expected; Expected=%d Received=%d", 120f08c3bdfSopenharmony_ci name, expected, result); 121f08c3bdfSopenharmony_ci cleanup_function(); 122f08c3bdfSopenharmony_ci fail_exit(); 123f08c3bdfSopenharmony_ci } 124f08c3bdfSopenharmony_ci if (errno != ENAMETOOLONG) { 125f08c3bdfSopenharmony_ci tst_resm(TFAIL, "%s failed: errno should be %i but is %i", name, 126f08c3bdfSopenharmony_ci ENAMETOOLONG, errno); 127f08c3bdfSopenharmony_ci cleanup_function(); 128f08c3bdfSopenharmony_ci fail_exit(); 129f08c3bdfSopenharmony_ci } 130f08c3bdfSopenharmony_ci} 131f08c3bdfSopenharmony_ci 132f08c3bdfSopenharmony_cistatic void 133f08c3bdfSopenharmony_citest_long_file_name(char *name, int (*callback) (const char *), int expected) 134f08c3bdfSopenharmony_ci{ 135f08c3bdfSopenharmony_ci char *ptr, *ptr_end, *buffer; 136f08c3bdfSopenharmony_ci size_t name_length; 137f08c3bdfSopenharmony_ci 138f08c3bdfSopenharmony_ci buffer = get_long_name_buffer(&name_length, 1); 139f08c3bdfSopenharmony_ci 140f08c3bdfSopenharmony_ci strcpy(buffer, tmp_path); 141f08c3bdfSopenharmony_ci ptr = buffer + strlen(buffer); 142f08c3bdfSopenharmony_ci ptr_end = ptr + name_length + 1; 143f08c3bdfSopenharmony_ci 144f08c3bdfSopenharmony_ci *(ptr++) = '/'; 145f08c3bdfSopenharmony_ci while (ptr <= ptr_end) 146f08c3bdfSopenharmony_ci *(ptr++) = 'E'; 147f08c3bdfSopenharmony_ci *ptr = '\000'; 148f08c3bdfSopenharmony_ci 149f08c3bdfSopenharmony_ci execute_function(name, callback, buffer, expected); 150f08c3bdfSopenharmony_ci free(buffer); 151f08c3bdfSopenharmony_ci} 152f08c3bdfSopenharmony_ci 153f08c3bdfSopenharmony_cistatic void 154f08c3bdfSopenharmony_citest_long_component_name(char *name, int (*callback) (const char *), 155f08c3bdfSopenharmony_ci int expected) 156f08c3bdfSopenharmony_ci{ 157f08c3bdfSopenharmony_ci char *ptr, *ptr_end, *buffer; 158f08c3bdfSopenharmony_ci size_t name_length; 159f08c3bdfSopenharmony_ci 160f08c3bdfSopenharmony_ci buffer = get_long_name_buffer(&name_length, 3); 161f08c3bdfSopenharmony_ci 162f08c3bdfSopenharmony_ci strcpy(buffer, tmp_path); 163f08c3bdfSopenharmony_ci ptr = buffer + strlen(buffer); 164f08c3bdfSopenharmony_ci ptr_end = ptr + name_length + 2; 165f08c3bdfSopenharmony_ci *(ptr++) = '/'; 166f08c3bdfSopenharmony_ci for (; ptr < ptr_end; ptr++) 167f08c3bdfSopenharmony_ci *ptr = 'E'; 168f08c3bdfSopenharmony_ci *(ptr++) = '/'; 169f08c3bdfSopenharmony_ci *(ptr++) = 'F'; 170f08c3bdfSopenharmony_ci *ptr = '\000'; 171f08c3bdfSopenharmony_ci execute_function(name, callback, buffer, expected); 172f08c3bdfSopenharmony_ci free(buffer); 173f08c3bdfSopenharmony_ci} 174f08c3bdfSopenharmony_ci 175f08c3bdfSopenharmony_civoid 176f08c3bdfSopenharmony_citest_ENAMETOOLONG_path(char *name, int (*callback) (const char *), int expected) 177f08c3bdfSopenharmony_ci{ 178f08c3bdfSopenharmony_ci size_t pcPathMax; 179f08c3bdfSopenharmony_ci char *path, *tmpPtr; 180f08c3bdfSopenharmony_ci int pathLength, tempPathLength; 181f08c3bdfSopenharmony_ci 182f08c3bdfSopenharmony_ci temp = stderr; 183f08c3bdfSopenharmony_ci if ((pcPathMax = pathconf(tmp_path, _PC_PATH_MAX)) == -1) { 184f08c3bdfSopenharmony_ci tst_resm(TFAIL, "pathconf(_PC_PATH_MAX) failed: %s", 185f08c3bdfSopenharmony_ci strerror(errno)); 186f08c3bdfSopenharmony_ci cleanup_function(); 187f08c3bdfSopenharmony_ci fail_exit(); 188f08c3bdfSopenharmony_ci } 189f08c3bdfSopenharmony_ci#ifdef DEBUG 190f08c3bdfSopenharmony_ci fprintf(temp, "INFO: pathconf(_PC_PATH_MAX) for %s is %lu\n", 191f08c3bdfSopenharmony_ci tmp_path, pcPathMax); 192f08c3bdfSopenharmony_ci#endif 193f08c3bdfSopenharmony_ci 194f08c3bdfSopenharmony_ci if ((path = malloc(pcPathMax + 2)) == NULL) { 195f08c3bdfSopenharmony_ci tst_resm(TFAIL, "malloc(%zu) for path failed: %s", 196f08c3bdfSopenharmony_ci pcPathMax + 2, strerror(errno)); 197f08c3bdfSopenharmony_ci cleanup_function(); 198f08c3bdfSopenharmony_ci fail_exit(); 199f08c3bdfSopenharmony_ci } 200f08c3bdfSopenharmony_ci path = strcpy(path, tmp_path); 201f08c3bdfSopenharmony_ci pathLength = (int)strlen(path); 202f08c3bdfSopenharmony_ci tempPathLength = pathLength + 1; 203f08c3bdfSopenharmony_ci 204f08c3bdfSopenharmony_ci /* leave some chars for element that pushes path over PC_PATH_MAX */ 205f08c3bdfSopenharmony_ci pcPathMax = pcPathMax - tempPathLength - 5; 206f08c3bdfSopenharmony_ci 207f08c3bdfSopenharmony_ci tmpPtr = path + strlen(path); 208f08c3bdfSopenharmony_ci while (pathLength < pcPathMax) { 209f08c3bdfSopenharmony_ci tmpPtr += sprintf(tmpPtr, "/%s", tmp_path); 210f08c3bdfSopenharmony_ci pathLength += tempPathLength; 211f08c3bdfSopenharmony_ci } 212f08c3bdfSopenharmony_ci 213f08c3bdfSopenharmony_ci /* reinstate pcPathMax correct value */ 214f08c3bdfSopenharmony_ci pcPathMax = pcPathMax + tempPathLength + 5; 215f08c3bdfSopenharmony_ci 216f08c3bdfSopenharmony_ci tmpPtr = path + pathLength; 217f08c3bdfSopenharmony_ci *tmpPtr++ = '/'; 218f08c3bdfSopenharmony_ci pathLength++; 219f08c3bdfSopenharmony_ci while (pathLength <= pcPathMax) { 220f08c3bdfSopenharmony_ci *tmpPtr++ = 'z'; 221f08c3bdfSopenharmony_ci pathLength++; 222f08c3bdfSopenharmony_ci } 223f08c3bdfSopenharmony_ci *tmpPtr = '\0'; 224f08c3bdfSopenharmony_ci 225f08c3bdfSopenharmony_ci pathLength = (int)strlen(path); 226f08c3bdfSopenharmony_ci if (pathLength != pcPathMax + 1) { 227f08c3bdfSopenharmony_ci tst_resm(TFAIL, 228f08c3bdfSopenharmony_ci "test logic failure, path length is %d, should be %lu", 229f08c3bdfSopenharmony_ci pathLength, (long unsigned int)pcPathMax + 1); 230f08c3bdfSopenharmony_ci free(path); 231f08c3bdfSopenharmony_ci cleanup_function(); 232f08c3bdfSopenharmony_ci fail_exit(); 233f08c3bdfSopenharmony_ci } 234f08c3bdfSopenharmony_ci execute_function(name, callback, path, expected); 235f08c3bdfSopenharmony_ci free(path); 236f08c3bdfSopenharmony_ci} 237f08c3bdfSopenharmony_ci 238f08c3bdfSopenharmony_civoid 239f08c3bdfSopenharmony_citest_ENAMETOOLONG_name(char *name, int (*callback) (const char *), int expected) 240f08c3bdfSopenharmony_ci{ 241f08c3bdfSopenharmony_ci test_long_file_name(name, callback, expected); 242f08c3bdfSopenharmony_ci test_long_component_name(name, callback, expected); 243f08c3bdfSopenharmony_ci} 244f08c3bdfSopenharmony_ci 245f08c3bdfSopenharmony_civoid test_ENOENT_empty(char *name, int (*callback) (const char *), int expected) 246f08c3bdfSopenharmony_ci{ 247f08c3bdfSopenharmony_ci char *empty_string; 248f08c3bdfSopenharmony_ci 249f08c3bdfSopenharmony_ci empty_string = ""; 250f08c3bdfSopenharmony_ci 251f08c3bdfSopenharmony_ci errno = 0; 252f08c3bdfSopenharmony_ci 253f08c3bdfSopenharmony_ci temp = stderr; 254f08c3bdfSopenharmony_ci#ifdef DEBUG 255f08c3bdfSopenharmony_ci fprintf(temp, "TEST: ENOENT when empty string is passed\n"); 256f08c3bdfSopenharmony_ci#endif 257f08c3bdfSopenharmony_ci 258f08c3bdfSopenharmony_ci if ((s2 = (*callback) (empty_string)) == expected) { 259f08c3bdfSopenharmony_ci if (errno != ENOENT) { 260f08c3bdfSopenharmony_ci tst_resm(TFAIL, 261f08c3bdfSopenharmony_ci "%s failed: errno should be %i but is %i", 262f08c3bdfSopenharmony_ci name, ENOENT, errno); 263f08c3bdfSopenharmony_ci cleanup_function(); 264f08c3bdfSopenharmony_ci fail_exit(); 265f08c3bdfSopenharmony_ci } 266f08c3bdfSopenharmony_ci } else { 267f08c3bdfSopenharmony_ci tst_resm(TFAIL, 268f08c3bdfSopenharmony_ci "%s did not return correct value; Expected=%d Received=%d", 269f08c3bdfSopenharmony_ci name, expected, s2); 270f08c3bdfSopenharmony_ci cleanup_function(); 271f08c3bdfSopenharmony_ci fail_exit(); 272f08c3bdfSopenharmony_ci } 273f08c3bdfSopenharmony_ci} 274f08c3bdfSopenharmony_ci 275f08c3bdfSopenharmony_civoid test_ENOTDIR(char *name, int (*callback) (const char *), int expected) 276f08c3bdfSopenharmony_ci{ 277f08c3bdfSopenharmony_ci int fd; 278f08c3bdfSopenharmony_ci 279f08c3bdfSopenharmony_ci if ((fd = creat(is_a_file, (mode_t) (S_IRWXU | S_IRWXG | S_IRWXO))) 280f08c3bdfSopenharmony_ci == -1) { 281f08c3bdfSopenharmony_ci tst_resm(TFAIL, "creat(%s) failed: %s", is_a_file, 282f08c3bdfSopenharmony_ci strerror(errno)); 283f08c3bdfSopenharmony_ci cleanup_function(); 284f08c3bdfSopenharmony_ci fail_exit(); 285f08c3bdfSopenharmony_ci } 286f08c3bdfSopenharmony_ci 287f08c3bdfSopenharmony_ci if (close(fd) == -1) { 288f08c3bdfSopenharmony_ci tst_resm(TFAIL, "close(%i) failed: %s", fd, strerror(errno)); 289f08c3bdfSopenharmony_ci remove_test_ENOTDIR_files(); 290f08c3bdfSopenharmony_ci cleanup_function(); 291f08c3bdfSopenharmony_ci fail_exit(); 292f08c3bdfSopenharmony_ci } 293f08c3bdfSopenharmony_ci 294f08c3bdfSopenharmony_ci errno = 0; 295f08c3bdfSopenharmony_ci 296f08c3bdfSopenharmony_ci temp = stderr; 297f08c3bdfSopenharmony_ci#ifdef DEBUG 298f08c3bdfSopenharmony_ci fprintf(temp, "TEST: ENOTDIR when a component is not a directory\n"); 299f08c3bdfSopenharmony_ci#endif 300f08c3bdfSopenharmony_ci 301f08c3bdfSopenharmony_ci s2 = (*callback) ("./tmp/is_a_file/no_file"); 302f08c3bdfSopenharmony_ci /*callback found an error, bail */ 303f08c3bdfSopenharmony_ci if (s2 == -752) { 304f08c3bdfSopenharmony_ci remove_test_ENOTDIR_files(); 305f08c3bdfSopenharmony_ci cleanup_function(); 306f08c3bdfSopenharmony_ci fail_exit(); 307f08c3bdfSopenharmony_ci } 308f08c3bdfSopenharmony_ci if (s2 == expected) { 309f08c3bdfSopenharmony_ci if (errno != ENOTDIR) { 310f08c3bdfSopenharmony_ci tst_resm(TFAIL, 311f08c3bdfSopenharmony_ci "%s failed: errno should be %i but is %i", 312f08c3bdfSopenharmony_ci name, ENOTDIR, errno); 313f08c3bdfSopenharmony_ci cleanup_function(); 314f08c3bdfSopenharmony_ci fail_exit(); 315f08c3bdfSopenharmony_ci } 316f08c3bdfSopenharmony_ci } else { 317f08c3bdfSopenharmony_ci tst_resm(TFAIL, 318f08c3bdfSopenharmony_ci "%s did not return correct value; Expected=%d Received=%d", 319f08c3bdfSopenharmony_ci name, expected, s2); 320f08c3bdfSopenharmony_ci cleanup_function(); 321f08c3bdfSopenharmony_ci fail_exit(); 322f08c3bdfSopenharmony_ci } 323f08c3bdfSopenharmony_ci remove_test_ENOTDIR_files(); 324f08c3bdfSopenharmony_ci} 325f08c3bdfSopenharmony_ci 326f08c3bdfSopenharmony_civoid 327f08c3bdfSopenharmony_citest_ENOENT_nofile(char *name, int (*callback) (const char *), int expected) 328f08c3bdfSopenharmony_ci{ 329f08c3bdfSopenharmony_ci 330f08c3bdfSopenharmony_ci remove_test_ENOENT_files(); 331f08c3bdfSopenharmony_ci 332f08c3bdfSopenharmony_ci temp = stderr; 333f08c3bdfSopenharmony_ci#ifdef DEBUG 334f08c3bdfSopenharmony_ci fprintf(temp, "TEST: ENOENT when file does not exist\n"); 335f08c3bdfSopenharmony_ci#endif 336f08c3bdfSopenharmony_ci 337f08c3bdfSopenharmony_ci if ((s2 = (*callback) (no_file)) == expected) { 338f08c3bdfSopenharmony_ci if (errno != ENOENT) { 339f08c3bdfSopenharmony_ci tst_resm(TFAIL, 340f08c3bdfSopenharmony_ci "%s failed: errno should be %i but is %i", 341f08c3bdfSopenharmony_ci name, ENOENT, errno); 342f08c3bdfSopenharmony_ci cleanup_function(); 343f08c3bdfSopenharmony_ci fail_exit(); 344f08c3bdfSopenharmony_ci } 345f08c3bdfSopenharmony_ci } else { 346f08c3bdfSopenharmony_ci tst_resm(TFAIL, 347f08c3bdfSopenharmony_ci "%s did not return correct value; Expected=%d Received=%d", 348f08c3bdfSopenharmony_ci name, expected, s2); 349f08c3bdfSopenharmony_ci cleanup_function(); 350f08c3bdfSopenharmony_ci fail_exit(); 351f08c3bdfSopenharmony_ci } 352f08c3bdfSopenharmony_ci} 353