Adapter for libpepmime, offering ObjC API.
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.
 
 
pEpMimeObjCAdapter/pEpMimeObjCAdapterTests_macOS/Interface/PEPMessage+PEPMessage_MIMET...

200 lines
9.5 KiB

//
// PEPMessage+MIMETest.m
// pEpMimeObjCAdapterTests_macOS
//
// Created by Andreas Buff on 07.07.21.
//
#import <XCTest/XCTest.h>
#import "PEPMessage+PEPMessage_MIME.h"
#import <PEPObjCTypes_macOS/PEPObjCTypes_macOS.h>
#import "PEPMessage+PEPTestTypes.h"
@interface PEPMessage_MIMETest : XCTestCase
@end
@implementation PEPMessage_MIMETest
- (void)testPEPMessage2MIMEString_onlyRequiredFieldsSet {
PEPMessage *testee = [PEPMessage testMessageWithOnlyRequiredFieldsSet:YES];
[self assertPEPMessageToMimeString:testee];
}
- (void)testPEPMessage2MIMEString_allFieldsSet {
PEPMessage *testee = [PEPMessage testMessageWithOnlyRequiredFieldsSet:NO];
[self assertPEPMessageToMimeString:testee];
}
- (void)testPEPMessage2MIMEData_onlyRequiredFieldsSet {
PEPMessage *testee = [PEPMessage testMessageWithOnlyRequiredFieldsSet:YES];
[self assertPEPMessageToMimeData:testee];
}
- (void)testPEPMessage2MIMEData_allFieldsSet {
PEPMessage *testee = [PEPMessage testMessageWithOnlyRequiredFieldsSet:NO];
[self assertPEPMessageToMimeData:testee];
}
- (void)testMIMEData2PEPMessage_onlyRequiredFieldsSet {
PEPMessage *pEpMessage = [PEPMessage testMessageWithOnlyRequiredFieldsSet:YES];
NSError *error = nil;
NSData *mimeData = [pEpMessage mimeDataWithError:&error];
XCTAssertNotNil(mimeData);
[self assertMimedata2PEPMessage:mimeData];
}
- (void)testMIMEData2PEPMessage_allFieldsSet {
PEPMessage *pEpMessage = [PEPMessage testMessageWithOnlyRequiredFieldsSet:NO];
NSError *error = nil;
NSData *mimeData = [pEpMessage mimeDataWithError:&error];
XCTAssertNotNil(mimeData);
[self assertMimedata2PEPMessage:mimeData];
}
- (void)testMIMEString2PEPMessage_onlyRequiredFieldsSet {
PEPMessage *pEpMessage = [PEPMessage testMessageWithOnlyRequiredFieldsSet:YES];
NSError *error = nil;
NSString *mimestring = [pEpMessage mimeStringWithError:&error];
XCTAssertNotNil(mimestring);
[self assertMimeString2PEPMessage:mimestring];
}
- (void)testMIMEString2PEPMessage_allFieldsSet {
PEPMessage *pEpMessage = [PEPMessage testMessageWithOnlyRequiredFieldsSet:NO];
NSError *error = nil;
NSString *mimestring = [pEpMessage mimeStringWithError:&error];
XCTAssertNotNil(mimestring);
[self assertMimeString2PEPMessage:mimestring];
}
- (void)testRoundtripPEPMessage2MIMEStringAndBack_allFieldsSet {
PEPMessage *pEpMessage = [PEPMessage testMessageWithOnlyRequiredFieldsSet:NO];
NSError *error = nil;
NSString *mimestring = [pEpMessage mimeStringWithError:&error];
XCTAssertNotNil(mimestring);
[self assertMimeString2PEPMessage:mimestring];
PEPMessage *backConvertedPEPMessage = [PEPMessage pEpMessageWithMimeString:mimestring
error:&error];
XCTAssertNotNil(backConvertedPEPMessage);
[pEpMessage isEqual:backConvertedPEPMessage];
}
- (void)testRoundtripPEPMessage2MIMEStringAndBack_onlyRequiredFieldsSet {
PEPMessage *pEpMessage = [PEPMessage testMessageWithOnlyRequiredFieldsSet:YES];
NSError *error = nil;
NSString *mimestring = [pEpMessage mimeStringWithError:&error];
XCTAssertNotNil(mimestring);
[self assertMimeString2PEPMessage:mimestring];
PEPMessage *backConvertedPEPMessage = [PEPMessage pEpMessageWithMimeString:mimestring
error:&error];
XCTAssertNotNil(backConvertedPEPMessage);
[pEpMessage isEqual:backConvertedPEPMessage];
}
- (void)testRoundtripPEPMessage2MIMEDataAndBack_allFieldsSet {
PEPMessage *pEpMessage = [PEPMessage testMessageWithOnlyRequiredFieldsSet:NO];
NSError *error = nil;
NSData *mimeData = [pEpMessage mimeDataWithError:&error];
XCTAssertNotNil(mimeData);
[self assertMimedata2PEPMessage:mimeData];
PEPMessage *backConvertedPEPMessage = [PEPMessage pEpMessageWithMimeData:mimeData error:&error];
XCTAssertNotNil(backConvertedPEPMessage);
[pEpMessage isEqual:backConvertedPEPMessage];
}
- (void)testRoundtripPEPMessage2MIMEDataAndBack_onlyRequiredFieldsSet {
PEPMessage *pEpMessage = [PEPMessage testMessageWithOnlyRequiredFieldsSet:YES];
NSError *error = nil;
NSData *mimeData = [pEpMessage mimeDataWithError:&error];
XCTAssertNotNil(mimeData);
[self assertMimedata2PEPMessage:mimeData];
PEPMessage *backConvertedPEPMessage = [PEPMessage pEpMessageWithMimeData:mimeData error:&error];
XCTAssertNotNil(backConvertedPEPMessage);
[pEpMessage isEqual:backConvertedPEPMessage];
}
/// If you have mails causing issue which you want to test, add them to Resoureces/test_mails group.
/// If OK for privacy reasons, feel free to check them in.
/// The must have ".eml" extension.
- (void)testRoundtripForAllMailsInTestmails {
NSBundle *bundle = [NSBundle bundleForClass:self.class];
NSArray<NSURL*> *urls = [bundle URLsForResourcesWithExtension:@"eml" subdirectory:nil];
XCTAssertTrue(urls.count > 0, @"This test does nothing if there are zero mails in test_mails directory");
for (NSURL *url in urls) {
NSError *error = nil;
NSString *mailString = [NSString stringWithContentsOfURL:url
encoding:NSUTF8StringEncoding
error:&error];
if (!mailString) {
mailString = [NSString stringWithContentsOfURL:url
encoding:NSASCIIStringEncoding
error:&error];
}
if (!mailString) {
XCTFail(@"Error lodaing url: %@", url);
continue;
}
[self assertRoundtripMIMEString2PEPMEssageAndBack:mailString];
}
}
// MIME-19
- (void)testBugParsingLineFeed_MIME19 {
NSString *mimeString = @"Return-Path: <peptest@peptest.ch>\nX-Original-To: iostest020@peptest.ch\nDelivered-To: iostest020@peptest.ch\nReceived: by peptest.ch (Postfix, from userid 10000)\n\tid B0B37101996; Wed, 28 Jul 2021 14:57:16 +0200 (CEST)\nReceived: from Davids-New-MacBook-Pro.local (unknown [185.219.139.95])\n\t(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))\n\t(No client certificate requested)\n\tby peptest.ch (Postfix) with ESMTPSA id 3E9331010E2;\n\tWed, 28 Jul 2021 14:57:16 +0200 (CEST)\nAuthentication-Results: plesk.peptest.ch;\n spf=pass (sender IP is 185.219.139.95) smtp.mailfrom=iostest020@peptest.ch smtp.helo=Davids-New-MacBook-Pro.local\nReceived-SPF: pass (plesk.peptest.ch: connection is authenticated)\nFrom: David Alarcon <iostest020@peptest.ch>\nSubject: =?UTF-8?B?cOKJoXA=?=\nTo: IOS Test 020 <iostest020@peptest.ch>,\n \"davidtest001@sq.pep.security\" <davidtest001@sq.pep.security>\nX-Pep-Version: 2.1\nMessage-ID: <0cf94c42-4393-0a5f-ac48-06080d725f70@peptest.ch>\nDate: Wed, 28 Jul 2021 14:57:15 +0200\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0)\n Gecko/20100101 Thunderbird/78.12.0\nMIME-Version: 1.0\nContent-Type: multipart/encrypted; boundary=\"5b25ace2175dfcf04f97e3e453b0a9e\";\n protocol=\"application/pgp-encrypted\"\n\n\n--5b25ace2175dfcf04f97e3e453b0a9e\nX-Apple-Content-Length: 10\nContent-Type: application/pgp-encrypted\nContent-Transfer-Encoding: 7bit\n\n\n--5b25ace2175dfcf04f97e3e453b0a9e\nContent-Transfer-Encoding: 7bit\nContent-Disposition: inline;\n\tfilename=msg.asc\nContent-Type: application/octet-stream;\n\tname=msg.asc\nX-Apple-Content-Length: 7634\n\n\n--5b25ace2175dfcf04f97e3e453b0a9e--\n";
NSError *error = nil;
PEPMessage *testee = [PEPMessage pEpMessageWithMimeString:mimeString error:&error];
XCTAssertNotNil(testee.from);
XCTAssertNotNil(testee.to);
XCTAssertNotNil(testee.shortMessage);
XCTAssertNotNil(testee.optionalFields);
XCTAssertGreaterThanOrEqual(testee.optionalFields.count, 1);
}
// MARK: - HELPER
- (void)assertPEPMessageToMimeString:(PEPMessage *)pEpMEssage {
NSError *error = nil;
NSString *testee = [pEpMEssage mimeStringWithError:&error];
XCTAssertNotNil(testee);
}
- (void)assertPEPMessageToMimeData:(PEPMessage *)pEpMEssage {
NSError *error = nil;
NSData *testee = [pEpMEssage mimeDataWithError:&error];
XCTAssertNotNil(testee);
NSString *convertedTestee = [[NSString alloc] initWithData:testee encoding:NSUTF8StringEncoding];
XCTAssertNotNil(convertedTestee, @"Somthins is wrong with data.");
}
- (void)assertMimeString2PEPMessage:(NSString *)mimeString {
NSError *error = nil;
PEPMessage *testee = [PEPMessage pEpMessageWithMimeString:mimeString error:&error];
XCTAssertNotNil(testee);
}
- (void)assertMimedata2PEPMessage:(NSData *)mimeData {
NSError *error = nil;
PEPMessage *testee = [PEPMessage pEpMessageWithMimeData:mimeData error:&error];
XCTAssertNotNil(testee);
}
- (void)assertRoundtripMIMEString2PEPMEssageAndBack:(NSString *)mimeString {
NSError *error = nil;
PEPMessage *pEpMEssage = [PEPMessage pEpMessageWithMimeString:mimeString error:&error];
XCTAssertNotNil(pEpMEssage);
NSData *mimeData = [pEpMEssage mimeDataWithError:&error];
XCTAssertNotNil(mimeData);
NSString *mimeStringFromData = [[NSString alloc] initWithData:mimeData
encoding:NSUTF8StringEncoding];
XCTAssertNotNil(mimeStringFromData);
NSString *mimeStringBackConverted = [pEpMEssage mimeStringWithError:&error];
XCTAssertNotNil(mimeStringBackConverted);
PEPMessage *onceMoreMessage = [PEPMessage pEpMessageWithMimeString:mimeStringBackConverted
error:&error];
XCTAssertTrue([pEpMEssage isEqual:onceMoreMessage]);
}
@end