mirror of
https://git.sr.ht/~thestr4ng3r/chiaki
synced 2025-08-14 02:36:51 -07:00
Add Basic Tests
This commit is contained in:
parent
27286817db
commit
546c452755
7 changed files with 143 additions and 42 deletions
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
[submodule "test/munit"]
|
||||||
|
path = test/munit
|
||||||
|
url = https://github.com/nemequ/munit.git
|
|
@ -3,5 +3,11 @@ cmake_minimum_required(VERSION 3.2)
|
||||||
|
|
||||||
project(chiaki)
|
project(chiaki)
|
||||||
|
|
||||||
|
option(CHIAKI_ENABLE_TESTS "Enable tests for Chiaki" ON)
|
||||||
|
|
||||||
add_subdirectory(lib)
|
add_subdirectory(lib)
|
||||||
add_subdirectory(test)
|
|
||||||
|
if(CHIAKI_ENABLE_TESTS)
|
||||||
|
enable_testing()
|
||||||
|
add_subdirectory(test)
|
||||||
|
endif()
|
41
gui/main.c
Normal file
41
gui/main.c
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
#include <chiaki/session.h>
|
||||||
|
#include <chiaki/base64.h>
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int main(int argc, const char *argv[])
|
||||||
|
{
|
||||||
|
if(argc != 6)
|
||||||
|
{
|
||||||
|
printf("Usage: %s <host> <registkey> <ostype> <auth> <morning (base64)>\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;
|
||||||
|
}
|
|
@ -1,5 +1,11 @@
|
||||||
|
|
||||||
add_executable(chiaki-test
|
add_library(munit "${CMAKE_CURRENT_SOURCE_DIR}/munit/munit.c")
|
||||||
main.c)
|
target_include_directories(munit PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/munit")
|
||||||
|
|
||||||
target_link_libraries(chiaki-test chiaki-lib)
|
add_executable(chiaki-unit
|
||||||
|
main.c
|
||||||
|
http.c)
|
||||||
|
|
||||||
|
target_link_libraries(chiaki-unit chiaki-lib munit)
|
||||||
|
|
||||||
|
add_test(unit chiaki-unit)
|
57
test/http.c
Normal file
57
test/http.c
Normal file
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <munit.h>
|
||||||
|
|
||||||
|
#include <chiaki/http.h>
|
||||||
|
|
||||||
|
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 }
|
||||||
|
};
|
63
test/main.c
63
test/main.c
|
@ -1,42 +1,29 @@
|
||||||
|
|
||||||
#include <chiaki/session.h>
|
|
||||||
#include <chiaki/base64.h>
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <munit.h>
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
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)
|
return munit_suite_main(&suite_main, NULL, argc, argv);
|
||||||
{
|
}
|
||||||
printf("Usage: %s <host> <registkey> <ostype> <auth> <morning (base64)>\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;
|
|
||||||
}
|
|
||||||
|
|
1
test/munit
Submodule
1
test/munit
Submodule
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit 439de4a9b136bc3b5163e73d4caf37c590bef875
|
Loading…
Add table
Add a link
Reference in a new issue