Skip to content

Commit fc2bd36

Browse files
committed
1.2.0.1 - Added IsParentGroup, MoveEntry, MoveGroup
1 parent 4abe92b commit fc2bd36

File tree

5 files changed

+213
-4
lines changed

5 files changed

+213
-4
lines changed

KPCLib.xunit/PxDatabaseTests.cs

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ public void DeleteEntryTests(bool permanent)
121121

122122
}
123123

124-
125124
[Fact]
126125
public void DeleteEmptyGroupTest()
127126
{
@@ -240,8 +239,143 @@ public void CurrentPathTests()
240239
Debug.WriteLine($"Current path is {passxyz.PxDb.CurrentPath}.");
241240
Assert.NotNull(passxyz.PxDb.CurrentPath);
242241
}
242+
243+
[Theory]
244+
[InlineData("/utdb")]
245+
[InlineData("/utdb/General/G1")]
246+
/// <summary>
247+
/// IsParentGroup test cases
248+
/// source: "/utdb/Windows/W1/W2/W3/W4/W5"
249+
/// destination: "/utdb/General/G1/G21"
250+
/// </summary>
251+
/// <param name="path">Source path. Must not be <c>null</c>.</param>
252+
public void IsParentGroupG1Tests(string path)
253+
{
254+
var dstPath = "/utdb/General/G1";
255+
var dstGroup = passxyz.PxDb.FindByPath<PwGroup>(dstPath);
256+
var srcGroup = passxyz.PxDb.FindByPath<PwGroup>(path);
257+
if (passxyz.PxDb.IsParentGroup(srcGroup, dstGroup))
258+
{
259+
Debug.WriteLine($"{path} is the parent of {dstPath}.");
260+
Assert.Equal("/utdb", path);
261+
}
262+
else
263+
{
264+
Debug.WriteLine($"{path} is not the parent of {dstPath}.");
265+
Assert.Equal("/utdb/General/G1", path);
266+
}
267+
}
268+
269+
[Theory]
270+
[InlineData("/utdb/Windows/W1")]
271+
[InlineData("/utdb/General")]
272+
/// <summary>
273+
/// IsParentGroup test cases
274+
/// source: "/utdb/Windows/W1/W2/W3/W4/W5"
275+
/// destination: "/utdb/General/G1/G21"
276+
/// </summary>
277+
/// <param name="path">Source path. Must not be <c>null</c>.</param>
278+
public void IsParentGroupG21Tests(string path)
279+
{
280+
var dstPath = "/utdb/General/G1/G21";
281+
var dstGroup = passxyz.PxDb.FindByPath<PwGroup>(dstPath);
282+
var srcGroup = passxyz.PxDb.FindByPath<PwGroup>(path);
283+
if(passxyz.PxDb.IsParentGroup(srcGroup, dstGroup))
284+
{
285+
Debug.WriteLine($"{path} is the parent of {dstPath}.");
286+
Assert.Equal("/utdb/General", path);
287+
}
288+
else
289+
{
290+
Debug.WriteLine($"{path} is not the parent of {dstPath}.");
291+
Assert.Equal("/utdb/Windows/W1", path);
292+
}
293+
}
294+
295+
[Theory]
296+
[InlineData("/utdb/General")]
297+
[InlineData("/utdb")]
298+
/// <summary>
299+
/// MoveEntry test cases
300+
/// Test case 1: srcEntry: "/utdb/General", dstGroup: "/utdb/General"
301+
/// Test case 2: srcEntry: "/utdb/TestEntry", dstGroup: "/utdb"
302+
/// </summary>
303+
/// <param name="path">Destination path. Must not be <c>null</c>.</param>
304+
public void MoveEntryTests(string path)
305+
{
306+
string srcPath;
307+
308+
if (path == "/utdb/General")
309+
{
310+
srcPath = "/utdb/General";
311+
}
312+
else
313+
{
314+
srcPath = "/utdb/TestEntry";
315+
}
316+
317+
var srcEntry = passxyz.PxDb.FindByPath<PwEntry>(srcPath);
318+
var dstGroup = passxyz.PxDb.FindByPath<PwGroup>(path);
319+
320+
if(passxyz.PxDb.MoveEntry(srcEntry, dstGroup))
321+
{
322+
Debug.WriteLine($"Moved entry {srcPath} to {path}.");
323+
Assert.Equal("/utdb/General", path);
324+
}
325+
else
326+
{
327+
Debug.WriteLine($"Cannot move {srcPath} to {path}.");
328+
Assert.Equal("/utdb", path);
329+
}
330+
}
331+
332+
[Theory]
333+
[InlineData("/utdb/Windows/W1/W2/W3/")]
334+
[InlineData("/utdb/General/G1/G21/")]
335+
[InlineData("/utdb/General/")]
336+
/// <summary>
337+
/// MoveGroup test cases
338+
/// Test case 1: srcGroup: "/utdb/Windows/W1/W2/W3/W4/W5/", dstGroup: "/utdb/Windows/W1/W2/W3/"
339+
/// Move sub-group to the parent group, this is a successful case
340+
/// Test case 2: srcGroup: "/utdb/General/", dstGroup: "/utdb/General/G1/G21/"
341+
/// Move parent group to the sub-group, this is a failure case
342+
/// Test case 2: srcGroup: "/utdb/General/", dstGroup: "/utdb/General/"
343+
/// Move group to the same location, this is a failure case
344+
/// </summary>
345+
/// <param name="path">Destination path. Must not be <c>null</c>.</param>
346+
public void MoveGroupTests(string path)
347+
{
348+
string srcPath;
349+
350+
if (path == "/utdb/General/")
351+
{
352+
srcPath = "/utdb/General/";
353+
}
354+
else if (path == "/utdb/General/G1/G21/")
355+
{
356+
srcPath = "/utdb/General/";
357+
}
358+
else
359+
{
360+
srcPath = "/utdb/Windows/W1/W2/W3/W4/W5/";
361+
}
362+
363+
var srcGroup = passxyz.PxDb.FindByPath<PwGroup>(srcPath);
364+
var dstGroup = passxyz.PxDb.FindByPath<PwGroup>(path);
365+
366+
if (passxyz.PxDb.MoveGroup(srcGroup, dstGroup))
367+
{
368+
Debug.WriteLine($"Moved entry {srcPath} to {path}.");
369+
Assert.Equal("/utdb/Windows/W1/W2/W3/", path);
370+
}
371+
else
372+
{
373+
Debug.WriteLine($"Cannot move {srcPath} to {path}.");
374+
}
375+
}
243376
}
244377

