FIRStorageMetadataTests.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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/FIRStorageMetadata.h"
  16. #import "FirebaseStorage/Sources/Public/FIRStorageMetadata.h"
  17. #import "FirebaseStorage/Sources/FIRStorageGetDownloadURLTask.h"
  18. #import "FirebaseStorage/Sources/FIRStorageGetDownloadURLTask_Private.h"
  19. #import "FirebaseStorage/Sources/FIRStorageMetadata_Private.h"
  20. #import "FirebaseStorage/Sources/FIRStorageUtils.h"
  21. @interface FIRStorageMetadataTests : XCTestCase
  22. @end
  23. @implementation FIRStorageMetadataTests
  24. - (void)testInitialzeNoMetadata {
  25. FIRStorageMetadata *metadata = [[FIRStorageMetadata alloc] initWithDictionary:@{}];
  26. XCTAssertNotNil(metadata);
  27. }
  28. - (void)testInitialzeFullMetadata {
  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. FIRStorageMetadata *metadata = [[FIRStorageMetadata 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. FIRStorageMetadata *metadata = [[FIRStorageMetadata 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. NSURL *actualURL = [FIRStorageGetDownloadURLTask downloadURLFromMetadataDictionary:metaDict];
  124. XCTAssertNil(actualURL);
  125. }
  126. - (void)testInitializeDownloadURLFromToken {
  127. NSDictionary *metaDict = @{
  128. kFIRStorageMetadataBucket : @"bucket",
  129. kFIRStorageMetadataDownloadTokens : @"12345,ignored",
  130. kFIRStorageMetadataName : @"path/to/object",
  131. };
  132. NSString *URLformat = @"https://firebasestorage.googleapis.com/v0/b/%@/o/%@?alt=media&token=%@";
  133. NSString *expectedURL = [NSString
  134. stringWithFormat:URLformat, metaDict[kFIRStorageMetadataBucket],
  135. [FIRStorageUtils GCSEscapedString:metaDict[kFIRStorageMetadataName]],
  136. @"12345"];
  137. NSURL *actualURL = [FIRStorageGetDownloadURLTask downloadURLFromMetadataDictionary:metaDict];
  138. XCTAssertEqualObjects([actualURL absoluteString], expectedURL);
  139. }
  140. - (void)testInitialzeMetadataWithFile {
  141. NSDictionary *metaDict = @{
  142. kFIRStorageMetadataBucket : @"bucket",
  143. kFIRStorageMetadataName : @"path/to/file",
  144. };
  145. FIRStorageMetadata *metadata = [[FIRStorageMetadata alloc] initWithDictionary:metaDict];
  146. [metadata setType:FIRStorageMetadataTypeFile];
  147. XCTAssertEqual(metadata.isFile, YES);
  148. XCTAssertEqual(metadata.isFolder, NO);
  149. }
  150. - (void)testInitialzeMetadataWithFolder {
  151. NSDictionary *metaDict = @{
  152. kFIRStorageMetadataBucket : @"bucket",
  153. kFIRStorageMetadataName : @"path/to/folder/",
  154. };
  155. FIRStorageMetadata *metadata = [[FIRStorageMetadata alloc] initWithDictionary:metaDict];
  156. [metadata setType:FIRStorageMetadataTypeFolder];
  157. XCTAssertEqual(metadata.isFolder, YES);
  158. XCTAssertEqual(metadata.isFile, NO);
  159. }
  160. - (void)testReflexiveMetadataEquality {
  161. NSDictionary *metaDict = @{
  162. kFIRStorageMetadataBucket : @"bucket",
  163. kFIRStorageMetadataName : @"path/to/object",
  164. };
  165. FIRStorageMetadata *metadata0 = [[FIRStorageMetadata alloc] initWithDictionary:metaDict];
  166. FIRStorageMetadata *metadata1 = metadata0;
  167. XCTAssertEqual(metadata0, metadata1);
  168. XCTAssertEqualObjects(metadata0, metadata1);
  169. }
  170. - (void)testNonsenseMetadataEquality {
  171. NSDictionary *metaDict = @{
  172. kFIRStorageMetadataBucket : @"bucket",
  173. kFIRStorageMetadataName : @"path/to/object",
  174. };
  175. FIRStorageMetadata *metadata0 = [[FIRStorageMetadata alloc] initWithDictionary:metaDict];
  176. XCTAssertNotEqualObjects(metadata0, @"I'm not object metadata!");
  177. }
  178. - (void)testMetadataEquality {
  179. NSDictionary *metaDict = @{
  180. kFIRStorageMetadataBucket : @"bucket",
  181. kFIRStorageMetadataName : @"path/to/object",
  182. kFIRStorageMetadataMd5Hash : @"d41d8cd98f00b204e9800998ecf8427e",
  183. };
  184. FIRStorageMetadata *metadata0 = [[FIRStorageMetadata alloc] initWithDictionary:metaDict];
  185. FIRStorageMetadata *metadata1 = [[FIRStorageMetadata alloc] initWithDictionary:metaDict];
  186. XCTAssertNotEqual(metadata0, metadata1);
  187. XCTAssertEqualObjects(metadata0, metadata1);
  188. }
  189. - (void)testMetadataMd5Inequality {
  190. NSDictionary *firstDict = @{
  191. kFIRStorageMetadataMd5Hash : @"d41d8cd98f00b204e9800998ecf8427e",
  192. };
  193. NSDictionary *secondDict = @{
  194. kFIRStorageMetadataMd5Hash : @"foo",
  195. };
  196. FIRStorageMetadata *firstMetadata = [[FIRStorageMetadata alloc] initWithDictionary:firstDict];
  197. FIRStorageMetadata *secondMetadata = [[FIRStorageMetadata alloc] initWithDictionary:secondDict];
  198. XCTAssertNotEqualObjects(firstMetadata, secondMetadata);
  199. }
  200. - (void)testMetadataCopy {
  201. NSDictionary *metaDict = @{
  202. kFIRStorageMetadataBucket : @"bucket",
  203. kFIRStorageMetadataName : @"path/to/object",
  204. kFIRStorageMetadataMd5Hash : @"d41d8cd98f00b204e9800998ecf8427e",
  205. };
  206. FIRStorageMetadata *metadata0 = [[FIRStorageMetadata alloc] initWithDictionary:metaDict];
  207. FIRStorageMetadata *metadata1 = [metadata0 copy];
  208. XCTAssertNotEqual(metadata0, metadata1);
  209. XCTAssertEqualObjects(metadata0, metadata1);
  210. }
  211. - (void)testUpdatedMetadata {
  212. NSDictionary *oldMetadata = @{
  213. kFIRStorageMetadataContentLanguage : @"old",
  214. kFIRStorageMetadataCustomMetadata : @{@"foo" : @"old", @"bar" : @"old"}
  215. };
  216. FIRStorageMetadata *metadata = [[FIRStorageMetadata alloc] initWithDictionary:oldMetadata];
  217. metadata.contentLanguage = @"new";
  218. metadata.customMetadata = @{@"foo" : @"new", @"bar" : @"old"};
  219. NSDictionary *update = [metadata updatedMetadata];
  220. NSDictionary *expectedUpdate = @{
  221. kFIRStorageMetadataContentLanguage : @"new",
  222. kFIRStorageMetadataCustomMetadata : @{@"foo" : @"new"}
  223. };
  224. XCTAssertEqualObjects(update, expectedUpdate);
  225. }
  226. - (void)testUpdatedMetadataWithEmptyUpdate {
  227. NSDictionary *oldMetadata = @{
  228. kFIRStorageMetadataContentLanguage : @"old",
  229. kFIRStorageMetadataCustomMetadata : @{@"foo" : @"old", @"bar" : @"old"}
  230. };
  231. FIRStorageMetadata *metadata = [[FIRStorageMetadata alloc] initWithDictionary:oldMetadata];
  232. NSDictionary *update = [metadata updatedMetadata];
  233. NSDictionary *expectedUpdate = @{kFIRStorageMetadataCustomMetadata : @{}};
  234. XCTAssertEqualObjects(update, expectedUpdate);
  235. }
  236. - (void)testUpdatedMetadataWithDelete {
  237. NSDictionary *oldMetadata = @{
  238. kFIRStorageMetadataContentLanguage : @"old",
  239. kFIRStorageMetadataCustomMetadata : @{@"foo" : @"old", @"bar" : @"old"}
  240. };
  241. FIRStorageMetadata *metadata = [[FIRStorageMetadata alloc] initWithDictionary:oldMetadata];
  242. metadata.contentLanguage = nil;
  243. metadata.customMetadata = @{@"foo" : @"old"};
  244. NSDictionary *update = [metadata updatedMetadata];
  245. NSDictionary *expectedUpdate = @{
  246. kFIRStorageMetadataContentLanguage : [NSNull null],
  247. kFIRStorageMetadataCustomMetadata : @{@"bar" : [NSNull null]}
  248. };
  249. XCTAssertEqualObjects(update, expectedUpdate);
  250. }
  251. - (void)testMetadataHashEquality {
  252. NSDictionary *metaDict = @{
  253. kFIRStorageMetadataBucket : @"bucket",
  254. kFIRStorageMetadataName : @"path/to/object",
  255. };
  256. FIRStorageMetadata *metadata0 = [[FIRStorageMetadata alloc] initWithDictionary:metaDict];
  257. FIRStorageMetadata *metadata1 = [[FIRStorageMetadata alloc] initWithDictionary:metaDict];
  258. XCTAssertNotEqual(metadata0, metadata1);
  259. XCTAssertEqual([metadata0 hash], [metadata1 hash]);
  260. }
  261. - (void)testZuluTimeOffset {
  262. NSDictionary *metaDict = @{kFIRStorageMetadataTimeCreated : @"1992-08-07T17:22:53.108Z"};
  263. FIRStorageMetadata *metadata = [[FIRStorageMetadata alloc] initWithDictionary:metaDict];
  264. XCTAssertNotNil(metadata.timeCreated);
  265. }
  266. - (void)testZuluZeroTimeOffset {
  267. NSDictionary *metaDict = @{kFIRStorageMetadataTimeCreated : @"1992-08-07T17:22:53.108+0000"};
  268. FIRStorageMetadata *metadata = [[FIRStorageMetadata alloc] initWithDictionary:metaDict];
  269. XCTAssertNotNil(metadata.timeCreated);
  270. }
  271. - (void)testGoogleStandardTimeOffset {
  272. NSDictionary *metaDict = @{kFIRStorageMetadataTimeCreated : @"1992-08-07T17:22:53.108-0700"};
  273. FIRStorageMetadata *metadata = [[FIRStorageMetadata alloc] initWithDictionary:metaDict];
  274. XCTAssertNotNil(metadata.timeCreated);
  275. }
  276. - (void)testUnspecifiedTimeOffset {
  277. NSDictionary *metaDict = @{kFIRStorageMetadataTimeCreated : @"1992-08-07T17:22:53.108-0000"};
  278. FIRStorageMetadata *metadata = [[FIRStorageMetadata alloc] initWithDictionary:metaDict];
  279. XCTAssertNotNil(metadata.timeCreated);
  280. }
  281. - (void)testNoTimeOffset {
  282. NSDictionary *metaDict = @{kFIRStorageMetadataTimeCreated : @"1992-08-07T17:22:53.108"};
  283. FIRStorageMetadata *metadata = [[FIRStorageMetadata alloc] initWithDictionary:metaDict];
  284. XCTAssertNil(metadata.timeCreated);
  285. }
  286. @end