Skip to content

Instantly share code, notes, and snippets.

@ryugoo
Last active August 29, 2015 14:20
Show Gist options
  • Save ryugoo/cfba4705114f38cf473b to your computer and use it in GitHub Desktop.
Save ryugoo/cfba4705114f38cf473b to your computer and use it in GitHub Desktop.
Realm encryption test
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "net.imthinker.android.realmenctyption"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'LICENSE.txt'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
compile 'com.android.support:support-annotations:22.1.1'
compile 'io.realm:realm-android:0.80.1'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
}
package net.imthinker.android.realmenctyption;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
package net.imthinker.android.realmenctyption;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.LargeTest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.security.SecureRandom;
import io.realm.Realm;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
private MainActivity mActivity;
public MainActivityTest() {
super(MainActivity.class);
}
@Before
public void setUp() throws Exception {
super.setUp();
setActivityInitialTouchMode(false);
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
mActivity = getActivity();
Realm.deleteRealmFile(mActivity);
}
@Test(expected = IllegalArgumentException.class)
public void testRaiseIllegalArgumentException() {
// Current sample code
byte[] key = new byte[32];
new SecureRandom().nextBytes(key);
Realm realm = Realm.getInstance(mActivity, key);
// Not running
realm.close();
}
@Test
public void testNotRaiseIllegalArgumentException() {
// Fix key length
byte[] key = new byte[64];
new SecureRandom().nextBytes(key);
Realm realm = Realm.getInstance(mActivity, key);
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
SampleModel model = realm.createObject(SampleModel.class);
model.setId(12345);
}
});
realm.close();
realm = Realm.getInstance(mActivity, key);
SampleModel model = realm.where(SampleModel.class).equalTo("id", 12345).findFirst();
long id = model.getId();
realm.close();
assertThat(id, is(12345l));
}
@After
public void tearDown() throws Exception {
super.tearDown();
}
}
package net.imthinker.android.realmenctyption;
import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;
public class SampleModel extends RealmObject {
@PrimaryKey
private long id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
$ ./gradlew connectedAndroidTest
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72211Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42211Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:processDebugJavaRes UP-TO-DATE
:app:compileDebugJava UP-TO-DATE
:app:compileDebugNdk UP-TO-DATE
:app:compileDebugSources UP-TO-DATE
:app:preDexDebug UP-TO-DATE
:app:dexDebug UP-TO-DATE
:app:validateDebugSigning
:app:packageDebug UP-TO-DATE
:app:zipalignDebug UP-TO-DATE
:app:assembleDebug UP-TO-DATE
:app:preDebugAndroidTestBuild UP-TO-DATE
:app:prepareComAndroidSupportTestTestingSupportLib01Library UP-TO-DATE
:app:prepareDebugAndroidTestDependencies
:app:compileDebugAndroidTestAidl UP-TO-DATE
:app:processDebugAndroidTestManifest UP-TO-DATE
:app:compileDebugAndroidTestRenderscript UP-TO-DATE
:app:generateDebugAndroidTestBuildConfig UP-TO-DATE
:app:generateDebugAndroidTestAssets UP-TO-DATE
:app:mergeDebugAndroidTestAssets UP-TO-DATE
:app:generateDebugAndroidTestResValues UP-TO-DATE
:app:generateDebugAndroidTestResources UP-TO-DATE
:app:mergeDebugAndroidTestResources UP-TO-DATE
:app:processDebugAndroidTestResources UP-TO-DATE
:app:generateDebugAndroidTestSources UP-TO-DATE
:app:processDebugAndroidTestJavaRes UP-TO-DATE
:app:compileDebugAndroidTestJava UP-TO-DATE
:app:compileDebugAndroidTestNdk UP-TO-DATE
:app:compileDebugAndroidTestSources UP-TO-DATE
:app:preDexDebugAndroidTest UP-TO-DATE
:app:dexDebugAndroidTest UP-TO-DATE
:app:packageDebugAndroidTest UP-TO-DATE
:app:assembleDebugAndroidTest UP-TO-DATE
:app:connectedAndroidTestDebug
:app:connectedAndroidTest
BUILD SUCCESSFUL
Total time: 22.284 secs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment