From dc33c2905fccf6de001567e551a7a2eefcce8db5 Mon Sep 17 00:00:00 2001 From: hubin73 Date: Tue, 1 Mar 2022 14:52:11 +0800 Subject: [PATCH] add libmetalink testcases for api and error handling Signed-off-by: hubin73 --- test/Makefile.am | 10 +- test/error.xml_local | 14 + test/test_libmetalink_api.c | 540 ++++++++++++++++++++++++++++++++ test/test_metalink_v3.xml_local | 78 +++++ test/test_metalink_v4.xml_local | 76 +++++ 5 files changed, 715 insertions(+), 3 deletions(-) create mode 100644 test/error.xml_local create mode 100644 test/test_libmetalink_api.c create mode 100644 test/test_metalink_v3.xml_local create mode 100644 test/test_metalink_v4.xml_local diff --git a/test/Makefile.am b/test/Makefile.am index 1447500..a2ee203 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,6 +1,6 @@ if HAVE_CUNIT -check_PROGRAMS = metalinktest +check_PROGRAMS = metalinktest metalinkapitest metalinktest_SOURCES = \ main.c\ metalink_list_test.c metalink_list_test.h\ @@ -11,11 +11,15 @@ metalinktest_SOURCES = \ metalinktest_LDADD = ${top_builddir}/lib/libmetalink.la metalinktest_LDFLAGS = -static @CUNIT_LIBS@ +metalinkapitest_SOURCES = test_libmetalink_api.c +metalinkapitest_LDADD = ${top_builddir}/lib/libmetalink.la +metalinkapitest_LDFLAGS = -static @CUNIT_LIBS@ + AM_CFLAGS = -I${top_srcdir}/lib -I${top_srcdir}/lib/includes \ -I${top_builddir}/lib/includes \ -DLIBMETALINK_TEST_DIR=\"$(top_srcdir)/test/\" @CUNIT_CFLAGS@ -TESTS = metalinktest +TESTS = metalinktest metalinkapitest -EXTRA_DIST = test1.xml test2.xml +EXTRA_DIST = test1.xml test2.xml test_metalink_v3.xml_local test_metalink_v4.xml_local error.xml_local endif # HAVE_CUNIT diff --git a/test/error.xml_local b/test/error.xml_local new file mode 100644 index 0000000..3de45c0 --- /dev/null +++ b/test/error.xml_local @@ -0,0 +1,14 @@ + + + + + + test-md5-hash + test-sha1-hash + + + ftp://ftp.test.com/test.ext + + + + diff --git a/test/test_libmetalink_api.c b/test/test_libmetalink_api.c new file mode 100644 index 0000000..277dd87 --- /dev/null +++ b/test/test_libmetalink_api.c @@ -0,0 +1,540 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved. + * Description: test libmetalink api + * Create: 2022-02-28 + */ + +#include +#include +#include +#include +#include +#include +#define PRIORITY_MAX 1000000 +#define CONN_TEST 1000 +#define LL_TEST 9223372036854775807LL +#define TEST_2 2 +#define TEST_3 3 +#define TEST_4 4 +#define TEST_5 5 +#define TEST_6 6 +#define TEST_7 7 +#define TEST_8 8 +#define TEST_90 90 +#define TEST_99 99 +#define TEST_100 100 +#define LEN_TEST 262144 +#define PUBLISHED_TIME 1641039803 +#define UPDATED_TIME 1643718203 +#define BUF_SIZE 4096 + +metalink_error_t g_r; +metalink_t *g_metalink; +metalink_file_t *g_file; +metalink_checksum_t *g_checksum; +metalink_resource_t *g_resource; +metalink_piece_hash_t *g_piece_hash; +metalink_metaurl_t *g_metaurl; + +size_t countArray(void **array) +{ + size_t count = 0; + while (*array) { + ++count; + ++array; + } + return count; +} + +void checkChecksum(char *type_t, char *hash_t) +{ + CU_ASSERT_STRING_EQUAL(type_t, g_checksum->type); + CU_ASSERT_STRING_EQUAL(hash_t, g_checksum->hash); +} + +void checkPiece(char *type_t, int piece_t) +{ + CU_ASSERT_EQUAL(piece_t, g_piece_hash->piece); + CU_ASSERT_STRING_EQUAL(type_t, g_piece_hash->hash); +} + +void checkResource(char *url_t, char *type_t, char *location_t, int preference_t, int maxconnections_t) +{ + CU_ASSERT_STRING_EQUAL(url_t, g_resource->url); + if (g_metalink->version == METALINK_VERSION_4) { + CU_ASSERT_PTR_NULL(g_resource->type); + } else { + CU_ASSERT_STRING_EQUAL(type_t, g_resource->type); + } + if (strcmp(location_t, "null") == 0) { + CU_ASSERT_PTR_NULL(g_resource->location); /* no location */ + } else { + CU_ASSERT_STRING_EQUAL(location_t, g_resource->location); + } + CU_ASSERT_EQUAL(preference_t, g_resource->preference); + CU_ASSERT_EQUAL(PRIORITY_MAX - preference_t, g_resource->priority); + CU_ASSERT_EQUAL(maxconnections_t, g_resource->maxconnections); +} + +void checkMetalinkItemV3(metalink_version_t version_t, char *origin_t, char *identity_t, char *tags_t) +{ + CU_ASSERT_EQUAL(version_t, g_metalink->version); + CU_ASSERT_STRING_EQUAL(origin_t, g_metalink->origin); + CU_ASSERT(g_metalink->origin_dynamic); + CU_ASSERT_STRING_EQUAL(identity_t, g_metalink->identity); + CU_ASSERT_STRING_EQUAL(tags_t, g_metalink->tags); +} + +void checkFile1V3(void) +{ + g_file = g_metalink->files[0]; + CU_ASSERT_STRING_EQUAL("test_v3.ext", g_file->name); + CU_ASSERT_EQUAL(LL_TEST, g_file->size); + CU_ASSERT_STRING_EQUAL("v1", g_file->version); + CU_ASSERT_EQUAL(1, countArray((void **)g_file->languages)); + CU_ASSERT_STRING_EQUAL("zh-CN", g_file->language); + CU_ASSERT_STRING_EQUAL("zh-CN", g_file->languages[0]); + CU_ASSERT_EQUAL(1, countArray((void **)g_file->oses)); + CU_ASSERT_STRING_EQUAL("Linux-arm", g_file->oses[0]); + CU_ASSERT_STRING_EQUAL("Linux-arm", g_file->os); + CU_ASSERT_EQUAL(CONN_TEST, g_file->maxconnections); + /* verification */ + CU_ASSERT_EQUAL_FATAL(TEST_2, countArray((void **)g_file->checksums)); + g_checksum = g_file->checksums[0]; + checkChecksum("md5", "test-md5-hash"); + g_checksum = g_file->checksums[1]; + checkChecksum("sha1", "test-sha1-hash"); + CU_ASSERT_EQUAL(TEST_2, countArray((void **)g_file->chunk_checksum->piece_hashes)); + CU_ASSERT_STRING_EQUAL("sha1", g_file->chunk_checksum->type); + CU_ASSERT_EQUAL(LEN_TEST, g_file->chunk_checksum->length); + g_piece_hash = g_file->chunk_checksum->piece_hashes[0]; + checkPiece("chunk_checksum_test1", 0); + g_piece_hash = g_file->chunk_checksum->piece_hashes[1]; + checkPiece("chunk_checksum_test3", 1); + /* resources */ + CU_ASSERT_EQUAL_FATAL(TEST_8, countArray((void **)g_file->resources)); + g_resource = g_file->resources[0]; + checkResource("http://test.org/test.ext.torrent", "bittorrent", "null", TEST_100, 0); + g_resource = g_file->resources[1]; + checkResource("ftp://ftp.test.net/test.ext", "ftp", "us", TEST_90, TEST_2); + g_resource = g_file->resources[TEST_2]; + checkResource("http://test.com/test.ext", "http", "uk", TEST_90, TEST_3); + g_resource = g_file->resources[TEST_3]; + checkResource("http://test.net/test.ext", "http", "de", TEST_90, 0); + g_resource = g_file->resources[TEST_4]; + checkResource("ftp://ftp.test.com/test.ext", "ftp", "cn", 0, 1); + g_resource = g_file->resources[TEST_5]; + checkResource("", "rsync", "null", 0, 0); + g_resource = g_file->resources[TEST_6]; + checkResource("", "magnet", "null", 0, 0); + g_resource = g_file->resources[TEST_7]; + checkResource("", "ed2k", "null", 0, 0); +} + +void checkFile2V3(void) +{ + g_file = g_metalink->files[1]; + CU_ASSERT_STRING_EQUAL("bad???", g_file->name); + CU_ASSERT_EQUAL(0, g_file->size); /* bad size, fallback to 0 */ + CU_ASSERT_STRING_EQUAL("???", g_file->version); + CU_ASSERT_EQUAL(1, countArray((void **)g_file->languages)); + CU_ASSERT_STRING_EQUAL("???", g_file->language); + CU_ASSERT_STRING_EQUAL("???", g_file->languages[0]); + CU_ASSERT_EQUAL(1, countArray((void **)g_file->oses)); + CU_ASSERT_STRING_EQUAL("???", g_file->oses[0]); + CU_ASSERT_STRING_EQUAL("???", g_file->os); + CU_ASSERT_EQUAL(0, g_file->maxconnections); + /* verification */ + CU_ASSERT_EQUAL_FATAL(1, countArray((void **)g_file->checksums)); + g_checksum = g_file->checksums[0]; + CU_ASSERT_STRING_EQUAL("???", g_checksum->type); + CU_ASSERT_STRING_EQUAL("???", g_checksum->hash); + CU_ASSERT_EQUAL(1, countArray((void **)g_file->chunk_checksum->piece_hashes)); + CU_ASSERT_STRING_EQUAL("???", g_file->chunk_checksum->type); + CU_ASSERT_EQUAL(0, g_file->chunk_checksum->length); + g_piece_hash = g_file->chunk_checksum->piece_hashes[0]; + CU_ASSERT_EQUAL(0, g_piece_hash->piece); + CU_ASSERT_STRING_EQUAL("???", g_piece_hash->hash); + /* resources */ + CU_ASSERT_EQUAL_FATAL(TEST_2, countArray((void **)g_file->resources)); + g_resource = g_file->resources[0]; + checkResource("???", "???", "??", 0, 0); + g_resource = g_file->resources[1]; + checkResource("http://badpreference/", "http", "null", 0, 0); +} + +void checkFile3V3(void) +{ + g_file = g_metalink->files[TEST_2]; + CU_ASSERT_STRING_EQUAL("test_v3_file2.tar.bz2", g_file->name); + CU_ASSERT_EQUAL(0, g_file->size); /* bad size, fallback to 0 */ + CU_ASSERT_PTR_NULL(g_file->chunk_checksum); /* no chunk checksum */ + CU_ASSERT_PTR_NULL(g_file->checksums); /* no chunk checksum */ + CU_ASSERT_PTR_NULL(g_file->languages); + CU_ASSERT_PTR_NULL(g_file->language); + CU_ASSERT_EQUAL(0, g_file->maxconnections); + CU_ASSERT_PTR_NULL(g_file->os); + CU_ASSERT_PTR_NULL(g_file->oses); + CU_ASSERT_PTR_NULL(g_file->resources); +} + +void testFileV3(void) +{ + checkMetalinkItemV3(METALINK_VERSION_3, "http://example.org/test.metalink.v3", "libmetalink-test-v3", + "metalink_v3.xml"); + /* count files */ + CU_ASSERT_EQUAL_FATAL(TEST_3, countArray((void **)g_metalink->files)); + /* check 1st file */ + checkFile1V3(); + /* check 2nd file */ + checkFile2V3(); + /* check 3rd file */ + checkFile3V3(); +} + +void testMetalinkParseFileV3(void) +{ + g_r = metalink_parse_file("test_metalink_v3.xml_local", &g_metalink); + CU_ASSERT_EQUAL(g_r, 0); + testFileV3(); +} + +void checkMetaurl(char *mediatype_t, char *name_t, char *url_t, int priority_t) +{ + CU_ASSERT_STRING_EQUAL(mediatype_t, g_metaurl->mediatype); + CU_ASSERT_STRING_EQUAL(name_t, g_metaurl->name); + CU_ASSERT_STRING_EQUAL(url_t, g_metaurl->url); + CU_ASSERT_EQUAL(priority_t, g_metaurl->priority); +} + +void checkFile1V4(void) +{ + g_file = g_metalink->files[0]; + CU_ASSERT_STRING_EQUAL("test_v4", g_file->copyright); + CU_ASSERT_STRING_EQUAL("test Metalink library", g_file->description); + CU_ASSERT_STRING_EQUAL("libmetalink-test-v4", g_file->identity); + CU_ASSERT_STRING_EQUAL("zh-CN", g_file->language); + CU_ASSERT_EQUAL(1, countArray((void **)g_file->languages)); + CU_ASSERT_STRING_EQUAL("zh-CN", g_file->languages[0]); + CU_ASSERT_STRING_EQUAL("http://libmetalink-test/logo", g_file->logo); + CU_ASSERT_EQUAL(0, g_file->maxconnections); + CU_ASSERT_STRING_EQUAL("test_v4.ext", g_file->name); + CU_ASSERT_EQUAL(1, countArray((void **)g_file->oses)); + CU_ASSERT_STRING_EQUAL("Linux-arm", g_file->oses[0]); + CU_ASSERT_STRING_EQUAL("Linux-arm", g_file->os); + CU_ASSERT_STRING_EQUAL("libmetalink-test", g_file->publisher_name); + CU_ASSERT_STRING_EQUAL("http://libmetalink-test", g_file->publisher_url); + CU_ASSERT_EQUAL(LL_TEST, g_file->size); + CU_ASSERT_STRING_EQUAL("v1", g_file->version); + /* verification */ + CU_ASSERT_EQUAL_FATAL(TEST_2, countArray((void **)g_file->checksums)); + g_checksum = g_file->checksums[0]; + checkChecksum("md5", "test-md5-hash"); + g_checksum = g_file->checksums[1]; + checkChecksum("sha1", "test-sha1-hash"); + CU_ASSERT_EQUAL(TEST_3, countArray((void **)g_file->chunk_checksum->piece_hashes)); + CU_ASSERT_STRING_EQUAL("sha1", g_file->chunk_checksum->type); + CU_ASSERT_EQUAL(LEN_TEST, g_file->chunk_checksum->length); + g_piece_hash = g_file->chunk_checksum->piece_hashes[0]; + checkPiece("chunk_checksum_test1", 0); + g_piece_hash = g_file->chunk_checksum->piece_hashes[1]; + checkPiece("chunk_checksum_test2", 0); + g_piece_hash = g_file->chunk_checksum->piece_hashes[TEST_2]; + checkPiece("chunk_checksum_test3", 0); + CU_ASSERT_STRING_EQUAL("pgp", g_file->signature->mediatype); + CU_ASSERT_STRING_EQUAL("test_signature", g_file->signature->signature); + /* resources */ + CU_ASSERT_EQUAL_FATAL(TEST_2, countArray((void **)g_file->metaurls)); + g_metaurl = g_file->metaurls[0]; + checkMetaurl("torrent", "test1", "http://test.org/test.ext.torrent", TEST_100); + g_metaurl = g_file->metaurls[1]; + checkMetaurl("test", "test2", "http://test.org/test.ext", PRIORITY_MAX - 1); + g_resource = g_file->resources[0]; + checkResource("ftp://ftp.test.net/test.ext", "null", "us", PRIORITY_MAX - TEST_90, 0); + g_resource = g_file->resources[1]; + checkResource("http://test.com/test.ext", "null", "uk", PRIORITY_MAX - TEST_90, 0); + g_resource = g_file->resources[TEST_2]; + checkResource("http://httphost/test.tar.bz2", "null", "null", PRIORITY_MAX - TEST_99, 0); + g_resource = g_file->resources[TEST_3]; + checkResource("ftp://ftp.test.com/test.ext", "null", "cn", 1, 0); + g_resource = g_file->resources[TEST_4]; + checkResource("http://httphost/nopriority", "null", "null", 1, 0); +} + +void checkFile2V4(void) +{ + g_file = g_metalink->files[1]; + /* verification */ + CU_ASSERT_EQUAL_FATAL(1, countArray((void **)g_file->checksums)); + g_checksum = g_file->checksums[0]; + CU_ASSERT_STRING_EQUAL("???", g_checksum->type); + CU_ASSERT_STRING_EQUAL("???", g_checksum->hash); + CU_ASSERT_EQUAL(1, countArray((void **)g_file->chunk_checksum->piece_hashes)); + CU_ASSERT_STRING_EQUAL("???", g_file->chunk_checksum->type); + CU_ASSERT_EQUAL(0, g_file->chunk_checksum->length); + g_piece_hash = g_file->chunk_checksum->piece_hashes[0]; + CU_ASSERT_EQUAL(0, g_piece_hash->piece); + CU_ASSERT_STRING_EQUAL("???", g_piece_hash->hash); + CU_ASSERT_STRING_EQUAL("???", g_file->signature->mediatype); + CU_ASSERT_STRING_EQUAL("???", g_file->signature->signature); + CU_ASSERT_STRING_EQUAL("???", g_file->language); + CU_ASSERT_EQUAL(1, countArray((void **)g_file->languages)); + CU_ASSERT_STRING_EQUAL("???", g_file->languages[0]); + CU_ASSERT_EQUAL(0, g_file->maxconnections); + CU_ASSERT_STRING_EQUAL("bad???", g_file->name); + CU_ASSERT_EQUAL(1, countArray((void **)g_file->oses)); + CU_ASSERT_STRING_EQUAL("???", g_file->oses[0]); + CU_ASSERT_STRING_EQUAL("???", g_file->os); + CU_ASSERT_STRING_EQUAL("???", g_file->copyright); + CU_ASSERT_STRING_EQUAL("???", g_file->description); + CU_ASSERT_STRING_EQUAL("???", g_file->identity); + CU_ASSERT_STRING_EQUAL("???", g_file->logo); + CU_ASSERT_STRING_EQUAL("???", g_file->publisher_name); + CU_ASSERT_STRING_EQUAL("???", g_file->publisher_url); + /* resources */ + CU_ASSERT_EQUAL_FATAL(TEST_2, countArray((void **)g_file->metaurls)); + g_metaurl = g_file->metaurls[0]; + checkMetaurl("???", "???", "???", 0); + g_metaurl = g_file->metaurls[1]; + checkMetaurl("-100", "-100", "-100", PRIORITY_MAX - 1); + g_resource = g_file->resources[0]; + checkResource("???", "null", "???", PRIORITY_MAX, 0); + g_resource = g_file->resources[1]; + checkResource("-100", "null", "-100", 1, 0); + CU_ASSERT_EQUAL(0, g_file->size); + CU_ASSERT_STRING_EQUAL("???", g_file->version); +} + +void checkFile3V4(void) +{ + g_file = g_metalink->files[TEST_2]; + CU_ASSERT_STRING_EQUAL("test_file2.tar.bz2", g_file->name); + CU_ASSERT_EQUAL(0, g_file->size); + CU_ASSERT_PTR_NULL(g_file->version); + CU_ASSERT_PTR_NULL(g_file->signature); + CU_ASSERT_PTR_NULL(g_file->chunk_checksum); /* no chunk checksum */ + CU_ASSERT_PTR_NULL(g_file->checksums); + CU_ASSERT_PTR_NULL(g_file->languages); + CU_ASSERT_PTR_NULL(g_file->language); + CU_ASSERT_EQUAL(0, g_file->maxconnections); + CU_ASSERT_PTR_NULL(g_file->os); + CU_ASSERT_PTR_NULL(g_file->oses); + CU_ASSERT_PTR_NULL(g_file->resources); + CU_ASSERT_PTR_NULL(g_file->publisher_name); + CU_ASSERT_PTR_NULL(g_file->publisher_url); + CU_ASSERT_PTR_NULL(g_file->metaurls); + CU_ASSERT_PTR_NULL(g_file->logo); + CU_ASSERT_PTR_NULL(g_file->identity); + CU_ASSERT_PTR_NULL(g_file->description); + CU_ASSERT_PTR_NULL(g_file->copyright); +} + +void testFileV4(void) +{ + CU_ASSERT_EQUAL(g_metalink->version, METALINK_VERSION_4); + CU_ASSERT_STRING_EQUAL("MetalinkEditor/2.0test_v4", g_metalink->generator); + CU_ASSERT_STRING_EQUAL("http://example.org/test.metalink.v4", g_metalink->origin); + CU_ASSERT(g_metalink->origin_dynamic); + CU_ASSERT_EQUAL(g_metalink->published, PUBLISHED_TIME); + CU_ASSERT_EQUAL(g_metalink->updated, UPDATED_TIME); + /* count files */ + CU_ASSERT_EQUAL_FATAL(TEST_3, countArray((void **)g_metalink->files)); + /* check 1st file */ + checkFile1V4(); + /* check 1st file */ + checkFile2V4(); + /* check 1st file */ + checkFile3V4(); +} + +void testMetalinkParseFileV4(void) +{ + g_r = metalink_parse_file("test_metalink_v4.xml_local", &g_metalink); + CU_ASSERT_EQUAL(g_r, 0); + testFileV4(); +} + +void testMetalinkParseFpV3(void) +{ + FILE *fp; + fp = fopen("test_metalink_v3.xml_local", "rb"); + g_r = metalink_parse_fp(fp, &g_metalink); + CU_ASSERT_EQUAL(g_r, 0); + fclose(fp); + testFileV3(); +} + +void testMetalinkParseFpV4(void) +{ + FILE *fp; + fp = fopen("test_metalink_v4.xml_local", "rb"); + g_r = metalink_parse_fp(fp, &g_metalink); + CU_ASSERT_EQUAL(g_r, 0); + fclose(fp); + testFileV4(); +} + +void testMetalinkParseFdV3(void) +{ + int fd; + fd = open("test_metalink_v3.xml_local", O_RDONLY); + g_r = metalink_parse_fd(fd, &g_metalink); + CU_ASSERT_EQUAL(g_r, 0); + close(fd); + testFileV3(); +} + +void testMetalinkParseFdV4(void) +{ + int fd; + fd = open("test_metalink_v4.xml_local", O_RDONLY); + g_r = metalink_parse_fd(fd, &g_metalink); + CU_ASSERT_EQUAL(g_r, 0); + close(fd); + testFileV4(); +} + +void testMetalinkParseMemoryV3(void) +{ + int fd; + fd = open("test_metalink_v3.xml_local", O_RDONLY); + while (1) { + char buf[BUF_SIZE]; + ssize_t nread; + while ((nread = read(fd, buf, sizeof(buf))) == -1 && errno == EINTR); + CU_ASSERT_FATAL(nread != -1); + if (nread == 0) { + break; + } + g_r = metalink_parse_memory(buf, nread, &g_metalink); + CU_ASSERT_EQUAL(g_r, 0); + } + close(fd); + testFileV3(); +} + +void testMetalinkParseMemoryV4(void) +{ + int fd; + fd = open("test_metalink_v4.xml_local", O_RDONLY); + while (1) { + char buf[BUF_SIZE]; + ssize_t nread; + while ((nread = read(fd, buf, sizeof(buf))) == -1 && errno == EINTR); + CU_ASSERT_FATAL(nread != -1); + if (nread == 0) { + break; + } + g_r = metalink_parse_memory(buf, nread, &g_metalink); + CU_ASSERT_EQUAL(g_r, 0); + } + close(fd); + testFileV4(); +} + +void testMetalinkParseFileNotExist(void) +{ + g_r = metalink_parse_file("not_exist_file", &g_metalink); + CU_ASSERT_EQUAL(g_r, METALINK_ERR_CANNOT_OPEN_FILE); +} + +void testMetalinkParseFileError(void) +{ + g_r = metalink_parse_file("error.xml_local", &g_metalink); + CU_ASSERT_EQUAL(g_r, METALINK_ERR_PARSER_ERROR); +} + +void testMetalinkParseFpError(void) +{ + FILE *fp; + fp = fopen("error.xml_local", "rb"); + g_r = metalink_parse_fp(fp, &g_metalink); + CU_ASSERT_EQUAL(g_r, METALINK_ERR_PARSER_ERROR); + fclose(fp); +} + +void testMetalinkParseFdError(void) +{ + int fd; + fd = open("error.xml_local", O_RDONLY); + g_r = metalink_parse_fd(fd, &g_metalink); + CU_ASSERT_EQUAL(g_r, METALINK_ERR_PARSER_ERROR); + close(fd); +} + +void testMetalinkParseMemoryError(void) +{ + int fd; + fd = open("error.xml_local", O_RDONLY); + while (1) { + char buf[BUF_SIZE]; + ssize_t nread; + while ((nread = read(fd, buf, sizeof(buf))) == -1 && errno == EINTR); + CU_ASSERT_FATAL(nread != -1); + if (nread == 0) { + break; + } + g_r = metalink_parse_memory(buf, nread, &g_metalink); + CU_ASSERT_EQUAL(g_r, METALINK_ERR_PARSER_ERROR); + } + close(fd); +} + +static int init_suite(void) +{ + return 0; +} + +static int clean_suite(void) +{ + return 0; +} + + +int main(void) +{ + CU_pSuite pSuite; + unsigned int num_tests_failed; + + /* initialize the CUnit test registry */ + if (CUE_SUCCESS != CU_initialize_registry()) + return CU_get_error(); + + /* add a suite to the registry */ + pSuite = CU_add_suite("libmetalink_TestSuite", init_suite, clean_suite); + if (NULL == pSuite) { + CU_cleanup_registry(); + return CU_get_error(); + } + + /* add the tests to the suite */ + if ((!CU_add_test(pSuite, "test of metalink_parse_file file not exist", testMetalinkParseFileNotExist)) || + (!CU_add_test(pSuite, "test of metalink_parse_file error file", testMetalinkParseFileError)) || + (!CU_add_test(pSuite, "test of metalink_parse_file v3", testMetalinkParseFileV3)) || + (!CU_add_test(pSuite, "test of metalink_parse_file v4", testMetalinkParseFileV4)) || + (!CU_add_test(pSuite, "test of metalink_parse_fp error file", testMetalinkParseFpError)) || + (!CU_add_test(pSuite, "test of metalink_parse_fd error file", testMetalinkParseFdError)) || + (!CU_add_test(pSuite, "test of metalink_parse_memory error file", testMetalinkParseMemoryError)) || + (!CU_add_test(pSuite, "test of metalink_parse_fp v3", testMetalinkParseFpV3)) || + (!CU_add_test(pSuite, "test of metalink_parse_fp v4", testMetalinkParseFpV4)) || + (!CU_add_test(pSuite, "test of metalink_parse_fd v3", testMetalinkParseFdV3)) || + (!CU_add_test(pSuite, "test of metalink_parse_fd v4", testMetalinkParseFdV4)) || + (!CU_add_test(pSuite, "test of metalink_parse_memory v3", testMetalinkParseMemoryV3)) || + (!CU_add_test(pSuite, "test of metalink_parse_memory v4", testMetalinkParseMemoryV4))) { + CU_cleanup_registry(); + return CU_get_error(); + } + + /* Run all tests using the CUnit Basic interface */ + CU_basic_set_mode(CU_BRM_VERBOSE); + CU_basic_run_tests(); + num_tests_failed = CU_get_number_of_tests_failed(); + CU_cleanup_registry(); + if (CU_get_error() == CUE_SUCCESS) { + return num_tests_failed; + } else { + printf("CUnit Error: %s\n", CU_get_error_msg()); + return CU_get_error(); + } +} + diff --git a/test/test_metalink_v3.xml_local b/test/test_metalink_v3.xml_local new file mode 100644 index 0000000..e987f83 --- /dev/null +++ b/test/test_metalink_v3.xml_local @@ -0,0 +1,78 @@ + + + libmetalink-test-v3 + metalink_v3.xml + + + 9223372036854775807 + v1 + zh-CN + Linux-arm + + test-md5-hash + test-sha1-hash + + notype + + chunk_checksum_test1 + + chunk_checksum_test2 + chunk_checksum_test3 + + + + ftp://ftp.test.com/test.ext + ftp://ftp.test.net/test.ext + http://test.com/test.ext + http://test.net/test.ext + + http://test.org/test.ext.torrent + + + + + + + + abc + ??? + ??? + ??? + + ??? + + ??? + + + + + ??? + + http://badpreference/ + + + + + + + + + http://mirror2/libmetalink-0.0.2a.tar.bz2 + + + + + + http://noname/file + + + + + + ftp://host/file + + + + + + diff --git a/test/test_metalink_v4.xml_local b/test/test_metalink_v4.xml_local new file mode 100644 index 0000000..47bfb8c --- /dev/null +++ b/test/test_metalink_v4.xml_local @@ -0,0 +1,76 @@ + + + MetalinkEditor/2.0test_v4 + http://example.org/test.metalink.v4 + 2022-01-01T12:23:23+00:00 + 2022-02-01T12:23:23+00:00 + metalink_v4.xml + + 9223372036854775807 + v1 + zh-CN + Linux-arm + libmetalink-test-v4 + test-md5-hash + test-sha1-hash + + notype + + chunk_checksum_test1 + + chunk_checksum_test2 + chunk_checksum_test3 + + test_signature + test_v4 + test Metalink library + http://libmetalink-test/logo + + ftp://ftp.test.com/test.ext + ftp://ftp.test.net/test.ext + http://test.com/test.ext + + http://httphost/test.tar.bz2 + + http://httphost/nopriority + http://test.org/test.ext.torrent + http://test.org/test.ext + + + + abc + ??? + ??? + ??? + ??? + ??? + + ??? + + ??? + ??? + ??? + ??? + + + ??? + -100 + ??? + -100 + + + + + + + + + + http://noname/file + + + + ftp://host/file + + + -- 2.27.0