You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

547 lines
18 KiB

  1. //
  2. // PEPSessionTest.m
  3. // pEpObjCAdapterTests
  4. //
  5. // Created by Dirk Zimmermann on 18.07.20.
  6. // Copyright © 2020 pp. All rights reserved.
  7. //
  8. #import <XCTest/XCTest.h>
  9. #import "PEPObjCAdapterTypes_iOS.h"
  10. #import "PEPObjCAdapter_iOS.h"
  11. #import "NSNumber+PEPRating.h"
  12. #import "PEPTestUtils.h"
  13. #import "PEPSessionProvider.h"
  14. #import "PEPInternalSession.h"
  15. #import "PEPInternalSession+SetIdentity.h"
  16. #import "XCTestCase+PEPSession.h"
  17. @interface PEPSessionTest : XCTestCase
  18. @end
  19. @implementation PEPSessionTest
  20. - (void)setUp
  21. {
  22. [super setUp];
  23. [self pEpCleanUp];
  24. [PEPObjCAdapter setUnEncryptedSubjectEnabled:NO];
  25. NSError *error = nil;
  26. XCTAssertTrue([PEPObjCAdapter configurePassphraseForNewKeys:nil error:&error]);
  27. XCTAssertNil(error);
  28. }
  29. - (void)tearDown
  30. {
  31. [self pEpCleanUp];
  32. [super tearDown];
  33. }
  34. - (void)pEpCleanUp
  35. {
  36. [PEPTestUtils cleanUp];
  37. }
  38. - (void)testMailToMyself
  39. {
  40. // Our test user:
  41. // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
  42. // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
  43. XCTAssertTrue([self importBundledKey:@"6FF00E97_sec.asc"]);
  44. PEPIdentity *identAlice = [[PEPIdentity alloc]
  45. initWithAddress:@"pep.test.alice@pep-project.org"
  46. userID:ownUserId
  47. userName:@"pEp Test Alice"
  48. isOwn:YES
  49. fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
  50. PEPSession *asyncSession = [PEPSession new];
  51. NSError *error = nil;
  52. PEPIdentity *identAliceMyselfed = [self mySelf:identAlice error:&error];
  53. XCTAssertNotNil(identAliceMyselfed);
  54. XCTAssertNil(error);
  55. PEPMessage *msg = [PEPMessage new];
  56. msg.from = identAlice;
  57. msg.to = @[identAlice];
  58. msg.shortMessage = @"Mail to Myself";
  59. msg.longMessage = @"This is a text content";
  60. msg.direction = PEPMsgDirectionOutgoing;
  61. error = nil;
  62. NSNumber *numRating = [self testOutgoingRatingForMessage:msg error:&error];
  63. XCTAssertNotNil(numRating);
  64. XCTAssertNil(error);
  65. XCTAssertEqual(numRating.pEpRating, PEPRatingTrustedAndAnonymized);
  66. for (NSNumber *boolNumWithEncFormat in @[@YES, @NO]) {
  67. error = nil;
  68. PEPMessage *encryptedMessage = [PEPMessage new];
  69. if (boolNumWithEncFormat.boolValue) {
  70. encryptedMessage = [self
  71. encryptMessage:msg
  72. extraKeys:nil
  73. encFormat:PEPEncFormatPEP
  74. status:nil
  75. error:&error];
  76. XCTAssertNotNil(encryptedMessage);
  77. XCTAssertNil(error);
  78. } else {
  79. encryptedMessage = [self encryptMessage:msg extraKeys:nil status:nil error:&error];
  80. XCTAssertNotNil(encryptedMessage);
  81. XCTAssertNil(error);
  82. }
  83. XCTestExpectation *expectationDec = [self expectationWithDescription:@"expectationDec"];
  84. [asyncSession
  85. decryptMessage:encryptedMessage
  86. flags:0
  87. extraKeys:nil
  88. errorCallback:^(NSError *error) {
  89. XCTFail();
  90. [expectationDec fulfill];
  91. }
  92. successCallback:^(PEPMessage * srcMessage,
  93. PEPMessage * dstMessage,
  94. PEPStringList * keyList,
  95. PEPRating rating,
  96. PEPDecryptFlags flags,
  97. BOOL isFormerlyEncryptedReuploadedMessage) {
  98. XCTAssertNotNil(dstMessage);
  99. XCTAssertEqual(rating, PEPRatingTrustedAndAnonymized);
  100. [expectationDec fulfill];
  101. }];
  102. [self waitForExpectations:@[expectationDec] timeout:PEPTestInternalSyncTimeout];
  103. }
  104. }
  105. - (void)testEncryptToSelf
  106. {
  107. // Write mail to yourself ...
  108. PEPMessage *encMessage = [self mailWrittenToMySelf];
  109. // ... and assert subject is encrypted
  110. XCTAssertEqualObjects(encMessage.shortMessage, @"pp");
  111. }
  112. - (void)testEncryptAndAttachPrivateKeyIllegalValue
  113. {
  114. PEPIdentity *identMe = [[PEPIdentity alloc]
  115. initWithAddress:@"me-myself-and-i@pep-project.org"
  116. userID:@"me-myself-and-i"
  117. userName:@"pEp Me"
  118. isOwn:YES];
  119. NSError *error = nil;
  120. identMe = [self mySelf:identMe error:&error];
  121. XCTAssertNotNil(identMe);
  122. XCTAssertNil(error);
  123. XCTAssertNotNil(identMe.fingerPrint);
  124. NSString *fprAlice = @"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
  125. PEPIdentity *identAlice = [self
  126. checkImportingKeyFilePath:@"6FF00E97_sec.asc"
  127. address:@"pep.test.alice@pep-project.org"
  128. userID:@"alice_user_id"
  129. fingerPrint:fprAlice];
  130. XCTAssertNotNil(identAlice);
  131. XCTAssertEqualObjects(identAlice.fingerPrint, fprAlice);
  132. NSString *shortMessage = @"whatever it may be";
  133. NSString *longMessage = [NSString stringWithFormat:@"%@ %@", shortMessage, shortMessage];
  134. PEPMessage *message = [PEPMessage new];
  135. message.from = identMe;
  136. message.to = @[identAlice];
  137. message.shortMessage = shortMessage;
  138. message.longMessage = longMessage;
  139. XCTestExpectation *expectationEnc = [self expectationWithDescription:@"expectationEnc"];
  140. PEPSession *asyncSession = [PEPSession new];
  141. [asyncSession
  142. encryptMessage:message
  143. toFpr:fprAlice
  144. encFormat:PEPEncFormatPEP
  145. flags:0
  146. errorCallback:^(NSError * _Nonnull error) {
  147. [expectationEnc fulfill];
  148. } successCallback:^(PEPMessage * _Nonnull srcMessage, PEPMessage * _Nonnull destMessage) {
  149. XCTFail();
  150. [expectationEnc fulfill];
  151. }];
  152. [self waitForExpectations:@[expectationEnc] timeout:PEPTestInternalSyncTimeout];
  153. }
  154. - (void)testRatingForIdentity
  155. {
  156. PEPIdentity *me = [self
  157. checkMySelfImportingKeyFilePath:@"6FF00E97_sec.asc"
  158. address:@"pep.test.alice@pep-project.org"
  159. userID:@"Alice_User_ID"
  160. fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
  161. XCTAssertNotNil(me);
  162. XCTAssertEqual([self ratingForIdentity:me], PEPRatingTrustedAndAnonymized);
  163. PEPIdentity *alice = [self
  164. checkImportingKeyFilePath:@"6FF00E97_sec.asc"
  165. address:@"pep.test.alice@pep-project.org"
  166. userID:@"This Is Alice"
  167. fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
  168. XCTAssertNotNil(alice);
  169. XCTAssertEqual([self ratingForIdentity:alice], PEPRatingReliable);
  170. }
  171. - (void)testTrustWords
  172. {
  173. NSError *error = nil;
  174. NSArray *trustwords = [self
  175. trustwordsForFingerprint:@"DB47DB47DB47DB47DB47DB47DB47DB47DB47DB47"
  176. languageID:@"en"
  177. shortened:false
  178. error:&error];
  179. XCTAssertNil(error);
  180. XCTAssertEqual([trustwords count], 10);
  181. for(id word in trustwords) {
  182. XCTAssertEqualObjects(word, @"BAPTISMAL");
  183. }
  184. }
  185. - (void)testIdentityRatingTrustResetMistrustUndo
  186. {
  187. PEPIdentity *me = [[PEPIdentity alloc]
  188. initWithAddress:@"me@example.org"
  189. userID:@"me_myself"
  190. userName:@"Me Me"
  191. isOwn:YES];
  192. NSError *error = nil;
  193. me = [self mySelf:me error:&error];
  194. XCTAssertNotNil(me);
  195. XCTAssertNil(error);
  196. XCTAssertNotNil(me.fingerPrint);
  197. XCTAssertEqual([self ratingForIdentity:me], PEPRatingTrustedAndAnonymized);
  198. PEPIdentity *alice = [self
  199. checkImportingKeyFilePath:@"6FF00E97_sec.asc"
  200. address:@"pep.test.alice@pep-project.org"
  201. userID:@"This Is Alice"
  202. fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
  203. XCTAssertNotNil(alice);
  204. XCTAssertEqual([self ratingForIdentity:alice], PEPRatingReliable);
  205. XCTAssertTrue([self trustPersonalKey:alice error:&error]);
  206. XCTAssertNil(error);
  207. XCTAssertEqual([self ratingForIdentity:alice], PEPRatingTrusted);
  208. XCTAssertTrue([self keyResetTrust:alice error:&error]);
  209. XCTAssertNil(error);
  210. XCTAssertEqual([self ratingForIdentity:alice], PEPRatingReliable);
  211. XCTAssertTrue([self keyMistrusted:alice error:&error]);
  212. XCTAssertNil(error);
  213. XCTAssertEqual([self ratingForIdentity:alice], PEPRatingHaveNoKey);
  214. }
  215. - (void)testQueryKeySyncOnOwnIdentityInALoop
  216. {
  217. PEPIdentity *identMe = [[PEPIdentity alloc]
  218. initWithAddress:@"me-myself-and-i@pep-project.org"
  219. userID:@"me-myself-and-i"
  220. userName:@"pEp Me"
  221. isOwn:YES];
  222. NSError *error = nil;
  223. identMe = [self mySelf:identMe error:&error];
  224. XCTAssertNotNil(identMe);
  225. XCTAssertNil(error);
  226. for (NSNumber *numBool in @[@YES, @NO]) {
  227. error = nil;
  228. if ([numBool boolValue]) {
  229. XCTAssertTrue([self enableSyncForIdentity:identMe error:&error]);
  230. } else {
  231. XCTAssertTrue([self disableSyncForIdentity:identMe error:&error]);
  232. }
  233. XCTAssertNil(error);
  234. }
  235. }
  236. - (void)testGetLogWithError
  237. {
  238. NSError *error = nil;
  239. NSString *log = [self getLogWithError:&error];
  240. XCTAssertGreaterThan(log.length, 0);
  241. XCTAssertNotNil(log);
  242. XCTAssertNil(error);
  243. }
  244. - (void)testGetTrustwords
  245. {
  246. PEPIdentity *partner1Orig = [[PEPIdentity alloc]
  247. initWithAddress:@"partner1@dontcare.me" userID:@"partner1"
  248. userName:@"partner1"
  249. isOwn:NO fingerPrint:@"F0CD3F7B422E5D587ABD885BF2D281C2789DD7F6"];
  250. PEPIdentity *meOrig = [[PEPIdentity alloc]
  251. initWithAddress:@"me@dontcare.me" userID:@"me"
  252. userName:@"me"
  253. isOwn:NO fingerPrint:@"CC1F73F6FB774BF08B197691E3BFBCA9248FC681"];
  254. NSError *error = nil;
  255. NSString *trustwordsFull = [self getTrustwordsIdentity1:meOrig identity2:partner1Orig
  256. language:@"en" full:YES error:&error];
  257. XCTAssertNil(error);
  258. XCTAssertEqualObjects(trustwordsFull,
  259. @"EMERSON GASPER TOKENISM BOLUS COLLAGE DESPISE BEDDED ENCRYPTION IMAGINE BEDFORD");
  260. NSString *trustwordsUndefined = [self getTrustwordsIdentity1:meOrig identity2:partner1Orig
  261. language:@"ZZ" full:YES error:&error];
  262. XCTAssertNotNil(error);
  263. XCTAssertNil(trustwordsUndefined);
  264. }
  265. - (void)testGenKey
  266. {
  267. PEPIdentity *identMe = [[PEPIdentity alloc]
  268. initWithAddress:@"pep.test.iosgenkey@pep-project.org"
  269. userID:@"Me"
  270. userName:@"pEp Test iOS GenKey"
  271. isOwn:YES];
  272. NSError *error = nil;
  273. identMe = [self mySelf:identMe error:&error];
  274. XCTAssertNotNil(identMe);
  275. XCTAssertNil(error);
  276. XCTAssertNotNil(identMe.fingerPrint);
  277. XCTAssertNotEqual(identMe.commType, PEPCommTypeUnknown);
  278. NSNumber *boolNum = [self isPEPUser:identMe error:&error];
  279. XCTAssertNil(error);
  280. XCTAssertNotNil(boolNum);
  281. XCTAssertTrue(boolNum.boolValue);
  282. }
  283. - (void)testTrustOwnKey
  284. {
  285. PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  286. userName:@"userName"];
  287. NSError *error = nil;
  288. me = [self mySelf:me error:&error];
  289. XCTAssertNotNil(me);
  290. XCTAssertNil(error);
  291. XCTAssertTrue([self trustOwnKeyIdentity:me error:&error]);
  292. XCTAssertNil(error);
  293. }
  294. - (void)testKeyResetIdentityOnOwnKeyIsIllegal
  295. {
  296. PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  297. userName:@"userName"];
  298. NSError *error = nil;
  299. me = [self mySelf:me error:&error];
  300. XCTAssertNotNil(me);
  301. XCTAssertNil(error);
  302. NSString *fprOriginal = me.fingerPrint;
  303. XCTAssertNotNil(fprOriginal);
  304. // Cannot reset all _own_ keys with this method, as documented
  305. XCTAssertFalse([self keyReset:me fingerprint:nil error:&error]);
  306. XCTAssertNotNil(error);
  307. me = [self mySelf:me error:&error];
  308. XCTAssertNotNil(me);
  309. XCTAssertNil(error);
  310. NSString *fprAfterReset = me.fingerPrint;
  311. XCTAssertNotNil(fprAfterReset);
  312. XCTAssertNotEqual(fprOriginal, fprAfterReset);
  313. }
  314. #pragma mark - Helpers
  315. - (PEPMessage *)mailWrittenToMySelf
  316. {
  317. // Write a e-mail to yourself ...
  318. PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  319. userName:@"userName"];
  320. NSError *error = nil;
  321. me = [self mySelf:me error:&error];
  322. XCTAssertNotNil(me);
  323. XCTAssertNil(error);
  324. NSString *shortMessage = @"Subject";
  325. NSString *longMessage = @"Oh, this is a long body text!";
  326. PEPMessage *mail = [PEPTestUtils mailFrom:me
  327. toIdent:me
  328. shortMessage:shortMessage
  329. longMessage:longMessage
  330. outgoing:YES];
  331. PEPSession *asyncSession = [PEPSession new];
  332. XCTestExpectation *expectationEnc = [self expectationWithDescription:@"expectationEnc"];
  333. __block PEPMessage *encryptedMessage = [PEPMessage new];
  334. [asyncSession
  335. encryptMessage:mail
  336. forSelf:me
  337. extraKeys:nil
  338. errorCallback:^(NSError * _Nonnull error) {
  339. XCTFail();
  340. [expectationEnc fulfill];
  341. } successCallback:^(PEPMessage * _Nonnull srcMessage, PEPMessage * _Nonnull destMessage) {
  342. encryptedMessage = destMessage;
  343. [expectationEnc fulfill];
  344. }];
  345. [self waitForExpectations:@[expectationEnc] timeout:PEPTestInternalSyncTimeout];
  346. return encryptedMessage;
  347. }
  348. - (NSNumber * _Nullable)testOutgoingRatingForMessage:(PEPMessage * _Nonnull)theMessage
  349. error:(NSError * _Nullable * _Nullable)error
  350. {
  351. NSError *theError = nil;
  352. NSNumber *ratingOriginal = [self outgoingRatingForMessage:theMessage error:&theError];
  353. XCTAssertNotNil(ratingOriginal);
  354. XCTAssertNil(theError);
  355. if (ratingOriginal == nil) {
  356. return nil;
  357. }
  358. NSNumber *ratingPreview = [[PEPSessionProvider session]
  359. outgoingRatingPreviewForMessage:theMessage
  360. error:error];
  361. XCTAssertEqual(ratingOriginal, ratingPreview);
  362. return ratingOriginal;
  363. }
  364. - (PEPIdentity *)checkImportingKeyFilePath:(NSString *)filePath address:(NSString *)address
  365. userID:(NSString *)userID
  366. fingerPrint:(NSString *)fingerPrint
  367. {
  368. BOOL success = [self importBundledKey:filePath];
  369. XCTAssertTrue(success);
  370. if (success) {
  371. // Our test user:
  372. PEPIdentity *identTest = [[PEPIdentity alloc]
  373. initWithAddress:address
  374. userID:userID
  375. userName:[NSString stringWithFormat:@"Some User Name %@", userID]
  376. isOwn:NO];
  377. NSError *error = nil;
  378. PEPIdentity *identTestUpdated = [self updateIdentity:identTest error:&error];
  379. XCTAssertNil(identTestUpdated.fingerPrint); // key election, no key is chosen yet
  380. PEPInternalSession *session = [PEPInternalSession new];
  381. error = nil;
  382. identTest.fingerPrint = fingerPrint;
  383. [session setIdentity:identTest error:&error];
  384. XCTAssertNil(error);
  385. identTest.fingerPrint = fingerPrint;
  386. identTestUpdated = [self updateIdentity:identTest error:&error];
  387. XCTAssertNil(error);
  388. XCTAssertNotNil(identTestUpdated);
  389. XCTAssertNotNil(identTestUpdated.fingerPrint);
  390. XCTAssertEqualObjects(identTestUpdated.fingerPrint, fingerPrint);
  391. return identTestUpdated;
  392. } else {
  393. return nil;
  394. }
  395. }
  396. - (BOOL)importBundledKey:(NSString *)item
  397. {
  398. PEPSession *asyncSession = [PEPSession new];
  399. NSString *txtFileContents = [PEPTestUtils loadStringFromFileName:item];
  400. if (!txtFileContents) {
  401. XCTFail();
  402. }
  403. __block BOOL success = YES;
  404. XCTestExpectation *expImport = [self expectationWithDescription:@"expImport"];
  405. [asyncSession importKey:txtFileContents
  406. errorCallback:^(NSError * _Nonnull error) {
  407. XCTFail();
  408. success = NO;
  409. [expImport fulfill];
  410. } successCallback:^(NSArray<PEPIdentity *> * _Nonnull identities) {
  411. [expImport fulfill];
  412. success = YES;
  413. }];
  414. [self waitForExpectations:@[expImport] timeout:PEPTestInternalSyncTimeout];
  415. return success;
  416. }
  417. - (PEPIdentity * _Nullable)checkMySelfImportingKeyFilePath:(NSString *)filePath
  418. address:(NSString *)address
  419. userID:(NSString *)userID
  420. fingerPrint:(NSString *)fingerPrint
  421. {
  422. PEPSession *asyncSession = [PEPSession new];
  423. XCTAssertTrue([self importBundledKey:filePath]);
  424. // Our test user:
  425. PEPIdentity *identTest = [[PEPIdentity alloc]
  426. initWithAddress:address
  427. userID:userID
  428. userName:[NSString stringWithFormat:@"Some User Name %@", userID]
  429. isOwn:YES
  430. fingerPrint: fingerPrint];
  431. __block BOOL success = NO;
  432. XCTestExpectation *expSetOwnKey = [self expectationWithDescription:@"expSetOwnKey"];
  433. [asyncSession setOwnKey:identTest
  434. fingerprint:fingerPrint
  435. errorCallback:^(NSError * _Nonnull error) {
  436. XCTFail();
  437. success = NO;
  438. [expSetOwnKey fulfill];
  439. } successCallback:^{
  440. success = YES;
  441. [expSetOwnKey fulfill];
  442. }];
  443. [self waitForExpectations:@[expSetOwnKey] timeout:PEPTestInternalSyncTimeout];
  444. if (success) {
  445. return identTest;
  446. } else {
  447. return nil;
  448. }
  449. }
  450. @end