From 7ce789790ce42b0e09a1e7df17c200e9027e596d Mon Sep 17 00:00:00 2001 From: michael starke Date: Wed, 3 Dec 2014 14:36:36 +0100 Subject: [PATCH] Added better test for modifier base key presses --- MacPass.xcodeproj/project.pbxproj | 8 +- MacPass/MPAutotypeCommand.m | 8 +- MacPass/MPAutotypePaste.h | 2 + MacPass/MPAutotypePaste.m | 3 +- MacPass/MacPass-Info.plist | 2 +- MacPassTests/KPKTestAutotype.m | 115 ++++++++++++++++++++ MacPassTests/KPKTestAutotypeNormalization.m | 68 ------------ 7 files changed, 127 insertions(+), 79 deletions(-) create mode 100644 MacPassTests/KPKTestAutotype.m delete mode 100644 MacPassTests/KPKTestAutotypeNormalization.m diff --git a/MacPass.xcodeproj/project.pbxproj b/MacPass.xcodeproj/project.pbxproj index fb0bb6a4..45d83955 100644 --- a/MacPass.xcodeproj/project.pbxproj +++ b/MacPass.xcodeproj/project.pbxproj @@ -138,7 +138,7 @@ 4C586FA216D07F6A00E7DB57 /* 02_MessageBoxWarningTemplate.pdf in Resources */ = {isa = PBXBuildFile; fileRef = 4C586FA116D07F6A00E7DB57 /* 02_MessageBoxWarningTemplate.pdf */; }; 4C58BD4F176370B100B8178C /* HNHBadgedTextField.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C58BD4E176370B100B8178C /* HNHBadgedTextField.m */; }; 4C591B57178F897A0080B16B /* KPKBinary.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C591B56178F897A0080B16B /* KPKBinary.m */; }; - 4C59745118B3CE7200C8EBD1 /* KPKTestAutotypeNormalization.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C59745018B3CE7200C8EBD1 /* KPKTestAutotypeNormalization.m */; }; + 4C59745118B3CE7200C8EBD1 /* KPKTestAutotype.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C59745018B3CE7200C8EBD1 /* KPKTestAutotype.m */; }; 4C5A11FE1708DE8700223D8A /* MPPasswordCreatorViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C5A11FC1708DE8700223D8A /* MPPasswordCreatorViewController.m */; }; 4C5AA591179549A1008ECAD7 /* KPKXmlTreeWriter.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C5AA590179549A1008ECAD7 /* KPKXmlTreeWriter.m */; }; 4C5CD35517D15DBD000B7F38 /* NSData+Gzip.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C5CD34A17D15DBD000B7F38 /* NSData+Gzip.m */; }; @@ -615,7 +615,7 @@ 4C58BD4E176370B100B8178C /* HNHBadgedTextField.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HNHBadgedTextField.m; sourceTree = ""; }; 4C591B55178F897A0080B16B /* KPKBinary.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KPKBinary.h; sourceTree = ""; }; 4C591B56178F897A0080B16B /* KPKBinary.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KPKBinary.m; sourceTree = ""; }; - 4C59745018B3CE7200C8EBD1 /* KPKTestAutotypeNormalization.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KPKTestAutotypeNormalization.m; sourceTree = ""; }; + 4C59745018B3CE7200C8EBD1 /* KPKTestAutotype.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KPKTestAutotype.m; sourceTree = ""; }; 4C5A11FB1708DE8700223D8A /* MPPasswordCreatorViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPPasswordCreatorViewController.h; sourceTree = ""; }; 4C5A11FC1708DE8700223D8A /* MPPasswordCreatorViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPPasswordCreatorViewController.m; sourceTree = ""; }; 4C5AA58F179549A1008ECAD7 /* KPKXmlTreeWriter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KPKXmlTreeWriter.h; sourceTree = ""; }; @@ -1453,7 +1453,7 @@ 4CBA561517C2EA4900CE13D3 /* KPKTestXmlWriting.m */, 4C473A7E18AFD6340073FD2E /* KPKTestReference.m */, 4CD25F9218B17A17006098E9 /* KPKTestUUIDAdditions.m */, - 4C59745018B3CE7200C8EBD1 /* KPKTestAutotypeNormalization.m */, + 4C59745018B3CE7200C8EBD1 /* KPKTestAutotype.m */, ); path = MacPassTests; sourceTree = ""; @@ -2315,7 +2315,7 @@ 4C6366AF17AF207600AAF17D /* KPKTestHexColor.m in Sources */, 4CE76DAD17B3AD010043B82B /* KPKHashedDataTest.m in Sources */, 4CEAF85B17BA9B44001307A6 /* KPKTestKeyfileParsing.m in Sources */, - 4C59745118B3CE7200C8EBD1 /* KPKTestAutotypeNormalization.m in Sources */, + 4C59745118B3CE7200C8EBD1 /* KPKTestAutotype.m in Sources */, 4C6FDD2117BC4F4C004AEEC8 /* KPKTestPlaceholder.m in Sources */, 4CBA561617C2EA4900CE13D3 /* KPKTestXmlWriting.m in Sources */, ); diff --git a/MacPass/MPAutotypeCommand.m b/MacPass/MPAutotypeCommand.m index 27adfa7c..1767d7db 100644 --- a/MacPass/MPAutotypeCommand.m +++ b/MacPass/MPAutotypeCommand.m @@ -319,10 +319,10 @@ CGEventRef pressKey = CGEventCreateKeyboardEvent (eventSource, keyCode, YES); CGEventRef releaseKey = CGEventCreateKeyboardEvent (eventSource, keyCode, NO); - /* The modifer Masks might be set, reset them */ - CGEventSetFlags(pressKey,0); - CGEventSetFlags(releaseKey, 0); - /* Set the modifiers to the ones we want */ + /* + Set the modifiers to the ones we want + We use our private event source so no modifier reset should be needed + */ CGEventSetFlags(pressKey,flags); CGEventSetFlags(releaseKey, flags); diff --git a/MacPass/MPAutotypePaste.h b/MacPass/MPAutotypePaste.h index c36097c1..299e9e72 100644 --- a/MacPass/MPAutotypePaste.h +++ b/MacPass/MPAutotypePaste.h @@ -13,6 +13,8 @@ */ @interface MPAutotypePaste : MPAutotypeCommand +@property (readonly, copy) NSString *pasteData; + - (instancetype)initWithString:(NSString *)aString; - (void)appendString:(NSString *)aString; diff --git a/MacPass/MPAutotypePaste.m b/MacPass/MPAutotypePaste.m index 4d89d0c6..d90e6f0b 100644 --- a/MacPass/MPAutotypePaste.m +++ b/MacPass/MPAutotypePaste.m @@ -13,7 +13,7 @@ @interface MPAutotypePaste () -@property (strong) NSString *pasteData; +@property (copy) NSString *pasteData; @end @@ -35,7 +35,6 @@ self.pasteData = [self.pasteData stringByAppendingString:aString]; } - - (void)execute { if([self.pasteData length] > 0) { MPPasteBoardController *controller = [MPPasteBoardController defaultController]; diff --git a/MacPass/MacPass-Info.plist b/MacPass/MacPass-Info.plist index 453848a8..92ebf48e 100644 --- a/MacPass/MacPass-Info.plist +++ b/MacPass/MacPass-Info.plist @@ -62,7 +62,7 @@ CFBundleSignature ???? CFBundleVersion - 2548 + 2550 LSMinimumSystemVersion ${MACOSX_DEPLOYMENT_TARGET} NSHumanReadableCopyright diff --git a/MacPassTests/KPKTestAutotype.m b/MacPassTests/KPKTestAutotype.m new file mode 100644 index 00000000..4cc91234 --- /dev/null +++ b/MacPassTests/KPKTestAutotype.m @@ -0,0 +1,115 @@ +// +// KPKTestAutotypeNormalization.m +// MacPass +// +// Created by Michael Starke on 18.02.14. +// Copyright (c) 2014 HicknHack Software GmbH. All rights reserved. +// + +#import +#import + +#import "NSString+Commands.h" +#import "MPAutotypeCommand.h" +#import "MPAutotypeContext.h" +#import "MPAutotypePaste.h" +#import "MPAutotypeKeyPress.h" + +#import "MPKeyMapper.h" + +#import "KPKEntry.h" + +@interface KPKTestAutotype : XCTestCase + +@end + +@implementation KPKTestAutotype + +- (void)testSimpleNormalization { + NSString *normalized = [@"Whoo %{%}{^}{SHIFT}+ {SPACE}{ENTER}^V%V~T" normalizedAutotypeSequence]; + XCTAssertTrue([normalized isEqualToString:@"Whoo{SPACE}{ALT}{%}{^}{SHIFT}{SHIFT}{SPACE}{SPACE}{ENTER}{CONTROL}V{ALT}V{ENTER}T"]); +} + +- (void)testCommandRepetition { + NSString *normalized = [@"Whoo %{% 2}{^}{SHIFT 5}+ {SPACE}{ENTER}^V%V~T" normalizedAutotypeSequence]; + XCTAssertTrue([normalized isEqualToString:@"Whoo{SPACE}{ALT}{%}{%}{^}{SHIFT}{SHIFT}{SHIFT}{SHIFT}{SHIFT}{SHIFT}{SPACE}{SPACE}{ENTER}{CONTROL}V{ALT}V{ENTER}T"]); + normalized = [@"{TAB 5}TAB{TAB}{SHIFT}{SHIFT 10}ENTER{ENTER}{%%}" normalizedAutotypeSequence]; + XCTAssertTrue([normalized isEqualToString:@"{TAB}{TAB}{TAB}{TAB}{TAB}TAB{TAB}{SHIFT}{SHIFT}{SHIFT}{SHIFT}{SHIFT}{SHIFT}{SHIFT}{SHIFT}{SHIFT}{SHIFT}{SHIFT}ENTER{ENTER}{%%}"]); +} + +- (void)testeBracketValidation { + XCTAssertFalse([@"{BOOO}NO-COMMAND{TAB}{WHOO}{WHOO}{SPACE}!!!thisIsFun{{MISMATCH!!!}" validateCommmand]); + XCTAssertFalse([@"{{}}}}" validateCommmand]); + XCTAssertFalse([@"{}{}{{{}{{{{{{}}" validateCommmand]); + XCTAssertTrue([@"{}{}{}{}{}{ }ThisIsValid{}{STOP}" validateCommmand]); +} + +- (void)testCommandCreation { + KPKEntry *entry = [[KPKEntry alloc] init]; + entry.title = @"Title"; + entry.url = @"www.myurl.com"; + entry.username = @"{{User{name}}}"; + entry.password = @"Pass{word}"; + + /* Command 1 */ + MPAutotypeContext *context = [[MPAutotypeContext alloc] initWithEntry:entry andSequence:@"{USERNAME}{TAB}{PASSWORD}{ENTER}"]; + NSArray *commands = [MPAutotypeCommand commandsForContext:context]; + + XCTAssertTrue(commands.count == 4); + XCTAssertTrue([commands[0] isKindOfClass:[MPAutotypePaste class]]); + XCTAssertTrue([commands[1] isKindOfClass:[MPAutotypeKeyPress class]]); + XCTAssertTrue([commands[2] isKindOfClass:[MPAutotypePaste class]]); + XCTAssertTrue([commands[3] isKindOfClass:[MPAutotypeKeyPress class]]); + + /* {USERNAME} */ + MPAutotypePaste *paste = commands[0]; + XCTAssertTrue([paste.pasteData isEqualToString:entry.username]); + + /* {TAB} */ + MPAutotypeKeyPress *keyPress = commands[1]; + XCTAssertTrue(keyPress.keyCode == kVK_Tab); // Tab is a fixed key, no mapping needed + XCTAssertTrue(keyPress.modifierMask == 0); + + /* {PASSWORD} */ + paste = commands[2]; + XCTAssertTrue([entry.password isEqualToString:paste.pasteData]); + + /* {ENTER} */ + keyPress = commands[3]; + XCTAssertTrue(keyPress.keyCode = kVK_Return); + + /* Command 2 */ + context = [[MPAutotypeContext alloc] initWithEntry:entry andSequence:@"^T{USERNAME}%+^{TAB}Whoo{PASSWORD}{ENTER}"]; + commands = [MPAutotypeCommand commandsForContext:context]; + XCTAssertTrue(commands.count == 5); + XCTAssertTrue([commands[0] isKindOfClass:[MPAutotypeKeyPress class]]); + XCTAssertTrue([commands[1] isKindOfClass:[MPAutotypePaste class]]); + XCTAssertTrue([commands[2] isKindOfClass:[MPAutotypeKeyPress class]]); + XCTAssertTrue([commands[3] isKindOfClass:[MPAutotypePaste class]]); + XCTAssertTrue([commands[4] isKindOfClass:[MPAutotypeKeyPress class]]); + + /* ^T */ + keyPress = commands[0]; + /* Lower case is ok, since we only need the key, not the sequence to reproduce the string */ + XCTAssertTrue([@"t" isEqualToString:[MPKeyMapper stringForKey:keyPress.keyCode]]); + XCTAssertTrue(keyPress.modifierMask == kCGEventFlagMaskCommand); + + /* {USERNAME} */ + paste = commands[1]; + XCTAssertTrue([paste.pasteData isEqualToString:entry.username]); + + /* %+^{TAB} */ + keyPress = commands[2]; + XCTAssertTrue(keyPress.keyCode == kVK_Tab); // Tab is a fixed key, no mapping needed + XCTAssertTrue(keyPress.modifierMask = kCGEventFlagMaskCommand | kCGEventFlagMaskAlphaShift | kCGEventFlagMaskAlternate); + + /* Whoo{PASSWORD} */ + paste = commands[3]; + NSString *pasteString = [[NSString alloc] initWithFormat:@"%@%@", @"Whoo", entry.password]; + XCTAssertTrue([pasteString isEqualToString:paste.pasteData]); + + /* {ENTER} */ + keyPress = commands[4]; + XCTAssertTrue(keyPress.keyCode = kVK_Return); +} +@end diff --git a/MacPassTests/KPKTestAutotypeNormalization.m b/MacPassTests/KPKTestAutotypeNormalization.m deleted file mode 100644 index 294f60f5..00000000 --- a/MacPassTests/KPKTestAutotypeNormalization.m +++ /dev/null @@ -1,68 +0,0 @@ -// -// KPKTestAutotypeNormalization.m -// MacPass -// -// Created by Michael Starke on 18.02.14. -// Copyright (c) 2014 HicknHack Software GmbH. All rights reserved. -// - -#import -#import "NSString+Commands.h" -#import "MPAutotypeCommand.h" -#import "MPAutotypeContext.h" -#import "MPAutotypePaste.h" -#import "MPAutotypeKeyPress.h" - -#import "KPKEntry.h" - -@interface KPKTestAutotypeNormalization : XCTestCase - -@end - -@implementation KPKTestAutotypeNormalization - -- (void)testSimpleNormalization { - NSString *normalized = [@"Whoo %{%}{^}{SHIFT}+ {SPACE}{ENTER}^V%V~T" normalizedAutotypeSequence]; - XCTAssertTrue([normalized isEqualToString:@"Whoo{SPACE}{ALT}{%}{^}{SHIFT}{SHIFT}{SPACE}{SPACE}{ENTER}{CONTROL}V{ALT}V{ENTER}T"]); -} - -- (void)testCommandRepetition { - NSString *normalized = [@"Whoo %{% 2}{^}{SHIFT 5}+ {SPACE}{ENTER}^V%V~T" normalizedAutotypeSequence]; - XCTAssertTrue([normalized isEqualToString:@"Whoo{SPACE}{ALT}{%}{%}{^}{SHIFT}{SHIFT}{SHIFT}{SHIFT}{SHIFT}{SHIFT}{SPACE}{SPACE}{ENTER}{CONTROL}V{ALT}V{ENTER}T"]); - normalized = [@"{TAB 5}TAB{TAB}{SHIFT}{SHIFT 10}ENTER{ENTER}{%%}" normalizedAutotypeSequence]; - XCTAssertTrue([normalized isEqualToString:@"{TAB}{TAB}{TAB}{TAB}{TAB}TAB{TAB}{SHIFT}{SHIFT}{SHIFT}{SHIFT}{SHIFT}{SHIFT}{SHIFT}{SHIFT}{SHIFT}{SHIFT}{SHIFT}ENTER{ENTER}{%%}"]); -} - -- (void)testeBracketValidation { - XCTAssertFalse([@"{BOOO}NO-COMMAND{TAB}{WHOO}{WHOO}{SPACE}!!!thisIsFun{{MISMATCH!!!}" validateCommmand]); - XCTAssertFalse([@"{{}}}}" validateCommmand]); - XCTAssertFalse([@"{}{}{{{}{{{{{{}}" validateCommmand]); - XCTAssertTrue([@"{}{}{}{}{}{ }ThisIsValid{}{STOP}" validateCommmand]); -} - -- (void)testCommandCreation { - KPKEntry *entry = [[KPKEntry alloc] init]; - entry.title = @"Title"; - entry.url = @"www.myurl.com"; - entry.username = @"{{User{name}}}"; - entry.password = @"Pass{word}"; - - MPAutotypeContext *context = [[MPAutotypeContext alloc] initWithEntry:entry andSequence:@"{USERNAME}{TAB}{PASSWORD}{ENTER}"]; - NSArray *commands = [MPAutotypeCommand commandsForContext:context]; - - XCTAssert([commands count] == 4); - XCTAssert([commands[0] isKindOfClass:[MPAutotypePaste class]]); - XCTAssert([commands[1] isKindOfClass:[MPAutotypeKeyPress class]]); - XCTAssert([commands[2] isKindOfClass:[MPAutotypePaste class]]); - XCTAssert([commands[3] isKindOfClass:[MPAutotypeKeyPress class]]); - - context = [[MPAutotypeContext alloc] initWithEntry:entry andSequence:@"{DELAY=5}{VKEY-NX 1}{VKEY-EX 200}{DELAY 5}{VKEY 300}"]; - commands = [MPAutotypeCommand commandsForContext:context]; - - context = [[MPAutotypeContext alloc] initWithEntry:entry andSequence:@"^T{USERNAME}%+^{TAB}Whoo{PASSWORD}{ENTER}"]; - commands = [MPAutotypeCommand commandsForContext:context]; - - - //XCTAssert([commands count] == 5); -} -@end