378+
245379
public class PxLibInfoTests
246380
{
247381
[Fact]

KPCLib.xunit/utdb.kdbx

432 Bytes
Binary file not shown.

KPCLib/KPCLib.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
1.1.6 - KPCLib has been tested on all three platforms (Android, iOS and UWP).</PackageReleaseNotes>
2424
<NeutralLanguage>en-US</NeutralLanguage>
25+
<AssemblyVersion>1.2.0.1</AssemblyVersion>
26+
<FileVersion>1.2.0.1</FileVersion>
2527
</PropertyGroup>
2628

2729
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

PassXYZLib/PassXYZLib.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<Version>1.2.0</Version>
6-
<AssemblyVersion>1.2.0.0</AssemblyVersion>
7-
<FileVersion>1.2.0.0</FileVersion>
6+
<AssemblyVersion>1.2.0.1</AssemblyVersion>
7+
<FileVersion>1.2.0.1</FileVersion>
88
<Company>PassXYZ Inc.</Company>
99
<Authors>Roger Ye</Authors>
1010
<Copyright>Roger Ye</Copyright>
11-
<PackageReleaseNotes>1.2.0 - Updated to KeePassLib 2.4.7. Enhanced KeePassLib</PackageReleaseNotes>
11+
<PackageReleaseNotes>1.2.0.0 - Updated to KeePassLib 2.4.7. Enhanced KeePassLib
12+
1.2.0.1 - Added IsParentGroup, MoveEntry, MoveGroup</PackageReleaseNotes>
1213
<PackageProjectUrl>https://github.com/passxyz/KPCLib</PackageProjectUrl>
1314
<RepositoryUrl>https://github.com/passxyz/KPCLib</RepositoryUrl>
1415
<NeutralLanguage>en-US</NeutralLanguage>

PassXYZLib/PxDatabase.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,5 +399,77 @@ public void DeleteEntry(PwEntry pe, bool permanent = false)
399399
pe.Touch(false);
400400
}
401401
}
402+
403+
/// <summary>
404+
/// Check whether the source group is the parent of destination group
405+
/// </summary>
406+
/// <param name="srcGroup">The entry to be moved. Must not be <c>null</c>.</param>
407+
/// <param name="dstGroup">New group for the entry</param>
408+
/// <returns>Success or failure.</returns>
409+
public bool IsParentGroup(PwGroup srcGroup, PwGroup dstGroup)
410+
{
411+
if (srcGroup == null) throw new ArgumentNullException("srcGroup");
412+
if (dstGroup == null) throw new ArgumentNullException("dstGroup");
413+
414+
// If the source group is the root group, return true.
415+
if (srcGroup.Uuid == this.RootGroup.Uuid) return true;
416+
417+
PwGroup group = dstGroup.ParentGroup;
418+
while (this.RootGroup.Uuid != group.Uuid)
419+
{
420+
if (group.Uuid == srcGroup.Uuid) return true;
421+
group = group.ParentGroup;
422+
}
423+
return false;
424+
}
425+
426+
/// <summary>
427+
/// Move an entry to a new location.
428+
/// </summary>
429+
/// <param name="pe">The entry to be moved. Must not be <c>null</c>.</param>
430+
/// <param name="group">New group for the entry</param>
431+
/// <returns>Success or failure.</returns>
432+
public bool MoveEntry(PwEntry pe, PwGroup group)
433+
{
434+
if (pe == null) throw new ArgumentNullException("pe");
435+
if (group == null) throw new ArgumentNullException("group");
436+
437+
PwGroup pgParent = pe.ParentGroup;
438+
if (pgParent == group) return false;
439+
440+
if (pgParent != null) // Remove from parent
441+
{
442+
if (!pgParent.Entries.Remove(pe)) { Debug.Assert(false); }
443+
}
444+
group.AddEntry(pe, true, true);
445+
return true;
446+
}
447+
448+
/// <summary>
449+
/// Move a group to a new location.
450+
/// The source group cannot be the parent of destination group
451+
/// </summary>
452+
/// <param name="srcGroup">The entry to be moved. Must not be <c>null</c>.</param>
453+
/// <param name="dstGroup">New group for the entry</param>
454+
/// <returns>Success or failure.</returns>
455+
public bool MoveGroup(PwGroup srcGroup, PwGroup dstGroup)
456+
{
457+
if (srcGroup == null) throw new ArgumentNullException("srcGroup");
458+
if (dstGroup == null) throw new ArgumentNullException("dstGroup");
459+
460+
if (srcGroup == dstGroup) return false;
461+
462+
PwGroup pgParent = srcGroup.ParentGroup;
463+
if (pgParent == dstGroup) return false;
464+
465+
if (IsParentGroup(srcGroup, dstGroup)) return false;
466+
467+
if (pgParent != null) // Remove from parent
468+
{
469+
if (!pgParent.Groups.Remove(srcGroup)) { Debug.Assert(false); }
470+
}
471+
dstGroup.AddGroup(srcGroup, true, true);
472+
return true;
473+
}
402474
}
403475
}

0 commit comments

Comments
 (0)