FIRStorageMetadataTests.m 15 KB

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