2013-08-06 16:21:58 -04:00
using System ;
using System.IO ;
using System.Linq ;
2013-06-25 15:27:41 -04:00
using Android.App ;
2014-11-11 11:19:22 -05:00
using Com.Keepassdroid.Database.Load ;
using Java.IO ;
2014-01-26 06:51:55 -05:00
using KeePassLib ;
2014-11-11 11:19:22 -05:00
using KeePassLib.Keys ;
2013-06-25 15:27:41 -04:00
using KeePassLib.Serialization ;
using Microsoft.VisualStudio.TestTools.UnitTesting ;
using keepass2android ;
2013-08-06 16:21:58 -04:00
using keepass2android.Io ;
2014-11-11 11:19:22 -05:00
using FileNotFoundException = System . IO . FileNotFoundException ;
2013-06-25 15:27:41 -04:00
namespace Kp2aUnitTests
{
[TestClass]
2013-07-12 10:39:55 -04:00
internal partial class TestLoadDb : TestBase
2013-06-25 15:27:41 -04:00
{
private void RunLoadTest ( string filenameWithoutDir , string password , string keyfile )
{
2014-01-26 06:51:55 -05:00
var app = PerformLoad ( filenameWithoutDir , password , keyfile ) ;
Assert . AreEqual ( 6 , app . GetDb ( ) . KpDatabase . RootGroup . Groups . Count ( ) ) ;
Assert . AreEqual ( 2 , app . GetDb ( ) . KpDatabase . RootGroup . Entries . Count ( ) ) ;
}
private IKp2aApp PerformLoad ( string filenameWithoutDir , string password , string keyfile )
{
Android . Util . Log . Debug ( "KP2ATest" , "Starting for " + filenameWithoutDir + " with " + password + "/" + keyfile ) ;
2013-06-25 15:27:41 -04:00
IKp2aApp app = new TestKp2aApp ( ) ;
app . CreateNewDatabase ( ) ;
bool loadSuccesful = false ;
2013-12-12 04:24:24 -05:00
var key = CreateKey ( password , keyfile ) ;
2014-01-25 22:38:12 -05:00
string loadErrorMessage = "" ;
2013-12-12 04:24:24 -05:00
2014-01-26 06:51:55 -05:00
LoadDb task = new LoadDb ( app , new IOConnectionInfo { Path = TestDbDirectory + filenameWithoutDir } , null ,
key , keyfile , new ActionOnFinish ( ( success , message ) = >
{
loadErrorMessage = message ;
if ( ! success )
Android . Util . Log . Debug ( "KP2ATest" , "error loading db: " + message ) ;
loadSuccesful = success ;
} )
2013-06-25 15:27:41 -04:00
) ;
2013-07-09 03:59:17 -04:00
ProgressTask pt = new ProgressTask ( app , Application . Context , task ) ;
2013-06-25 15:27:41 -04:00
Android . Util . Log . Debug ( "KP2ATest" , "Running ProgressTask" ) ;
pt . Run ( ) ;
pt . JoinWorkerThread ( ) ;
Android . Util . Log . Debug ( "KP2ATest" , "PT.run finished" ) ;
2014-01-26 06:51:55 -05:00
Assert . IsTrue ( loadSuccesful , "didn't succesfully load database :-( " + loadErrorMessage ) ;
return app ;
2013-06-25 15:27:41 -04:00
}
[TestMethod]
public void TestLoadWithPasswordOnly ( )
{
RunLoadTest ( "passwordonly.kdbx" , DefaultPassword , "" ) ;
2014-01-26 06:51:55 -05:00
2013-06-25 15:27:41 -04:00
}
2014-01-25 22:38:12 -05:00
[TestMethod]
public void TestLoadKdb1 ( )
{
2014-01-26 06:51:55 -05:00
var app = PerformLoad ( "keepass.kdb" , "test" , "" ) ;
//contents of the kdb file are a little different because the root group cannot have entries
Assert . AreEqual ( 6 , app . GetDb ( ) . KpDatabase . RootGroup . Groups . Count ( ) ) ;
PwGroup generalGroup = app . GetDb ( ) . KpDatabase . RootGroup . Groups . Single ( g = > g . Name = = "General" ) ;
Assert . AreEqual ( 2 , generalGroup . Entries . Count ( ) ) ;
2014-01-26 08:27:27 -05:00
foreach ( PwEntry e in generalGroup . Entries )
Assert . IsFalse ( e . Binaries . Any ( ) ) ;
2014-01-25 22:38:12 -05:00
}
2014-11-11 11:19:22 -05:00
[TestMethod]
public void TestLoadKdb1WithKeyfileByDirectCall ( )
{
ImporterV3 importer = new ImporterV3 ( ) ;
try
{
FileStream dbStream = new FileStream ( TestDbDirectory + "withkeyfile_nopwd.kdb" , FileMode . Open ) ;
FileStream keyfileStream = new FileStream ( TestDbDirectory + "withkeyfile.key" , FileMode . Open ) ;
/ *
for ( int i = 0 ; i < 10 ; i + + )
{
int b = keyfileStream . ReadByte ( ) ;
Kp2aLog . Log ( i + ": " + b ) ;
}
keyfileStream . Close ( ) ;
Kp2aLog . Log ( "stream 2" ) ;
var keyfileStream2 = new MemoryStream ( new KcpKeyFile ( TestDbDirectory + "withkeyfile.key" ) . RawFileData . ReadData ( ) ) ;
for ( int i = 0 ; i < 10 ; i + + )
{
int b = keyfileStream2 . ReadByte ( ) ;
Kp2aLog . Log ( i + ": " + b ) ;
} * /
importer . OpenDatabase ( dbStream , "" , keyfileStream ) ;
}
catch ( Exception e )
{
Kp2aLog . Log ( e . ToString ( ) ) ;
Assert . Fail ( "exception occured: " + e ) ;
}
}
[TestMethod]
public void TestLoadKdb1WithKeyfile ( )
{
var app = PerformLoad ( "withkeyfile.kdb" , "test" , TestDbDirectory + "withkeyfile.key" ) ;
}
[TestMethod]
public void TestLoadKdb1WithKeyfileOnly ( )
{
var app = PerformLoad ( "withkeyfile_nopwd.kdb" , "" , TestDbDirectory + "withkeyfile.key" ) ;
}
2013-06-25 15:27:41 -04:00
[TestMethod]
public void TestLoadWithKeyfileOnly ( )
{
RunLoadTest ( "keyfileonly.kdbx" , "" , TestDbDirectory + "keyfile.txt" ) ;
}
[TestMethod]
public void TestLoadWithPasswordAndKeyfile ( )
{
RunLoadTest ( "PasswordAndKeyfile.kdbx" , DefaultPassword , TestDbDirectory + "keyfile.txt" ) ;
}
[TestMethod]
public void TestLoadWithEmptyPassword ( )
{
RunLoadTest ( "EmptyPasswordAndKeyfile.kdbx" , "" , TestDbDirectory + "keyfile.txt" ) ;
}
[TestMethod]
public void TestLoadWithEmptyPasswordOnly ( )
{
RunLoadTest ( "EmptyPassword.kdbx" , "" , "" ) ;
}
2013-07-12 10:39:55 -04:00
[TestMethod]
public void LoadFromRemoteWithDomain ( )
{
2014-03-25 01:15:05 -04:00
//warning, looks like credentials are no longer valid
2013-07-12 10:39:55 -04:00
var ioc = RemoteDomainIoc ; //note: this property is defined in "TestLoadDbCredentials.cs" which is deliberately excluded from Git because the credentials are not public!
2013-12-12 04:24:24 -05:00
var app = new TestKp2aApp ( ) ;
app . ServerCertificateErrorResponse = true ; //accept invalid cert
2013-07-12 10:39:55 -04:00
app . CreateNewDatabase ( ) ;
bool loadSuccesful = false ;
2013-12-12 04:24:24 -05:00
LoadDb task = new LoadDb ( app , ioc , null , CreateKey ( "a" ) , null , new ActionOnFinish ( ( success , message ) = >
2013-07-12 10:39:55 -04:00
{
if ( ! success )
Android . Util . Log . Debug ( "KP2ATest" , "error loading db: " + message ) ;
loadSuccesful = success ;
} )
) ;
ProgressTask pt = new ProgressTask ( app , Application . Context , task ) ;
Android . Util . Log . Debug ( "KP2ATest" , "Running ProgressTask" ) ;
pt . Run ( ) ;
pt . JoinWorkerThread ( ) ;
Android . Util . Log . Debug ( "KP2ATest" , "PT.run finished" ) ;
Assert . IsTrue ( loadSuccesful , "didn't succesfully load database :-(" ) ;
}
2013-06-25 15:27:41 -04:00
2013-08-06 16:21:58 -04:00
[TestMethod]
2013-12-12 04:24:24 -05:00
public void LoadErrorWithCertificateTrustFailure ( )
{
var ioc = RemoteCertFailureIoc ; //note: this property is defined in "TestLoadDbCredentials.cs" which is deliberately excluded from Git because the credentials are not public!
var app = new TestKp2aApp ( ) ;
app . ServerCertificateErrorResponse = false ;
app . CreateNewDatabase ( ) ;
bool loadSuccesful = false ;
string theMessage = "" ;
LoadDb task = new LoadDb ( app , ioc , null , CreateKey ( "test" ) , null , new ActionOnFinish ( ( success , message ) = >
{
if ( ! success )
Android . Util . Log . Debug ( "KP2ATest" , "error loading db: " + message ) ;
loadSuccesful = success ;
theMessage = message ;
} )
) ;
ProgressTask pt = new ProgressTask ( app , Application . Context , task ) ;
Android . Util . Log . Debug ( "KP2ATest" , "Running ProgressTask" ) ;
pt . Run ( ) ;
pt . JoinWorkerThread ( ) ;
Android . Util . Log . Debug ( "KP2ATest" , "PT.run finished" ) ;
Assert . IsFalse ( loadSuccesful , "database should not be loaded because invalid certificates are not accepted" ) ;
Assert . AreEqual ( theMessage , UiStringKey . ErrorOcurred + " " + UiStringKey . CertificateFailure ) ;
}
[TestMethod]
public void LoadWithAcceptedCertificateTrustFailure ( )
{
var ioc = RemoteCertFailureIoc ; //note: this property is defined in "TestLoadDbCredentials.cs" which is deliberately excluded from Git because the credentials are not public!
var app = new TestKp2aApp ( ) ;
app . ServerCertificateErrorResponse = true ;
app . CreateNewDatabase ( ) ;
bool loadSuccesful = false ;
LoadDb task = new LoadDb ( app , ioc , null , CreateKey ( "test" ) , null , new ActionOnFinish ( ( success , message ) = >
{
if ( ! success )
Android . Util . Log . Debug ( "KP2ATest" , "error loading db: " + message ) ;
loadSuccesful = success ;
} )
) ;
ProgressTask pt = new ProgressTask ( app , Application . Context , task ) ;
Android . Util . Log . Debug ( "KP2ATest" , "Running ProgressTask" ) ;
pt . Run ( ) ;
pt . JoinWorkerThread ( ) ;
Android . Util . Log . Debug ( "KP2ATest" , "PT.run finished" ) ;
Assert . IsTrue ( loadSuccesful , "database should be loaded because invalid certificates are accepted" ) ;
}
[TestMethod]
public void LoadFromRemote1And1 ( )
2013-08-06 16:21:58 -04:00
{
var ioc = RemoteIoc1and1 ; //note: this property is defined in "TestLoadDbCredentials.cs" which is deliberately excluded from Git because the credentials are not public!
IKp2aApp app = new TestKp2aApp ( ) ;
app . CreateNewDatabase ( ) ;
bool loadSuccesful = false ;
2013-12-12 04:24:24 -05:00
LoadDb task = new LoadDb ( app , ioc , null , CreateKey ( "test" ) , null , new ActionOnFinish ( ( success , message ) = >
2013-08-06 16:21:58 -04:00
{
if ( ! success )
Android . Util . Log . Debug ( "KP2ATest" , "error loading db: " + message ) ;
loadSuccesful = success ;
} )
) ;
ProgressTask pt = new ProgressTask ( app , Application . Context , task ) ;
Android . Util . Log . Debug ( "KP2ATest" , "Running ProgressTask" ) ;
pt . Run ( ) ;
pt . JoinWorkerThread ( ) ;
Android . Util . Log . Debug ( "KP2ATest" , "PT.run finished" ) ;
Assert . IsTrue ( loadSuccesful , "didn't succesfully load database :-(" ) ;
}
2014-02-19 15:49:18 -05:00
[TestMethod]
public void LoadAndSaveFromRemote1And1Ftp ( )
{
var ioc = RemoteIoc1and1Ftp ; //note: this property is defined in "TestLoadDbCredentials.cs" which is deliberately excluded from Git because the credentials are not public!
var app = new TestKp2aApp ( ) ;
app . CreateNewDatabase ( ) ;
bool loadSuccesful = false ;
LoadDb task = new LoadDb ( app , ioc , null , CreateKey ( "test" ) , null , new ActionOnFinish ( ( success , message ) = >
{
if ( ! success )
Android . Util . Log . Debug ( "KP2ATest" , "error loading db: " + message ) ;
loadSuccesful = success ;
} )
) ;
ProgressTask pt = new ProgressTask ( app , Application . Context , task ) ;
Android . Util . Log . Debug ( "KP2ATest" , "Running ProgressTask" ) ;
pt . Run ( ) ;
pt . JoinWorkerThread ( ) ;
Android . Util . Log . Debug ( "KP2ATest" , "PT.run finished" ) ;
Assert . IsTrue ( loadSuccesful , "didn't succesfully load database :-(" ) ;
Assert . IsTrue ( TrySaveDatabase ( app ) , "didn't successfully save database." ) ;
}
2013-08-06 16:21:58 -04:00
[TestMethod]
2013-12-12 04:24:24 -05:00
public void LoadFromRemote1And1NonExisting ( )
2013-08-06 16:21:58 -04:00
{
var ioc = RemoteIoc1and1NonExisting ; //note: this property is defined in "TestLoadDbCredentials.cs" which is deliberately excluded from Git because the credentials are not public!
IKp2aApp app = new TestKp2aApp ( ) ;
app . CreateNewDatabase ( ) ;
bool loadSuccesful = false ;
bool gotError = false ;
2013-12-12 04:24:24 -05:00
LoadDb task = new LoadDb ( app , ioc , null , CreateKey ( "test" ) , null , new ActionOnFinish ( ( success , message ) = >
2013-08-06 16:21:58 -04:00
{
if ( ! success )
{
Android . Util . Log . Debug ( "KP2ATest" , "error loading db: " + message ) ;
gotError = true ;
}
loadSuccesful = success ;
} )
) ;
ProgressTask pt = new ProgressTask ( app , Application . Context , task ) ;
Android . Util . Log . Debug ( "KP2ATest" , "Running ProgressTask" ) ;
pt . Run ( ) ;
pt . JoinWorkerThread ( ) ;
Android . Util . Log . Debug ( "KP2ATest" , "PT.run finished" ) ;
Assert . IsFalse ( loadSuccesful ) ;
Assert . IsTrue ( gotError ) ;
}
[TestMethod]
2013-12-12 04:24:24 -05:00
public void LoadFromRemote1And1WrongCredentials ( )
2013-08-06 16:21:58 -04:00
{
var ioc = RemoteIoc1and1WrongCredentials ; //note: this property is defined in "TestLoadDbCredentials.cs" which is deliberately excluded from Git because the credentials are not public!
IKp2aApp app = new TestKp2aApp ( ) ;
app . CreateNewDatabase ( ) ;
bool loadSuccesful = false ;
bool gotError = false ;
2013-12-12 04:24:24 -05:00
LoadDb task = new LoadDb ( app , ioc , null , CreateKey ( "test" ) , null , new ActionOnFinish ( ( success , message ) = >
2013-08-06 16:21:58 -04:00
{
if ( ! success )
{
Android . Util . Log . Debug ( "KP2ATest" , "error loading db: " + message ) ;
gotError = true ;
}
loadSuccesful = success ;
} )
) ;
ProgressTask pt = new ProgressTask ( app , Application . Context , task ) ;
Android . Util . Log . Debug ( "KP2ATest" , "Running ProgressTask" ) ;
pt . Run ( ) ;
pt . JoinWorkerThread ( ) ;
Android . Util . Log . Debug ( "KP2ATest" , "PT.run finished" ) ;
Assert . IsFalse ( loadSuccesful ) ;
Assert . IsTrue ( gotError ) ;
}
[TestMethod]
public void FileNotFoundExceptionWithWebDav ( )
{
2014-02-02 17:36:19 -05:00
var app = new TestKp2aApp ( ) ;
var fileStorage = app . FileStorage ;
2013-08-06 16:21:58 -04:00
//should work:
using ( var stream = fileStorage . OpenFileForRead ( RemoteIoc1and1 ) )
{
stream . CopyTo ( new MemoryStream ( ) ) ;
}
//shouldn't give FileNotFound:
bool gotException = false ;
try
{
using ( var stream = fileStorage . OpenFileForRead ( RemoteIoc1and1WrongCredentials ) )
{
stream . CopyTo ( new MemoryStream ( ) ) ;
}
}
catch ( FileNotFoundException )
{
Assert . Fail ( "shouldn't get FileNotFound with wrong credentials" ) ;
}
catch ( Exception e )
{
Kp2aLog . Log ( "received " + e ) ;
gotException = true ;
}
Assert . IsTrue ( gotException ) ;
//should give FileNotFound:
gotException = false ;
try
{
using ( var stream = fileStorage . OpenFileForRead ( RemoteIoc1and1NonExisting ) )
{
stream . CopyTo ( new MemoryStream ( ) ) ;
}
}
catch ( FileNotFoundException )
{
gotException = true ;
}
Assert . IsTrue ( gotException ) ;
}
2013-07-13 01:57:34 -04:00
[TestMethod]
public void TestLoadKdbpWithPasswordOnly ( )
{
RunLoadTest ( "passwordonly.kdbp" , DefaultPassword , "" ) ;
}
2013-06-25 15:27:41 -04:00
}
}