OCCommunicationLibTests.m 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231
  1. //
  2. // Owncloud_iOs_ClientTests.m
  3. // Owncloud iOs ClientTests
  4. //
  5. // Copyright (C) 2016, ownCloud GmbH. ( http://www.owncloud.org/ )
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #import "OCCommunicationLibTests.h"
  24. #import "ConfigTests.h"
  25. #import "OCCommunication.h"
  26. #import "OCFrameworkConstants.h"
  27. #import "OCFileDto.h"
  28. #import "OCSharedDto.h"
  29. #import "ConfigTests.h"
  30. #import "AFURLSessionManager.h"
  31. #import "OCConstants.h"
  32. #import <UIKit/UIKit.h>
  33. /*
  34. * With this implementation we allow the connection with any HTTPS server
  35. */
  36. #if DEBUG
  37. @implementation NSURLRequest (NSURLRequestWithIgnoreSSL)
  38. + (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host {
  39. return YES;
  40. }
  41. @end
  42. #endif
  43. @implementation OCCommunicationLibTests
  44. //You must enter this information of your server in order that the unit test works
  45. ///-----------------------------------
  46. /// @name setUp
  47. ///-----------------------------------
  48. /**
  49. * Method to get ready the tests
  50. */
  51. - (void)setUp
  52. {
  53. [super setUp];
  54. _sharedOCCommunication = [[OCCommunication alloc] init];
  55. [_sharedOCCommunication setCredentialsWithUser:k_user andPassword:k_password];
  56. [_sharedOCCommunication setSecurityPolicyManagers:[_sharedOCCommunication createSecurityPolicy]];
  57. //Create Tests folder
  58. [self createFolderWithName:k_path_test_folder];
  59. }
  60. - (void)tearDown
  61. {
  62. //Delete Test folder
  63. [self deleteFolderWithName:k_path_test_folder];
  64. [super tearDown];
  65. }
  66. #pragma mark - Util Methods to Set Up the Tests
  67. ///-----------------------------------
  68. /// @name Create Folder With Name
  69. ///-----------------------------------
  70. /**
  71. * This method create a new folder with the name passed in the server
  72. *
  73. * @param NSString -> path
  74. */
  75. - (void) createFolderWithName:(NSString*)path{
  76. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  77. NSString *folder = [NSString stringWithFormat:@"%@%@",k_webdav_base_url,path];
  78. folder = [folder stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  79. [_sharedOCCommunication createFolder:folder onCommunication:_sharedOCCommunication withForbiddenCharactersSupported:NO successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  80. //Folder created
  81. NSLog(@"Folder created");
  82. dispatch_semaphore_signal(semaphore);
  83. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  84. NSLog(@"Error created folder");
  85. // Signal that block has completed
  86. dispatch_semaphore_signal(semaphore);
  87. } errorBeforeRequest:^(NSError *error) {
  88. NSLog(@"Error created folder");
  89. // Signal that block has completed
  90. dispatch_semaphore_signal(semaphore);
  91. }];
  92. // Run loop
  93. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  94. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  95. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  96. }
  97. ///-----------------------------------
  98. /// @name Delete folder With Name
  99. ///-----------------------------------
  100. /**
  101. * This method delete a folder with the name passed
  102. *
  103. * @param NSString -> path
  104. */
  105. - (void) deleteFolderWithName:(NSString *)path{
  106. NSString *folder = [NSString stringWithFormat:@"%@%@",k_webdav_base_url,path];
  107. folder = [folder stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  108. //We create a semaphore to wait until we recive the responses from Async calls
  109. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  110. [_sharedOCCommunication deleteFileOrFolder:folder onCommunication:_sharedOCCommunication successRequest:^(NSURLResponse * response, NSString *redirectedServer) {
  111. //Folder deleted
  112. NSLog(@"Folder deleted");
  113. dispatch_semaphore_signal(semaphore);
  114. } failureRquest:^(NSURLResponse * response, NSError * error, NSString *redirectedServer) {
  115. //Error
  116. NSLog(@"Error deleted folder");
  117. // Signal that block has completed
  118. dispatch_semaphore_signal(semaphore);
  119. }];
  120. // Run loop
  121. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  122. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  123. beforeDate:[NSDate dateWithTimeIntervalSinceNow:10]];
  124. }
  125. ///-----------------------------------
  126. /// @name Upload File
  127. ///-----------------------------------
  128. /**
  129. * This method upload a file from local path to remote path
  130. *
  131. * @param NSString -> localPath
  132. *
  133. * @param NSString -> remotePath
  134. */
  135. - (void) uploadFilePath:(NSString*)localPath inRemotePath:(NSString*)remotePath{
  136. //We create a semaphore to wait until we recive the responses from Async calls
  137. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  138. //Create the complete url
  139. NSString *serverUrl = [NSString stringWithFormat:@"%@%@",k_webdav_base_url,remotePath];
  140. //Path of server file file
  141. remotePath = [remotePath stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  142. NSURLSessionUploadTask *uploadTask = nil;
  143. uploadTask = [_sharedOCCommunication uploadFileSession:localPath toDestiny:serverUrl onCommunication:_sharedOCCommunication progress:^(NSProgress *progress) {
  144. NSLog(@"File: %lld bytes", progress.completedUnitCount);
  145. } successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  146. NSLog(@"File: %@ uploaded", localPath);
  147. dispatch_semaphore_signal(semaphore);
  148. } failureRequest:^(NSURLResponse *response, NSString *redirectedServer, NSError *error) {
  149. NSLog(@"Failed uploading: %@", localPath);
  150. NSLog(@"Error uploading");
  151. dispatch_semaphore_signal(semaphore);
  152. } failureBeforeRequest:^(NSError *error) {
  153. NSLog(@"File that do not exist does not upload");
  154. dispatch_semaphore_signal(semaphore);
  155. }];
  156. // Run loop
  157. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  158. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  159. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  160. }
  161. #pragma mark - Tests
  162. ///-----------------------------------
  163. /// @name testCreateFolder
  164. ///-----------------------------------
  165. /**
  166. * Method to test if we can create a folder
  167. */
  168. - (void)testCreateFolder
  169. {
  170. //We create a semaphore to wait until we recive the responses from Async calls
  171. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  172. NSString *folder = [NSString stringWithFormat:@"%@%@/%@",k_webdav_base_url,k_path_test_folder,[NSString stringWithFormat:@"%f", [NSDate timeIntervalSinceReferenceDate]]];
  173. [_sharedOCCommunication createFolder:folder onCommunication:_sharedOCCommunication withForbiddenCharactersSupported:NO successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  174. //Folder created
  175. NSLog(@"Folder created");
  176. dispatch_semaphore_signal(semaphore);
  177. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  178. XCTFail(@"Error testCreateFolder failureRequest");
  179. // Signal that block has completed
  180. dispatch_semaphore_signal(semaphore);
  181. } errorBeforeRequest:^(NSError *error) {
  182. XCTFail(@"Error testCreateFolder beforeRequest");
  183. // Signal that block has completed
  184. dispatch_semaphore_signal(semaphore);
  185. }];
  186. // Run loop
  187. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  188. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  189. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  190. }
  191. ///-----------------------------------
  192. /// @name testCreateFolderWithForbiddenCharacters
  193. ///-----------------------------------
  194. /**
  195. * Method to check if we check the forbidden characters when we try to create a folder
  196. *
  197. * @warning The special characters are: "\","<",">",":",""","|","?","*"
  198. */
  199. - (void)testCreateFolderWithForbiddenCharacters {
  200. NSArray* arrayForbiddenCharacters = [NSArray arrayWithObjects:@"\\",@"<",@">",@":",@"\"",@"|",@"?",@"*", nil];
  201. for (NSString *currentCharacter in arrayForbiddenCharacters) {
  202. NSString *folder = [NSString stringWithFormat:@"%@%@/%@",k_webdav_base_url,k_path_test_folder,[NSString stringWithFormat:@"%f%@-folder", [NSDate timeIntervalSinceReferenceDate], currentCharacter]];
  203. //We create a semaphore to wait until we recive the responses from Async calls
  204. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  205. [_sharedOCCommunication createFolder:folder onCommunication:_sharedOCCommunication withForbiddenCharactersSupported:NO successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  206. //Folder created
  207. NSLog(@"Folder created");
  208. XCTFail(@"Error testCreateFolderWithSpecialCharacters problem on: %@", currentCharacter);
  209. dispatch_semaphore_signal(semaphore);
  210. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  211. XCTFail(@"Error testCreateFolderWithSpecialCharacters problem on: %@", currentCharacter);
  212. // Signal that block has completed
  213. dispatch_semaphore_signal(semaphore);
  214. } errorBeforeRequest:^(NSError *error) {
  215. NSLog(@"Forbbiden character detected: %@", currentCharacter);
  216. // Signal that block has completed
  217. dispatch_semaphore_signal(semaphore);
  218. }];
  219. // Run loop
  220. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  221. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  222. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  223. }
  224. }
  225. ///-----------------------------------
  226. /// @name testMoveFileOnSameFolder
  227. ///-----------------------------------
  228. /**
  229. * Method to test move file on the same folder
  230. */
  231. - (void)testMoveFileOnSameFolder {
  232. //Create Folder A for the Test
  233. NSString *testPath = [NSString stringWithFormat:@"%@/Folder A", k_path_test_folder];
  234. [self createFolderWithName:testPath];
  235. //Upload file /Tests/Folder A/test.jpeg
  236. NSString *bundlePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"test" ofType:@"jpeg"];
  237. NSString *remotePath = [NSString stringWithFormat:@"%@/Folder A/Test.jpg", k_path_test_folder];
  238. [self uploadFilePath:bundlePath inRemotePath:remotePath];
  239. NSString *origin = [NSString stringWithFormat:@"%@%@/Folder A/Test.jpeg", k_webdav_base_url, k_path_test_folder];
  240. NSString *destiny = [NSString stringWithFormat:@"%@%@/Folder A/Test.jpeg", k_webdav_base_url, k_path_test_folder];
  241. //We create a semaphore to wait until we recive the responses from Async calls
  242. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  243. [_sharedOCCommunication moveFileOrFolder:origin toDestiny:destiny onCommunication:_sharedOCCommunication withForbiddenCharactersSupported:NO successRequest:^(NSURLResponse *response, NSString *redirectServer) {
  244. XCTFail(@"File Moved on the same folder");
  245. dispatch_semaphore_signal(semaphore);
  246. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  247. XCTFail(@"Error moving file on the same folder and Error");
  248. dispatch_semaphore_signal(semaphore);
  249. } errorBeforeRequest:^(NSError *error) {
  250. if (error.code == OCErrorMovingTheDestinyAndOriginAreTheSame) {
  251. NSLog(@"File on the same folder not moved");
  252. } else {
  253. XCTFail(@"Error moving file on same folder");
  254. }
  255. dispatch_semaphore_signal(semaphore);
  256. }];
  257. // Run loop
  258. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  259. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  260. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  261. }
  262. ///-----------------------------------
  263. /// @name testMoveFile
  264. ///-----------------------------------
  265. /**
  266. * Method to try move a file
  267. */
  268. - (void)testMoveFile {
  269. //Create Folder A for the Test
  270. NSString *testPathA = [NSString stringWithFormat:@"%@/Folder A", k_path_test_folder];
  271. [self createFolderWithName:testPathA];
  272. //Create Folder B for the Test
  273. NSString *testPathB = [NSString stringWithFormat:@"%@/Folder B", k_path_test_folder];
  274. [self createFolderWithName:testPathB];
  275. //Upload file /Tests/Folder A/test.jpeg
  276. NSString *bundlePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"test" ofType:@"jpeg"];
  277. NSString *uploadPath = [NSString stringWithFormat:@"%@/Folder A/Test.jpeg", k_path_test_folder];
  278. [self uploadFilePath:bundlePath inRemotePath:uploadPath];
  279. NSString *origin = [NSString stringWithFormat:@"%@%@/Folder A/Test.jpeg", k_webdav_base_url, k_path_test_folder];
  280. NSString *destiny = [NSString stringWithFormat:@"%@%@/Folder B/Test.jpeg", k_webdav_base_url, k_path_test_folder];
  281. //We create a semaphore to wait until we recive the responses from Async calls
  282. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  283. [_sharedOCCommunication moveFileOrFolder:origin toDestiny:destiny onCommunication:_sharedOCCommunication withForbiddenCharactersSupported:NO successRequest:^(NSURLResponse *response, NSString *redirectServer) {
  284. NSLog(@"File moved");
  285. dispatch_semaphore_signal(semaphore);
  286. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  287. XCTFail(@"Error moving file and Error");
  288. dispatch_semaphore_signal(semaphore);
  289. } errorBeforeRequest:^(NSError *error) {
  290. XCTFail(@"Error moving file");
  291. dispatch_semaphore_signal(semaphore);
  292. }];
  293. // Run loop
  294. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  295. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  296. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  297. }
  298. ///-----------------------------------
  299. /// @name testMoveFileForbiddenCharacters
  300. ///-----------------------------------
  301. /**
  302. * Method to try to move a file with destiny name have forbidden characters
  303. */
  304. - (void)testMoveFileForbiddenCharacters {
  305. //Create Folder A for the Test
  306. NSString *testPathA = [NSString stringWithFormat:@"%@/Folder A", k_path_test_folder];
  307. [self createFolderWithName:testPathA];
  308. //Create Folder C for the Test
  309. NSString *testPathC = [NSString stringWithFormat:@"%@/Folder C", k_path_test_folder];
  310. [self createFolderWithName:testPathC];
  311. //Upload file /Tests/Folder A/test.jpeg
  312. NSString *bundlePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"test" ofType:@"jpeg"];
  313. NSString *uploadPath = [NSString stringWithFormat:@"%@/Folder A/Test.jpeg", k_path_test_folder];
  314. [self uploadFilePath:bundlePath inRemotePath:uploadPath];
  315. NSArray *arrayForbiddenCharacters = [NSArray arrayWithObjects:@"\\",@"<",@">",@":",@"\"",@"|",@"?",@"*", nil];
  316. for (NSString *currentCharacter in arrayForbiddenCharacters) {
  317. NSString *origin = [NSString stringWithFormat:@"%@%@/Folder A/Test.jpeg", k_webdav_base_url, k_path_test_folder];
  318. NSString *destiny = [NSString stringWithFormat:@"%@%@/Folder C/Test%@.jpeg", k_webdav_base_url,k_path_test_folder, currentCharacter];
  319. //We create a semaphore to wait until we recive the responses from Async calls
  320. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  321. [_sharedOCCommunication moveFileOrFolder:origin toDestiny:destiny onCommunication:_sharedOCCommunication withForbiddenCharactersSupported:NO successRequest:^(NSURLResponse *response, NSString *redirectServer) {
  322. XCTFail(@"File Moved and renamed");
  323. dispatch_semaphore_signal(semaphore);
  324. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  325. XCTFail(@"Error moving file and renamed and Error");
  326. dispatch_semaphore_signal(semaphore);
  327. } errorBeforeRequest:^(NSError *error) {
  328. if (error.code == OCErrorMovingDestinyNameHaveForbiddenCharacters) {
  329. NSLog(@"File with forbidden characters not moved");
  330. } else {
  331. XCTFail(@"Error moving and renaming file");
  332. }
  333. dispatch_semaphore_signal(semaphore);
  334. }];
  335. // Run loop
  336. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  337. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  338. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  339. }
  340. }
  341. ///-----------------------------------
  342. /// @name testMoveFolderInsideHimself
  343. ///-----------------------------------
  344. /**
  345. * Method to try to move a folder inside himself
  346. */
  347. - (void)testMoveFolderInsideHimself {
  348. //Create Folder A for the Test
  349. NSString *testPathA = [NSString stringWithFormat:@"%@/Folder A", k_path_test_folder];
  350. [self createFolderWithName:testPathA];
  351. NSString *origin = [NSString stringWithFormat:@"%@%@/Folder A/", k_webdav_base_url, k_path_test_folder];
  352. NSString *destiny = [NSString stringWithFormat:@"%@%@/Folder A/Folder A/", k_webdav_base_url, k_path_test_folder];
  353. //We create a semaphore to wait until we recive the responses from Async calls
  354. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  355. [_sharedOCCommunication moveFileOrFolder:origin toDestiny:destiny onCommunication:_sharedOCCommunication withForbiddenCharactersSupported:NO successRequest:^(NSURLResponse *response, NSString *redirectServer) {
  356. XCTFail(@"Folder Moved inside himself");
  357. dispatch_semaphore_signal(semaphore);
  358. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  359. XCTFail(@"Error moving folder inside himself and Error");
  360. dispatch_semaphore_signal(semaphore);
  361. } errorBeforeRequest:^(NSError *error) {
  362. if (error.code == OCErrorMovingFolderInsideHimself) {
  363. NSLog(@"File renamed not moved");
  364. } else {
  365. XCTFail(@"Error moving folder inside himself");
  366. }
  367. dispatch_semaphore_signal(semaphore);
  368. }];
  369. // Run loop
  370. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  371. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  372. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  373. }
  374. ///-----------------------------------
  375. /// @name testMoveFolder
  376. ///-----------------------------------
  377. /**
  378. * Method to try to move a folder
  379. */
  380. - (void)testMoveFolder {
  381. //Create Folder A for the Test
  382. NSString *testPathA = [NSString stringWithFormat:@"%@/Folder A", k_path_test_folder];
  383. [self createFolderWithName:testPathA];
  384. //Create Folder C for the Test
  385. NSString *testPathB = [NSString stringWithFormat:@"%@/Folder B", k_path_test_folder];
  386. [self createFolderWithName:testPathB];
  387. NSString *origin = [NSString stringWithFormat:@"%@%@/Folder A/", k_webdav_base_url, k_path_test_folder];
  388. NSString *destiny = [NSString stringWithFormat:@"%@%@/Folder B/Folder A/", k_webdav_base_url, k_path_test_folder];
  389. //We create a semaphore to wait until we recive the responses from Async calls
  390. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  391. [_sharedOCCommunication moveFileOrFolder:origin toDestiny:destiny onCommunication:_sharedOCCommunication withForbiddenCharactersSupported:NO successRequest:^(NSURLResponse *response, NSString *redirectServer) {
  392. NSLog(@"Folder Moved");
  393. dispatch_semaphore_signal(semaphore);
  394. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  395. XCTFail(@"Error moving folder and Error");
  396. dispatch_semaphore_signal(semaphore);
  397. } errorBeforeRequest:^(NSError *error) {
  398. XCTFail(@"Error moving folder");
  399. dispatch_semaphore_signal(semaphore);
  400. }];
  401. // Run loop
  402. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  403. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  404. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  405. }
  406. ///-----------------------------------
  407. /// @name testRenameFileWithForbiddenCharacters
  408. ///-----------------------------------
  409. /**
  410. * Method try to rename a file with forbidden characters
  411. *
  412. */
  413. - (void)testRenameFileWithForbiddenCharacters {
  414. //Create Folder B for the Test
  415. NSString *testPathB = [NSString stringWithFormat:@"%@/Folder B", k_path_test_folder];
  416. [self createFolderWithName:testPathB];
  417. //Upload file /Tests/Folder B/test.jpeg
  418. NSString *bundlePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"test" ofType:@"jpeg"];
  419. NSString *uploadPath = [NSString stringWithFormat:@"%@/Folder B/Test.jpeg", k_path_test_folder];
  420. [self uploadFilePath:bundlePath inRemotePath:uploadPath];
  421. NSArray *arrayForbiddenCharacters = [NSArray arrayWithObjects:@"\\",@"<",@">",@":",@"\"",@"|",@"?",@"*", nil];
  422. for (NSString *currentCharacter in arrayForbiddenCharacters) {
  423. NSString *origin = [NSString stringWithFormat:@"%@%@/Folder B/Test.jpeg", k_webdav_base_url, k_path_test_folder];
  424. NSString *destiny = [NSString stringWithFormat:@"%@%@/Folder B/Test-%@.jpeg", k_webdav_base_url, k_path_test_folder, currentCharacter];
  425. //We create a semaphore to wait until we recive the responses from Async calls
  426. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  427. [_sharedOCCommunication moveFileOrFolder:origin toDestiny:destiny onCommunication:_sharedOCCommunication withForbiddenCharactersSupported:NO successRequest:^(NSURLResponse *response, NSString *redirectServer) {
  428. XCTFail(@"File renamed with forbidden characters");
  429. dispatch_semaphore_signal(semaphore);
  430. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  431. XCTFail(@"Error renaming file with forbidden characters and Error");
  432. dispatch_semaphore_signal(semaphore);
  433. } errorBeforeRequest:^(NSError *error) {
  434. if (error.code == OCErrorMovingDestinyNameHaveForbiddenCharacters) {
  435. NSLog(@"File not renamed with forbidden characters");
  436. dispatch_semaphore_signal(semaphore);
  437. } else {
  438. XCTFail(@"Error renaming file with forbidden characters");
  439. dispatch_semaphore_signal(semaphore);
  440. }
  441. }];
  442. // Run loop
  443. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  444. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  445. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  446. }
  447. }
  448. ///-----------------------------------
  449. /// @name testRenameFile
  450. ///-----------------------------------
  451. /**
  452. * Method try to rename a file
  453. *
  454. */
  455. - (void)testRenameFile {
  456. //Create Folder B for the Test
  457. NSString *testPathB = [NSString stringWithFormat:@"%@/Folder B", k_path_test_folder];
  458. [self createFolderWithName:testPathB];
  459. //Upload file /Tests/Folder B/test.jpeg
  460. NSString *bundlePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"test" ofType:@"jpeg"];
  461. NSString *uploadPath = [NSString stringWithFormat:@"%@/Folder B/Test.jpeg", k_path_test_folder];
  462. [self uploadFilePath:bundlePath inRemotePath:uploadPath];
  463. NSString *origin = [NSString stringWithFormat:@"%@%@/Folder B/Test.jpeg", k_webdav_base_url, k_path_test_folder];
  464. NSString *destiny = [NSString stringWithFormat:@"%@%@/Folder B/Test Renamed.jpeg", k_webdav_base_url, k_path_test_folder];
  465. //We create a semaphore to wait until we recive the responses from Async calls
  466. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  467. [_sharedOCCommunication moveFileOrFolder:origin toDestiny:destiny onCommunication:_sharedOCCommunication withForbiddenCharactersSupported:NO successRequest:^(NSURLResponse *response, NSString *redirectServer) {
  468. NSLog(@"File Renamed");
  469. dispatch_semaphore_signal(semaphore);
  470. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  471. XCTFail(@"Error renaming file and Error");
  472. dispatch_semaphore_signal(semaphore);
  473. } errorBeforeRequest:^(NSError *error) {
  474. XCTFail(@"Error renaming file");
  475. dispatch_semaphore_signal(semaphore);
  476. }];
  477. // Run loop
  478. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  479. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  480. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  481. }
  482. ///-----------------------------------
  483. /// @name testRenameFolderWithForbiddenCharacters
  484. ///-----------------------------------
  485. /**
  486. * Method try to rename a folder with forbidden characters
  487. *
  488. */
  489. - (void)testRenameFolderWithForbiddenCharacters {
  490. //Create Folder A for the Test
  491. NSString *testPathB = [NSString stringWithFormat:@"%@/Folder B", k_path_test_folder];
  492. [self createFolderWithName:testPathB];
  493. NSArray *arrayForbiddenCharacters = [NSArray arrayWithObjects:@"\\",@"<",@">",@":",@"\"",@"|",@"?",@"*", nil];
  494. for (NSString *currentCharacter in arrayForbiddenCharacters) {
  495. NSString *origin = [NSString stringWithFormat:@"%@%@/Folder B/", k_webdav_base_url, k_path_test_folder];
  496. NSString *destiny = [NSString stringWithFormat:@"%@%@/Folder B-%@/", k_webdav_base_url, k_path_test_folder, currentCharacter];
  497. //We create a semaphore to wait until we recive the responses from Async calls
  498. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  499. [_sharedOCCommunication moveFileOrFolder:origin toDestiny:destiny onCommunication:_sharedOCCommunication withForbiddenCharactersSupported:NO successRequest:^(NSURLResponse *response, NSString *redirectServer) {
  500. XCTFail(@"Folder renamed with forbidden characters");
  501. dispatch_semaphore_signal(semaphore);
  502. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  503. XCTFail(@"Error renaming folder with forbidden characters and Error");
  504. dispatch_semaphore_signal(semaphore);
  505. } errorBeforeRequest:^(NSError *error) {
  506. if (error.code == OCErrorMovingDestinyNameHaveForbiddenCharacters) {
  507. NSLog(@"Folder not renamed with forbidden characters");
  508. dispatch_semaphore_signal(semaphore);
  509. } else {
  510. XCTFail(@"Error renaming folder with forbidden characters");
  511. dispatch_semaphore_signal(semaphore);
  512. }
  513. }];
  514. // Run loop
  515. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  516. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  517. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  518. }
  519. }
  520. ///-----------------------------------
  521. /// @name testRenameFolder
  522. ///-----------------------------------
  523. /**
  524. * Method try to rename a folder
  525. *
  526. */
  527. - (void)testRenameFolder {
  528. //Create Folder A for the Test
  529. NSString *testPathB = [NSString stringWithFormat:@"%@/Folder B", k_path_test_folder];
  530. [self createFolderWithName:testPathB];
  531. NSString *origin = [NSString stringWithFormat:@"%@%@/Folder B/", k_webdav_base_url, k_path_test_folder];
  532. NSString *destiny = [NSString stringWithFormat:@"%@%@/Folder B Renamed/", k_webdav_base_url, k_path_test_folder];
  533. //We create a semaphore to wait until we recive the responses from Async calls
  534. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  535. [_sharedOCCommunication moveFileOrFolder:origin toDestiny:destiny onCommunication:_sharedOCCommunication withForbiddenCharactersSupported:NO successRequest:^(NSURLResponse *response, NSString *redirectServer) {
  536. NSLog(@"Folder Renamed");
  537. dispatch_semaphore_signal(semaphore);
  538. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  539. XCTFail(@"Error renaming folder and Error");
  540. dispatch_semaphore_signal(semaphore);
  541. } errorBeforeRequest:^(NSError *error) {
  542. XCTFail(@"Error renaming folder");
  543. dispatch_semaphore_signal(semaphore);
  544. }];
  545. // Run loop
  546. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  547. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  548. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  549. }
  550. ///-----------------------------------
  551. /// @name testToDeleteAFolder
  552. ///-----------------------------------
  553. /**
  554. * Method to test if we can create a folder
  555. */
  556. - (void)testDeleteAFolder
  557. {
  558. //Create Tests/DeleteFolder
  559. NSString *testPathDelete = [NSString stringWithFormat:@"%@/DeleteFolder", k_path_test_folder];
  560. [self createFolderWithName:testPathDelete];
  561. NSString *folder = [NSString stringWithFormat:@"%@%@/DeleteFolder", k_webdav_base_url, k_path_test_folder];
  562. //We create a semaphore to wait until we recive the responses from Async calls
  563. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  564. [_sharedOCCommunication deleteFileOrFolder:folder onCommunication:_sharedOCCommunication successRequest:^(NSURLResponse * response, NSString *redirectedServer) {
  565. //Folder deleted
  566. NSLog(@"Folder deleted");
  567. dispatch_semaphore_signal(semaphore);
  568. } failureRquest:^(NSURLResponse * response, NSError * error, NSString *redirectedServer) {
  569. //Error
  570. XCTFail(@"Error testDeleteFolder");
  571. // Signal that block has completed
  572. dispatch_semaphore_signal(semaphore);
  573. }];
  574. // Run loop
  575. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  576. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  577. beforeDate:[NSDate dateWithTimeIntervalSinceNow:10]];
  578. }
  579. ///-----------------------------------
  580. /// @name test to Delete a File
  581. ///-----------------------------------
  582. /**
  583. * Method to test if we can delete a folder
  584. */
  585. - (void)testDeleteFile
  586. {
  587. //Create Tests/DeleteFolder
  588. NSString *testPathDelete = [NSString stringWithFormat:@"%@/DeleteFolder", k_path_test_folder];
  589. [self createFolderWithName:testPathDelete];
  590. //Upload a file
  591. NSString *bundlePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"test" ofType:@"jpeg"];
  592. //Upload file Tests/Test Read Folder/File1
  593. NSString *uploadPath = [NSString stringWithFormat:@"%@/DeleteFolder/File1.jpeg", k_path_test_folder];
  594. [self uploadFilePath:bundlePath inRemotePath:uploadPath];
  595. NSString *filePath = [NSString stringWithFormat:@"%@%@/DeleteFolder/File1.jpeg", k_webdav_base_url, k_path_test_folder];
  596. //We create a semaphore to wait until we recive the responses from Async calls
  597. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  598. [_sharedOCCommunication deleteFileOrFolder:filePath onCommunication:_sharedOCCommunication successRequest:^(NSURLResponse * response, NSString *redirectedServer) {
  599. //File deleted
  600. NSLog(@"File deleted");
  601. dispatch_semaphore_signal(semaphore);
  602. } failureRquest:^(NSURLResponse * response, NSError * error, NSString *redirectedServer) {
  603. //Error
  604. XCTFail(@"Error test delete file");
  605. // Signal that block has completed
  606. dispatch_semaphore_signal(semaphore);
  607. }];
  608. // Run loop
  609. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  610. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  611. beforeDate:[NSDate dateWithTimeIntervalSinceNow:10]];
  612. }
  613. ///-----------------------------------
  614. /// @name Test Read Folder
  615. ///-----------------------------------
  616. /**
  617. * In this test we check many things:
  618. * 1.- The read folder method works, conected with server and get the answer
  619. * 2.- Check the parser checking a specific number of items in the selected path
  620. * 3.- Check the parser checking a specific number of files and folders
  621. *
  622. */
  623. - (void)testReadFolder{
  624. //Create Tests/Test Read Folder
  625. NSString *testPathReadFolder = [NSString stringWithFormat:@"%@/Test Read Folder", k_path_test_folder];
  626. [self createFolderWithName:testPathReadFolder];
  627. //Create Tests/Test Read Folder/Folder1
  628. NSString *testPathReadFolder1 = [NSString stringWithFormat:@"%@/Test Read Folder/Folder1", k_path_test_folder];
  629. [self createFolderWithName:testPathReadFolder1];
  630. //Create Tests/Test Read Folder/Folder2
  631. NSString *testPathReadFolder2 = [NSString stringWithFormat:@"%@/Test Read Folder/Folder2", k_path_test_folder];
  632. [self createFolderWithName:testPathReadFolder2];
  633. //Create Tests/Test Read Folder/Folder3
  634. NSString *testPathReadFolder3 = [NSString stringWithFormat:@"%@/Test Read Folder/Folder3", k_path_test_folder];
  635. [self createFolderWithName:testPathReadFolder3];
  636. NSString *bundlePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"test" ofType:@"jpeg"];
  637. //Upload file Tests/Test Read Folder/File1
  638. NSString *uploadPath1 = [NSString stringWithFormat:@"%@/Test Read Folder/File1.jpeg", k_path_test_folder];
  639. [self uploadFilePath:bundlePath inRemotePath:uploadPath1];
  640. //Upload file Tests/Test Read Folder/File2
  641. NSString *uploadPath2 = [NSString stringWithFormat:@"%@/Test Read Folder/File2.jpeg", k_path_test_folder];
  642. [self uploadFilePath:bundlePath inRemotePath:uploadPath2];
  643. //Upload file Tests/Test Read Folder/File3
  644. NSString *uploadPath3 = [NSString stringWithFormat:@"%@/Test Read Folder/File3.jpeg", k_path_test_folder];
  645. [self uploadFilePath:bundlePath inRemotePath:uploadPath3];
  646. //We create a semaphore to wait until we recive the responses from Async calls
  647. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  648. //Path with 7 elements: {3 files, 3 folders and the parent folder}
  649. NSString *path = [NSString stringWithFormat:@"%@%@/Test Read Folder/", k_webdav_base_url, k_path_test_folder];
  650. path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  651. [_sharedOCCommunication readFolder:path withUserSessionToken:nil onCommunication:_sharedOCCommunication successRequest:^(NSURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  652. //Counters
  653. NSInteger foldersCount = 0;
  654. NSInteger filesCount = 0;
  655. //Remove the parent folder item
  656. NSInteger realItemsCount = items.count - 1;
  657. //Constants
  658. const int k_items = 6;
  659. const int k_files = 3;
  660. const int k_folders = 3;
  661. //Loop the items
  662. for (OCFileDto *itemDto in items) {
  663. //Check parser
  664. NSLog(@"Item file name: %@", itemDto.fileName);
  665. NSLog(@"Item file path: %@", itemDto.filePath);
  666. //Not include the root folder
  667. if (itemDto.fileName !=nil) {
  668. //File or folder
  669. if (itemDto.isDirectory) {
  670. foldersCount++;
  671. } else {
  672. filesCount++;
  673. }
  674. }
  675. }
  676. if (realItemsCount == k_items) {
  677. //Check account of files and folders
  678. if (foldersCount == k_folders && filesCount == k_files) {
  679. NSLog(@"Read Folder Test OK");
  680. } else {
  681. XCTFail(@"Error reading a folder - There are: %ld folders and %ld files insead of %d folders and %d files", (long)foldersCount, (long)filesCount, k_folders, k_files);
  682. }
  683. } else {
  684. XCTFail(@"Error reading a folder - There are: %ld elements insead of 6 elements", (long)realItemsCount);
  685. }
  686. dispatch_semaphore_signal(semaphore);
  687. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  688. XCTFail(@"Error reading a folder - and Error");
  689. dispatch_semaphore_signal(semaphore);
  690. }];
  691. // Run loop
  692. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  693. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  694. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  695. }
  696. ///-----------------------------------
  697. /// @name Test Read File
  698. ///-----------------------------------
  699. /**
  700. * In this test we check the etag of a specific folder
  701. * we do changes in the folder in order to know the etag changed
  702. *
  703. */
  704. -(void)testReadFile{
  705. //Create Tests/Test Read File
  706. NSString *testReadFilePath = [NSString stringWithFormat:@"%@/Test Read File", k_path_test_folder];
  707. [self createFolderWithName:testReadFilePath];
  708. //1.- Get and Store the etag of a specific folder
  709. //2.- Create a new folder with a specific name
  710. //3.- Delete the folder created
  711. //4.- Get and Compare the etag of the same folder with the preview, if is different the TEST is OK
  712. //Block Store Attributes
  713. __block NSString *etag = @"";
  714. //Path of new folder
  715. NSString *newFolder = [NSString stringWithFormat:@"%@%@/Test Read File/DeletedFolder/", k_webdav_base_url, k_path_test_folder];
  716. newFolder = [newFolder stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  717. //We create a semaphore to wait until we recive the responses from Async calls
  718. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  719. //Path to the test
  720. NSString *path = [NSString stringWithFormat:@"%@%@/Test Read File/", k_webdav_base_url, k_path_test_folder];
  721. path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  722. [_sharedOCCommunication readFile:path onCommunication:_sharedOCCommunication successRequest:^(NSURLResponse *response, NSArray *items, NSString *redirectedServer) {
  723. for (OCFileDto *itemDto in items) {
  724. //Check parser
  725. NSLog(@"Item file path: %@", itemDto.filePath);
  726. NSLog(@"Item etag: %@", itemDto.etag);
  727. if (itemDto.etag) {
  728. etag = itemDto.etag;
  729. } else {
  730. XCTFail(@"Error getting the etag");
  731. }
  732. }
  733. dispatch_semaphore_signal(semaphore);
  734. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  735. XCTFail(@"Error reading the folder properties - and Error");
  736. dispatch_semaphore_signal(semaphore);
  737. }];
  738. // Run loop
  739. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  740. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  741. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  742. //Check if etag exists
  743. if (etag) {
  744. //Create Folder
  745. //We create a semaphore to wait until we recive the responses from Async calls
  746. semaphore = dispatch_semaphore_create(0);
  747. [_sharedOCCommunication createFolder:newFolder onCommunication:_sharedOCCommunication withForbiddenCharactersSupported:NO successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  748. //Folder created
  749. NSLog(@"Folder created");
  750. dispatch_semaphore_signal(semaphore);
  751. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  752. XCTFail(@"Error testCreateFolder");
  753. // Signal that block has completed
  754. dispatch_semaphore_signal(semaphore);
  755. } errorBeforeRequest:^(NSError *error) {
  756. XCTFail(@"Error testCreateFolder");
  757. // Signal that block has completed
  758. dispatch_semaphore_signal(semaphore);
  759. }];
  760. // Run loop
  761. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  762. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  763. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  764. //Delete Folder
  765. //We create a semaphore to wait until we recive the responses from Async calls
  766. semaphore = dispatch_semaphore_create(0);
  767. [_sharedOCCommunication deleteFileOrFolder:newFolder onCommunication:_sharedOCCommunication successRequest:^(NSURLResponse * response, NSString *redirectedServer) {
  768. //Folder deleted
  769. NSLog(@"Folder deleted");
  770. dispatch_semaphore_signal(semaphore);
  771. } failureRquest:^(NSURLResponse * response, NSError * error, NSString *redirectedServer) {
  772. //Error
  773. XCTFail(@"Error testDeleteFolder");
  774. // Signal that block has completed
  775. dispatch_semaphore_signal(semaphore);
  776. }];
  777. // Run loop
  778. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  779. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  780. beforeDate:[NSDate dateWithTimeIntervalSinceNow:10]];
  781. //Get the folder etag again
  782. //We create a semaphore to wait until we recive the responses from Async calls
  783. semaphore = dispatch_semaphore_create(0);
  784. [_sharedOCCommunication readFile:path onCommunication:_sharedOCCommunication successRequest:^(NSURLResponse *response, NSArray *items, NSString *redirectedServer) {
  785. for (OCFileDto *itemDto in items) {
  786. //Check parser
  787. NSLog(@"Item file path: %@", itemDto.filePath);
  788. NSLog(@"Item etag: %@", itemDto.etag);
  789. if (itemDto.etag) {
  790. if ([etag isEqual:itemDto.etag]) {
  791. XCTFail(@"The same etag after the changes");
  792. }else{
  793. NSLog(@"Test OK");
  794. }
  795. } else {
  796. XCTFail(@"Error getting the etag");
  797. }
  798. }
  799. dispatch_semaphore_signal(semaphore);
  800. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  801. XCTFail(@"Error reading the folder properties - and Error");
  802. dispatch_semaphore_signal(semaphore);
  803. }];
  804. // Run loop
  805. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  806. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  807. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  808. }
  809. }
  810. ///-----------------------------------
  811. /// @name Test Download File
  812. ///-----------------------------------
  813. /**
  814. * This test try to download a specific file
  815. * It the file download the test is ok
  816. *
  817. */
  818. - (void) testDownloadFile {
  819. //Create Tests/Test Download Folder
  820. NSString *downloadPath = [NSString stringWithFormat:@"%@/Test Download", k_path_test_folder];
  821. [self createFolderWithName:downloadPath];
  822. //Upload test file
  823. NSString *bundlePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"test" ofType:@"jpeg"];
  824. //Upload file /Tests/Test Download/test.jpeg
  825. NSString *uploadPath = [NSString stringWithFormat:@"%@/Test Download/Test.jpeg", k_path_test_folder];
  826. [self uploadFilePath:bundlePath inRemotePath:uploadPath];
  827. //We create a semaphore to wait until we recive the responses from Async calls
  828. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  829. //Create Folder in File Sytem to test
  830. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  831. NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
  832. //Documents/Test Download/
  833. NSString *localPath = documentsDirectory;
  834. //Make the path if not exists
  835. NSError *error = nil;
  836. if (![[NSFileManager defaultManager] fileExistsAtPath:localPath])
  837. [[NSFileManager defaultManager] createDirectoryAtPath:localPath withIntermediateDirectories:NO attributes:nil error:&error];
  838. //Documents/Test Download/image.png
  839. localPath = [localPath stringByAppendingString:@"/image.jpeg"];
  840. //Path of server file file
  841. NSString *serverUrl = [NSString stringWithFormat:@"%@%@/Test Download/Test.jpeg", k_webdav_base_url, k_path_test_folder];
  842. serverUrl = [serverUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  843. NSURLSessionTask *downloadTask = nil;
  844. downloadTask = [_sharedOCCommunication downloadFileSession:serverUrl toDestiny:localPath defaultPriority:YES onCommunication:_sharedOCCommunication progress:^(NSProgress *progress) {
  845. NSLog(@"File: %lld bytes", progress.completedUnitCount);
  846. } successRequest:^(NSURLResponse *response, NSURL *filePath) {
  847. NSLog(@"File Downloaded ok");
  848. //Delete the file
  849. NSError *theError = nil;
  850. [[NSFileManager defaultManager] removeItemAtPath:localPath error:&theError];
  851. dispatch_semaphore_signal(semaphore);
  852. } failureRequest:^(NSURLResponse *response, NSError *error) {
  853. XCTFail(@"Error download a file - Response - Error");
  854. //Delete the file
  855. NSError *theError = nil;
  856. [[NSFileManager defaultManager] removeItemAtPath:localPath error:&theError];
  857. dispatch_semaphore_signal(semaphore);
  858. }];
  859. // Run loop
  860. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  861. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  862. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  863. }
  864. ///-----------------------------------
  865. /// @name Test download not existing file
  866. ///-----------------------------------
  867. /**
  868. * This test try to download a unexisting file
  869. * The test works fine if the file is not download
  870. *
  871. */
  872. - (void) testDownloadNotExistingFile {
  873. //Create Tests/Test Download
  874. NSString *downloadPath = [NSString stringWithFormat:@"%@/Test Download", k_path_test_folder];
  875. [self createFolderWithName:downloadPath];
  876. //We create a semaphore to wait until we recive the responses from Async calls
  877. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  878. //Create Folder in File Sytem to test
  879. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  880. NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
  881. //Documents/Test Download/
  882. NSString *localPath = [documentsDirectory stringByAppendingPathComponent:@"Test Download"];
  883. //Make the path if not exists
  884. NSError *error = nil;
  885. if (![[NSFileManager defaultManager] fileExistsAtPath:documentsDirectory])
  886. [[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory withIntermediateDirectories:NO attributes:nil error:&error];
  887. //Documents/Test Download/image.png
  888. localPath = [localPath stringByAppendingString:@"/image.png"];
  889. //Path of server file that not exist
  890. NSString *serverUrl = [NSString stringWithFormat:@"%@%@/Test Download/test image not exist.PNG", k_webdav_base_url, k_path_test_folder];
  891. serverUrl = [serverUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  892. NSURLSessionDownloadTask *downloadTask = nil;
  893. downloadTask = [_sharedOCCommunication downloadFileSession:serverUrl toDestiny:localPath defaultPriority:YES onCommunication:_sharedOCCommunication progress:^(NSProgress *progress) {
  894. NSLog(@"File: %lld bytes", progress.completedUnitCount);
  895. } successRequest:^(NSURLResponse *response, NSURL *filePath) {
  896. XCTFail(@"Download file ok, not possible");
  897. //Delete the file
  898. NSError *theError = nil;
  899. [[NSFileManager defaultManager] removeItemAtPath:localPath error:&theError];
  900. dispatch_semaphore_signal(semaphore);
  901. } failureRequest:^(NSURLResponse *response, NSError *error) {
  902. //Delete the file
  903. NSError *theError = nil;
  904. [[NSFileManager defaultManager] removeItemAtPath:localPath error:&theError];
  905. dispatch_semaphore_signal(semaphore);
  906. }];
  907. // Run loop
  908. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  909. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  910. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  911. }
  912. ///-----------------------------------
  913. /// @name Test Download File With Session
  914. ///-----------------------------------
  915. /**
  916. * This test try to download a specific file using NSURLSession
  917. * If the file is downloaded the test is ok
  918. *
  919. */
  920. - (void) testDownloadFileWithSession {
  921. //Create Tests/Test Download Folder
  922. NSString *downloadPath = [NSString stringWithFormat:@"%@/Test Download", k_path_test_folder];
  923. [self createFolderWithName:downloadPath];
  924. //Upload test file
  925. NSString *bundlePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"test" ofType:@"jpeg"];
  926. //Upload file /Tests/Test Download/test.jpeg
  927. NSString *uploadPath = [NSString stringWithFormat:@"%@/Test Download/Test.jpeg", k_path_test_folder];
  928. [self uploadFilePath:bundlePath inRemotePath:uploadPath];
  929. //We create a semaphore to wait until we recive the responses from Async calls
  930. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  931. //Create Folder in File Sytem to test
  932. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  933. NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
  934. //Documents/Test Download/
  935. NSString *localPath = documentsDirectory;
  936. //Make the path if not exists
  937. NSError *error = nil;
  938. if (![[NSFileManager defaultManager] fileExistsAtPath:localPath])
  939. [[NSFileManager defaultManager] createDirectoryAtPath:localPath withIntermediateDirectories:NO attributes:nil error:&error];
  940. //Documents/Test Download/image.png
  941. localPath = [localPath stringByAppendingString:@"/image.jpeg"];
  942. //Path of server file file
  943. NSString *serverUrl = [NSString stringWithFormat:@"%@%@/Test Download/Test.jpeg", k_webdav_base_url, k_path_test_folder];
  944. serverUrl = [serverUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  945. NSURLSessionDownloadTask *downloadTask = nil;
  946. downloadTask = [_sharedOCCommunication downloadFileSession:serverUrl toDestiny:localPath defaultPriority:YES onCommunication:_sharedOCCommunication progress:^(NSProgress *progress) {
  947. NSLog(@"File: %lld bytes", progress.completedUnitCount);
  948. } successRequest:^(NSURLResponse *response, NSURL *filePath) {
  949. NSLog(@"File Downloaded ok");
  950. //Delete the file
  951. NSError *theError = nil;
  952. [[NSFileManager defaultManager] removeItemAtPath:localPath error:&theError];
  953. dispatch_semaphore_signal(semaphore);
  954. } failureRequest:^(NSURLResponse *response, NSError *error) {
  955. XCTFail(@"Error download a file - Error");
  956. //Delete the file
  957. NSError *theError = nil;
  958. [[NSFileManager defaultManager] removeItemAtPath:localPath error:&theError];
  959. dispatch_semaphore_signal(semaphore);
  960. }];
  961. // Run loop
  962. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  963. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  964. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  965. }
  966. ///-----------------------------------
  967. /// @name Test Download With Session A File that does not exist
  968. ///-----------------------------------
  969. /**
  970. * This test try to download a file that does not exist using NSURLSession
  971. * If the file is not downloaded, the test is ok
  972. *
  973. */
  974. - (void) testDownloadWithSessionAFileThatDoesNotExist {
  975. //Create Tests/Test Download Folder
  976. NSString *downloadPath = [NSString stringWithFormat:@"%@/Test Download", k_path_test_folder];
  977. [self createFolderWithName:downloadPath];
  978. //We create a semaphore to wait until we recive the responses from Async calls
  979. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  980. //Create Folder in File Sytem to test
  981. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  982. NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
  983. //Documents/Test Download/
  984. NSString *localPath = documentsDirectory;
  985. //Make the path if not exists
  986. NSError *error = nil;
  987. if (![[NSFileManager defaultManager] fileExistsAtPath:localPath])
  988. [[NSFileManager defaultManager] createDirectoryAtPath:localPath withIntermediateDirectories:NO attributes:nil error:&error];
  989. //Documents/Test Download/image.png
  990. localPath = [localPath stringByAppendingString:@"/image.jpeg"];
  991. //Path of server file file
  992. NSString *serverUrl = [NSString stringWithFormat:@"%@%@/Test Download/Test.jpeg", k_webdav_base_url, k_path_test_folder];
  993. serverUrl = [serverUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  994. NSURLSessionDownloadTask *downloadTask = nil;
  995. downloadTask = [_sharedOCCommunication downloadFileSession:serverUrl toDestiny:localPath defaultPriority:YES onCommunication:_sharedOCCommunication progress:^(NSProgress *progress) {
  996. NSLog(@"File: %lld bytes", progress.completedUnitCount);
  997. } successRequest:^(NSURLResponse *response, NSURL *filePath) {
  998. XCTFail(@"Download file ok, not possible");
  999. //Delete the file
  1000. NSError *theError = nil;
  1001. [[NSFileManager defaultManager] removeItemAtPath:localPath error:&theError];
  1002. dispatch_semaphore_signal(semaphore);
  1003. } failureRequest:^(NSURLResponse *response, NSError *error) {
  1004. NSLog(@"Error downloading a file - Error");
  1005. //Delete the file
  1006. NSError *theError = nil;
  1007. [[NSFileManager defaultManager] removeItemAtPath:localPath error:&theError];
  1008. dispatch_semaphore_signal(semaphore);
  1009. }];
  1010. // Run loop
  1011. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  1012. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  1013. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  1014. }
  1015. ///-----------------------------------
  1016. /// @name Test to upload a file that does not exist
  1017. ///-----------------------------------
  1018. /**
  1019. * This test try to upload that does not exist on the filesystem
  1020. * This test is passed if we detect that the file does not exist
  1021. *
  1022. */
  1023. - (void) testUploadAFileThatDoesNotExist {
  1024. //Create Tests/Test Upload
  1025. NSString *uploadPath = [NSString stringWithFormat:@"%@/Test Upload", k_path_test_folder];
  1026. [self createFolderWithName:uploadPath];
  1027. //We create a semaphore to wait until we recive the responses from Async calls
  1028. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  1029. //Create Folder in File Sytem to test
  1030. NSString *localPath = [NSString stringWithFormat:@"%@/Name of the file that does not exist.png", [[NSBundle mainBundle] resourcePath]];
  1031. //Path of server file file
  1032. NSString *serverUrl = [NSString stringWithFormat:@"%@%@/Test Upload/Name of the file that does not exist.png", k_webdav_base_url, k_path_test_folder];
  1033. serverUrl = [serverUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  1034. NSURLSessionUploadTask *uploadTask = nil;
  1035. uploadTask = [_sharedOCCommunication uploadFileSession:localPath toDestiny:serverUrl onCommunication:_sharedOCCommunication progress:^(NSProgress *progress) {
  1036. NSLog(@"File: %lld bytes", progress.completedUnitCount);
  1037. } successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  1038. XCTFail(@"Error We upload a file that does not exist");
  1039. dispatch_semaphore_signal(semaphore);
  1040. } failureRequest:^(NSURLResponse *response, NSString *redirectedServer, NSError *error) {
  1041. XCTFail(@"Error. File do not uploaded");
  1042. dispatch_semaphore_signal(semaphore);
  1043. } failureBeforeRequest:^(NSError *error) {
  1044. NSLog(@"File that do not exist does not upload");
  1045. dispatch_semaphore_signal(semaphore);
  1046. }];
  1047. // Run loop
  1048. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  1049. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  1050. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  1051. }
  1052. ///-----------------------------------
  1053. /// @name Test to upload a file with Forbbiden Characters
  1054. ///-----------------------------------
  1055. /**
  1056. * This test try to uplad with special characters in destiny name
  1057. */
  1058. - (void) testUploadAFileWithSpecialCharacters {
  1059. //Create Tests/Test Upload
  1060. NSString *uploadPath = [NSString stringWithFormat:@"%@/Test Upload", k_path_test_folder];
  1061. [self createFolderWithName:uploadPath];
  1062. //We create a semaphore to wait until we recive the responses from Async calls
  1063. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  1064. //Upload test file
  1065. NSString *localPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"video" ofType:@"MOV"];
  1066. //Path of server file file (Special character added in file name)
  1067. NSString *serverUrl = [NSString stringWithFormat:@"%@%@/Test Upload/video@.mov", k_webdav_base_url, k_path_test_folder];
  1068. serverUrl = [serverUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  1069. NSURLSessionUploadTask *uploadTask = nil;
  1070. uploadTask = [_sharedOCCommunication uploadFileSession:localPath toDestiny:serverUrl onCommunication:_sharedOCCommunication progress:^(NSProgress *progress) {
  1071. NSLog(@"File: %lld bytes", progress.completedUnitCount);
  1072. } successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  1073. NSLog(@"File Uploaded with Special Characters");
  1074. dispatch_semaphore_signal(semaphore);
  1075. } failureRequest:^(NSURLResponse *response, NSString *redirectedServer, NSError *error) {
  1076. XCTFail(@"Error. File do not uploaded");
  1077. dispatch_semaphore_signal(semaphore);
  1078. } failureBeforeRequest:^(NSError *error) {
  1079. XCTFail(@"Error File does not exist");
  1080. dispatch_semaphore_signal(semaphore);
  1081. }];
  1082. // Run loop
  1083. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  1084. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  1085. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  1086. }
  1087. ///-----------------------------------
  1088. /// @name Test to upload with session
  1089. ///-----------------------------------
  1090. /**
  1091. * This test try to uplad a file using NSURLSession
  1092. *
  1093. */
  1094. - (void) testUploadFileWithSession {
  1095. //Create Tests/Test Upload
  1096. NSString *uploadPath = [NSString stringWithFormat:@"%@/Test Upload", k_path_test_folder];
  1097. [self createFolderWithName:uploadPath];
  1098. //We create a semaphore to wait until we recive the responses from Async calls
  1099. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  1100. //Upload test file
  1101. NSString *localPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"test" ofType:@"jpeg"];
  1102. //Path of server file file
  1103. NSString *serverUrl = [NSString stringWithFormat:@"%@%@/Test Upload/CompanyLogo.png", k_webdav_base_url, k_path_test_folder];
  1104. serverUrl = [serverUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  1105. NSURLSessionUploadTask *uploadTask = nil;
  1106. uploadTask = [_sharedOCCommunication uploadFileSession:localPath toDestiny:serverUrl onCommunication:_sharedOCCommunication progress:^(NSProgress *progress) {
  1107. NSLog(@"File: %lld bytes", progress.completedUnitCount);
  1108. } successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  1109. NSLog(@"File Uploaded");
  1110. dispatch_semaphore_signal(semaphore);
  1111. } failureRequest:^(NSURLResponse *response, NSString *redirectedServer, NSError *error) {
  1112. XCTFail(@"Error. File do not uploaded");
  1113. dispatch_semaphore_signal(semaphore);
  1114. } failureBeforeRequest:^(NSError *error) {
  1115. NSLog(@"File that do not exist does not upload");
  1116. dispatch_semaphore_signal(semaphore);
  1117. }];
  1118. // Run loop
  1119. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  1120. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  1121. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  1122. }
  1123. //-----------------------------------
  1124. /// @name Test to upload with session and special characters
  1125. ///-----------------------------------
  1126. /**
  1127. * This test try to uplad a file using NSURLSession
  1128. *
  1129. */
  1130. - (void) testUploadFileWithSessionAndSpecialCharacters {
  1131. //Create Tests/Test Upload
  1132. NSString *uploadPath = [NSString stringWithFormat:@"%@/Test Upload", k_path_test_folder];
  1133. [self createFolderWithName:uploadPath];
  1134. //We create a semaphore to wait until we recive the responses from Async calls
  1135. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  1136. //Upload test file
  1137. NSString *localPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"video" ofType:@"MOV"];
  1138. //Path of server file file (Special character added in file name)
  1139. NSString *serverUrl = [NSString stringWithFormat:@"%@%@/Test Upload/video@.mov", k_webdav_base_url, k_path_test_folder];
  1140. serverUrl = [serverUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  1141. NSURLSessionUploadTask *uploadTask = nil;
  1142. uploadTask = [_sharedOCCommunication uploadFileSession:localPath toDestiny:serverUrl onCommunication:_sharedOCCommunication progress:^(NSProgress *progress) {
  1143. NSLog(@"File: %lld bytes", progress.completedUnitCount);
  1144. } successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  1145. NSLog(@"File Uploaded");
  1146. dispatch_semaphore_signal(semaphore);
  1147. } failureRequest:^(NSURLResponse *response, NSString *redirectedServer, NSError *error) {
  1148. XCTFail(@"Error. File do not uploaded");
  1149. dispatch_semaphore_signal(semaphore);
  1150. } failureBeforeRequest:^(NSError *error) {
  1151. NSLog(@"File that do not exist does not upload");
  1152. dispatch_semaphore_signal(semaphore);
  1153. }];
  1154. // Run loop
  1155. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  1156. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  1157. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  1158. }
  1159. ///-----------------------------------
  1160. /// @name Test to upload with session file that does not exists
  1161. ///-----------------------------------
  1162. /**
  1163. * This test try to uplad a file using NSURLSession
  1164. *
  1165. */
  1166. - (void) testUploadWithSessionAFileThatDoesNotExist {
  1167. //Create Tests/Test Upload
  1168. NSString *uploadPath = [NSString stringWithFormat:@"%@/Test Upload", k_path_test_folder];
  1169. [self createFolderWithName:uploadPath];
  1170. //We create a semaphore to wait until we recive the responses from Async calls
  1171. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  1172. //Upload test file
  1173. NSString *localPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"videoA" ofType:@"MOV"];
  1174. //Path of server file file (Special character added in file name)
  1175. NSString *serverUrl = [NSString stringWithFormat:@"%@%@/Test Upload/video@.mov", k_webdav_base_url, k_path_test_folder];
  1176. serverUrl = [serverUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  1177. NSURLSessionUploadTask *uploadTask = nil;
  1178. uploadTask = [_sharedOCCommunication uploadFileSession:localPath toDestiny:serverUrl onCommunication:_sharedOCCommunication progress:^(NSProgress *progress) {
  1179. NSLog(@"File: %lld bytes", progress.completedUnitCount);
  1180. } successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  1181. XCTFail(@"Error We upload a file that does not exist");
  1182. dispatch_semaphore_signal(semaphore);
  1183. } failureRequest:^(NSURLResponse *response, NSString *redirectedServer, NSError *error) {
  1184. NSLog(@"File that do not exist does not upload");
  1185. dispatch_semaphore_signal(semaphore);
  1186. } failureBeforeRequest:^(NSError *error) {
  1187. NSLog(@"File that do not exist does not upload");
  1188. dispatch_semaphore_signal(semaphore);
  1189. }];
  1190. // Run loop
  1191. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  1192. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  1193. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  1194. }
  1195. ///-----------------------------------
  1196. /// @name Test the share a folder
  1197. ///-----------------------------------
  1198. /**
  1199. * This test try to share a folder
  1200. */
  1201. - (void) testShareAFolder {
  1202. //We create a semaphore to wait until we recive the responses from Async calls
  1203. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  1204. [_sharedOCCommunication shareFileOrFolderByServer:k_base_url andFileOrFolderPath:[NSString stringWithFormat:@"/%@", k_path_test_folder] onCommunication:_sharedOCCommunication successRequest:^(NSURLResponse *response, NSString *listOfShared, NSString *redirectedServer) {
  1205. NSLog(@"Folder shared");
  1206. dispatch_semaphore_signal(semaphore);
  1207. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  1208. XCTFail(@"Error sharing folder");
  1209. dispatch_semaphore_signal(semaphore);
  1210. }];
  1211. // Run loop
  1212. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  1213. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  1214. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  1215. }
  1216. ///-----------------------------------
  1217. /// @name Test read shares items
  1218. ///-----------------------------------
  1219. /**
  1220. * This test try to check if a shared folder is shared and obtain his information
  1221. */
  1222. - (void) testReadShared {
  1223. //1. create the folder and share it
  1224. [self testShareAFolder];
  1225. //We create a semaphore to wait until we recive the responses from Async calls
  1226. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  1227. //2. Check if the folder is shared
  1228. [_sharedOCCommunication readSharedByServer:k_base_url onCommunication: _sharedOCCommunication successRequest:^(NSURLResponse *response, NSArray *listOfShared, NSString *redirectedServer) {
  1229. BOOL isFolderShared = NO;
  1230. for (OCSharedDto *current in listOfShared) {
  1231. if ([current.path isEqualToString:[NSString stringWithFormat:@"/%@/", k_path_test_folder]]) {
  1232. isFolderShared = YES;
  1233. }
  1234. }
  1235. if (!isFolderShared) {
  1236. XCTFail(@"Folder not shared");
  1237. dispatch_semaphore_signal(semaphore);
  1238. }
  1239. dispatch_semaphore_signal(semaphore);
  1240. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  1241. XCTFail(@"Error reading shares");
  1242. dispatch_semaphore_signal(semaphore);
  1243. }];
  1244. // Run loop
  1245. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  1246. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  1247. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  1248. }
  1249. ///-----------------------------------
  1250. /// @name Test unshare items
  1251. ///-----------------------------------
  1252. /**
  1253. * This test try unshare a item
  1254. */
  1255. - (void) testUnShareAFolder {
  1256. //1. create the folder and share it
  1257. [self testShareAFolder];
  1258. //We create a semaphore to wait until we recive the responses from Async calls
  1259. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  1260. //2. read the folder to obtain the info of OCSharedDto
  1261. [_sharedOCCommunication readSharedByServer:k_base_url onCommunication: _sharedOCCommunication successRequest:^(NSURLResponse *response, NSArray *listOfShared, NSString *redirectedServer) {
  1262. OCSharedDto *shared;
  1263. for (OCSharedDto *current in listOfShared) {
  1264. if ([current.path isEqualToString:[NSString stringWithFormat:@"/%@/", k_path_test_folder]]) {
  1265. shared = current;
  1266. }
  1267. }
  1268. if (shared) {
  1269. //3. Unshare the folder
  1270. [_sharedOCCommunication unShareFileOrFolderByServer:k_base_url andIdRemoteShared:shared.idRemoteShared onCommunication:_sharedOCCommunication successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  1271. NSLog(@"File unshared");
  1272. dispatch_semaphore_signal(semaphore);
  1273. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  1274. XCTFail(@"Error unsharing folder");
  1275. dispatch_semaphore_signal(semaphore);
  1276. }];
  1277. } else {
  1278. XCTFail(@"Folder not shared on testUnShareAFolder");
  1279. dispatch_semaphore_signal(semaphore);
  1280. }
  1281. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  1282. XCTFail(@"Error reading shares on testUnShareAFolder");
  1283. dispatch_semaphore_signal(semaphore);
  1284. }];
  1285. // Run loop
  1286. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  1287. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  1288. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  1289. }
  1290. ///-----------------------------------
  1291. /// @name Test read capabilities
  1292. ///-----------------------------------
  1293. /**
  1294. * This test try to check if a shared folder is shared and obtain his information
  1295. */
  1296. - (void) testGetCapabilitiesOfServer {
  1297. //We create a semaphore to wait until we recive the responses from Async calls
  1298. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  1299. [_sharedOCCommunication getCapabilitiesOfServer:k_base_url onCommunication:_sharedOCCommunication successRequest:^(NSURLResponse *response, OCCapabilities *capabilities, NSString *redirectedServer) {
  1300. NSLog(@"Get capabilities ok");
  1301. XCTAssertNotNil(capabilities, @"Error get capabilites of server");
  1302. dispatch_semaphore_signal(semaphore);
  1303. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  1304. XCTFail(@"Error get capabilites of server");
  1305. dispatch_semaphore_signal(semaphore);
  1306. }];
  1307. // Run loop
  1308. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  1309. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  1310. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  1311. }
  1312. ///-----------------------------------
  1313. /// @name Test read capabilities
  1314. ///-----------------------------------
  1315. /**
  1316. * This test check get capabilities
  1317. */
  1318. - (void) testShareLinkWithPassword {
  1319. //We create a semaphore to wait until we recive the responses from Async calls
  1320. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  1321. [_sharedOCCommunication shareFileOrFolderByServer:k_base_url andFileOrFolderPath:[NSString stringWithFormat:@"/%@", k_path_test_folder] andPassword:@"testing" onCommunication:_sharedOCCommunication successRequest:^(NSURLResponse *response, NSString *shareLink, NSString *redirectedServer) {
  1322. NSLog(@"Folder shared by link with password");
  1323. dispatch_semaphore_signal(semaphore);
  1324. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  1325. XCTFail(@"Error sharing folder by link with password");
  1326. dispatch_semaphore_signal(semaphore);
  1327. }];
  1328. // Run loop
  1329. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  1330. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  1331. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  1332. }
  1333. ///-----------------------------------
  1334. /// @name Test share link with expiration date and password
  1335. ///-----------------------------------
  1336. /**
  1337. * This test check the creation of a link with expiration date
  1338. */
  1339. - (void) testShareLinkWithExpirationDateAndPassword {
  1340. //We create a semaphore to wait until we recive the responses from Async calls
  1341. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  1342. //1. Share a folder
  1343. [self testShareAFolder];
  1344. //2. Read the shared
  1345. [_sharedOCCommunication readSharedByServer:k_base_url onCommunication: _sharedOCCommunication successRequest:^(NSURLResponse *response, NSArray *listOfShared, NSString *redirectedServer) {
  1346. BOOL isFolderShared = NO;
  1347. for (OCSharedDto *current in listOfShared) {
  1348. if ([current.path isEqualToString:[NSString stringWithFormat:@"/%@/", k_path_test_folder]]) {
  1349. isFolderShared = YES;
  1350. NSDateFormatter *dateFormatter = [NSDateFormatter new];
  1351. [dateFormatter setDateFormat:@"YYYY-MM-dd"];
  1352. NSDate *tomorrow = [NSDate dateWithTimeInterval:(24*60*60) sinceDate:[NSDate date]];
  1353. NSString *foarmatedDate = [dateFormatter stringFromDate:tomorrow];
  1354. //3. Update the share with password and expiration date
  1355. [_sharedOCCommunication updateShare:current.idRemoteShared ofServerPath:k_base_url withPasswordProtect:@"testing" andExpirationTime:foarmatedDate andPermissions:k_read_share_permission onCommunication:_sharedOCCommunication successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  1356. NSLog(@"Updated shared by link with expiration date and password");
  1357. dispatch_semaphore_signal(semaphore);
  1358. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  1359. XCTFail(@"Error updating shared by link with expiration date and password");
  1360. dispatch_semaphore_signal(semaphore);
  1361. }];
  1362. }
  1363. }
  1364. if (!isFolderShared) {
  1365. XCTFail(@"Folder not shared");
  1366. dispatch_semaphore_signal(semaphore);
  1367. }
  1368. dispatch_semaphore_signal(semaphore);
  1369. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  1370. XCTFail(@"Error reading shares");
  1371. dispatch_semaphore_signal(semaphore);
  1372. }];
  1373. // Run loop
  1374. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  1375. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  1376. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  1377. }
  1378. ///-----------------------------------
  1379. /// @name Tests search users and groups
  1380. ///-----------------------------------
  1381. /**
  1382. * This test search for first 30 users or groups on server that match the pattern "aa"
  1383. */
  1384. - (void) testSearchUsersAndGroups {
  1385. //We create a semaphore to wait until we recive the responses from Async calls
  1386. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  1387. [_sharedOCCommunication searchUsersAndGroupsWith:@"aa" forPage:1 with:30 ofServer:k_base_url onCommunication:_sharedOCCommunication successRequest:^(NSURLResponse *response, NSArray *itemList, NSString *redirectedServer) {
  1388. NSLog(@"Search users and groups");
  1389. XCTAssertNotNil(itemList, @"Error search users and groups");
  1390. dispatch_semaphore_signal(semaphore);
  1391. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  1392. XCTFail(@"Error get capabilites of server");
  1393. dispatch_semaphore_signal(semaphore);
  1394. }];
  1395. // Run loop
  1396. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  1397. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  1398. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  1399. }
  1400. /**
  1401. * This test search for first 30 users or groups with special characters on server that match the pattern "user@"
  1402. */
  1403. - (void) testSearchUsersAndGroupsWithSpecialCharacters {
  1404. //We create a semaphore to wait until we recive the responses from Async calls
  1405. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  1406. [_sharedOCCommunication searchUsersAndGroupsWith:@"user@" forPage:1 with:30 ofServer:k_base_url onCommunication:_sharedOCCommunication successRequest:^(NSURLResponse *response, NSArray *itemList, NSString *redirectedServer) {
  1407. NSLog(@"Search users and groups");
  1408. XCTAssertNotNil(itemList, @"Error search users and groups");
  1409. dispatch_semaphore_signal(semaphore);
  1410. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  1411. XCTFail(@"Error get capabilites of server");
  1412. dispatch_semaphore_signal(semaphore);
  1413. }];
  1414. // Run loop
  1415. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  1416. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  1417. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  1418. }
  1419. ///-----------------------------------
  1420. /// @name Test share with user with special character
  1421. ///-----------------------------------
  1422. /**
  1423. * This test share with a userToShare the folder pathTestFolder
  1424. */
  1425. - (void) testShareWithUser {
  1426. //We create a semaphore to wait until we recive the responses from Async calls
  1427. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  1428. [_sharedOCCommunication shareWith:k_user_to_share shareeType:shareTypeUser inServer:k_base_url andFileOrFolderPath:[NSString stringWithFormat:@"/%@", k_path_test_folder] andPermissions:k_read_share_permission onCommunication:_sharedOCCommunication successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  1429. NSLog(@"Share with user");
  1430. dispatch_semaphore_signal(semaphore);
  1431. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  1432. XCTFail(@"Error share with user");
  1433. dispatch_semaphore_signal(semaphore);
  1434. }];
  1435. // Run loop
  1436. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  1437. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  1438. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  1439. }
  1440. ///-----------------------------------
  1441. /// @name Test share with group with special character
  1442. ///-----------------------------------
  1443. /**
  1444. * This test share with groupToShare the folder pathTestFolder
  1445. */
  1446. - (void) testShareWithGroup {
  1447. //We create a semaphore to wait until we recive the responses from Async calls
  1448. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  1449. [_sharedOCCommunication shareWith:k_group_to_share shareeType:shareTypeGroup inServer:k_base_url andFileOrFolderPath:[NSString stringWithFormat:@"/%@", k_path_test_folder] andPermissions:k_read_share_permission onCommunication:_sharedOCCommunication successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  1450. NSLog(@"Share with group");
  1451. dispatch_semaphore_signal(semaphore);
  1452. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  1453. XCTFail(@"Error share with group");
  1454. dispatch_semaphore_signal(semaphore);
  1455. }];
  1456. // Run loop
  1457. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  1458. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  1459. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  1460. }
  1461. ///-----------------------------------
  1462. /// @name Test unshare with user with special character
  1463. ///-----------------------------------
  1464. /**
  1465. * This test unShare with user the folder pathTestFolder
  1466. */
  1467. - (void) testUnShareWithUser {
  1468. //1. create the folder and share it with user
  1469. [self testShareWithUser];
  1470. //We create a semaphore to wait until we recive the responses from Async calls
  1471. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  1472. //2. read the folder to obtain the info of OCSharedDto
  1473. [_sharedOCCommunication readSharedByServer:k_base_url
  1474. onCommunication: _sharedOCCommunication successRequest:^(NSURLResponse *response, NSArray *listOfShared, NSString *redirectedServer) {
  1475. OCSharedDto *shared;
  1476. for (OCSharedDto *current in listOfShared) {
  1477. if ([current.path isEqualToString:[NSString stringWithFormat:@"/%@/", k_path_test_folder]]
  1478. && [current.shareWith isEqualToString:k_user_to_share]) {
  1479. shared = current;
  1480. }
  1481. }
  1482. if (shared) {
  1483. //3. Unshare the folder
  1484. [_sharedOCCommunication unShareFileOrFolderByServer:k_base_url andIdRemoteShared:shared.idRemoteShared onCommunication:_sharedOCCommunication successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  1485. NSLog(@"File unshared with user");
  1486. dispatch_semaphore_signal(semaphore);
  1487. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  1488. XCTFail(@"Error unsharing folder with user");
  1489. dispatch_semaphore_signal(semaphore);
  1490. }];
  1491. } else {
  1492. XCTFail(@"Folder not shared on testUnShareWithUser");
  1493. dispatch_semaphore_signal(semaphore);
  1494. }
  1495. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  1496. XCTFail(@"Error reading shares on testUnShareWithUser");
  1497. dispatch_semaphore_signal(semaphore);
  1498. }];
  1499. // Run loop
  1500. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  1501. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  1502. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];}
  1503. ///-----------------------------------
  1504. /// @name Test is share by server
  1505. ///-----------------------------------
  1506. /**
  1507. * This test check if a shared file is shared
  1508. */
  1509. - (void) testIsShareByServer {
  1510. //We create a semaphore to wait until we recive the responses from Async calls
  1511. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  1512. //1. Share a folder
  1513. [self testShareAFolder];
  1514. //2. Read the shared
  1515. [_sharedOCCommunication readSharedByServer:k_base_url onCommunication: _sharedOCCommunication successRequest:^(NSURLResponse *response, NSArray *listOfShared, NSString *redirectedServer) {
  1516. BOOL isFolderShared = NO;
  1517. for (OCSharedDto *current in listOfShared) {
  1518. if ([current.path isEqualToString:[NSString stringWithFormat:@"/%@/", k_path_test_folder]]) {
  1519. isFolderShared = YES;
  1520. //3. Check if the share folder is shared by the id
  1521. [_sharedOCCommunication isShareFileOrFolderByServer:k_base_url andIdRemoteShared:current.idRemoteShared onCommunication:_sharedOCCommunication successRequest:^(NSURLResponse *response, NSString *redirectedServer, BOOL isShared, id shareDto) {
  1522. NSLog(@"File is shared");
  1523. dispatch_semaphore_signal(semaphore);
  1524. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  1525. XCTFail(@"Error checking if a share is shared by id share");
  1526. dispatch_semaphore_signal(semaphore);
  1527. }];
  1528. }
  1529. }
  1530. if (!isFolderShared) {
  1531. XCTFail(@"Folder not shared before check");
  1532. dispatch_semaphore_signal(semaphore);
  1533. }
  1534. dispatch_semaphore_signal(semaphore);
  1535. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  1536. XCTFail(@"Error reading shares");
  1537. dispatch_semaphore_signal(semaphore);
  1538. }];
  1539. // Run loop
  1540. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  1541. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  1542. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  1543. }
  1544. ///-----------------------------------
  1545. /// @name Test unshare file or folder
  1546. ///-----------------------------------
  1547. /**
  1548. * This test check if we can unshare a file or folder
  1549. */
  1550. - (void) testUnShareByServer {
  1551. //We create a semaphore to wait until we recive the responses from Async calls
  1552. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  1553. //1. Share a folder
  1554. [self testShareAFolder];
  1555. //2. Read the shared
  1556. [_sharedOCCommunication readSharedByServer:k_base_url onCommunication: _sharedOCCommunication successRequest:^(NSURLResponse *response, NSArray *listOfShared, NSString *redirectedServer) {
  1557. BOOL isFolderShared = NO;
  1558. for (OCSharedDto *current in listOfShared) {
  1559. if ([current.path isEqualToString:[NSString stringWithFormat:@"/%@/", k_path_test_folder]]) {
  1560. isFolderShared = YES;
  1561. //3. Unshare the share folder by the id
  1562. [_sharedOCCommunication unShareFileOrFolderByServer:k_base_url andIdRemoteShared:current.idRemoteShared onCommunication:_sharedOCCommunication successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  1563. NSLog(@"Share unshared correctly");
  1564. dispatch_semaphore_signal(semaphore);
  1565. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  1566. XCTFail(@"Error unsharing file by id share");
  1567. dispatch_semaphore_signal(semaphore);
  1568. }];
  1569. }
  1570. }
  1571. if (!isFolderShared) {
  1572. XCTFail(@"Folder not shared before check");
  1573. dispatch_semaphore_signal(semaphore);
  1574. }
  1575. dispatch_semaphore_signal(semaphore);
  1576. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  1577. XCTFail(@"Error reading shares");
  1578. dispatch_semaphore_signal(semaphore);
  1579. }];
  1580. // Run loop
  1581. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  1582. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  1583. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  1584. }
  1585. ///-----------------------------------
  1586. /// @name testGetFeaturesSupportedByServer
  1587. ///-----------------------------------
  1588. /**
  1589. * This test check if we can get all the features supported by the server
  1590. */
  1591. - (void) testGetFeaturesSupportedByServer {
  1592. //We create a semaphore to wait until we recive the responses from Async calls
  1593. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  1594. [_sharedOCCommunication getFeaturesSupportedByServer:k_base_url onCommunication:_sharedOCCommunication successRequest:^(NSURLResponse *response, BOOL hasShareSupport, BOOL hasShareeSupport, BOOL hasCookiesSupport, BOOL hasForbiddenCharactersSupport, BOOL hasCapabilitiesSupport, BOOL hasFedSharesOptionShareSupport, NSString *redirectedServer) {
  1595. NSLog(@"Server features correctly read");
  1596. NSLog(@"hasShareSupport: %d", hasShareSupport);
  1597. NSLog(@"hasShareeSupport: %d", hasShareeSupport);
  1598. NSLog(@"hasCookiesSupport: %d", hasCookiesSupport);
  1599. NSLog(@"hasForbiddenCharactersSupport: %d", hasForbiddenCharactersSupport);
  1600. NSLog(@"hasCapabilitiesSupport: %d", hasCapabilitiesSupport);
  1601. NSLog(@"hasFedSharesOptionShareSupport: %d", hasFedSharesOptionShareSupport);
  1602. dispatch_semaphore_signal(semaphore);
  1603. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  1604. XCTFail(@"Error reading server features");
  1605. dispatch_semaphore_signal(semaphore);
  1606. }];
  1607. // Run loop
  1608. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  1609. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  1610. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  1611. }
  1612. ///-----------------------------------
  1613. /// @name Test share with remote user federated sharing
  1614. ///-----------------------------------
  1615. /**
  1616. * This test share with a userToShare the folder pathTestFolder
  1617. */
  1618. - (void) testShareWithRemoteUser {
  1619. //We create a semaphore to wait until we recive the responses from Async calls
  1620. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  1621. [_sharedOCCommunication shareWith:k_remote_user_to_share shareeType:shareTypeRemote inServer:k_base_url andFileOrFolderPath:[NSString stringWithFormat:@"/%@", k_path_test_folder] andPermissions:k_max_folder_share_permission onCommunication:_sharedOCCommunication successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  1622. NSLog(@"Share with remote user");
  1623. dispatch_semaphore_signal(semaphore);
  1624. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  1625. XCTFail(@"Error share with remote user");
  1626. dispatch_semaphore_signal(semaphore);
  1627. }];
  1628. // Run loop
  1629. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  1630. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  1631. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  1632. }
  1633. ///-----------------------------------
  1634. /// @name Test to get a remote thumbnail
  1635. ///-----------------------------------
  1636. /**
  1637. * This test try get a thumbnail of an image using the Thumbnail API
  1638. *
  1639. */
  1640. - (void) testGetRemoteThumbnail {
  1641. //Create Folder A for the Test
  1642. NSString *testPath = [NSString stringWithFormat:@"%@/Folder A", k_path_test_folder];
  1643. [self createFolderWithName:testPath];
  1644. //Upload file /Tests/Folder A/test.jpeg
  1645. NSString *bundlePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"test" ofType:@"jpeg"];
  1646. NSString *remotePath = [NSString stringWithFormat:@"%@/Folder A/Test.jpg", k_path_test_folder];
  1647. [self uploadFilePath:bundlePath inRemotePath:remotePath];
  1648. //We create a semaphore to wait until we recive the responses from Async calls
  1649. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  1650. [_sharedOCCommunication getRemoteThumbnailByServer:k_base_url ofFilePath:remotePath withWidth:64 andHeight:64 onCommunication:_sharedOCCommunication successRequest:^(NSURLResponse *response, NSData *thumbnail, NSString *redirectedServer) {
  1651. if ([UIImage imageWithData:thumbnail]) {
  1652. NSLog(@"Thumbnail getted");
  1653. } else {
  1654. XCTFail(@"Thumbnail getted but it is not an image");
  1655. }
  1656. dispatch_semaphore_signal(semaphore);
  1657. } failureRequest:^(NSURLResponse *response, NSError *error, NSString *redirectedServer) {
  1658. XCTFail(@"Error getting thumbnail");
  1659. dispatch_semaphore_signal(semaphore);
  1660. }];
  1661. // Run loop
  1662. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  1663. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  1664. beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  1665. }
  1666. @end