- Timestamp:
- Feb 18, 2014, 1:03:12 PM (11 years ago)
- Location:
- trunk/libc/tests/libc
- Files:
-
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/libc/tests/libc/Makefile
r3633 r3813 155 155 io/open-1.c \ 156 156 io/open-2.c \ 157 io/chown-1.c \ 157 158 io/sprintf-1.c \ 158 159 io/sscanf-1.c \ -
trunk/libc/tests/libc/io/chown-1.c
r3807 r3813 1 1 /* $Id$ */ 2 2 /** @file 3 * Testcase chown-1, tests simple owner ship changes using chown, lchown and fchown. 4 */ 5 6 /* 3 7 * 4 * Testcase open-1, tests O_EXCL. 5 * 6 * Copyright (c) 2006 knut st. osmundsen <bird@innotek.de> 8 * Copyright (c) 2006-2014 knut st. osmundsen <bird@innotek.de> 7 9 * 8 10 * … … 26 28 27 29 28 30 /******************************************************************************* 31 * Header Files * 32 *******************************************************************************/ 29 33 #include <errno.h> 30 34 #include <limits.h> … … 35 39 #include <sys/stat.h> 36 40 41 /******************************************************************************* 42 * Defined Constants And Macros * 43 *******************************************************************************/ 44 #define XSTR_SUB(a_Expr) #a_Expr 45 #define XSTR(a_Expr) XSTR_SUB(a_Expr) 46 47 #define TEST_CHANGE(a_OperationExpr, a_uidExpect, a_gidExpect) \ 48 do { \ 49 int rc = a_OperationExpr; \ 50 if (rc != 0) \ 51 { \ 52 g_cErrors++; \ 53 int err = errno; \ 54 fprintf(stderr, "chown-1: errno=%d (%s) at line " XSTR(__LINE__) " doing: " #a_OperationExpr "\n", \ 55 err, strerror(err)); \ 56 } \ 57 else \ 58 assertOwners(fd, szTmp, a_uidExpect, a_gidExpect, #a_OperationExpr " at line " XSTR(__LINE__)); \ 59 } while (0) 60 61 62 63 /******************************************************************************* 64 * Global Variables * 65 *******************************************************************************/ 66 static int g_cErrors = 0; 67 68 69 static int assertOwners(int fd, const char *pszFilename, uid_t uidExpect, gid_t gidExpect, const char *pszClue) 70 { 71 int rc; 72 struct stat st; 73 if (fd != -1) 74 rc = fstat(fd, &st); 75 else 76 rc = lstat(pszFilename, &st); 77 if (rc) 78 { 79 g_cErrors++; 80 int err = errno; 81 fprintf(stderr, "chown-1: failed to stat the file%s (fd=%d,%s): errno=%d (%s)\n", 82 pszClue, fd, pszFilename, err, strerror(err)); 83 rc = 1; 84 } 85 else 86 { 87 88 if (st.st_uid != uidExpect) 89 { 90 g_cErrors++; 91 rc++; 92 fprintf(stderr, "chown-1: st_uid mismatch%s (fd=%d,%s): expected=%u, found %u\n", 93 pszClue, fd, pszFilename, uidExpect, st.st_uid); 94 } 95 96 if (st.st_gid != gidExpect) 97 { 98 g_cErrors++; 99 rc++; 100 fprintf(stderr, "chown-1: st_gid mismatch%s (fd=%d,%s): expected=%u, found %u\n", 101 pszClue, fd, pszFilename, gidExpect, st.st_gid); 102 } 103 } 104 return rc; 105 } 37 106 38 107 int main() 39 108 { 40 int cErrors = 0;41 109 42 110 /* 43 * Get a non-existing temp filename .111 * Get a non-existing temp filename we can play around with. 44 112 */ 45 113 char szTmp[L_tmpnam]; … … 48 116 { 49 117 if (!tmpnam(szTmp)) 50 return !!printf(" open-1: FAILURE - tmpnam failed!\n");118 return !!printf("chown-1: FAILURE - tmpnam failed!\n"); 51 119 fd = open(szTmp, O_RDWR | O_CREAT | O_EXCL, 0666); 52 120 if (fd >= 0) … … 57 125 if ( fd < 0 58 126 && stat(szTmp, &st)) 59 return !!printf(" open-1: FAILURE - open O_EXCL failed. file='%s' errno=%d %s\n",127 return !!printf("chown-1: FAILURE - open O_EXCL failed. file='%s' errno=%d %s\n", 60 128 szTmp, err, strerror(err)); 61 129 /* racing someone, retry. */ … … 63 131 64 132 /* 65 * Try open it again.133 * Get the current ownership. 66 134 */ 67 int fd2 = open(szTmp, O_RDWR | O_CREAT | O_EXCL, 0666);68 if (f d2 != -1)135 struct stat st1; 136 if (fstat(fd, &st1) != 0) 69 137 { 70 cErrors++; 71 printf("open-1: FAILURE - reopen with O_EXCL succeeded! fd2=%d\n", fd2); 72 close(fd2); 73 } 74 else if (errno != EEXIST) 75 { 76 cErrors++; 138 g_cErrors++; 77 139 int err = errno; 78 printf("open-1: FAILURE - errno=%d (%s) != %d (EEXIST)!\n", err, strerror(err), EEXIST); 79 } 80 81 close(fd); 82 83 fd = open(szTmp, O_RDWR | O_CREAT | O_EXCL, 0666); 84 if (fd != -1) 85 { 86 cErrors++; 87 printf("open-1: FAILURE - open existing file with O_EXCL succeeded! fd2=%d\n", fd2); 88 close(fd); 89 } 90 else if (errno != EEXIST) 91 { 92 cErrors++; 93 int err = errno; 94 printf("open-1: FAILURE - errno=%d (%s) != %d (EEXIST)!\n", err, strerror(err), EEXIST); 140 printf("chown-1: FAILURE - fstat failed - errno=%d (%s)\n", err, strerror(err)); 141 memset(&st1, 0, sizeof(st1)); 95 142 } 96 143 97 144 /* 98 * Check that normal open works.145 * No change. 99 146 */ 100 fd = open(szTmp, O_RDWR | O_CREAT, 0666); 101 if (fd >= 0) 102 close(fd); 103 else 104 { 105 cErrors++; 106 int err = errno; 107 printf("open-1: FAILURE - open(%s, O_RDWR | O_CREAT, 0666) failed! errno=%d (%s)\n", szTmp, err, strerror(err)); 108 } 147 TEST_CHANGE(fchown(fd, st1.st_uid, st1.st_gid), st1.st_uid, st1.st_gid); 148 TEST_CHANGE(chown(szTmp, st1.st_uid, st1.st_gid), st1.st_uid, st1.st_gid); 149 TEST_CHANGE(lchown(szTmp, st1.st_uid, st1.st_gid), st1.st_uid, st1.st_gid); 150 151 TEST_CHANGE(fchown(fd, -1, -1), st1.st_uid, st1.st_gid); 152 TEST_CHANGE(chown(szTmp, -1, -1), st1.st_uid, st1.st_gid); 153 TEST_CHANGE(lchown(szTmp, -1, -1), st1.st_uid, st1.st_gid); 154 155 TEST_CHANGE(fchown(fd, st1.st_uid, -1), st1.st_uid, st1.st_gid); 156 TEST_CHANGE(chown(szTmp, -1, st1.st_gid), st1.st_uid, st1.st_gid); 157 TEST_CHANGE(lchown(szTmp, -1, st1.st_gid), st1.st_uid, st1.st_gid); 158 159 /* 160 * If we're super user we change change the file ownership without 161 * restriction, otherwise we cannot. Restrictions are also affected on group 162 * changes. 163 * 164 * However, on OS/2 we don't check these things right now, so the below always works fine. 165 */ 166 TEST_CHANGE(fchown(fd, st1.st_uid + 1, st1.st_gid + 1), st1.st_uid + 1, st1.st_gid + 1); 167 TEST_CHANGE(lchown(szTmp, st1.st_uid + 1, st1.st_gid + 1), st1.st_uid + 1, st1.st_gid + 1); 168 TEST_CHANGE(chown(szTmp, st1.st_uid + 1, st1.st_gid + 1), st1.st_uid + 1, st1.st_gid + 1); 169 170 TEST_CHANGE(fchown(fd, st1.st_uid + 2, -1), st1.st_uid + 2, st1.st_gid + 1); 171 TEST_CHANGE(lchown(szTmp, st1.st_uid + 2, -1), st1.st_uid + 2, st1.st_gid + 1); 172 TEST_CHANGE(chown(szTmp, st1.st_uid + 2, -1), st1.st_uid + 2, st1.st_gid + 1); 173 174 TEST_CHANGE(fchown(fd, -1, st1.st_gid + 3), st1.st_uid + 2, st1.st_gid + 3); 175 TEST_CHANGE(lchown(szTmp, -1, st1.st_gid + 3), st1.st_uid + 2, st1.st_gid + 3); 176 TEST_CHANGE(chown(szTmp, -1, st1.st_gid + 3), st1.st_uid + 2, st1.st_gid + 3); 109 177 110 178 /* … … 113 181 if (unlink(szTmp)) 114 182 { 115 cErrors++;183 g_cErrors++; 116 184 int err = errno; 117 printf(" open-1: FAILURE - unlink(%s) failed! errno=%d (%s)\n", szTmp, err, strerror(err));185 printf("chown-1: FAILURE - unlink(%s) failed! errno=%d (%s)\n", szTmp, err, strerror(err)); 118 186 } 119 187 … … 121 189 * Summary. 122 190 */ 123 if (! cErrors)124 printf(" open-1: SUCCESS\n");191 if (!g_cErrors) 192 printf("chown-1: SUCCESS\n"); 125 193 else 126 printf(" open-1: FAILURE - %d errors\n",cErrors);127 return !! cErrors;194 printf("chown-1: FAILURE - %d errors\n", g_cErrors); 195 return !!g_cErrors; 128 196 }
Note:
See TracChangeset
for help on using the changeset viewer.