* 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.
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
- netkeystore.c keymgmt.c
+ netkeystore.c netkeystore_main.c keymgmt.c
LOCAL_C_INCLUDES := \
$(call include-path-for, system-core)/cutils \
return -1;
}
while ((de = readdir(d))) {
- if (unlink(de->d_name) != 0) ret = -1;
+ char *dirname = de->d_name;
+ if (strcmp(".", dirname) == 0) continue;
+ if (strcmp("..", dirname) == 0) continue;
+ if (unlink(dirname) != 0) ret = -1;
}
closedir(d);
state = UNINITIALIZED;
** limitations under the License.
*/
-#define LOG_TAG "keystore"
-
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
reply->retcode = reset_keystore();
}
-static void execute(LPC_MARSHAL *cmd, LPC_MARSHAL *reply)
+void execute(LPC_MARSHAL *cmd, LPC_MARSHAL *reply)
{
uint32_t cmd_max = sizeof(cmds)/sizeof(struct cmdinfo);
return 0;
}
-static int parse_cmd(int argc, const char **argv, LPC_MARSHAL *cmd)
+int parse_cmd(int argc, const char **argv, LPC_MARSHAL *cmd)
{
uint32_t i, len = 0;
uint32_t cmd_max = sizeof(cmds)/sizeof(cmds[0]);
}
}
-static int shell_command(const int argc, const char **argv)
+int shell_command(const int argc, const char **argv)
{
int fd, i;
LPC_MARSHAL cmd;
- if (parse_cmd(argc, argv , &cmd)) {
+ if (parse_cmd(argc, argv, &cmd)) {
fprintf(stderr, "Incorrect command or command line is too long.\n");
- exit(1);
+ return -1;
}
fd = socket_local_client(SOCKET_PATH,
ANDROID_SOCKET_NAMESPACE_RESERVED,
SOCK_STREAM);
if (fd == -1) {
fprintf(stderr, "Keystore service is not up and running.\n");
- exit(1);
+ return -1;
}
if (write_marshal(fd, &cmd)) {
fprintf(stderr, "Incorrect command or command line is too long.\n");
- exit(1);
+ return -1;
}
if (read_marshal(fd, &cmd)) {
fprintf(stderr, "Failed to read the result.\n");
- exit(1);
+ return -1;
}
cmd.data[cmd.len] = 0;
fprintf(stdout, "%s\n", (cmd.retcode == 0) ? "Succeeded!" : "Failed!");
return 0;
}
-int main(const int argc, const char *argv[])
+int server_main(const int argc, const char *argv[])
{
struct sockaddr addr;
socklen_t alen;
int lsocket, s;
LPC_MARSHAL cmd, reply;
- if (argc > 1) {
- return shell_command(argc - 1, argv + 1);
- }
-
if (init_keystore(KEYSTORE_DIR)) {
LOGE("Can not initialize the keystore, the directory exist?\n");
- exit(1);
+ return -1;
}
lsocket = android_get_control_socket(SOCKET_PATH);
if (lsocket < 0) {
LOGE("Failed to get socket from environment: %s\n", strerror(errno));
- exit(1);
+ return -1;
}
if (listen(lsocket, 5)) {
LOGE("Listen on socket failed: %s\n", strerror(errno));
- exit(1);
+ return -1;
}
fcntl(lsocket, F_SETFD, FD_CLOEXEC);
memset(&reply, 0, sizeof(LPC_MARSHAL));
#include "common.h"
+// for testing
+int parse_cmd(int argc, const char **argv, LPC_MARSHAL *cmd);
+void execute(LPC_MARSHAL *cmd, LPC_MARSHAL *reply);
+
static inline int readx(int s, void *_buf, int count)
{
char *buf = _buf;
--- /dev/null
+/*
+** Copyright 2009, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+** http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+
+#define LOG_TAG "keystore"
+
+int shell_command(const int argc, const char **argv);
+int server_main(const int argc, const char *argv[]);
+
+int main(const int argc, const char *argv[])
+{
+ if (argc > 1) {
+ return shell_command(argc - 1, argv + 1);
+ } else {
+ return server_main(argc, argv);
+ }
+}
ifdef KEYSTORE_TESTS
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
-LOCAL_SRC_FILES:= netkeystore_test.c ../keymgmt.c
+LOCAL_SRC_FILES:= netkeystore_test.c ../keymgmt.c ../netkeystore.c
LOCAL_SHARED_LIBRARIES := libcutils libssl
LOCAL_MODULE:= netkeystore_test
LOCAL_MODULE_TAGS := optional
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
+#include <cutils/log.h>
#include "common.h"
#include "keymgmt.h"
+#include "netkeystore.h"
+
+#define LOG_TAG "keystore_test"
typedef int FUNC_PTR();
typedef struct {
void teardown()
{
- reset_keystore();
+ if (reset_keystore() != 0) {
+ fprintf(stderr, "Can not reset the test directory %s\n", TEST_DIR);
+ }
rmdir(TEST_DIR);
}
FUNC_BODY(reset_keystore)
{
- chdir("/procx");
+ int ret = chdir("/proc");
if (reset_keystore() == 0) return -1;
chdir(TEST_DIR);
return EXIT_SUCCESS;
if (get_state() != UNLOCKED) return -1;
lock();
if (get_state() != LOCKED) return -1;
- reset_keystore();
+
+ if (reset_keystore() != 0) return -1;
if (get_state() != UNINITIALIZED) return -1;
return EXIT_SUCCESS;
}
return EXIT_SUCCESS;
}
+static int execute_cmd(int argc, const char *argv[], LPC_MARSHAL *cmd,
+ LPC_MARSHAL *reply)
+{
+ memset(cmd, 0, sizeof(LPC_MARSHAL));
+ memset(reply, 0, sizeof(LPC_MARSHAL));
+ if (parse_cmd(argc, argv, cmd)) return -1;
+ execute(cmd, reply);
+ return (reply->retcode ? -1 : 0);
+}
+
+FUNC_BODY(client_passwd)
+{
+ LPC_MARSHAL cmd, reply;
+ const char *set_passwd_cmds[2] = {"passwd", TEST_PASSWD};
+ const char *change_passwd_cmds[3] = {"passwd", TEST_PASSWD, TEST_NPASSWD};
+
+ if (execute_cmd(2, set_passwd_cmds, &cmd, &reply)) return -1;
+
+ lock();
+ if (unlock("55555555") == 0) return -1;
+ if (unlock(TEST_PASSWD) != 0) return -1;
+
+ if (execute_cmd(3, change_passwd_cmds, &cmd, &reply)) return -1;
+
+ lock();
+ if (unlock(TEST_PASSWD) == 0) return -1;
+ if (unlock(TEST_NPASSWD) != 0) return -1;
+
+ return EXIT_SUCCESS;
+}
+
TESTFUNC all_tests[] = {
FUNC_NAME(init_keystore),
FUNC_NAME(reset_keystore),
FUNC_NAME(get_key),
FUNC_NAME(remove_key),
FUNC_NAME(list_keys),
+ FUNC_NAME(client_passwd),
};
int main(int argc, char **argv) {
int i, ret;
for (i = 0 ; i < (int)(sizeof(all_tests)/sizeof(TESTFUNC)) ; ++i) {
+ LOGD("run %s...\n", all_tests[i].name);
setup();
if ((ret = all_tests[i].func()) != EXIT_SUCCESS) {
fprintf(stderr, "ERROR in function %s\n", all_tests[i].name);