diff --git a/MacPass.xcodeproj/project.pbxproj b/MacPass.xcodeproj/project.pbxproj index 2ed47040..bda055b5 100644 --- a/MacPass.xcodeproj/project.pbxproj +++ b/MacPass.xcodeproj/project.pbxproj @@ -138,6 +138,7 @@ 4C61EA0316D2FD0800AC519E /* MPOutlineViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C61EA0216D2FD0800AC519E /* MPOutlineViewController.m */; }; 4C61EA0516D2FFE200AC519E /* OutlineView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 4C61EA0416D2FFE200AC519E /* OutlineView.xib */; }; 4C63B8FB17A3154D0091BD72 /* MPContextButton.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C63B8FA17A3154D0091BD72 /* MPContextButton.m */; }; + 4C642AE92195E3B700689431 /* MPTestCustomAttributeGetter.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C642AE82195E3B700689431 /* MPTestCustomAttributeGetter.m */; }; 4C65C79C16DD283900E32CFF /* MPToolbarButton.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C65C79B16DD283900E32CFF /* MPToolbarButton.m */; }; 4C65FAE916D16DDB006E0577 /* MPPasswordInputController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C65FAE716D16DDB006E0577 /* MPPasswordInputController.m */; }; 4C663D411D6D91A900CB6237 /* MPNumberFormatter.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C663D401D6D91A900CB6237 /* MPNumberFormatter.m */; }; @@ -552,6 +553,7 @@ 4C61EA0416D2FFE200AC519E /* OutlineView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = OutlineView.xib; sourceTree = ""; }; 4C63B8F917A3154D0091BD72 /* MPContextButton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPContextButton.h; sourceTree = ""; }; 4C63B8FA17A3154D0091BD72 /* MPContextButton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPContextButton.m; sourceTree = ""; }; + 4C642AE82195E3B700689431 /* MPTestCustomAttributeGetter.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MPTestCustomAttributeGetter.m; sourceTree = ""; }; 4C65C79A16DD283900E32CFF /* MPToolbarButton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPToolbarButton.h; sourceTree = ""; }; 4C65C79B16DD283900E32CFF /* MPToolbarButton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPToolbarButton.m; sourceTree = ""; }; 4C65FAE616D16DDB006E0577 /* MPPasswordInputController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPPasswordInputController.h; sourceTree = ""; }; @@ -1219,8 +1221,9 @@ 4C8030491E2FBAA300133E4C /* MPTestKeyMapper.m */, 4C8F0C721FCF1B7A00BE157F /* MPTestPickcharsParser.m */, 4C71BCB32167B75900B4CBDA /* MPTestPluginVersionComparator.m */, - 4C45FB1F178E09ED0010007D /* Supporting Files */, 4C58A4A72192EEBE00B13370 /* MPTestIndexPathAdditions.m */, + 4C642AE82195E3B700689431 /* MPTestCustomAttributeGetter.m */, + 4C45FB1F178E09ED0010007D /* Supporting Files */, ); path = MacPassTests; sourceTree = ""; @@ -1952,6 +1955,7 @@ 4C8DEAA21C314D2C00D24C32 /* MPTestAutotypeDelay.m in Sources */, 4C71BCB42167B75900B4CBDA /* MPTestPluginVersionComparator.m in Sources */, 4C8F0C731FCF1B7A00BE157F /* MPTestPickcharsParser.m in Sources */, + 4C642AE92195E3B700689431 /* MPTestCustomAttributeGetter.m in Sources */, 4C45FB30178E0CE20010007D /* MPTestDocument.m in Sources */, 4C6BC6601A36717E00BDDF3D /* MPDatabaseSearch.m in Sources */, 4C10207F1B750E2F00BFCD59 /* MPTestAutotype.m in Sources */, diff --git a/MacPass/KPKEntry+MPCustomAttributeProperties.h b/MacPass/KPKEntry+MPCustomAttributeProperties.h index f1fc9840..dcdeec2e 100644 --- a/MacPass/KPKEntry+MPCustomAttributeProperties.h +++ b/MacPass/KPKEntry+MPCustomAttributeProperties.h @@ -22,6 +22,8 @@ #import +FOUNDATION_EXTERN NSString *const MPCustomAttributePropertyPrefix; + @interface KPKEntry (MPCustomAttributeProperties) @end diff --git a/MacPass/KPKEntry+MPCustomAttributeProperties.m b/MacPass/KPKEntry+MPCustomAttributeProperties.m index b5a3b7ff..8e2eb37e 100644 --- a/MacPass/KPKEntry+MPCustomAttributeProperties.m +++ b/MacPass/KPKEntry+MPCustomAttributeProperties.m @@ -21,30 +21,25 @@ // #import "KPKEntry+MPCustomAttributeProperties.h" +#import NSString *const MPCustomAttributePropertyPrefix = @"valueForCustomAttribute"; @implementation KPKEntry (MPCustomAttributeProperties) -/*- (void)forwardInvocation:(NSInvocation *)anInvocation { - NSString *selector = NSStringFromSelector(anInvocation.selector); - if([selector hasPrefix:MPCustomAttributePropertyPrefix]) { - NSString *key = [selector substringFromIndex:MPCustomAttributePropertyPrefix.length]; - KPKAttribute *attribute = [self attributeWithKey:key]; - if(attribute) { - anInvocation.selector = @selector(value); - [anInvocation invokeWithTarget:attribute]; - } - else { - anInvocation.selector = @selector(_unkownCustomAttributeValue); - [anInvocation invokeWithTarget:self]; - } - } - else [super forwardInvocation:anInvocation]; -}*/ +// generic getter +static id propertyIMP(id self, SEL _cmd) { + NSString *propertyKey = [NSStringFromSelector(_cmd) substringFromIndex:MPCustomAttributePropertyPrefix.length]; + return [self valueForAttributeWithKey:propertyKey]; +} -- (NSString *)_unkownCustomAttributeValue { - return @""; + ++ (BOOL)resolveInstanceMethod:(SEL)aSEL { + if ([NSStringFromSelector(aSEL) hasPrefix:MPCustomAttributePropertyPrefix]) { + class_addMethod([self class], aSEL,(IMP)propertyIMP, "@@:"); + return YES; + } + return [super resolveInstanceMethod:aSEL]; } @end diff --git a/MacPassTests/MPTestCustomAttributeGetter.m b/MacPassTests/MPTestCustomAttributeGetter.m new file mode 100644 index 00000000..4ccf20b4 --- /dev/null +++ b/MacPassTests/MPTestCustomAttributeGetter.m @@ -0,0 +1,53 @@ +// +// MPTestCustomAttributeGetter.m +// MacPassTests +// +// Created by Michael Starke on 09.11.18. +// Copyright © 2018 HicknHack Software GmbH. All rights reserved. +// + +#import +#import + +#import "KPKEntry+MPCustomAttributeProperties.h" + +@interface MPTestCustomAttributeGetter : XCTestCase +@end + +@implementation MPTestCustomAttributeGetter + +- (void)setUp { +} + +- (void)testValidCustomAttribute { + KPKEntry *entry = [[KPKEntry alloc] init]; + KPKAttribute *attribute1 = [[KPKAttribute alloc] initWithKey:@"custom1" value:@"value1"]; + KPKAttribute *attribute2 = [[KPKAttribute alloc] initWithKey:@"custom2" value:@"value2"]; + [entry addCustomAttribute:attribute1]; + [entry addCustomAttribute:attribute2]; + + SEL selector1 = NSSelectorFromString([MPCustomAttributePropertyPrefix stringByAppendingString:attribute1.key]); + IMP imp1 = [entry methodForSelector:selector1]; + NSString *(*func1)(id, SEL, NSString*) = (void *)imp1; + NSString *value1 = func1(entry, selector1 , attribute1.key); + XCTAssertEqualObjects(value1, attribute1.value); + + SEL selector2 = NSSelectorFromString([MPCustomAttributePropertyPrefix stringByAppendingString:attribute2.key]); + IMP imp2 = [entry methodForSelector:selector2]; + NSString *(*func2)(id, SEL, NSString*) = (void *)imp2; + NSString *value2 = func2(entry, selector2, attribute2.key); + XCTAssertEqualObjects(value2, attribute2.value); + + SEL selector3 = NSSelectorFromString([MPCustomAttributePropertyPrefix stringByAppendingString:@"novalidkey"]); + IMP imp3 = [entry methodForSelector:selector3]; + NSString *(*func3)(id, SEL, NSString*) = (void *)imp3; + NSString *value3 = func3(entry, selector3, @"novalidkey"); + XCTAssertNil(value3); +} + +- (void)testInvalidCustomAttribute { +} + + + +@end