From 62579b157a591b6aa464b28a5cabbffa2c190d8b Mon Sep 17 00:00:00 2001 From: Scott Mertz Date: Fri, 22 Jan 2016 14:36:19 -0800 Subject: cmsdk: add invalid input checking for persistent storage APIs Some of this exists in the PersistentStorage implementation, but it was never formally documented in the APIs. Inherit the cmhw implementation error checking & move it into the service. Add tests to validate the new restrictions & a test that was previously failing. Change-Id: I3ecda29fdd28bbc4e6d8ccce7511c4644065ea46 --- .../tests/hardware/unit/PersistentStorageTest.java | 70 ++++++++++++++++++++++ 1 file changed, 70 insertions(+) (limited to 'tests') diff --git a/tests/src/org/cyanogenmod/tests/hardware/unit/PersistentStorageTest.java b/tests/src/org/cyanogenmod/tests/hardware/unit/PersistentStorageTest.java index 7f7b215..10bc390 100644 --- a/tests/src/org/cyanogenmod/tests/hardware/unit/PersistentStorageTest.java +++ b/tests/src/org/cyanogenmod/tests/hardware/unit/PersistentStorageTest.java @@ -35,6 +35,58 @@ public class PersistentStorageTest extends AndroidTestCase { } @SmallTest + public void testUdidFailure() { + String key = "udid"; + String value = "542bc67e510e82bd6d44e4f7015d7970"; + assertTrue(mHardwareManager.writePersistentString(key, value)); + } + + @SmallTest + public void testPersistentStringInvalidInput() { + String testKey = UUID.randomUUID().toString(); + String testString = "IM IN UR STORAGE"; + String testKeyTooLong = getStringOfLength(65); + String testStringTooLong = getStringOfLength(4097); + + assertFalse(mHardwareManager.writePersistentString(null, testString)); + assertFalse(mHardwareManager.writePersistentString("", testString)); + assertFalse(mHardwareManager.writePersistentString(testKeyTooLong, testString)); + assertFalse(mHardwareManager.writePersistentString(testKey, testStringTooLong)); + assertFalse(mHardwareManager.writePersistentString(testKey, "")); + assertNull(mHardwareManager.readPersistentString(testKey)); + assertNull(mHardwareManager.readPersistentString(testKeyTooLong)); + } + + @SmallTest + public void testPersistentIntInvalidInput() { + String testKey = UUID.randomUUID().toString(); + String testString = "IM IN UR STORAGE"; + String testKeyTooLong = getStringOfLength(65); + + assertFalse(mHardwareManager.writePersistentInt(null, 49152)); + assertFalse(mHardwareManager.writePersistentInt("", 49152)); + assertFalse(mHardwareManager.writePersistentInt(testKeyTooLong, 49152)); + assertEquals(0, mHardwareManager.readPersistentInt(testKey)); + assertEquals(0, mHardwareManager.readPersistentInt(testKeyTooLong)); + } + + @SmallTest + public void testPersistentBytesInvalidInput() { + String testKey = UUID.randomUUID().toString(); + byte[] testArray = new byte[1024]; + byte[] testArrayTooLong = new byte[4097]; + String testKeyTooLong = getStringOfLength(65); + + assertFalse(mHardwareManager.writePersistentBytes(null, testArray)); + assertFalse(mHardwareManager.writePersistentBytes("", testArray)); + assertFalse(mHardwareManager.writePersistentBytes(testKeyTooLong, testArray)); + assertFalse(mHardwareManager.writePersistentBytes(testKey, testArrayTooLong)); + assertFalse(mHardwareManager.writePersistentBytes(testKey, new byte[0])); + assertNull(mHardwareManager.readPersistentBytes(testKey)); + assertNull(mHardwareManager.readPersistentBytes(testKeyTooLong)); + } + + @SmallTest public void testPersistentString() { assertTrue(mHardwareManager.isSupported(CMHardwareManager.FEATURE_PERSISTENT_STORAGE)); @@ -52,6 +104,12 @@ public class PersistentStorageTest extends AndroidTestCase { // erase + read assertTrue(mHardwareManager.deletePersistentObject(testKey)); assertNull(mHardwareManager.readPersistentString(testKey)); + + // erase through write null + assertTrue(mHardwareManager.writePersistentString(testKey, testString + " AGAIN")); + assertEquals(testString + " AGAIN", mHardwareManager.readPersistentString(testKey)); + assertTrue(mHardwareManager.writePersistentString(testKey, null)); + assertNull(mHardwareManager.readPersistentString(testKey)); } @SmallTest @@ -96,5 +154,17 @@ public class PersistentStorageTest extends AndroidTestCase { // erase + read assertTrue(mHardwareManager.deletePersistentObject(testKey)); assertNull(mHardwareManager.readPersistentBytes(testKey)); + + // erase through write null + assertTrue(mHardwareManager.writePersistentBytes(testKey, testArray)); + assertTrue(Arrays.equals(testArray, mHardwareManager.readPersistentBytes(testKey))); + assertTrue(mHardwareManager.writePersistentBytes(testKey, null)); + assertNull(mHardwareManager.readPersistentBytes(testKey)); + } + + private String getStringOfLength(int length) { + char[] chars = new char[length]; + Arrays.fill(chars, 'z'); + return new String(chars); } } -- cgit v1.1