diff options
author | Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org> | 2021-03-01 13:17:59 +0100 |
---|---|---|
committer | Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org> | 2021-03-01 22:31:08 +0100 |
commit | 6e4ef6a815f4d8722e83d2bba3b25a0f791369a2 (patch) | |
tree | d95eea66f97c712fde735defb1bea446087268b8 | |
parent | bdbb1daa9d97a9075e77ce3c2f25b7c10230877f (diff) | |
download | hardware_replicant_libsamsung-ipc-6e4ef6a815f4d8722e83d2bba3b25a0f791369a2.tar.gz hardware_replicant_libsamsung-ipc-6e4ef6a815f4d8722e83d2bba3b25a0f791369a2.tar.bz2 hardware_replicant_libsamsung-ipc-6e4ef6a815f4d8722e83d2bba3b25a0f791369a2.zip |
partitions: android: add tests
TODO:
- move libsamsung-ipc-test in samsung-ipc/tests/
- cleanup the code
Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
-rw-r--r-- | samsung-ipc/Makefile.am | 11 | ||||
-rw-r--r-- | samsung-ipc/tests/Makefile.am | 33 | ||||
-rwxr-xr-x | samsung-ipc/tests/libsamsung-ipc-test.py | 58 | ||||
-rw-r--r-- | samsung-ipc/tests/main.c | 155 | ||||
-rw-r--r-- | samsung-ipc/tests/partitions/android.c | 92 |
5 files changed, 349 insertions, 0 deletions
diff --git a/samsung-ipc/Makefile.am b/samsung-ipc/Makefile.am index ed6331f..1cf760d 100644 --- a/samsung-ipc/Makefile.am +++ b/samsung-ipc/Makefile.am @@ -37,3 +37,14 @@ include partitions/Makefile.am libsamsung_ipc_la_LIBADD = \ $(OPENSSL_LIBS) \ $(NULL) + +# As we reuse $(libsamsung_ipc_la_SOURCES) in the libsamsung-ipc-test test +# program, we need to tell the autotools to separate the libsamsung-ipc and +# libsamsung-ipc-test .o, otherwise libsamsung-ipc' .o will be linked +# directoy in libsamsung-ipc-test. +# As they need to be compiled with different options, this is an issue. +# Without that we might have errors like this one during autogen.sh: +# error: object 'ipc.$(OBJEXT)' created both with libtool and without +libsamsung_ipc_CPPFLAGS = ${AM_CPPFLAGS} + +include tests/Makefile.am diff --git a/samsung-ipc/tests/Makefile.am b/samsung-ipc/tests/Makefile.am new file mode 100644 index 0000000..34925be --- /dev/null +++ b/samsung-ipc/tests/Makefile.am @@ -0,0 +1,33 @@ +check_PROGRAMS = \ + libsamsung-ipc-test \ + $(NULL) + +libsamsung_ipc_test_SOURCES = \ + tests/main.c \ + tests/partitions/android.c \ + tests/partitions/android.h \ + $(NULL) + +libsamsung_ipc_test_SOURCES += $(libsamsung_ipc_la_SOURCES) + +libsamsung_ipc_test_LDADD = \ + $(OPENSSL_LIBS) \ + $(NULL) + +libsamsung_ipc_test_LDFLAGS = + +# As we reuse $(libsamsung_ipc_la_SOURCES) in the libsamsung-ipc-test test +# program, we need to tell the autotools to separate the libsamsung-ipc and +# libsamsung-ipc-test .o, otherwise libsamsung-ipc' .o will be linked +# directoy in libsamsung-ipc-test. +# As they need to be compiled with different options, this is an issue. +# Without that we might have errors like this one during autogen.sh: +# error: object 'ipc.$(OBJEXT)' created both with libtool and without +libsamsung_ipc_test_CPPFLAGS = ${AM_CPPFLAGS} + +# TODO: Find a way to make test more modular and represent each run of +# libsamsung-ipc-test in TEST while having it implemented in a single +# python file +PY_LOG_COMPILER = $(PYTHON) +TEST_EXTENSIONS = .py +TESTS = libsamsung-ipc-test.py diff --git a/samsung-ipc/tests/libsamsung-ipc-test.py b/samsung-ipc/tests/libsamsung-ipc-test.py new file mode 100755 index 0000000..7d93e18 --- /dev/null +++ b/samsung-ipc/tests/libsamsung-ipc-test.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python +# +# This file is part of libsamsung-ipc. +# +# Copyright (C) 2020 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org> +# +# libsamsung-ipc is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# +# libsamsung-ipc is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with libsamsung-ipc. If not, see <http://www.gnu.org/licenses/>. + +import os +import re +import sys +import sh + +class libsamsung_ipc_test(object): + def __init__(self): + srcdir = os.environ.get('srcdir', None) + + # Enable also to test without automake + if not srcdir: + srcdir = os.path.dirname(sys.argv[0]) + + self.run = sh.Command(srcdir + + os.sep + + ".." + + os.sep + + "libsamsung-ipc-test") + + def run_all_tests(self): + output = str(self.run("list-tests")).split(os.linesep) + # Remove the last line break + output.remove('') + + # Remove the first line. We have an output like that: + # Available tests: + # [list of tests] + output.pop(0) + + for test_name in output: + self.run("test", test_name.replace(' ', '')) + +def main(): + tests = libsamsung_ipc_test() + tests.run_all_tests() + +if __name__ == '__main__': + rc = main() + sys.exit(rc) diff --git a/samsung-ipc/tests/main.c b/samsung-ipc/tests/main.c new file mode 100644 index 0000000..b89daa2 --- /dev/null +++ b/samsung-ipc/tests/main.c @@ -0,0 +1,155 @@ +/* + * This file is part of libsamsung-ipc. + * + * Copyright (C) 2016 Paul Kocialkowsk <contact@paulk.fr> + * Copyright (C) 2021 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org> + * + * libsamsung-ipc is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * libsamsung-ipc is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with libsamsung-ipc. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sysexits.h> + +#include <samsung-ipc.h> + +/* TODO: Fixme */ +#include <ipc.h> +int test_open_android_modem_partition(struct ipc_client *client); + +struct test { + char *name; + int (*func)(struct ipc_client *client); +}; + +static struct test tests[] = { + { + "open_android_modem_partition", + test_open_android_modem_partition + }, +}; + +static void usage(char *progname) +{ + printf("Usage: %s list-tests\n", progname); + printf("Usage: %s test <test>\n", progname); +} + +static void list_tests(char *progname) +{ + int i; + + printf("Available tests:\n"); + + for (i=0; i < sizeof(tests) / sizeof (struct test); i++) { + printf(" %s\n", tests[i].name); + } +} + +static struct test *get_test(char *name) +{ + int i; + + for (i=0; i < sizeof(tests) / sizeof (struct test); i++) { + if (!strcmp(tests[i].name, name)) + return &(tests[i]); + } + + return NULL; +} + +static void log_callback(void *data, const char *message) +{ + /* TODO: add better logging mechanism in libsamsung-ipc with + * tags and log levels which would enable to filter the + * messages from different provenance (this file, the code + * under test, other parts of libsamsung-ipc, etc). + */ +#if 0 + char *buffer; + size_t length; + int i; + + if (message == NULL) + return; + + buffer = strdup(message); + length = strlen(message); + + for (i = length; i > 0; i--) { + if (buffer[i] == '\n') + buffer[i] = '\0'; + else if (buffer[i] != '\0') + break; + } + + printf("[ipc] %s\n", buffer); + + free(buffer); +#endif +} + +int main(int argc, char *argv[]) +{ + struct ipc_client *client = NULL; + char *given_test_name; + struct test *test; + int rc = 0; + + if (argc == 2 && !strcmp("list-tests", argv[1])) { + list_tests(argv[0]); + return 0; + } else if (argc == 3 && !strcmp("test", argv[1])) { + given_test_name = argv[2]; + } else { + usage(argv[0]); + return EX_USAGE; + } + + test = get_test(given_test_name); + if (test == NULL) { + printf("Unknown test %s\n", given_test_name); + return EX_USAGE; + } + + client = ipc_client_create(IPC_CLIENT_TYPE_DUMMY); + if (client == NULL) { + printf("[ !! ] Creating client failed\n"); + goto error; + } + + rc = ipc_client_log_callback_register(client, log_callback, NULL); + if (rc < 0) { + printf("[ !! ] Registering log callback failed: error %d\n", rc); + goto error; + } + + rc = test->func(client); + if (rc == 0) { + printf("[ OK ] %s succedded\n", test->name); + } else { + printf("[ !! ] %s failed\n", test->name); + goto error; + } + + return 0; + +error: + if (client != NULL) + ipc_client_destroy(client); + + return EX_SOFTWARE; +} diff --git a/samsung-ipc/tests/partitions/android.c b/samsung-ipc/tests/partitions/android.c new file mode 100644 index 0000000..e6b6252 --- /dev/null +++ b/samsung-ipc/tests/partitions/android.c @@ -0,0 +1,92 @@ +/* + * This file is part of libsamsung-ipc. + * + * Copyright (C) 2021 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org> + * + * libsamsung-ipc is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * libsamsung-ipc is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with libsamsung-ipc. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <errno.h> +#include <fcntl.h> +#include <libgen.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <sys/stat.h> +#include <sys/types.h> + +#include <samsung-ipc.h> + +#include <partitions/android/android.h> + +static char const * const dummy_modem_image_paths[] = { + /* We can't use mktemp here since everything is const + * echo libsamsung-ipc | sha1sum | cut -c-20 + * gives 55f4731d2e11e85bd889 + */ + "/tmp/libsamsung-ipc.55f4731d2e11e85bd889/modem.img", + NULL +}; + + +int create_dummy_modem_image(struct ipc_client *client, const char * const path) +{ + /* TODO: replace it by C code but make sure that the replacement code + * is as robust as the shell commands + */ + system("mkdir -p /tmp/libsamsung-ipc.55f4731d2e11e85bd889/"); + system("touch /tmp/libsamsung-ipc.55f4731d2e11e85bd889/modem.img"); + + return 0; +} + +int test_open_android_modem_partition(struct ipc_client *client) +{ + int fd; + int i; + int rc; + + for (i = 0; dummy_modem_image_paths[i] != NULL; i++) { + rc = create_dummy_modem_image(client, + dummy_modem_image_paths[i]); + if (rc == -1) { + ipc_client_log(client, "%s: create_dummy_modem_image(client, %s) " + "failed\n", + __func__, dummy_modem_image_paths[i]); + return -1; + } + } + + rc = open_android_modem_partition(client, dummy_modem_image_paths); + if (rc == -1) { + rc= errno; + ipc_client_log(client, "%s: open_android_modem_partition" + " failed with errror %d: %s\n", + __func__, rc, strerror(rc)); + errno = rc; + return -1; + } + + rc = close(rc); + if (rc == -1) { + rc = errno; + ipc_client_log(client, "%s: close() failed with errror %d: %s\n", + __func__, rc, strerror(rc)); + return -1; + } + + return 0; +} |