diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..4b398d5 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "test/munit"] + path = test/munit + url = https://github.com/nemequ/munit.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 9dcf447..4dc8837 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,5 +3,11 @@ cmake_minimum_required(VERSION 3.2) project(chiaki) +option(CHIAKI_ENABLE_TESTS "Enable tests for Chiaki" ON) + add_subdirectory(lib) -add_subdirectory(test) \ No newline at end of file + +if(CHIAKI_ENABLE_TESTS) + enable_testing() + add_subdirectory(test) +endif() \ No newline at end of file diff --git a/gui/main.c b/gui/main.c new file mode 100644 index 0000000..a9dd25b --- /dev/null +++ b/gui/main.c @@ -0,0 +1,41 @@ +#include +#include + +#include +#include + +int main(int argc, const char *argv[]) +{ + if(argc != 6) + { + printf("Usage: %s \n", argv[0]); + return 1; + } + + ChiakiConnectInfo connect_info; + connect_info.host = argv[1]; + connect_info.regist_key = argv[2]; + connect_info.ostype = argv[3]; + + size_t auth_len = strlen(argv[4]); + if(auth_len > sizeof(connect_info.auth)) + auth_len = sizeof(connect_info.auth); + memcpy(connect_info.auth, argv[4], auth_len); + if(auth_len < sizeof(connect_info.auth)) + memset(connect_info.auth + auth_len, 0, sizeof(connect_info.auth) - auth_len); + + size_t morning_size = sizeof(connect_info.morning); + bool r = chiaki_base64_decode(argv[5], strlen(argv[5]), connect_info.morning, &morning_size); + if(!r || morning_size != sizeof(connect_info.morning)) + { + printf("morning invalid.\n"); + return 1; + } + + ChiakiSession session; + chiaki_session_init(&session, &connect_info); + chiaki_session_start(&session); + chiaki_session_join(&session); + chiaki_session_fini(&session); + return 0; +} \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 6c42fb5..1168217 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,5 +1,11 @@ -add_executable(chiaki-test - main.c) +add_library(munit "${CMAKE_CURRENT_SOURCE_DIR}/munit/munit.c") +target_include_directories(munit PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/munit") -target_link_libraries(chiaki-test chiaki-lib) \ No newline at end of file +add_executable(chiaki-unit + main.c + http.c) + +target_link_libraries(chiaki-unit chiaki-lib munit) + +add_test(unit chiaki-unit) \ No newline at end of file diff --git a/test/http.c b/test/http.c new file mode 100644 index 0000000..99dc984 --- /dev/null +++ b/test/http.c @@ -0,0 +1,57 @@ +/* + * This file is part of Chiaki. + * + * Chiaki 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 3 of the License, or + * (at your option) any later version. + * + * Chiaki 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 Chiaki. If not, see . + */ + +#include + +#include + +static const char *response = + "HTTP/1.1 200 OK\r\n" + "Content-type: text/html, text, plain\r\n" + "\r\n"; + +static void *test_http_response_parse_setup(const MunitParameter params[], void *user) +{ + return strdup(response); +} + +static void test_http_response_parse_teardown(void *fixture) +{ + free(fixture); +} + +static MunitResult test_http_response_parse(const MunitParameter params[], void *fixture) +{ + char *buf = fixture; + ChiakiHttpResponse parsed_response; + ChiakiErrorCode err = chiaki_http_response_parse(&parsed_response, buf, strlen(buf)); + munit_assert_int(err, ==, CHIAKI_ERR_SUCCESS); + munit_assert_int(parsed_response.code, ==, 200); + return MUNIT_OK; +} + +MunitTest tests_http[] = { + { + "/response_parse", + test_http_response_parse, + test_http_response_parse_setup, + test_http_response_parse_teardown, + MUNIT_TEST_OPTION_NONE, + NULL + }, + { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } +}; \ No newline at end of file diff --git a/test/main.c b/test/main.c index cee38eb..2d363a2 100644 --- a/test/main.c +++ b/test/main.c @@ -1,42 +1,29 @@ -#include -#include -#include -#include +#include -int main(int argc, const char *argv[]) +extern MunitTest tests_http[]; + +static MunitSuite suites[] = { + { + "/http", + tests_http, + NULL, + 1, + MUNIT_SUITE_OPTION_NONE + }, + { NULL, NULL, NULL, 0, MUNIT_SUITE_OPTION_NONE } +}; + +static const MunitSuite suite_main = { + "/chiaki", + NULL, + suites, + 1, + MUNIT_SUITE_OPTION_NONE +}; + +int main(int argc, char *argv[]) { - if(argc != 6) - { - printf("Usage: %s \n", argv[0]); - return 1; - } - - ChiakiConnectInfo connect_info; - connect_info.host = argv[1]; - connect_info.regist_key = argv[2]; - connect_info.ostype = argv[3]; - - size_t auth_len = strlen(argv[4]); - if(auth_len > sizeof(connect_info.auth)) - auth_len = sizeof(connect_info.auth); - memcpy(connect_info.auth, argv[4], auth_len); - if(auth_len < sizeof(connect_info.auth)) - memset(connect_info.auth + auth_len, 0, sizeof(connect_info.auth) - auth_len); - - size_t morning_size = sizeof(connect_info.morning); - bool r = chiaki_base64_decode(argv[5], strlen(argv[5]), connect_info.morning, &morning_size); - if(!r || morning_size != sizeof(connect_info.morning)) - { - printf("morning invalid.\n"); - return 1; - } - - ChiakiSession session; - chiaki_session_init(&session, &connect_info); - chiaki_session_start(&session); - chiaki_session_join(&session); - chiaki_session_fini(&session); - return 0; -} \ No newline at end of file + return munit_suite_main(&suite_main, NULL, argc, argv); +} diff --git a/test/munit b/test/munit new file mode 160000 index 0000000..439de4a --- /dev/null +++ b/test/munit @@ -0,0 +1 @@ +Subproject commit 439de4a9b136bc3b5163e73d4caf37c590bef875