aboutsummaryrefslogtreecommitdiff
path: root/Foundation/GTMNSFileHandle+UniqueNameTest.m
blob: 0f0dc49188524b65a34a593b4798d3e2e645cade (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//
//  GTMNSFileHandle+UniqueNameTest.m
//
//  Copyright 2010 Google Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations under
//  the License.
//

#import "GTMSenTestCase.h"
#import "GTMNSFileHandle+UniqueName.h"

@interface GTMNSFileHandle_UniqueNameTest : GTMTestCase
@end

@implementation GTMNSFileHandle_UniqueNameTest

- (void)testFileHandleWithUniqueNameBasedOnFinalPath {
  NSFileHandle *handle
    = [NSFileHandle gtm_fileHandleWithUniqueNameBasedOn:nil
                                              finalPath:nil];
  XCTAssertNil(handle);

  // Try and create a file where we shouldn't be able to.
  NSString *path = nil;
  handle = [NSFileHandle gtm_fileHandleWithUniqueNameBasedOn:@"/System/HappyXXX.txt"
                                                   finalPath:&path];
  XCTAssertNil(handle);
  XCTAssertNil(path);

  NSFileManager *fm = [NSFileManager defaultManager];
  NSString *tempDir
    = [fm gtm_createTemporaryDirectoryBasedOn:@"GTMNSFileHandle_UniqueNameTestXXXXXX"];
  XCTAssertNotNil(tempDir);
  BOOL isDirectory = NO;
  XCTAssertTrue([fm fileExistsAtPath:tempDir isDirectory:&isDirectory] && isDirectory);

  // Test with extension
  handle = [NSFileHandle gtm_fileHandleWithUniqueNameBasedOn:@"HappyXXX.txt"
                                                 inDirectory:tempDir
                                                   finalPath:&path];
  XCTAssertNotNil(handle);
  XCTAssertEqualObjects([path pathExtension], @"txt");
  XCTAssertTrue([fm fileExistsAtPath:path]);

  // Test without extension
  handle = [NSFileHandle gtm_fileHandleWithUniqueNameBasedOn:@"HappyXXX"
                                                 inDirectory:tempDir
                                                   finalPath:&path];
  XCTAssertNotNil(handle);
  XCTAssertEqualObjects([path pathExtension], @"");
  XCTAssertTrue([fm fileExistsAtPath:path]);

  // Test passing in same name twice
  NSString *fullPath = [tempDir stringByAppendingPathComponent:@"HappyXXX"];
  NSString *newPath = nil;
  handle = [NSFileHandle gtm_fileHandleWithUniqueNameBasedOn:fullPath
                                                   finalPath:&newPath];
  XCTAssertNotNil(handle);
  XCTAssertNotNil(newPath);
  XCTAssertNotEqualObjects(path, newPath);
  XCTAssertTrue([fm fileExistsAtPath:newPath]);

  // Test passing in same name twice with no template
  fullPath = [tempDir stringByAppendingPathComponent:@"Sad"];
  newPath = nil;
  handle = [NSFileHandle gtm_fileHandleWithUniqueNameBasedOn:fullPath
                                                   finalPath:&newPath];
  XCTAssertNotNil(handle);
  XCTAssertNotNil(newPath);

  newPath = nil;
  handle = [NSFileHandle gtm_fileHandleWithUniqueNameBasedOn:fullPath
                                                   finalPath:&newPath];
  XCTAssertNil(handle);
  XCTAssertNil(newPath);

  [fm removeItemAtPath:tempDir error:nil];
}

- (void)testFileHandleWithUniqueNameBasedOnInDirectorySearchMaskFinalPath {
  NSFileManager *fm = [NSFileManager defaultManager];
  NSString *path = nil;
  NSFileHandle *handle
    = [NSFileHandle gtm_fileHandleWithUniqueNameBasedOn:nil
                                            inDirectory:NSCachesDirectory
                                             domainMask:NSUserDomainMask
                                              finalPath:&path];
  XCTAssertNil(handle);
  XCTAssertNil(path);

  handle  = [NSFileHandle gtm_fileHandleWithUniqueNameBasedOn:@"HappyXXX.txt"
                                                  inDirectory:NSCachesDirectory
                                                   domainMask:NSUserDomainMask
                                                    finalPath:&path];
  XCTAssertNotNil(handle);
  XCTAssertNotNil(path);
  XCTAssertTrue([fm fileExistsAtPath:path]);
  [fm removeItemAtPath:path error:nil];
}

@end

@interface GTMNSFileManager_UniqueNameTest : GTMTestCase
@end

@implementation GTMNSFileManager_UniqueNameTest

- (void)testCreateDirectoryWithUniqueNameBasedOn {
  NSFileManager *fm = [NSFileManager defaultManager];
  NSString *path
    = [fm gtm_createDirectoryWithUniqueNameBasedOn:@"/System/HappyXXX.txt"];
  XCTAssertNil(path);
}

- (void)testCreateDirectoryWithUniqueNameBasedOnInDirectorySearchMask {
  NSFileManager *fm = [NSFileManager defaultManager];
  NSString *path = [fm gtm_createDirectoryWithUniqueNameBasedOn:nil
                                                    inDirectory:NSCachesDirectory
                                                     domainMask:NSUserDomainMask];
  XCTAssertNil(path);

  path = [fm gtm_createDirectoryWithUniqueNameBasedOn:@"HappyXXX.txt"
                                          inDirectory:NSCachesDirectory
                                           domainMask:NSUserDomainMask];
  XCTAssertNotNil(path);
  BOOL isDirectory = NO;
  XCTAssertTrue([fm fileExistsAtPath:path isDirectory:&isDirectory] && isDirectory);
  NSError *error;
  XCTAssertTrue([fm removeItemAtPath:path error:&error], "%@", error);
}

@end