PageRenderTime 53ms CodeModel.GetById 11ms app.highlight 38ms RepoModel.GetById 2ms app.codeStats 0ms

/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
 19#import "GTMSenTestCase.h"
 20#import "GTMPath.h"
 21#import "GTMUnitTestDevLog.h"
 22#import "GTMNSFileHandle+UniqueName.h"
 23
 24#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
 25// NSFileManager has improved substantially in Leopard and beyond, so GTMPath
 26// is now deprecated.
 27
 28@interface GTMPathTest : GTMTestCase {
 29 @private
 30  NSString *testDirectory_;
 31}
 32@end
 33
 34@implementation GTMPathTest
 35
 36- (void)setUp {
 37  NSFileManager *mgr = [NSFileManager defaultManager];
 38  testDirectory_
 39    = [[mgr gtm_createTemporaryDirectoryBasedOn:@"GTMPathTestXXXXXX"] retain];
 40
 41  STAssertNotNil(testDirectory_, nil);
 42}
 43
 44- (void)tearDown {
 45  // Make sure it's safe to remove this directory before nuking it.
 46  STAssertNotNil(testDirectory_, nil);
 47  STAssertNotEqualObjects(testDirectory_, @"/", nil);
 48#if GTM_MACOS_SDK && (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5)
 49  [[NSFileManager defaultManager] removeFileAtPath:testDirectory_ handler:nil];
 50#else
 51  [[NSFileManager defaultManager] removeItemAtPath:testDirectory_ error:NULL];
 52#endif
 53  [testDirectory_ release];
 54}
 55
 56- (void)testBasicCreation {
 57  GTMPath *path = nil;
 58
 59  path = [[[GTMPath alloc] init] autorelease];
 60  STAssertNil(path, nil);
 61
 62  path = [GTMPath pathWithFullPath:@"/"];
 63  STAssertNotNil(path, nil);
 64  STAssertNil([path parent], nil);
 65  STAssertTrue([path isRoot], nil);
 66  STAssertTrue([path isDirectory], nil);
 67  STAssertEqualObjects([path name], @"/", nil);
 68  STAssertEqualObjects([path fullPath], @"/", nil);
 69}
 70
 71- (void)testRecursiveInitialization {
 72  GTMPath *path = nil;
 73
 74  path = [GTMPath pathWithFullPath:nil];
 75  STAssertNil(path, nil);
 76
 77  path = [GTMPath pathWithFullPath:@""];
 78  STAssertNil(path, nil);
 79
 80  path = [GTMPath pathWithFullPath:@"etc"];
 81  STAssertNil(path, nil);
 82
 83  path = [GTMPath pathWithFullPath:@"/"];
 84  STAssertNotNil(path, nil);
 85  STAssertNil([path parent], nil);
 86  STAssertTrue([path isRoot], nil);
 87  STAssertTrue([path isDirectory], nil);
 88  STAssertEqualObjects([path name], @"/", nil);
 89  STAssertEqualObjects([path fullPath], @"/", nil);
 90
 91  path = [GTMPath pathWithFullPath:@"/etc"];
 92  STAssertNotNil(path, nil);
 93  STAssertEqualObjects([path name], @"etc", nil);
 94  STAssertEqualObjects([path fullPath], @"/etc", nil);
 95  STAssertTrue([path isDirectory], nil);
 96  STAssertFalse([path isRoot], nil);
 97  STAssertNotNil([path parent], nil);
 98  STAssertTrue([[path parent] isRoot], nil);
 99
100  path = [GTMPath pathWithFullPath:@"/etc/passwd"];
101  STAssertNotNil(path, nil);
102  STAssertEqualObjects([path name], @"passwd", nil);
103  STAssertEqualObjects([path fullPath], @"/etc/passwd", nil);
104  STAssertFalse([path isDirectory], nil);
105  STAssertFalse([path isRoot], nil);
106  STAssertNotNil([path parent], nil);
107  STAssertFalse([[path parent] isRoot], nil);
108  STAssertTrue([[path parent] isDirectory], nil);
109  STAssertTrue([[[path parent] parent] isRoot], nil);
110
111  STAssertTrue([[path description] length] > 1, nil);
112}
113
114- (void)testCreationWithNonExistentPath {
115  GTMPath *path = nil;
116
117  path = [GTMPath pathWithFullPath:@" "];
118  STAssertNil(path, nil);
119
120  path = [GTMPath pathWithFullPath:@"/abcxyz"];
121  STAssertNil(path, nil);
122
123  path = [GTMPath pathWithFullPath:@"/etc/foo"];
124  STAssertNil(path, nil);
125
126  path = [GTMPath pathWithFullPath:@"/foo/bar/baz"];
127  STAssertNil(path, nil);
128}
129
130- (void)testDirectoryCreation {
131  GTMPath *tmp = [GTMPath pathWithFullPath:testDirectory_];
132  GTMPath *path = nil;
133
134  NSString *fooPath = [[tmp fullPath] stringByAppendingPathComponent:@"foo"];
135  path = [GTMPath pathWithFullPath:fooPath];
136  STAssertNil(path, nil);
137
138  path = [tmp createDirectoryName:@"foo" mode:0555];
139  STAssertNotNil(path, nil);
140  STAssertEqualObjects([path name], @"foo", nil);
141  // filePosixPermissions has odd return types in different SDKs, so we use
142  // STAssertTrue to avoid the macros type checks from choking us.
143  STAssertTrue([[path attributes] filePosixPermissions] == 0555,
144               @"got %o", (int)[[path attributes] filePosixPermissions]);
145  STAssertTrue([path isDirectory], nil);
146  STAssertFalse([path isRoot], nil);
147
148  // Trying to create a file where a dir already exists should fail
149  path = [tmp createFileName:@"foo" mode:0555];
150  STAssertNil(path, nil);
151
152  // Calling create again should succeed
153  path = [tmp createDirectoryName:@"foo" mode:0555];
154  STAssertNotNil(path, nil);
155  STAssertEqualObjects([path name], @"foo", nil);
156  STAssertTrue([[path attributes] filePosixPermissions] == 0555,
157               @"got %o", (int)[[path attributes] filePosixPermissions]);
158  STAssertTrue([path isDirectory], nil);
159  STAssertFalse([path isRoot], nil);
160
161  GTMPath *foo = [GTMPath pathWithFullPath:fooPath];
162  STAssertNotNil(foo, nil);
163  STAssertEqualObjects([path name], @"foo", nil);
164  STAssertTrue([[path attributes] filePosixPermissions] == 0555,
165               @"got %o", (int)[[path attributes] filePosixPermissions]);
166  STAssertTrue([path isDirectory], nil);
167  STAssertFalse([path isRoot], nil);
168}
169
170- (void)testFileCreation {
171  GTMPath *tmp = [GTMPath pathWithFullPath:testDirectory_];
172  GTMPath *path = nil;
173
174  NSString *fooPath = [[tmp fullPath] stringByAppendingPathComponent:@"foo"];
175  path = [GTMPath pathWithFullPath:fooPath];
176  STAssertNil(path, nil);
177
178  path = [tmp createFileName:@"foo" mode:0555];
179  STAssertNotNil(path, nil);
180  STAssertEqualObjects([path name], @"foo", nil);
181  STAssertTrue([[path attributes] filePosixPermissions] == 0555, nil);
182  STAssertFalse([path isDirectory], nil);
183  STAssertFalse([path isRoot], nil);
184
185  // Trying to create a dir where a file already exists should fail.
186  path = [tmp createDirectoryName:@"foo" mode:0555];
187  STAssertNil(path, nil);
188
189  // Calling create again should succeed
190  path = [tmp createFileName:@"foo" mode:0555];
191  STAssertNotNil(path, nil);
192  STAssertEqualObjects([path name], @"foo", nil);
193  STAssertTrue([[path attributes] filePosixPermissions] == 0555, nil);
194  STAssertFalse([path isDirectory], nil);
195  STAssertFalse([path isRoot], nil);
196
197  GTMPath *foo = [GTMPath pathWithFullPath:fooPath];
198  STAssertNotNil(foo, nil);
199  STAssertEqualObjects([path name], @"foo", nil);
200  STAssertTrue([[path attributes] filePosixPermissions] == 0555, nil);
201  STAssertFalse([path isDirectory], nil);
202  STAssertFalse([path isRoot], nil);
203
204  // Make sure we can't create a file/directory rooted off of |foo|, since it's
205  // not a directory.
206  path = [foo createFileName:@"bar" mode:0555];
207  STAssertNil(path, nil);
208  path = [foo createDirectoryName:@"bar" mode:0555];
209  STAssertNil(path, nil);
210}
211
212- (void)testHierarchyCreation {
213  GTMPath *tmp = [GTMPath pathWithFullPath:testDirectory_];
214  NSString *fooPath = [[tmp fullPath] stringByAppendingPathComponent:@"foo"];
215  GTMPath *path = [GTMPath pathWithFullPath:fooPath];
216  STAssertNil(path, nil);
217
218  path = [[[tmp createDirectoryName:@"foo" mode:0755]
219                  createDirectoryName:@"bar" mode:0756]
220                    createDirectoryName:@"baz" mode:0757];
221  STAssertNotNil(path, nil);
222
223  // Check "baz"
224  STAssertEqualObjects([path name], @"baz", nil);
225  STAssertTrue([[path attributes] filePosixPermissions] == 0757, nil);
226
227  // Check "bar"
228  path = [path parent];
229  STAssertEqualObjects([path name], @"bar", nil);
230  STAssertTrue([[path attributes] filePosixPermissions] == 0756, nil);
231
232  // Check "foo"
233  path = [path parent];
234  STAssertEqualObjects([path name], @"foo", nil);
235  STAssertTrue([[path attributes] filePosixPermissions] == 0755, nil);
236}
237
238@end
239
240#endif //  MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5