diff --git a/ChangeLog.md b/ChangeLog.md index cc49f51..1559e4b 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -3,6 +3,8 @@ KeePass databases change log ## ?.?.? / ????-??-?? +* Added path accessor to groups and entries - @thekid + ## 0.3.0 / 2016-07-16 * Fixed issue #1: HHVM XML parsing - @thekid diff --git a/src/main/php/info/keepass/Entry.class.php b/src/main/php/info/keepass/Entry.class.php index 820bf89..baf40eb 100755 --- a/src/main/php/info/keepass/Entry.class.php +++ b/src/main/php/info/keepass/Entry.class.php @@ -3,6 +3,11 @@ use util\UUID; use lang\ElementNotFoundException; +/** + * A password entry + * + * @test xp://info.keepass.unittest.EntryTest + */ class Entry extends Object { /** @return util.UUID */ diff --git a/src/main/php/info/keepass/Group.class.php b/src/main/php/info/keepass/Group.class.php index 74e2fff..c7a58c7 100755 --- a/src/main/php/info/keepass/Group.class.php +++ b/src/main/php/info/keepass/Group.class.php @@ -3,6 +3,11 @@ use util\UUID; use lang\ElementNotFoundException; +/** + * A group + * + * @test xp://info.keepass.unittest.GroupTest + */ class Group extends Object { /** @return util.UUID */ @@ -39,7 +44,7 @@ public function password($title) { public function passwords() { if (isset($this->backing['Entry'])) { foreach ($this->backing['Entry'] as $uuid => $entry) { - yield $entry['String']['Title'] => $entry['String']['Password']; + yield $this->path.$entry['String']['Title'] => $entry['String']['Password']; } } } @@ -52,7 +57,7 @@ public function passwords() { public function groups() { if (isset($this->backing['Group'])) { foreach ($this->backing['Group'] as $uuid => $group) { - yield $this->decodeUUID($uuid) => new Group($group); + yield $this->decodeUUID($uuid) => new Group($group, $this->path.$group['Name']); } } } @@ -65,7 +70,7 @@ public function groups() { public function entries() { if (isset($this->backing['Entry'])) { foreach ($this->backing['Entry'] as $uuid => $entry) { - yield $this->decodeUUID($uuid) => new Entry($entry); + yield $this->decodeUUID($uuid) => new Entry($entry, $this->path.$entry['String']['Title']); } } } diff --git a/src/main/php/info/keepass/KeePassDatabase.class.php b/src/main/php/info/keepass/KeePassDatabase.class.php index a7217cd..b00229d 100755 --- a/src/main/php/info/keepass/KeePassDatabase.class.php +++ b/src/main/php/info/keepass/KeePassDatabase.class.php @@ -87,7 +87,7 @@ public function group($path) { } } - return new Group($structure); + return new Group($structure, $path); } /** diff --git a/src/main/php/info/keepass/Object.class.php b/src/main/php/info/keepass/Object.class.php index 718f08f..416b26f 100755 --- a/src/main/php/info/keepass/Object.class.php +++ b/src/main/php/info/keepass/Object.class.php @@ -1,17 +1,21 @@ backing= $backing; + $this->path= rtrim($path, '/').'/'; } + /** @return string */ + public function path() { return '/' === $this->path ? '/' : substr($this->path, 0, -1); } + /** * Decodes a UUID * diff --git a/src/test/php/info/keepass/unittest/DatabaseWithEntriesInRootTest.class.php b/src/test/php/info/keepass/unittest/DatabaseWithEntriesInRootTest.class.php index 3f397e7..6e2c442 100755 --- a/src/test/php/info/keepass/unittest/DatabaseWithEntriesInRootTest.class.php +++ b/src/test/php/info/keepass/unittest/DatabaseWithEntriesInRootTest.class.php @@ -57,8 +57,8 @@ public function entries_in_root() { with ($this->database(), function($db) { $this->assertEquals( [ - self::ID_ONE => new Entry($this->entries[self::ID_ONE]), - self::ID_TWO => new Entry($this->entries[self::ID_TWO]) + self::ID_ONE => new Entry($this->entries[self::ID_ONE], '/Entry #1'), + self::ID_TWO => new Entry($this->entries[self::ID_TWO], '/Entry #2') ], iterator_to_array($db->group('/')->entries()) ); @@ -70,8 +70,8 @@ public function all_passwords_in_root() { with ($this->database(), function($db) { $this->assertEquals( [ - 'Entry #1' => $this->entries[self::ID_ONE]['String']['Password'], - 'Entry #2' => $this->entries[self::ID_TWO]['String']['Password'] + '/Entry #1' => $this->entries[self::ID_ONE]['String']['Password'], + '/Entry #2' => $this->entries[self::ID_TWO]['String']['Password'] ], iterator_to_array($db->passwords('/')) ); diff --git a/src/test/php/info/keepass/unittest/DatabaseWithOneGroupTest.class.php b/src/test/php/info/keepass/unittest/DatabaseWithOneGroupTest.class.php index 4a2f43f..db37530 100755 --- a/src/test/php/info/keepass/unittest/DatabaseWithOneGroupTest.class.php +++ b/src/test/php/info/keepass/unittest/DatabaseWithOneGroupTest.class.php @@ -52,7 +52,7 @@ public function setUp() { #[@test] public function test_group() { with ($this->database(), function($db) { - $this->assertEquals(new Group($this->group), $db->group('/Test')); + $this->assertEquals(new Group($this->group, '/Test'), $db->group('/Test')); }); } @@ -66,7 +66,7 @@ public function test_password() { #[@test] public function all_passwords_in_test_group() { with ($this->database(), function($db) { - $this->assertEquals(['Test' => $this->entry['String']['Password']], iterator_to_array($db->passwords('/Test'))); + $this->assertEquals(['/Test/Test' => $this->entry['String']['Password']], iterator_to_array($db->passwords('/Test'))); }); } @@ -87,7 +87,7 @@ public function password_in_non_existant_folder() { #[@test] public function groups_in_root() { with ($this->database(), function($db) { - $this->assertEquals([self::GROUPID => new Group($this->group)], iterator_to_array($db->groups())); + $this->assertEquals([self::GROUPID => new Group($this->group, '/Test')], iterator_to_array($db->groups())); }); } @@ -101,7 +101,7 @@ public function subgroups_of_test_group_are_empty() { #[@test] public function entries_in_test_group() { with ($this->database(), function($db) { - $this->assertEquals([self::ENTRYID => new Entry($this->entry)], iterator_to_array($db->group('/Test')->entries())); + $this->assertEquals([self::ENTRYID => new Entry($this->entry, '/Test/Test')], iterator_to_array($db->group('/Test')->entries())); }); } diff --git a/src/test/php/info/keepass/unittest/EmptyDatabaseTest.class.php b/src/test/php/info/keepass/unittest/EmptyDatabaseTest.class.php index 022daf9..b6eaa94 100755 --- a/src/test/php/info/keepass/unittest/EmptyDatabaseTest.class.php +++ b/src/test/php/info/keepass/unittest/EmptyDatabaseTest.class.php @@ -25,7 +25,7 @@ public function setUp() { #[@test] public function root_group() { with ($this->database(), function($db) { - $this->assertEquals(new Group($this->root), $db->group('/')); + $this->assertEquals(new Group($this->root, '/'), $db->group('/')); }); } diff --git a/src/test/php/info/keepass/unittest/EntryTest.class.php b/src/test/php/info/keepass/unittest/EntryTest.class.php new file mode 100755 index 0000000..0f63d08 --- /dev/null +++ b/src/test/php/info/keepass/unittest/EntryTest.class.php @@ -0,0 +1,77 @@ +entry= [ + 'UUID' => 'fZhlFzAGRU24qsKpoxQ2Lg==', + 'IconID' => '42', + 'ForegroundColor' => null, + 'BackgroundColor' => null, + 'OverrideURL' => null, + 'Tags' => null, + 'Times' => '', + 'String' => [ + 'Notes' => 'Notes', + 'Password' => new ProtectedValue("\323\$c", "\274J\006"), + 'Title' => 'Entry #1', + 'URL' => 'http://example.com/', + 'UserName' => 'test' + ], + 'History' => [ + ] + ]; + } + + #[@test] + public function can_create() { + new Entry($this->entry, '/Entry #1'); + } + + #[@test] + public function path() { + $this->assertEquals('/Entry #1', (new Entry($this->entry, '/Entry #1'))->path()); + } + + #[@test] + public function uuid() { + $this->assertEquals(new UUID('7d986517-3006-454d-b8aa-c2a9a314362e'), (new Entry($this->entry, '/Entry #1'))->uuid()); + } + + #[@test] + public function title() { + $this->assertEquals('Entry #1', (new Entry($this->entry, '/Entry #1'))->title()); + } + + #[@test] + public function notes() { + $this->assertEquals('Notes', (new Entry($this->entry, '/Entry #1'))->notes()); + } + + #[@test] + public function url() { + $this->assertEquals('http://example.com/', (new Entry($this->entry, '/Entry #1'))->url()); + } + + #[@test] + public function username() { + $this->assertEquals('test', (new Entry($this->entry, '/Entry #1'))->username()); + } + + #[@test] + public function password() { + $this->assertEquals(new ProtectedValue("\323\$c", "\274J\006"), (new Entry($this->entry, '/Entry #1'))->password()); + } + + #[@test] + public function icon_field() { + $this->assertEquals('42', (new Entry($this->entry, '/Entry #1'))->field('IconID')); + } +} + diff --git a/src/test/php/info/keepass/unittest/GroupTest.class.php b/src/test/php/info/keepass/unittest/GroupTest.class.php new file mode 100755 index 0000000..36569ea --- /dev/null +++ b/src/test/php/info/keepass/unittest/GroupTest.class.php @@ -0,0 +1,55 @@ +group= [ + 'UUID' => 'Vy28plCgw0u5Y0PtP2c/2Q==', + 'Name' => 'Test', + 'Notes' => 'Notes', + 'IconID' => '48', + 'Times' => '', + 'IsExpanded' => 'True', + 'DefaultAutoTypeSequence' => null, + 'EnableAutoType' => 'null', + 'EnableSearching' => 'null', + 'LastTopVisibleEntry' => 'AAAAAAAAAAAAAAAAAAAAAA==', + ]; + } + + #[@test] + public function can_create() { + new Group($this->group, '/Test'); + } + + #[@test] + public function path() { + $this->assertEquals('/Test', (new Group($this->group, '/Test'))->path()); + } + + #[@test] + public function uuid() { + $this->assertEquals(new UUID('572dbca6-50a0-c34b-b963-43ed3f673fd9'), (new Group($this->group, '/Test'))->uuid()); + } + + #[@test] + public function name() { + $this->assertEquals('Test', (new Group($this->group, '/Test'))->name()); + } + + #[@test] + public function notes() { + $this->assertEquals('Notes', (new Group($this->group, '/Test'))->notes()); + } + + #[@test] + public function icon_field() { + $this->assertEquals('48', (new Group($this->group, '/Test'))->field('IconID')); + } +} +