diff --git a/hawk/src/main/java/com/orhanobut/hawk/Base64Encryption.java b/hawk/src/main/java/com/orhanobut/hawk/Base64Encryption.java new file mode 100644 index 0000000..4bcf814 --- /dev/null +++ b/hawk/src/main/java/com/orhanobut/hawk/Base64Encryption.java @@ -0,0 +1,22 @@ +package com.orhanobut.hawk; + +/** + * Provides Base64 Algorithm + */ +public class Base64Encryption implements Encryption { + @Override public boolean init() { + return true; + } + + @Override public String encrypt(byte[] value) { + return DataHelper.encodeBase64(value); + } + + @Override public byte[] decrypt(String value) { + return DataHelper.decodeBase64(value); + } + + @Override public boolean reset() { + return true; + } +} diff --git a/hawk/src/main/java/com/orhanobut/hawk/Hawk.java b/hawk/src/main/java/com/orhanobut/hawk/Hawk.java index 41e9dae..7edbb61 100644 --- a/hawk/src/main/java/com/orhanobut/hawk/Hawk.java +++ b/hawk/src/main/java/com/orhanobut/hawk/Hawk.java @@ -12,7 +12,7 @@ public final class Hawk { - private static HawkBuilder hawkBuilder; + private static HawkInternal internal; private Hawk() { // no instance @@ -27,8 +27,13 @@ public static HawkBuilder init(Context context) { if (context == null) { throw new NullPointerException("Context should not be null"); } - hawkBuilder = new HawkBuilder(context); - return hawkBuilder; + + internal = null; + return new HawkBuilder(context); + } + + static void onHawkBuilt(HawkBuilder builder) { + Hawk.internal = new HawkInternal(builder); } /** @@ -49,7 +54,7 @@ public static boolean put(String key, T value) { String encodedText = zip(value); //if any exception occurs during encoding, encodedText will be null and thus operation is unsuccessful - return encodedText != null && hawkBuilder.getStorage().put(key, encodedText); + return encodedText != null && internal.getStorage().put(key, encodedText); } /** @@ -88,15 +93,9 @@ private static String zip(T value) { if (value == null) { throw new NullPointerException("Value cannot be null"); } - byte[] encodedValue = hawkBuilder.getEncoder().encode(value); + byte[] encodedValue = internal.getEncoder().encode(value); - String cipherText; - - if (!hawkBuilder.isEncrypted()) { - cipherText = DataHelper.encodeBase64(encodedValue); - } else { - cipherText = hawkBuilder.getEncryption().encrypt(encodedValue); - } + String cipherText = internal.getEncryption().encrypt(encodedValue); if (cipherText == null) { return null; @@ -112,25 +111,19 @@ public static T get(String key) { if (key == null) { throw new NullPointerException("Key cannot be null"); } - String fullText = hawkBuilder.getStorage().get(key); + String fullText = internal.getStorage().get(key); if (fullText == null) { return null; } DataInfo dataInfo = DataHelper.getDataInfo(fullText); - byte[] bytes; - - if (!hawkBuilder.isEncrypted()) { - bytes = DataHelper.decodeBase64(dataInfo.getCipherText()); - } else { - bytes = hawkBuilder.getEncryption().decrypt(dataInfo.getCipherText()); - } + byte[] bytes = internal.getEncryption().decrypt(dataInfo.getCipherText()); if (bytes == null) { return null; } try { - return hawkBuilder.getEncoder().decode(bytes, dataInfo); + return internal.getEncoder().decode(bytes, dataInfo); } catch (Exception e) { Logger.d(e.getMessage()); } @@ -219,7 +212,7 @@ public static Chain chain(int capacity) { * @return the size */ public static long count() { - return hawkBuilder.getStorage().count(); + return internal.getStorage().count(); } /** @@ -229,7 +222,7 @@ public static long count() { * @return true if clear is successful */ public static boolean clear() { - return hawkBuilder.getStorage().clear(); + return internal.getStorage().clear(); } /** @@ -239,7 +232,7 @@ public static boolean clear() { * @return true if remove is successful */ public static boolean remove(String key) { - return hawkBuilder.getStorage().remove(key); + return internal.getStorage().remove(key); } /** @@ -249,7 +242,7 @@ public static boolean remove(String key) { * @return true if all removals are successful */ public static boolean remove(String... keys) { - return hawkBuilder.getStorage().remove(keys); + return internal.getStorage().remove(keys); } /** @@ -259,7 +252,7 @@ public static boolean remove(String... keys) { * @return true if it exists in the storage */ public static boolean contains(String key) { - return hawkBuilder.getStorage().contains(key); + return internal.getStorage().contains(key); } /** @@ -268,14 +261,14 @@ public static boolean contains(String key) { * @return true if reset is successful */ public static boolean resetCrypto() { - return hawkBuilder.getEncryption() == null || hawkBuilder.getEncryption().reset(); + return internal.getEncryption().reset(); } public static LogLevel getLogLevel() { - if (hawkBuilder == null) { + if (internal == null) { return LogLevel.NONE; } - return hawkBuilder.getLogLevel(); + return internal.getLogLevel(); } /** @@ -322,7 +315,7 @@ public Chain put(String key, T value) { * @return true if successfully saved, false otherwise. */ public boolean commit() { - return hawkBuilder.getStorage().put(items); + return internal.getStorage().put(items); } } diff --git a/hawk/src/main/java/com/orhanobut/hawk/HawkBuilder.java b/hawk/src/main/java/com/orhanobut/hawk/HawkBuilder.java index b22c9b0..0e9e79d 100644 --- a/hawk/src/main/java/com/orhanobut/hawk/HawkBuilder.java +++ b/hawk/src/main/java/com/orhanobut/hawk/HawkBuilder.java @@ -81,61 +81,53 @@ public HawkBuilder setParser(Parser parser) { return this; } - public Context getContext() { - return context; - } - - public EncryptionMethod getEncryptionMethod() { + EncryptionMethod getEncryptionMethod() { if (encryptionMethod == null) { encryptionMethod = EncryptionMethod.MEDIUM; } return encryptionMethod; } - public String getPassword() { + String getPassword() { return password; } - public LogLevel getLogLevel() { + LogLevel getLogLevel() { if (logLevel == null) { logLevel = LogLevel.NONE; } return logLevel; } - public Storage getStorage() { + Storage getStorage() { if (cryptoStorage == null) { cryptoStorage = new SharedPreferencesStorage(context, TAG); } return cryptoStorage; } - public Encoder getEncoder() { + Encoder getEncoder() { if (encoder == null) { encoder = new HawkEncoder(getParser()); } return encoder; } - public Storage getInfoStorage() { + Storage getInfoStorage() { return new SharedPreferencesStorage(context, TAG_INFO); } - public Parser getParser() { + Parser getParser() { if (parser == null) { parser = new GsonParser(new Gson()); } return parser; } - public Encryption getEncryption() { + Encryption getEncryption() { return encryption; } - public boolean isEncrypted() { - return encryptionMethod != EncryptionMethod.NO_ENCRYPTION; - } - private void validate() { if (getEncryptionMethod() == EncryptionMethod.HIGHEST) { if (TextUtils.isEmpty(getPassword())) { @@ -165,28 +157,28 @@ public void build() { private void startBuild() { validate(); setEncryption(); + Hawk.onHawkBuilt(this); } private void setEncryption() { switch (getEncryptionMethod()) { case NO_ENCRYPTION: + encryption = new Base64Encryption(); break; case HIGHEST: encryption = new AesEncryption(getStorage(), getPassword()); if (!getEncryption().init()) { getInfoStorage().put(KEY_NO_CRYPTO, true); - encryptionMethod = EncryptionMethod.NO_ENCRYPTION; + encryption = new Base64Encryption(); } break; case MEDIUM: encryption = new AesEncryption(getStorage(), null); if (!getEncryption().init()) { getInfoStorage().put(KEY_NO_CRYPTO, true); - encryptionMethod = EncryptionMethod.NO_ENCRYPTION; + encryption = new Base64Encryption(); } break; - default: - throw new IllegalStateException("Encryption mode is not correct"); } } diff --git a/hawk/src/main/java/com/orhanobut/hawk/HawkEncoder.java b/hawk/src/main/java/com/orhanobut/hawk/HawkEncoder.java index 08bb914..54a993c 100644 --- a/hawk/src/main/java/com/orhanobut/hawk/HawkEncoder.java +++ b/hawk/src/main/java/com/orhanobut/hawk/HawkEncoder.java @@ -107,7 +107,7 @@ private T toList(String json, Class type) throws Exception { List list = parser.fromJson( json, new TypeToken>() { - } .getType() + }.getType() ); int size = list.size(); @@ -124,7 +124,7 @@ private T toSet(String json, Class type) throws Exception { return (T) resultSet; } Set set = parser.fromJson(json, new TypeToken>() { - } .getType()); + }.getType()); for (T t : set) { String valueJson = parser.toJson(t); @@ -141,7 +141,7 @@ private T toMap(String json, Class keyType, Class valueType) thr return (T) resultMap; } Map map = parser.fromJson(json, new TypeToken>() { - } .getType()); + }.getType()); for (Map.Entry entry : map.entrySet()) { String keyJson = parser.toJson(entry.getKey()); diff --git a/hawk/src/main/java/com/orhanobut/hawk/HawkInternal.java b/hawk/src/main/java/com/orhanobut/hawk/HawkInternal.java new file mode 100644 index 0000000..58474e0 --- /dev/null +++ b/hawk/src/main/java/com/orhanobut/hawk/HawkInternal.java @@ -0,0 +1,33 @@ +package com.orhanobut.hawk; + +public class HawkInternal { + + private Storage cryptoStorage; + private Encoder encoder; + private Encryption encryption; + private LogLevel logLevel; + + HawkInternal(HawkBuilder hawkBuilder) { + cryptoStorage = hawkBuilder.getStorage(); + encoder = hawkBuilder.getEncoder(); + encryption = hawkBuilder.getEncryption(); + logLevel = hawkBuilder.getLogLevel(); + } + + Storage getStorage() { + return cryptoStorage; + } + + Encoder getEncoder() { + return encoder; + } + + Encryption getEncryption() { + return encryption; + } + + LogLevel getLogLevel() { + return logLevel; + } + +} diff --git a/hawk/src/test/java/com/orhanobut/hawk/DataHelperTest.java b/hawk/src/test/java/com/orhanobut/hawk/DataHelperTest.java index 1cdca94..418c32e 100644 --- a/hawk/src/test/java/com/orhanobut/hawk/DataHelperTest.java +++ b/hawk/src/test/java/com/orhanobut/hawk/DataHelperTest.java @@ -2,8 +2,6 @@ import android.util.Base64; -import junit.framework.TestCase; - import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricGradleTestRunner; @@ -16,11 +14,13 @@ import java.util.Map; import java.util.Set; +import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; import static org.assertj.core.api.Assertions.assertThat; @RunWith(RobolectricGradleTestRunner.class) @Config(constants = BuildConfig.class, sdk = 18) -public class DataHelperTest extends TestCase { +public class DataHelperTest { private static final String CIPHER_TEXT = "CIPHER"; diff --git a/hawk/src/test/java/com/orhanobut/hawk/HawkBuilderTest.java b/hawk/src/test/java/com/orhanobut/hawk/HawkBuilderTest.java index c12312b..0cc6468 100644 --- a/hawk/src/test/java/com/orhanobut/hawk/HawkBuilderTest.java +++ b/hawk/src/test/java/com/orhanobut/hawk/HawkBuilderTest.java @@ -7,8 +7,6 @@ import com.google.gson.Gson; import com.google.gson.JsonSyntaxException; -import junit.framework.TestCase; - import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -21,11 +19,13 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; import static org.assertj.core.api.Assertions.assertThat; @RunWith(RobolectricGradleTestRunner.class) @Config(constants = BuildConfig.class, sdk = 21) -public class HawkBuilderTest extends TestCase { +public class HawkBuilderTest { private static final long LATCH_TIMEOUT_IN_SECONDS = 5; @@ -158,7 +158,12 @@ public CustomParser(Gson gson) { @Test public void testDefaultEncryption() { builder.build(); - assertThat(builder.getEncryption()).isInstanceOf(AesEncryption.class); + try { + assertThat(builder.getEncryption()).isInstanceOf(AesEncryption.class); + } catch (AssertionError e) { + assertThat(builder.getEncryptionMethod()).isEqualTo(HawkBuilder.EncryptionMethod.MEDIUM); + assertThat(builder.getEncryption()).isInstanceOf(Base64Encryption.class); + } } @Test public void initWithCallback() throws InterruptedException { diff --git a/hawk/src/test/java/com/orhanobut/hawk/HawkEncoderTest.java b/hawk/src/test/java/com/orhanobut/hawk/HawkEncoderTest.java index 27d615b..4c82f76 100644 --- a/hawk/src/test/java/com/orhanobut/hawk/HawkEncoderTest.java +++ b/hawk/src/test/java/com/orhanobut/hawk/HawkEncoderTest.java @@ -2,8 +2,6 @@ import com.google.gson.Gson; -import junit.framework.TestCase; - import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricGradleTestRunner; @@ -16,11 +14,12 @@ import java.util.Map; import java.util.Set; +import static junit.framework.Assert.fail; import static org.assertj.core.api.Assertions.assertThat; @RunWith(RobolectricGradleTestRunner.class) @Config(constants = BuildConfig.class, sdk = 18) -public class HawkEncoderTest extends TestCase { +public class HawkEncoderTest { private final Encoder encoder; private final Parser parser; diff --git a/hawk/src/test/java/com/orhanobut/hawk/HawkTest.java b/hawk/src/test/java/com/orhanobut/hawk/HawkTest.java index 22dca6f..1af57d5 100644 --- a/hawk/src/test/java/com/orhanobut/hawk/HawkTest.java +++ b/hawk/src/test/java/com/orhanobut/hawk/HawkTest.java @@ -3,8 +3,8 @@ import android.app.Activity; import android.content.Context; -import junit.framework.TestCase; - +import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.Robolectric; @@ -26,6 +26,9 @@ import rx.functions.Func1; import rx.schedulers.Schedulers; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; import static org.assertj.core.api.Assertions.assertThat; /** @@ -33,7 +36,7 @@ */ @RunWith(RobolectricGradleTestRunner.class) @Config(constants = BuildConfig.class, sdk = 21) -public class HawkTest extends TestCase { +public class HawkTest { private static final String KEY = "TAG"; private static final long LATCH_TIMEOUT_IN_SECONDS = 5; @@ -44,7 +47,7 @@ public HawkTest() { context = Robolectric.buildActivity(Activity.class).create().get(); } - @Override public void setUp() throws Exception { + @Before public void setUp() { init(); } @@ -52,8 +55,7 @@ public void init() { Hawk.init(context).build(); } - @Override public void tearDown() throws Exception { - super.tearDown(); + @After public void tearDown() { Hawk.clear(); } @@ -129,7 +131,7 @@ public void init() { Map map1 = Hawk.get("map"); - assertThat(map).isNotNull(); + assertThat(map1).isNotNull(); assertThat(map1.get("key")).isEqualTo("value"); } diff --git a/hawk/src/test/java/com/orhanobut/hawk/SqliteStorageTest.java b/hawk/src/test/java/com/orhanobut/hawk/SqliteStorageTest.java index 37cc34e..12240da 100644 --- a/hawk/src/test/java/com/orhanobut/hawk/SqliteStorageTest.java +++ b/hawk/src/test/java/com/orhanobut/hawk/SqliteStorageTest.java @@ -4,8 +4,8 @@ import android.content.Context; import android.util.Pair; -import junit.framework.TestCase; - +import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.Robolectric; @@ -15,11 +15,12 @@ import java.util.ArrayList; import java.util.List; +import static junit.framework.Assert.fail; import static org.assertj.core.api.Assertions.assertThat; @RunWith(RobolectricGradleTestRunner.class) @Config(constants = BuildConfig.class, sdk = 18) -public class SqliteStorageTest extends TestCase { +public class SqliteStorageTest { private final Context context; @@ -29,8 +30,7 @@ public SqliteStorageTest() { context = Robolectric.buildActivity(Activity.class).create().get(); } - @Override public void setUp() throws Exception { - super.setUp(); + @Before public void setUp() throws Exception { storage = new SqliteStorage(context); } @@ -48,7 +48,7 @@ public SqliteStorageTest() { } } - @Override public void tearDown() { + @After public void tearDown() { storage = null; }