/core/externals/update-engine/externals/google-toolbox-for-mac/Foundation/GTMPathTest.m

http://macfuse.googlecode.com/ · Objective C · 240 lines · 166 code · 43 blank · 31 comment · 10 complexity · 0175988607b9672988c6363a8d512277 MD5 · raw file

  1. //
  2. // GTMPathTest.m
  3. //
  4. // Copyright 2007-2008 Google Inc.
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. // use this file except in compliance with the License. You may obtain a copy
  8. // of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. // License for the specific language governing permissions and limitations under
  16. // the License.
  17. //
  18. #import "GTMSenTestCase.h"
  19. #import "GTMPath.h"
  20. #import "GTMUnitTestDevLog.h"
  21. #import "GTMNSFileHandle+UniqueName.h"
  22. #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
  23. // NSFileManager has improved substantially in Leopard and beyond, so GTMPath
  24. // is now deprecated.
  25. @interface GTMPathTest : GTMTestCase {
  26. @private
  27. NSString *testDirectory_;
  28. }
  29. @end
  30. @implementation GTMPathTest
  31. - (void)setUp {
  32. NSFileManager *mgr = [NSFileManager defaultManager];
  33. testDirectory_
  34. = [[mgr gtm_createTemporaryDirectoryBasedOn:@"GTMPathTestXXXXXX"] retain];
  35. STAssertNotNil(testDirectory_, nil);
  36. }
  37. - (void)tearDown {
  38. // Make sure it's safe to remove this directory before nuking it.
  39. STAssertNotNil(testDirectory_, nil);
  40. STAssertNotEqualObjects(testDirectory_, @"/", nil);
  41. #if GTM_MACOS_SDK && (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5)
  42. [[NSFileManager defaultManager] removeFileAtPath:testDirectory_ handler:nil];
  43. #else
  44. [[NSFileManager defaultManager] removeItemAtPath:testDirectory_ error:NULL];
  45. #endif
  46. [testDirectory_ release];
  47. }
  48. - (void)testBasicCreation {
  49. GTMPath *path = nil;
  50. path = [[[GTMPath alloc] init] autorelease];
  51. STAssertNil(path, nil);
  52. path = [GTMPath pathWithFullPath:@"/"];
  53. STAssertNotNil(path, nil);
  54. STAssertNil([path parent], nil);
  55. STAssertTrue([path isRoot], nil);
  56. STAssertTrue([path isDirectory], nil);
  57. STAssertEqualObjects([path name], @"/", nil);
  58. STAssertEqualObjects([path fullPath], @"/", nil);
  59. }
  60. - (void)testRecursiveInitialization {
  61. GTMPath *path = nil;
  62. path = [GTMPath pathWithFullPath:nil];
  63. STAssertNil(path, nil);
  64. path = [GTMPath pathWithFullPath:@""];
  65. STAssertNil(path, nil);
  66. path = [GTMPath pathWithFullPath:@"etc"];
  67. STAssertNil(path, nil);
  68. path = [GTMPath pathWithFullPath:@"/"];
  69. STAssertNotNil(path, nil);
  70. STAssertNil([path parent], nil);
  71. STAssertTrue([path isRoot], nil);
  72. STAssertTrue([path isDirectory], nil);
  73. STAssertEqualObjects([path name], @"/", nil);
  74. STAssertEqualObjects([path fullPath], @"/", nil);
  75. path = [GTMPath pathWithFullPath:@"/etc"];
  76. STAssertNotNil(path, nil);
  77. STAssertEqualObjects([path name], @"etc", nil);
  78. STAssertEqualObjects([path fullPath], @"/etc", nil);
  79. STAssertTrue([path isDirectory], nil);
  80. STAssertFalse([path isRoot], nil);
  81. STAssertNotNil([path parent], nil);
  82. STAssertTrue([[path parent] isRoot], nil);
  83. path = [GTMPath pathWithFullPath:@"/etc/passwd"];
  84. STAssertNotNil(path, nil);
  85. STAssertEqualObjects([path name], @"passwd", nil);
  86. STAssertEqualObjects([path fullPath], @"/etc/passwd", nil);
  87. STAssertFalse([path isDirectory], nil);
  88. STAssertFalse([path isRoot], nil);
  89. STAssertNotNil([path parent], nil);
  90. STAssertFalse([[path parent] isRoot], nil);
  91. STAssertTrue([[path parent] isDirectory], nil);
  92. STAssertTrue([[[path parent] parent] isRoot], nil);
  93. STAssertTrue([[path description] length] > 1, nil);
  94. }
  95. - (void)testCreationWithNonExistentPath {
  96. GTMPath *path = nil;
  97. path = [GTMPath pathWithFullPath:@" "];
  98. STAssertNil(path, nil);
  99. path = [GTMPath pathWithFullPath:@"/abcxyz"];
  100. STAssertNil(path, nil);
  101. path = [GTMPath pathWithFullPath:@"/etc/foo"];
  102. STAssertNil(path, nil);
  103. path = [GTMPath pathWithFullPath:@"/foo/bar/baz"];
  104. STAssertNil(path, nil);
  105. }
  106. - (void)testDirectoryCreation {
  107. GTMPath *tmp = [GTMPath pathWithFullPath:testDirectory_];
  108. GTMPath *path = nil;
  109. NSString *fooPath = [[tmp fullPath] stringByAppendingPathComponent:@"foo"];
  110. path = [GTMPath pathWithFullPath:fooPath];
  111. STAssertNil(path, nil);
  112. path = [tmp createDirectoryName:@"foo" mode:0555];
  113. STAssertNotNil(path, nil);
  114. STAssertEqualObjects([path name], @"foo", nil);
  115. // filePosixPermissions has odd return types in different SDKs, so we use
  116. // STAssertTrue to avoid the macros type checks from choking us.
  117. STAssertTrue([[path attributes] filePosixPermissions] == 0555,
  118. @"got %o", (int)[[path attributes] filePosixPermissions]);
  119. STAssertTrue([path isDirectory], nil);
  120. STAssertFalse([path isRoot], nil);
  121. // Trying to create a file where a dir already exists should fail
  122. path = [tmp createFileName:@"foo" mode:0555];
  123. STAssertNil(path, nil);
  124. // Calling create again should succeed
  125. path = [tmp createDirectoryName:@"foo" mode:0555];
  126. STAssertNotNil(path, nil);
  127. STAssertEqualObjects([path name], @"foo", nil);
  128. STAssertTrue([[path attributes] filePosixPermissions] == 0555,
  129. @"got %o", (int)[[path attributes] filePosixPermissions]);
  130. STAssertTrue([path isDirectory], nil);
  131. STAssertFalse([path isRoot], nil);
  132. GTMPath *foo = [GTMPath pathWithFullPath:fooPath];
  133. STAssertNotNil(foo, nil);
  134. STAssertEqualObjects([path name], @"foo", nil);
  135. STAssertTrue([[path attributes] filePosixPermissions] == 0555,
  136. @"got %o", (int)[[path attributes] filePosixPermissions]);
  137. STAssertTrue([path isDirectory], nil);
  138. STAssertFalse([path isRoot], nil);
  139. }
  140. - (void)testFileCreation {
  141. GTMPath *tmp = [GTMPath pathWithFullPath:testDirectory_];
  142. GTMPath *path = nil;
  143. NSString *fooPath = [[tmp fullPath] stringByAppendingPathComponent:@"foo"];
  144. path = [GTMPath pathWithFullPath:fooPath];
  145. STAssertNil(path, nil);
  146. path = [tmp createFileName:@"foo" mode:0555];
  147. STAssertNotNil(path, nil);
  148. STAssertEqualObjects([path name], @"foo", nil);
  149. STAssertTrue([[path attributes] filePosixPermissions] == 0555, nil);
  150. STAssertFalse([path isDirectory], nil);
  151. STAssertFalse([path isRoot], nil);
  152. // Trying to create a dir where a file already exists should fail.
  153. path = [tmp createDirectoryName:@"foo" mode:0555];
  154. STAssertNil(path, nil);
  155. // Calling create again should succeed
  156. path = [tmp createFileName:@"foo" mode:0555];
  157. STAssertNotNil(path, nil);
  158. STAssertEqualObjects([path name], @"foo", nil);
  159. STAssertTrue([[path attributes] filePosixPermissions] == 0555, nil);
  160. STAssertFalse([path isDirectory], nil);
  161. STAssertFalse([path isRoot], nil);
  162. GTMPath *foo = [GTMPath pathWithFullPath:fooPath];
  163. STAssertNotNil(foo, nil);
  164. STAssertEqualObjects([path name], @"foo", nil);
  165. STAssertTrue([[path attributes] filePosixPermissions] == 0555, nil);
  166. STAssertFalse([path isDirectory], nil);
  167. STAssertFalse([path isRoot], nil);
  168. // Make sure we can't create a file/directory rooted off of |foo|, since it's
  169. // not a directory.
  170. path = [foo createFileName:@"bar" mode:0555];
  171. STAssertNil(path, nil);
  172. path = [foo createDirectoryName:@"bar" mode:0555];
  173. STAssertNil(path, nil);
  174. }
  175. - (void)testHierarchyCreation {
  176. GTMPath *tmp = [GTMPath pathWithFullPath:testDirectory_];
  177. NSString *fooPath = [[tmp fullPath] stringByAppendingPathComponent:@"foo"];
  178. GTMPath *path = [GTMPath pathWithFullPath:fooPath];
  179. STAssertNil(path, nil);
  180. path = [[[tmp createDirectoryName:@"foo" mode:0755]
  181. createDirectoryName:@"bar" mode:0756]
  182. createDirectoryName:@"baz" mode:0757];
  183. STAssertNotNil(path, nil);
  184. // Check "baz"
  185. STAssertEqualObjects([path name], @"baz", nil);
  186. STAssertTrue([[path attributes] filePosixPermissions] == 0757, nil);
  187. // Check "bar"
  188. path = [path parent];
  189. STAssertEqualObjects([path name], @"bar", nil);
  190. STAssertTrue([[path attributes] filePosixPermissions] == 0756, nil);
  191. // Check "foo"
  192. path = [path parent];
  193. STAssertEqualObjects([path name], @"foo", nil);
  194. STAssertTrue([[path attributes] filePosixPermissions] == 0755, nil);
  195. }
  196. @end
  197. #endif // MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5