FIRStorageMetadataTests.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // Copyright 2017 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <XCTest/XCTest.h>
  15. #import "FirebaseStorage/Sources/Public/FirebaseStorage/FIRStorageMetadata.h"
  16. #import "FirebaseStorage/Sources/FIRStorageGetDownloadURLTask.h"
  17. #import "FirebaseStorage/Sources/FIRStorageGetDownloadURLTask_Private.h"
  18. #import "FirebaseStorage/Sources/FIRStorageMetadata_Private.h"
  19. #import "FirebaseStorage/Sources/FIRStorageUtils.h"
  20. #import "FirebaseStorage/Tests/Unit/FIRStorageTestHelpers.h"
  21. @interface FIRIMPLStorageMetadataTests : XCTestCase
  22. @end
  23. @implementation FIRIMPLStorageMetadataTests
  24. - (void)testInitialzeNoMetadata {
  25. FIRIMPLStorageMetadata *metadata = [[FIRIMPLStorageMetadata alloc] initWithDictionary:@{}];
  26. XCTAssertNotNil(metadata);
  27. }
  28. - (void)testInitializeFullMetadata {
  29. NSDictionary *metaDict = @{
  30. kFIRStorageMetadataBucket : @"bucket",
  31. kFIRStorageMetadataCacheControl : @"max-age=3600, no-cache",
  32. kFIRStorageMetadataContentDisposition : @"inline",
  33. kFIRStorageMetadataContentEncoding : @"gzip",
  34. kFIRStorageMetadataContentLanguage : @"en-us",
  35. kFIRStorageMetadataContentType : @"application/octet-stream",
  36. kFIRStorageMetadataCustomMetadata : @{@"foo" : @{@"bar" : @"baz"}},
  37. kFIRStorageMetadataGeneration : @"12345",
  38. kFIRStorageMetadataMetageneration : @"67890",
  39. kFIRStorageMetadataName : @"path/to/object",
  40. kFIRStorageMetadataTimeCreated : @"1992-08-07T17:22:53.108Z",
  41. kFIRStorageMetadataUpdated : @"2016-03-01T20:16:01.673Z",
  42. kFIRStorageMetadataMd5Hash : @"d41d8cd98f00b204e9800998ecf8427e",
  43. kFIRStorageMetadataSize : @1337
  44. };
  45. FIRIMPLStorageMetadata *metadata = [[FIRIMPLStorageMetadata alloc] initWithDictionary:metaDict];
  46. XCTAssertNotNil(metadata);
  47. XCTAssertEqualObjects(metadata.bucket, metaDict[kFIRStorageMetadataBucket]);
  48. XCTAssertEqualObjects(metadata.cacheControl, metaDict[kFIRStorageMetadataCacheControl]);
  49. XCTAssertEqualObjects(metadata.contentDisposition,
  50. metaDict[kFIRStorageMetadataContentDisposition]);
  51. XCTAssertEqualObjects(metadata.contentEncoding, metaDict[kFIRStorageMetadataContentEncoding], );
  52. XCTAssertEqualObjects(metadata.contentType, metaDict[kFIRStorageMetadataContentType]);
  53. XCTAssertEqualObjects(metadata.customMetadata, metaDict[kFIRStorageMetadataCustomMetadata]);
  54. XCTAssertEqualObjects(metadata.md5Hash, metaDict[kFIRStorageMetadataMd5Hash]);
  55. NSString *generation = [NSString stringWithFormat:@"%lld", metadata.generation];
  56. XCTAssertEqualObjects(generation, metaDict[kFIRStorageMetadataGeneration]);
  57. NSString *metageneration = [NSString stringWithFormat:@"%lld", metadata.metageneration];
  58. XCTAssertEqualObjects(metageneration, metaDict[kFIRStorageMetadataMetageneration]);
  59. XCTAssertEqualObjects(metadata.path, metaDict[kFIRStorageMetadataName]);
  60. XCTAssertEqualObjects([metadata RFC3339StringFromDate:metadata.timeCreated],
  61. metaDict[kFIRStorageMetadataTimeCreated]);
  62. XCTAssertEqualObjects([metadata RFC3339StringFromDate:metadata.updated],
  63. metaDict[kFIRStorageMetadataUpdated]);
  64. NSNumber *size = [NSNumber numberWithLongLong:metadata.size];
  65. XCTAssertEqualObjects(size, metaDict[kFIRStorageMetadataSize]);
  66. }
  67. - (void)testDictionaryRepresentation {
  68. NSDictionary *metaDict = @{
  69. kFIRStorageMetadataBucket : @"bucket",
  70. kFIRStorageMetadataCacheControl : @"max-age=3600, no-cache",
  71. kFIRStorageMetadataContentDisposition : @"inline",
  72. kFIRStorageMetadataContentEncoding : @"gzip",
  73. kFIRStorageMetadataContentLanguage : @"en-us",
  74. kFIRStorageMetadataContentType : @"application/octet-stream",
  75. kFIRStorageMetadataCustomMetadata : @{@"foo" : @{@"bar" : @"baz"}},
  76. kFIRStorageMetadataGeneration : @"12345",
  77. kFIRStorageMetadataMetageneration : @"67890",
  78. kFIRStorageMetadataName : @"path/to/object",
  79. kFIRStorageMetadataTimeCreated : @"1992-08-07T17:22:53.108Z",
  80. kFIRStorageMetadataUpdated : @"2016-03-01T20:16:01.673Z",
  81. kFIRStorageMetadataMd5Hash : @"d41d8cd98f00b204e9800998ecf8427e",
  82. kFIRStorageMetadataSize : @1337
  83. };
  84. FIRIMPLStorageMetadata *metadata = [[FIRIMPLStorageMetadata alloc] initWithDictionary:metaDict];
  85. NSDictionary *dictRepresentation = [metadata dictionaryRepresentation];
  86. XCTAssertNotNil(dictRepresentation);
  87. XCTAssertEqualObjects(dictRepresentation[kFIRStorageMetadataBucket],
  88. metaDict[kFIRStorageMetadataBucket]);
  89. XCTAssertEqualObjects(dictRepresentation[kFIRStorageMetadataCacheControl],
  90. metaDict[kFIRStorageMetadataCacheControl]);
  91. XCTAssertEqualObjects(dictRepresentation[kFIRStorageMetadataContentDisposition],
  92. metaDict[kFIRStorageMetadataContentDisposition]);
  93. XCTAssertEqualObjects(dictRepresentation[kFIRStorageMetadataContentEncoding],
  94. metaDict[kFIRStorageMetadataContentEncoding]);
  95. XCTAssertEqualObjects(dictRepresentation[kFIRStorageMetadataContentLanguage],
  96. metaDict[kFIRStorageMetadataContentLanguage]);
  97. XCTAssertEqualObjects(dictRepresentation[kFIRStorageMetadataContentType],
  98. metaDict[kFIRStorageMetadataContentType]);
  99. XCTAssertEqualObjects(dictRepresentation[kFIRStorageMetadataCustomMetadata],
  100. metaDict[kFIRStorageMetadataCustomMetadata]);
  101. XCTAssertEqualObjects(dictRepresentation[kFIRStorageMetadataDownloadTokens],
  102. metaDict[kFIRStorageMetadataDownloadTokens]);
  103. XCTAssertEqualObjects(dictRepresentation[kFIRStorageMetadataGeneration],
  104. metaDict[kFIRStorageMetadataGeneration]);
  105. XCTAssertEqualObjects(dictRepresentation[kFIRStorageMetadataMetageneration],
  106. metaDict[kFIRStorageMetadataMetageneration]);
  107. XCTAssertEqualObjects(dictRepresentation[kFIRStorageMetadataName],
  108. metaDict[kFIRStorageMetadataName]);
  109. XCTAssertEqualObjects(dictRepresentation[kFIRStorageMetadataTimeCreated],
  110. metaDict[kFIRStorageMetadataTimeCreated]);
  111. XCTAssertEqualObjects(dictRepresentation[kFIRStorageMetadataUpdated],
  112. metaDict[kFIRStorageMetadataUpdated]);
  113. XCTAssertEqualObjects(dictRepresentation[kFIRStorageMetadataSize],
  114. metaDict[kFIRStorageMetadataSize]);
  115. XCTAssertEqualObjects(dictRepresentation[kFIRStorageMetadataMd5Hash],
  116. metaDict[kFIRStorageMetadataMd5Hash]);
  117. }
  118. - (void)testInitializeEmptyDownloadURL {
  119. NSDictionary *metaDict = @{
  120. kFIRStorageMetadataBucket : @"bucket",
  121. kFIRStorageMetadataName : @"path/to/object",
  122. };
  123. #pragma clang diagnostic push
  124. #pragma clang diagnostic ignored "-Wnonnull"
  125. FIRStorageGetDownloadURLTask *task =
  126. [[FIRStorageGetDownloadURLTask alloc] initWithReference:[FIRStorageTestHelpers rootReference]
  127. fetcherService:nil
  128. dispatchQueue:nil
  129. completion:nil];
  130. #pragma clang diagnostic pop
  131. NSURL *actualURL = [task downloadURLFromMetadataDictionary:metaDict];
  132. XCTAssertNil(actualURL);
  133. }
  134. - (void)testInitializeDownloadURLFromToken {
  135. NSDictionary *metaDict = @{
  136. kFIRStorageMetadataBucket : @"bucket",
  137. kFIRStorageMetadataDownloadTokens : @"12345,ignored",
  138. kFIRStorageMetadataName : @"path/to/object",
  139. };
  140. NSString *URLformat =
  141. @"https://firebasestorage.googleapis.com:443/v0/b/%@/o/%@?alt=media&token=%@";
  142. NSString *expectedURL = [NSString
  143. stringWithFormat:URLformat, metaDict[kFIRStorageMetadataBucket],
  144. [FIRStorageUtils GCSEscapedString:metaDict[kFIRStorageMetadataName]],
  145. @"12345"];
  146. #pragma clang diagnostic push
  147. #pragma clang diagnostic ignored "-Wnonnull"
  148. FIRStorageGetDownloadURLTask *task = [[FIRStorageGetDownloadURLTask alloc]
  149. initWithReference:[[FIRStorageTestHelpers rootReference] child:@"path/to/object"]
  150. fetcherService:nil
  151. dispatchQueue:nil
  152. completion:nil];
  153. #pragma clang diagnostic pop
  154. NSURL *actualURL = [task downloadURLFromMetadataDictionary:metaDict];
  155. XCTAssertEqualObjects([actualURL absoluteString], expectedURL);
  156. }
  157. - (void)testInitialzeMetadataWithFile {
  158. NSDictionary *metaDict = @{
  159. kFIRStorageMetadataBucket : @"bucket",
  160. kFIRStorageMetadataName : @"path/to/file",
  161. };
  162. FIRIMPLStorageMetadata *metadata = [[FIRIMPLStorageMetadata alloc] initWithDictionary:metaDict];
  163. [metadata setType:FIRStorageMetadataTypeFile];
  164. XCTAssertEqual(metadata.isFile, YES);
  165. XCTAssertEqual(metadata.isFolder, NO);
  166. }
  167. - (void)testInitialzeMetadataWithFolder {
  168. NSDictionary *metaDict = @{
  169. kFIRStorageMetadataBucket : @"bucket",
  170. kFIRStorageMetadataName : @"path/to/folder/",
  171. };
  172. FIRIMPLStorageMetadata *metadata = [[FIRIMPLStorageMetadata alloc] initWithDictionary:metaDict];
  173. [metadata setType:FIRStorageMetadataTypeFolder];
  174. XCTAssertEqual(metadata.isFolder, YES);
  175. XCTAssertEqual(metadata.isFile, NO);
  176. }
  177. - (void)testReflexiveMetadataEquality {
  178. NSDictionary *metaDict = @{
  179. kFIRStorageMetadataBucket : @"bucket",
  180. kFIRStorageMetadataName : @"path/to/object",
  181. };
  182. FIRIMPLStorageMetadata *metadata0 = [[FIRIMPLStorageMetadata alloc] initWithDictionary:metaDict];
  183. FIRIMPLStorageMetadata *metadata1 = metadata0;
  184. XCTAssertEqual(metadata0, metadata1);
  185. XCTAssertEqualObjects(metadata0, metadata1);
  186. }
  187. - (void)testNonsenseMetadataEquality {
  188. NSDictionary *metaDict = @{
  189. kFIRStorageMetadataBucket : @"bucket",
  190. kFIRStorageMetadataName : @"path/to/object",
  191. };
  192. FIRIMPLStorageMetadata *metadata0 = [[FIRIMPLStorageMetadata alloc] initWithDictionary:metaDict];
  193. XCTAssertNotEqualObjects(metadata0, @"I'm not object metadata!");
  194. }
  195. - (void)testMetadataEquality {
  196. NSDictionary *metaDict = @{
  197. kFIRStorageMetadataBucket : @"bucket",
  198. kFIRStorageMetadataName : @"path/to/object",
  199. kFIRStorageMetadataMd5Hash : @"d41d8cd98f00b204e9800998ecf8427e",
  200. };
  201. FIRIMPLStorageMetadata *metadata0 = [[FIRIMPLStorageMetadata alloc] initWithDictionary:metaDict];
  202. FIRIMPLStorageMetadata *metadata1 = [[FIRIMPLStorageMetadata alloc] initWithDictionary:metaDict];
  203. XCTAssertNotEqual(metadata0, metadata1);
  204. XCTAssertEqualObjects(metadata0, metadata1);
  205. }
  206. - (void)testMetadataMd5Inequality {
  207. NSDictionary *firstDict = @{
  208. kFIRStorageMetadataMd5Hash : @"d41d8cd98f00b204e9800998ecf8427e",
  209. };
  210. NSDictionary *secondDict = @{
  211. kFIRStorageMetadataMd5Hash : @"foo",
  212. };
  213. FIRIMPLStorageMetadata *firstMetadata =
  214. [[FIRIMPLStorageMetadata alloc] initWithDictionary:firstDict];
  215. FIRIMPLStorageMetadata *secondMetadata =
  216. [[FIRIMPLStorageMetadata alloc] initWithDictionary:secondDict];
  217. XCTAssertNotEqualObjects(firstMetadata, secondMetadata);
  218. }
  219. - (void)testMetadataCopy {
  220. NSDictionary *metaDict = @{
  221. kFIRStorageMetadataBucket : @"bucket",
  222. kFIRStorageMetadataName : @"path/to/object",
  223. kFIRStorageMetadataMd5Hash : @"d41d8cd98f00b204e9800998ecf8427e",
  224. };
  225. FIRIMPLStorageMetadata *metadata0 = [[FIRIMPLStorageMetadata alloc] initWithDictionary:metaDict];
  226. FIRIMPLStorageMetadata *metadata1 = [metadata0 copy];
  227. XCTAssertNotEqual(metadata0, metadata1);
  228. XCTAssertEqualObjects(metadata0, metadata1);
  229. }
  230. - (void)testUpdatedMetadata {
  231. NSDictionary *oldMetadata = @{
  232. kFIRStorageMetadataContentLanguage : @"old",
  233. kFIRStorageMetadataCustomMetadata : @{@"foo" : @"old", @"bar" : @"old"}
  234. };
  235. FIRIMPLStorageMetadata *metadata =
  236. [[FIRIMPLStorageMetadata alloc] initWithDictionary:oldMetadata];
  237. metadata.contentLanguage = @"new";
  238. metadata.customMetadata = @{@"foo" : @"new", @"bar" : @"old"};
  239. NSDictionary *update = [metadata updatedMetadata];
  240. NSDictionary *expectedUpdate = @{
  241. kFIRStorageMetadataContentLanguage : @"new",
  242. kFIRStorageMetadataCustomMetadata : @{@"foo" : @"new"}
  243. };
  244. XCTAssertEqualObjects(update, expectedUpdate);
  245. }
  246. - (void)testUpdatedMetadataWithEmptyUpdate {
  247. NSDictionary *oldMetadata = @{
  248. kFIRStorageMetadataContentLanguage : @"old",
  249. kFIRStorageMetadataCustomMetadata : @{@"foo" : @"old", @"bar" : @"old"}
  250. };
  251. FIRIMPLStorageMetadata *metadata =
  252. [[FIRIMPLStorageMetadata alloc] initWithDictionary:oldMetadata];
  253. NSDictionary *update = [metadata updatedMetadata];
  254. NSDictionary *expectedUpdate = @{kFIRStorageMetadataCustomMetadata : @{}};
  255. XCTAssertEqualObjects(update, expectedUpdate);
  256. }
  257. - (void)testUpdatedMetadataWithDelete {
  258. NSDictionary *oldMetadata = @{
  259. kFIRStorageMetadataContentLanguage : @"old",
  260. kFIRStorageMetadataCustomMetadata : @{@"foo" : @"old", @"bar" : @"old"}
  261. };
  262. FIRIMPLStorageMetadata *metadata =
  263. [[FIRIMPLStorageMetadata alloc] initWithDictionary:oldMetadata];
  264. metadata.contentLanguage = nil;
  265. metadata.customMetadata = @{@"foo" : @"old"};
  266. NSDictionary *update = [metadata updatedMetadata];
  267. NSDictionary *expectedUpdate = @{
  268. kFIRStorageMetadataContentLanguage : [NSNull null],
  269. kFIRStorageMetadataCustomMetadata : @{@"bar" : [NSNull null]}
  270. };
  271. XCTAssertEqualObjects(update, expectedUpdate);
  272. }
  273. - (void)testMetadataHashEquality {
  274. NSDictionary *metaDict = @{
  275. kFIRStorageMetadataBucket : @"bucket",
  276. kFIRStorageMetadataName : @"path/to/object",
  277. };
  278. FIRIMPLStorageMetadata *metadata0 = [[FIRIMPLStorageMetadata alloc] initWithDictionary:metaDict];
  279. FIRIMPLStorageMetadata *metadata1 = [[FIRIMPLStorageMetadata alloc] initWithDictionary:metaDict];
  280. XCTAssertNotEqual(metadata0, metadata1);
  281. XCTAssertEqual([metadata0 hash], [metadata1 hash]);
  282. }
  283. - (void)testZuluTimeOffset {
  284. NSDictionary *metaDict = @{kFIRStorageMetadataTimeCreated : @"1992-08-07T17:22:53.108Z"};
  285. FIRIMPLStorageMetadata *metadata = [[FIRIMPLStorageMetadata alloc] initWithDictionary:metaDict];
  286. XCTAssertNotNil(metadata.timeCreated);
  287. }
  288. - (void)testZuluZeroTimeOffset {
  289. NSDictionary *metaDict = @{kFIRStorageMetadataTimeCreated : @"1992-08-07T17:22:53.108+0000"};
  290. FIRIMPLStorageMetadata *metadata = [[FIRIMPLStorageMetadata alloc] initWithDictionary:metaDict];
  291. XCTAssertNotNil(metadata.timeCreated);
  292. }
  293. - (void)testGoogleStandardTimeOffset {
  294. NSDictionary *metaDict = @{kFIRStorageMetadataTimeCreated : @"1992-08-07T17:22:53.108-0700"};
  295. FIRIMPLStorageMetadata *metadata = [[FIRIMPLStorageMetadata alloc] initWithDictionary:metaDict];
  296. XCTAssertNotNil(metadata.timeCreated);
  297. }
  298. - (void)testUnspecifiedTimeOffset {
  299. NSDictionary *metaDict = @{kFIRStorageMetadataTimeCreated : @"1992-08-07T17:22:53.108-0000"};
  300. FIRIMPLStorageMetadata *metadata = [[FIRIMPLStorageMetadata alloc] initWithDictionary:metaDict];
  301. XCTAssertNotNil(metadata.timeCreated);
  302. }
  303. - (void)testNoTimeOffset {
  304. NSDictionary *metaDict = @{kFIRStorageMetadataTimeCreated : @"1992-08-07T17:22:53.108"};
  305. FIRIMPLStorageMetadata *metadata = [[FIRIMPLStorageMetadata alloc] initWithDictionary:metaDict];
  306. XCTAssertNil(metadata.timeCreated);
  307. }
  308. @end