Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a7df557a authored by Hung-ying Tyan's avatar Hung-ying Tyan
Browse files

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.
parent d304ae58
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -4,7 +4,7 @@ LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
include $(CLEAR_VARS)


LOCAL_SRC_FILES:= \
LOCAL_SRC_FILES:= \
    netkeystore.c keymgmt.c
    netkeystore.c netkeystore_main.c keymgmt.c


LOCAL_C_INCLUDES := \
LOCAL_C_INCLUDES := \
    $(call include-path-for, system-core)/cutils \
    $(call include-path-for, system-core)/cutils \
+4 −1
Original line number Original line Diff line number Diff line
@@ -385,7 +385,10 @@ int reset_keystore()
        return -1;
        return -1;
    }
    }
    while ((de = readdir(d))) {
    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);
    closedir(d);
    state = UNINITIALIZED;
    state = UNINITIALIZED;
+12 −18
Original line number Original line Diff line number Diff line
@@ -14,8 +14,6 @@
** limitations under the License.
** limitations under the License.
*/
*/


#define LOG_TAG "keystore"

#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/stat.h>
@@ -247,7 +245,7 @@ static void do_reset_keystore(LPC_MARSHAL *cmd, LPC_MARSHAL *reply)
    reply->retcode = reset_keystore();
    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);
    uint32_t cmd_max = sizeof(cmds)/sizeof(struct cmdinfo);


@@ -309,7 +307,7 @@ static int flatten_str_args(int argc, const char **argv, LPC_MARSHAL *cmd)
    return 0;
    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 i, len = 0;
    uint32_t cmd_max = sizeof(cmds)/sizeof(cmds[0]);
    uint32_t cmd_max = sizeof(cmds)/sizeof(cmds[0]);
@@ -335,30 +333,30 @@ static int parse_cmd(int argc, const char **argv, LPC_MARSHAL *cmd)
    }
    }
}
}


static int shell_command(const int argc, const char **argv)
int shell_command(const int argc, const char **argv)
{
{
    int fd, i;
    int fd, i;
    LPC_MARSHAL  cmd;
    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");
        fprintf(stderr, "Incorrect command or command line is too long.\n");
        exit(1);
        return -1;
    }
    }
    fd = socket_local_client(SOCKET_PATH,
    fd = socket_local_client(SOCKET_PATH,
                             ANDROID_SOCKET_NAMESPACE_RESERVED,
                             ANDROID_SOCKET_NAMESPACE_RESERVED,
                             SOCK_STREAM);
                             SOCK_STREAM);
    if (fd == -1) {
    if (fd == -1) {
        fprintf(stderr, "Keystore service is not up and running.\n");
        fprintf(stderr, "Keystore service is not up and running.\n");
        exit(1);
        return -1;
    }
    }


    if (write_marshal(fd, &cmd)) {
    if (write_marshal(fd, &cmd)) {
        fprintf(stderr, "Incorrect command or command line is too long.\n");
        fprintf(stderr, "Incorrect command or command line is too long.\n");
        exit(1);
        return -1;
    }
    }
    if (read_marshal(fd, &cmd)) {
    if (read_marshal(fd, &cmd)) {
        fprintf(stderr, "Failed to read the result.\n");
        fprintf(stderr, "Failed to read the result.\n");
        exit(1);
        return -1;
    }
    }
    cmd.data[cmd.len] = 0;
    cmd.data[cmd.len] = 0;
    fprintf(stdout, "%s\n", (cmd.retcode == 0) ? "Succeeded!" : "Failed!");
    fprintf(stdout, "%s\n", (cmd.retcode == 0) ? "Succeeded!" : "Failed!");
@@ -367,30 +365,26 @@ static int shell_command(const int argc, const char **argv)
    return 0;
    return 0;
}
}


int main(const int argc, const char *argv[])
int server_main(const int argc, const char *argv[])
{
{
    struct sockaddr addr;
    struct sockaddr addr;
    socklen_t alen;
    socklen_t alen;
    int lsocket, s;
    int lsocket, s;
    LPC_MARSHAL  cmd, reply;
    LPC_MARSHAL  cmd, reply;


    if (argc > 1) {
        return shell_command(argc - 1, argv + 1);
    }

    if (init_keystore(KEYSTORE_DIR)) {
    if (init_keystore(KEYSTORE_DIR)) {
        LOGE("Can not initialize the keystore, the directory exist?\n");
        LOGE("Can not initialize the keystore, the directory exist?\n");
        exit(1);
        return -1;
    }
    }


    lsocket = android_get_control_socket(SOCKET_PATH);
    lsocket = android_get_control_socket(SOCKET_PATH);
    if (lsocket < 0) {
    if (lsocket < 0) {
        LOGE("Failed to get socket from environment: %s\n", strerror(errno));
        LOGE("Failed to get socket from environment: %s\n", strerror(errno));
        exit(1);
        return -1;
    }
    }
    if (listen(lsocket, 5)) {
    if (listen(lsocket, 5)) {
        LOGE("Listen on socket failed: %s\n", strerror(errno));
        LOGE("Listen on socket failed: %s\n", strerror(errno));
        exit(1);
        return -1;
    }
    }
    fcntl(lsocket, F_SETFD, FD_CLOEXEC);
    fcntl(lsocket, F_SETFD, FD_CLOEXEC);
    memset(&reply, 0, sizeof(LPC_MARSHAL));
    memset(&reply, 0, sizeof(LPC_MARSHAL));
+4 −0
Original line number Original line Diff line number Diff line
@@ -25,6 +25,10 @@


#include "common.h"
#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)
static inline int readx(int s, void *_buf, int count)
{
{
    char *buf = _buf;
    char *buf = _buf;
+29 −0
Original line number Original line Diff line number Diff line
/*
** 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);
    }
}
Loading