Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
briar
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Julian Dehm
briar
Commits
39aa2d96
Verified
Commit
39aa2d96
authored
6 years ago
by
akwizgran
Browse files
Options
Downloads
Patches
Plain Diff
Unit tests for DB key storage and retrieval.
parent
21dae824
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
briar-android/src/test/java/org/briarproject/briar/android/controller/ConfigControllerImplTest.java
+202
-0
202 additions, 0 deletions
...ct/briar/android/controller/ConfigControllerImplTest.java
with
202 additions
and
0 deletions
briar-android/src/test/java/org/briarproject/briar/android/controller/ConfigControllerImplTest.java
0 → 100644
+
202
−
0
View file @
39aa2d96
package
org.briarproject.briar.android.controller
;
import
android.content.SharedPreferences
;
import
android.content.SharedPreferences.Editor
;
import
org.briarproject.bramble.api.db.DatabaseConfig
;
import
org.briarproject.bramble.test.BrambleMockTestCase
;
import
org.jmock.Expectations
;
import
org.junit.After
;
import
org.junit.Test
;
import
java.io.File
;
import
static
junit
.
framework
.
Assert
.
assertEquals
;
import
static
junit
.
framework
.
Assert
.
assertFalse
;
import
static
junit
.
framework
.
Assert
.
assertNull
;
import
static
junit
.
framework
.
Assert
.
assertTrue
;
import
static
org
.
briarproject
.
bramble
.
test
.
TestUtils
.
deleteTestDirectory
;
import
static
org
.
briarproject
.
bramble
.
test
.
TestUtils
.
getRandomBytes
;
import
static
org
.
briarproject
.
bramble
.
test
.
TestUtils
.
getTestDirectory
;
import
static
org
.
briarproject
.
bramble
.
util
.
StringUtils
.
toHexString
;
import
static
org
.
briarproject
.
briar
.
android
.
TestDatabaseKeyUtils
.
loadDatabaseKey
;
import
static
org
.
briarproject
.
briar
.
android
.
TestDatabaseKeyUtils
.
storeDatabaseKey
;
public
class
ConfigControllerImplTest
extends
BrambleMockTestCase
{
private
final
SharedPreferences
prefs
=
context
.
mock
(
SharedPreferences
.
class
);
private
final
DatabaseConfig
databaseConfig
=
context
.
mock
(
DatabaseConfig
.
class
);
private
final
Editor
editor
=
context
.
mock
(
Editor
.
class
);
private
final
byte
[]
encryptedKey
=
getRandomBytes
(
123
);
private
final
String
encryptedKeyHex
=
toHexString
(
encryptedKey
);
private
final
String
oldEncryptedKeyHex
=
toHexString
(
getRandomBytes
(
123
));
private
final
File
testDir
=
getTestDirectory
();
private
final
File
keyDir
=
new
File
(
testDir
,
"key"
);
private
final
File
keyFile
=
new
File
(
keyDir
,
"db.key"
);
private
final
File
keyBackupFile
=
new
File
(
keyDir
,
"db.key.bak"
);
@Test
public
void
testDbKeyIsMigratedFromPreferencesToFile
()
throws
Exception
{
context
.
checking
(
new
Expectations
()
{{
oneOf
(
prefs
).
getString
(
"key"
,
null
);
will
(
returnValue
(
encryptedKeyHex
));
allowing
(
databaseConfig
).
getDatabaseKeyDirectory
();
will
(
returnValue
(
keyDir
));
oneOf
(
prefs
).
edit
();
will
(
returnValue
(
editor
));
oneOf
(
editor
).
remove
(
"key"
);
will
(
returnValue
(
editor
));
oneOf
(
editor
).
commit
();
will
(
returnValue
(
true
));
}});
assertFalse
(
keyFile
.
exists
());
assertFalse
(
keyBackupFile
.
exists
());
ConfigControllerImpl
c
=
new
ConfigControllerImpl
(
prefs
,
databaseConfig
);
assertEquals
(
encryptedKeyHex
,
c
.
getEncryptedDatabaseKey
());
assertTrue
(
keyFile
.
exists
());
assertFalse
(
keyBackupFile
.
exists
());
assertEquals
(
encryptedKeyHex
,
loadDatabaseKey
(
keyFile
));
}
@Test
public
void
testDbKeyIsLoadedFromPrimaryFile
()
throws
Exception
{
context
.
checking
(
new
Expectations
()
{{
oneOf
(
prefs
).
getString
(
"key"
,
null
);
will
(
returnValue
(
null
));
allowing
(
databaseConfig
).
getDatabaseKeyDirectory
();
will
(
returnValue
(
keyDir
));
}});
assertFalse
(
keyFile
.
exists
());
assertFalse
(
keyBackupFile
.
exists
());
storeDatabaseKey
(
keyFile
,
encryptedKeyHex
);
assertTrue
(
keyFile
.
exists
());
assertFalse
(
keyBackupFile
.
exists
());
assertEquals
(
encryptedKeyHex
,
loadDatabaseKey
(
keyFile
));
ConfigControllerImpl
c
=
new
ConfigControllerImpl
(
prefs
,
databaseConfig
);
assertEquals
(
encryptedKeyHex
,
c
.
getEncryptedDatabaseKey
());
assertTrue
(
keyFile
.
exists
());
assertFalse
(
keyBackupFile
.
exists
());
assertEquals
(
encryptedKeyHex
,
loadDatabaseKey
(
keyFile
));
}
@Test
public
void
testDbKeyIsLoadedFromBackupFile
()
throws
Exception
{
context
.
checking
(
new
Expectations
()
{{
oneOf
(
prefs
).
getString
(
"key"
,
null
);
will
(
returnValue
(
null
));
allowing
(
databaseConfig
).
getDatabaseKeyDirectory
();
will
(
returnValue
(
keyDir
));
}});
assertFalse
(
keyFile
.
exists
());
assertFalse
(
keyBackupFile
.
exists
());
storeDatabaseKey
(
keyBackupFile
,
encryptedKeyHex
);
assertFalse
(
keyFile
.
exists
());
assertTrue
(
keyBackupFile
.
exists
());
assertEquals
(
encryptedKeyHex
,
loadDatabaseKey
(
keyBackupFile
));
ConfigControllerImpl
c
=
new
ConfigControllerImpl
(
prefs
,
databaseConfig
);
assertEquals
(
encryptedKeyHex
,
c
.
getEncryptedDatabaseKey
());
assertFalse
(
keyFile
.
exists
());
assertTrue
(
keyBackupFile
.
exists
());
assertEquals
(
encryptedKeyHex
,
loadDatabaseKey
(
keyBackupFile
));
}
@Test
public
void
testDbKeyIsNullIfNotFound
()
{
context
.
checking
(
new
Expectations
()
{{
oneOf
(
prefs
).
getString
(
"key"
,
null
);
will
(
returnValue
(
null
));
allowing
(
databaseConfig
).
getDatabaseKeyDirectory
();
will
(
returnValue
(
keyDir
));
}});
assertFalse
(
keyFile
.
exists
());
assertFalse
(
keyBackupFile
.
exists
());
ConfigControllerImpl
c
=
new
ConfigControllerImpl
(
prefs
,
databaseConfig
);
assertNull
(
c
.
getEncryptedDatabaseKey
());
assertFalse
(
keyFile
.
exists
());
assertFalse
(
keyBackupFile
.
exists
());
}
@Test
public
void
testStoringDbKeyOverwritesPrimary
()
throws
Exception
{
context
.
checking
(
new
Expectations
()
{{
allowing
(
databaseConfig
).
getDatabaseKeyDirectory
();
will
(
returnValue
(
keyDir
));
}});
assertFalse
(
keyFile
.
exists
());
assertFalse
(
keyBackupFile
.
exists
());
storeDatabaseKey
(
keyFile
,
oldEncryptedKeyHex
);
assertTrue
(
keyFile
.
exists
());
assertFalse
(
keyBackupFile
.
exists
());
assertEquals
(
oldEncryptedKeyHex
,
loadDatabaseKey
(
keyFile
));
ConfigController
c
=
new
ConfigControllerImpl
(
prefs
,
databaseConfig
);
assertTrue
(
c
.
storeEncryptedDatabaseKey
(
encryptedKeyHex
));
assertTrue
(
keyFile
.
exists
());
assertFalse
(
keyBackupFile
.
exists
());
assertEquals
(
encryptedKeyHex
,
loadDatabaseKey
(
keyFile
));
}
@Test
public
void
testStoringDbKeyOverwritesBackup
()
throws
Exception
{
context
.
checking
(
new
Expectations
()
{{
allowing
(
databaseConfig
).
getDatabaseKeyDirectory
();
will
(
returnValue
(
keyDir
));
}});
assertFalse
(
keyFile
.
exists
());
assertFalse
(
keyBackupFile
.
exists
());
storeDatabaseKey
(
keyBackupFile
,
oldEncryptedKeyHex
);
assertFalse
(
keyFile
.
exists
());
assertTrue
(
keyBackupFile
.
exists
());
assertEquals
(
oldEncryptedKeyHex
,
loadDatabaseKey
(
keyBackupFile
));
ConfigController
c
=
new
ConfigControllerImpl
(
prefs
,
databaseConfig
);
assertTrue
(
c
.
storeEncryptedDatabaseKey
(
encryptedKeyHex
));
assertTrue
(
keyFile
.
exists
());
assertFalse
(
keyBackupFile
.
exists
());
assertEquals
(
encryptedKeyHex
,
loadDatabaseKey
(
keyFile
));
}
@After
public
void
tearDown
()
{
deleteTestDirectory
(
testDir
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment