PageRenderTime 74ms CodeModel.GetById 42ms app.highlight 29ms RepoModel.GetById 2ms app.codeStats 0ms

/core/externals/update-engine/externals/google-toolbox-for-mac/Foundation/GTMNSFileHandle+UniqueNameTest.m

http://macfuse.googlecode.com/
Objective C | 157 lines | 112 code | 23 blank | 22 comment | 5 complexity | a74004961ad65d0a58680d845fb06656 MD5 | raw file
  1//
  2//  GTMNSFileHandle+UniqueNameTest.m
  3//
  4//  Copyright 2010 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 "GTMNSFileHandle+UniqueName.h"
 21
 22@interface GTMNSFileHandle_UniqueNameTest : GTMTestCase
 23@end
 24
 25@implementation GTMNSFileHandle_UniqueNameTest
 26
 27- (void)testFileHandleWithUniqueNameBasedOnFinalPath {
 28  NSFileHandle *handle
 29    = [NSFileHandle gtm_fileHandleWithUniqueNameBasedOn:nil
 30                                              finalPath:nil];
 31  STAssertNil(handle, nil);
 32
 33  // Try and create a file where we shouldn't be able to.
 34  NSString *path = nil;
 35  handle = [NSFileHandle gtm_fileHandleWithUniqueNameBasedOn:@"/System/HappyXXX.txt"
 36                                                   finalPath:&path];
 37  STAssertNil(handle, nil);
 38  STAssertNil(path, nil);
 39
 40  NSFileManager *fm = [NSFileManager defaultManager];
 41  NSString *tempDir
 42    = [fm gtm_createTemporaryDirectoryBasedOn:@"GTMNSFileHandle_UniqueNameTestXXXXXX"];
 43  STAssertNotNil(tempDir, nil);
 44  BOOL isDirectory = NO;
 45  STAssertTrue([fm fileExistsAtPath:tempDir isDirectory:&isDirectory]
 46               && isDirectory, nil);
 47
 48  // Test with extension
 49  handle = [NSFileHandle gtm_fileHandleWithUniqueNameBasedOn:@"HappyXXX.txt"
 50                                                 inDirectory:tempDir
 51                                                   finalPath:&path];
 52  STAssertNotNil(handle, nil);
 53  STAssertEqualObjects([path pathExtension], @"txt", nil);
 54  STAssertTrue([fm fileExistsAtPath:path], nil);
 55
 56  // Test without extension
 57  handle = [NSFileHandle gtm_fileHandleWithUniqueNameBasedOn:@"HappyXXX"
 58                                                 inDirectory:tempDir
 59                                                   finalPath:&path];
 60  STAssertNotNil(handle, nil);
 61  STAssertEqualObjects([path pathExtension], @"", nil);
 62  STAssertTrue([fm fileExistsAtPath:path], nil);
 63
 64  // Test passing in same name twice
 65  NSString *fullPath = [tempDir stringByAppendingPathComponent:@"HappyXXX"];
 66  NSString *newPath = nil;
 67  handle = [NSFileHandle gtm_fileHandleWithUniqueNameBasedOn:fullPath
 68                                                   finalPath:&newPath];
 69  STAssertNotNil(handle, nil);
 70  STAssertNotNil(newPath, nil);
 71  STAssertNotEqualObjects(path, newPath, nil);
 72  STAssertTrue([fm fileExistsAtPath:newPath], nil);
 73
 74  // Test passing in same name twice with no template
 75  fullPath = [tempDir stringByAppendingPathComponent:@"Sad"];
 76  newPath = nil;
 77  handle = [NSFileHandle gtm_fileHandleWithUniqueNameBasedOn:fullPath
 78                                                   finalPath:&newPath];
 79  STAssertNotNil(handle, nil);
 80  STAssertNotNil(newPath, nil);
 81
 82  newPath = nil;
 83  handle = [NSFileHandle gtm_fileHandleWithUniqueNameBasedOn:fullPath
 84                                                   finalPath:&newPath];
 85  STAssertNil(handle, nil);
 86  STAssertNil(newPath, nil);
 87
 88#if GTM_MACOS_SDK && (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5)
 89  [fm removeFileAtPath:tempDir handler:nil];
 90#else //  MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
 91  [fm removeItemAtPath:tempDir error:nil];
 92#endif
 93
 94}
 95
 96- (void)testFileHandleWithUniqueNameBasedOnInDirectorySearchMaskFinalPath {
 97  NSFileManager *fm = [NSFileManager defaultManager];
 98  NSString *path = nil;
 99  NSFileHandle *handle
100    = [NSFileHandle gtm_fileHandleWithUniqueNameBasedOn:nil
101                                            inDirectory:NSCachesDirectory
102                                             domainMask:NSUserDomainMask
103                                              finalPath:&path];
104  STAssertNil(handle, nil);
105  STAssertNil(path, nil);
106
107  handle  = [NSFileHandle gtm_fileHandleWithUniqueNameBasedOn:@"HappyXXX.txt"
108                                                  inDirectory:NSCachesDirectory
109                                                   domainMask:NSUserDomainMask
110                                                    finalPath:&path];
111  STAssertNotNil(handle, nil);
112  STAssertNotNil(path, nil);
113  STAssertTrue([fm fileExistsAtPath:path], nil);
114#if GTM_MACOS_SDK && (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5)
115  [fm removeFileAtPath:path handler:nil];
116#else //  MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
117  [fm removeItemAtPath:path error:nil];
118#endif
119}
120
121@end
122
123@interface GTMNSFileManager_UniqueNameTest : GTMTestCase
124@end
125
126@implementation GTMNSFileManager_UniqueNameTest
127
128- (void)testCreateDirectoryWithUniqueNameBasedOn {
129  NSFileManager *fm = [NSFileManager defaultManager];
130  NSString *path
131    = [fm gtm_createDirectoryWithUniqueNameBasedOn:@"/System/HappyXXX.txt"];
132  STAssertNil(path, nil);
133}
134
135- (void)testCreateDirectoryWithUniqueNameBasedOnInDirectorySearchMask {
136  NSFileManager *fm = [NSFileManager defaultManager];
137  NSString *path = [fm gtm_createDirectoryWithUniqueNameBasedOn:nil
138                                                    inDirectory:NSCachesDirectory
139                                                     domainMask:NSUserDomainMask];
140  STAssertNil(path, nil);
141
142  path = [fm gtm_createDirectoryWithUniqueNameBasedOn:@"HappyXXX.txt"
143                                          inDirectory:NSCachesDirectory
144                                           domainMask:NSUserDomainMask];
145  STAssertNotNil(path, nil);
146  BOOL isDirectory = NO;
147  STAssertTrue([fm fileExistsAtPath:path isDirectory:&isDirectory]
148               && isDirectory, nil);
149#if GTM_MACOS_SDK && (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5)
150  [fm removeFileAtPath:path handler:nil];
151#else //  MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
152  [fm removeItemAtPath:path error:nil];
153#endif
154}
155
156@end
157