diff options
author | Hung-ying Tyan <tyanh@google.com> | 2009-09-11 18:34:28 +0800 |
---|---|---|
committer | Hung-ying Tyan <tyanh@google.com> | 2009-09-11 19:30:13 +0800 |
commit | a7df557aa2573a4718c7bbd069268e62bc036753 (patch) | |
tree | 606824416def47dd79b782fb4037aa180fca8d93 /keystore/java/android | |
parent | d304ae583d862250a21b5949fc3dbdf3af1febac (diff) | |
download | frameworks_base-a7df557aa2573a4718c7bbd069268e62bc036753.zip frameworks_base-a7df557aa2573a4718c7bbd069268e62bc036753.tar.gz frameworks_base-a7df557aa2573a4718c7bbd069268e62bc036753.tar.bz2 |
Add tests and misc fixes on keystore.
* Refactor netkeystore.c to make client and server code testable.
* Add a client test for setting new passwd and changing passwd.
* Exclude "." and ".." from reset_keystore().
* Change ServerCommand.executeCommand() to accept variable length of
arguments and add convert() to marshalling the args to bytes.
* Keystore.java is revised accordingly.
Diffstat (limited to 'keystore/java/android')
-rw-r--r-- | keystore/java/android/security/Keystore.java | 15 | ||||
-rw-r--r-- | keystore/java/android/security/ServiceCommand.java | 20 |
2 files changed, 21 insertions, 14 deletions
diff --git a/keystore/java/android/security/Keystore.java b/keystore/java/android/security/Keystore.java index a706c89..b47ae12 100644 --- a/keystore/java/android/security/Keystore.java +++ b/keystore/java/android/security/Keystore.java @@ -60,7 +60,7 @@ public abstract class Keystore { @Override public int lock() { - Reply result = mServiceCommand.execute(ServiceCommand.LOCK, null); + Reply result = mServiceCommand.execute(ServiceCommand.LOCK); return (result != null) ? result.returnCode : -1; } @@ -73,15 +73,14 @@ public abstract class Keystore { @Override public int getState() { - Reply result = mServiceCommand.execute(ServiceCommand.GET_STATE, - null); + Reply result = mServiceCommand.execute(ServiceCommand.GET_STATE); return (result != null) ? result.returnCode : -1; } @Override public int changePassword(String oldPassword, String newPassword) { Reply result = mServiceCommand.execute(ServiceCommand.PASSWD, - oldPassword + "\0" + newPassword + "\0"); + oldPassword, newPassword); return (result != null) ? result.returnCode : -1; } @@ -106,14 +105,14 @@ public abstract class Keystore { @Override public int put(String namespace, String keyname, String value) { Reply result = mServiceCommand.execute(ServiceCommand.PUT_KEY, - namespace + "\0" + keyname + "\0" + value); + namespace, keyname, value); return (result != null) ? result.returnCode : -1; } @Override public String get(String namespace, String keyname) { Reply result = mServiceCommand.execute(ServiceCommand.GET_KEY, - namespace + "\0" + keyname + "\0"); + namespace, keyname); return (result != null) ? ((result.returnCode != 0) ? null : new String(result.data, 0, result.len)) : null; } @@ -121,13 +120,13 @@ public abstract class Keystore { @Override public int remove(String namespace, String keyname) { Reply result = mServiceCommand.execute(ServiceCommand.REMOVE_KEY, - namespace + "\0" + keyname + "\0"); + namespace, keyname); return (result != null) ? result.returnCode : -1; } @Override public int reset() { - Reply result = mServiceCommand.execute(ServiceCommand.RESET, null); + Reply result = mServiceCommand.execute(ServiceCommand.RESET); return (result != null) ? result.returnCode : -1; } } diff --git a/keystore/java/android/security/ServiceCommand.java b/keystore/java/android/security/ServiceCommand.java index cefae40..ee80014 100644 --- a/keystore/java/android/security/ServiceCommand.java +++ b/keystore/java/android/security/ServiceCommand.java @@ -141,10 +141,18 @@ public class ServiceCommand { return reply; } - private boolean writeCommand(int cmd, String _data) { + private byte[] convert(String... data) { + StringBuilder sb = new StringBuilder(); + if (data.length >=1) sb.append(data[0]).append("\0"); + if (data.length >=2) sb.append(data[1]).append("\0"); + if (data.length >=3) sb.append(data[2]); + return sb.toString().getBytes(); + } + + private boolean writeCommand(int cmd, String... data) { byte buf[] = new byte[8]; - byte[] data = (_data == null) ? new byte[0] : _data.getBytes(); - int len = data.length; + byte[] dataBytes = convert(data); + int len = dataBytes.length; // the length of data buf[0] = (byte) ((len >> 24) & 0xff); buf[1] = (byte) ((len >> 16) & 0xff); @@ -157,7 +165,7 @@ public class ServiceCommand { buf[7] = (byte) (cmd & 0xff); try { mOut.write(buf, 0, 8); - mOut.write(data, 0, len); + mOut.write(dataBytes, 0, len); } catch (IOException ex) { Log.e(mTag,"write error", ex); disconnect(); @@ -166,7 +174,7 @@ public class ServiceCommand { return true; } - private Reply executeCommand(int cmd, String data) { + private Reply executeCommand(int cmd, String... data) { if (!writeCommand(cmd, data)) { /* If service died and restarted in the background * (unlikely but possible) we'll fail on the next @@ -181,7 +189,7 @@ public class ServiceCommand { return readReply(); } - public synchronized Reply execute(int cmd, String data) { + public synchronized Reply execute(int cmd, String... data) { Reply result; if (!connect()) { Log.e(mTag, "connection failed"); |