1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Diagnostics ;
4+ using System . Text ;
5+
6+ using Xunit ;
7+ using KeePassLib ;
8+ using KeePassLib . Keys ;
9+ using KeePassLib . Security ;
10+ using KeePassLib . Serialization ;
11+ using KeePassLib . Utility ;
12+ using PassXYZLib ;
13+
14+ namespace KPCLib . xunit
15+ {
16+ public class PassXYZLibFixture : IDisposable
17+ {
18+ const string TEST_DB = "pass_d_E8f4pEk.xyz" ;
19+ const string TEST_DB_KEY = "12345" ;
20+
21+ public PassXYZLibFixture ( )
22+ {
23+ Logger = new KPCLibLogger ( ) ;
24+ PxDb = new PxDatabase ( ) ;
25+ IOConnectionInfo ioc = IOConnectionInfo . FromPath ( TEST_DB ) ;
26+ CompositeKey cmpKey = new CompositeKey ( ) ;
27+ cmpKey . AddUserKey ( new KcpPassword ( TEST_DB_KEY ) ) ;
28+ PxDb . Open ( ioc , cmpKey , Logger ) ;
29+ }
30+
31+ public void Dispose ( )
32+ {
33+ PxDb . Close ( ) ;
34+ }
35+
36+ public PxDatabase PxDb { get ; private set ; }
37+ public KPCLibLogger Logger { get ; private set ; }
38+
39+ public string Username {
40+ get { return PxDefs . GetUserNameFromDataFile ( TEST_DB ) ; }
41+ }
42+ }
43+
44+ [ CollectionDefinition ( "PassXYZLib collection" ) ]
45+ public class PassXYZLibCollection : ICollectionFixture < PassXYZLibFixture >
46+ {
47+ // This class has no code, and is never created. Its purpose is simply
48+ // to be the place to apply [CollectionDefinition] and all the
49+ // ICollectionFixture<> interfaces.
50+ }
51+
52+ [ Collection ( "PassXYZLib collection" ) ]
53+ public class PassXYZLibTests
54+ {
55+ PassXYZLibFixture passxyz ;
56+
57+ public PassXYZLibTests ( PassXYZLibFixture passXYZLibFixture )
58+ {
59+ this . passxyz = passXYZLibFixture ;
60+ }
61+
62+ [ Fact ]
63+ public void IsOpenDbTest ( )
64+ {
65+ Debug . WriteLine ( $ "{ passxyz . PxDb } , Username is { passxyz . Username } .") ;
66+ Assert . True ( ( passxyz . PxDb . IsOpen ) ) ;
67+ }
68+
69+ [ Fact ]
70+ public void CurrentGroupTests ( )
71+ {
72+ var currentGroup = passxyz . PxDb . CurrentGroup ;
73+ Debug . WriteLine ( $ "{ currentGroup } ") ;
74+ Assert . NotNull ( currentGroup ) ;
75+ }
76+
77+ [ Fact ]
78+ public void CurrentPathTests ( )
79+ {
80+ var currentPath = passxyz . PxDb . CurrentPath ;
81+ Debug . WriteLine ( $ "Current path is { currentPath } .") ;
82+ Assert . NotNull ( currentPath ) ;
83+ }
84+
85+ [ Theory ]
86+ [ InlineData ( "/test1/WebDAV" ) ]
87+ /// <summary>
88+ /// Change a protect field
89+ /// </summary>
90+ /// <param name="path">Destination path. Must not be <c>null</c>.</param>
91+ public void ChangeProtectedFieldTests ( string path )
92+ {
93+ var entry = passxyz . PxDb . FindByPath < PwEntry > ( path ) ;
94+
95+ Debug . WriteLine ( $ "Current path is { entry } .") ;
96+ // Update the existing protected field
97+ PxDefs . UpdatePxEntry ( entry , PxDefs . PasswordField , "123456" , true ) ;
98+ Assert . True ( entry . Strings . Get ( PxDefs . FindEncodeKey ( entry . Strings , PxDefs . PasswordField ) ) . IsProtected ) ;
99+ // Add a new protected field "PIN"
100+ PxDefs . UpdatePxEntry ( entry , "PIN" , "1234" , true ) ;
101+ Assert . True ( entry . Strings . Get ( PxDefs . FindEncodeKey ( entry . Strings , "PIN" ) ) . IsProtected ) ;
102+ // Remove a field
103+ PxDefs . UpdatePxEntry ( entry , "002Email" , String . Empty , false ) ;
104+ foreach ( KeyValuePair < string , ProtectedString > kvp in entry . Strings )
105+ {
106+ Debug . WriteLine ( $ " { kvp . Key } ={ kvp . Value . ReadString ( ) } , IsProtected={ kvp . Value . IsProtected } ") ;
107+ }
108+
109+ }
110+
111+ // The end of PassXYZLibTests
112+ }
113+
114+ }
0 commit comments