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