!5 add libmetalink testcases for api and error handling, fix automake error

From: @hubin95 
Reviewed-by: @hanxinke 
Signed-off-by: @hanxinke
This commit is contained in:
openeuler-ci-bot 2022-03-01 10:48:52 +00:00 committed by Gitee
commit edd24b0809
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 796 additions and 3 deletions

View File

@ -0,0 +1,783 @@
From dc33c2905fccf6de001567e551a7a2eefcce8db5 Mon Sep 17 00:00:00 2001
From: hubin73 <hubin73@huawei.com>
Date: Tue, 1 Mar 2022 14:52:11 +0800
Subject: [PATCH] add libmetalink testcases for api and error handling
Signed-off-by: hubin73 <hubin73@huawei.com>
---
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 @@
+<?xml version="1.0" encoding="utf-8"?>
+<metalink version="3.0" xmlns="http://www.metalinker.org/">
+ <files>
+ <file name="libmetalink-0.0.1.tar.bz2">
+ <verification>
+ <hash type="md5">test-md5-hash</hash>
+ <hash type="sha1">test-sha1-hash</hash>
+ </verification>
+ <resources maxconnections="1">
+ <url type="ftp" location="cn" preference="90">ftp://ftp.test.com/test.ext</url>
+ </file>
+ </files>
+</metalink>
+
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 <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <metalink/metalink.h>
+#include <CUnit/Basic.h>
+#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 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<metalink version="3.0" xmlns="http://www.metalinker.org/" origin="http://example.org/test.metalink.v3" type="dynamic">
+ <identity>libmetalink-test-v3</identity>
+ <tags>metalink_v3.xml</tags>
+ <files>
+ <file name="test_v3.ext">
+ <size>9223372036854775807</size>
+ <version>v1</version>
+ <language>zh-CN</language>
+ <os>Linux-arm</os>
+ <verification>
+ <hash type="md5">test-md5-hash</hash>
+ <hash type="sha1">test-sha1-hash</hash>
+ <!-- no type -->
+ <hash>notype</hash>
+ <pieces length="262144" type="sha1">
+ <hash piece="0">chunk_checksum_test1</hash>
+ <!-- no piece -->
+ <hash>chunk_checksum_test2</hash>
+ <hash piece="1">chunk_checksum_test3</hash>
+ </pieces>
+ </verification>
+ <resources maxconnections="1000">
+ <url type="ftp" location="cn" maxconnections="1">ftp://ftp.test.com/test.ext</url>
+ <url type="ftp" location="us" preference="90" maxconnections="2">ftp://ftp.test.net/test.ext</url>
+ <url type="http" location="uk" preference="90" maxconnections="3">http://test.com/test.ext</url>
+ <url type="http" location="de" preference="90">http://test.net/test.ext</url>
+ <!-- no location -->
+ <url type="bittorrent" preference="100">http://test.org/test.ext.torrent</url>
+ <url type="rsync"/>
+ <url type="magnet"/>
+ <url type="ed2k"/>
+ </resources>
+ </file>
+ <file name="bad???">
+ <!-- bad size -->
+ <size>abc</size>
+ <version>???</version>
+ <language>???</language>
+ <os>???</os>
+ <verification>
+ <hash type="???">???</hash>
+ <pieces length="abc" type="???">
+ <hash piece="???">???</hash>
+ </pieces>
+ </verification>
+ <!-- bad maxconnections -->
+ <resources maxconnections="-1">
+ <url type="???" location="??" maxconnections="??">???</url>
+ <!-- bad preference -->
+ <url type="http" preference="-100" maxconnections="-1">http://badpreference/</url>
+ </resources>
+ </file>
+ <file name="test_v3_file2.tar.bz2">
+ <!-- no size tag -->
+ <!-- no checksum -->
+ <!-- no maxconnections -->
+ <resources>
+ <!-- no type -->
+ <url>http://mirror2/libmetalink-0.0.2a.tar.bz2</url>
+ </resources>
+ </file>
+ <!-- no name -->
+ <file>
+ <resources>
+ <url type="http">http://noname/file</url>
+ </resources>
+ </file>
+ <!-- unsafe path -->
+ <file name="../unsafe">
+ <resources>
+ <url type="ftp" preference="100">ftp://host/file</url>
+ </resources>
+ </file>
+ </files>
+</metalink>
+
+
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 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<metalink xmlns="urn:ietf:params:xml:ns:metalink">
+ <generator>MetalinkEditor/2.0test_v4</generator>
+ <origin dynamic="true">http://example.org/test.metalink.v4</origin>
+ <published>2022-01-01T12:23:23+00:00</published>
+ <updated>2022-02-01T12:23:23+00:00</updated>
+ <tags>metalink_v4.xml</tags>
+ <file name="test_v4.ext">
+ <size>9223372036854775807</size>
+ <version>v1</version>
+ <language>zh-CN</language>
+ <os>Linux-arm</os>
+ <identity>libmetalink-test-v4</identity>
+ <hash type="md5">test-md5-hash</hash>
+ <hash type="sha1">test-sha1-hash</hash>
+ <!-- no type -->
+ <hash>notype</hash>
+ <pieces length="262144" type="sha1">
+ <hash piece="0">chunk_checksum_test1</hash>
+ <!-- no piece -->
+ <hash>chunk_checksum_test2</hash>
+ <hash piece="1">chunk_checksum_test3</hash>
+ </pieces>
+ <signature mediatype="pgp">test_signature</signature>
+ <copyright>test_v4</copyright>
+ <description>test Metalink library</description>
+ <logo>http://libmetalink-test/logo</logo>
+ <publisher name="libmetalink-test" url="http://libmetalink-test"/>
+ <url location="cn">ftp://ftp.test.com/test.ext</url>
+ <url priority="90" location="us">ftp://ftp.test.net/test.ext</url>
+ <url priority="90" location="uk">http://test.com/test.ext</url>
+ <!-- no location -->
+ <url priority="99">http://httphost/test.tar.bz2</url>
+ <!-- no priority -->
+ <url>http://httphost/nopriority</url>
+ <metaurl mediatype="torrent" priority="100" name="test1">http://test.org/test.ext.torrent</metaurl>
+ <metaurl mediatype="test" name="test2">http://test.org/test.ext</metaurl>
+ </file>
+ <file name="bad???">
+ <!-- bad size -->
+ <size>abc</size>
+ <version>???</version>
+ <language>???</language>
+ <os>???</os>
+ <identity>???</identity>
+ <hash type="???">???</hash>
+ <pieces length="???" type="???">
+ <hash>???</hash>
+ </pieces>
+ <signature mediatype="???">???</signature>
+ <copyright>???</copyright>
+ <description>???</description>
+ <logo>???</logo>
+ <publisher name="???" url="???"/>
+ <!-- bad priority -->
+ <url priority="???" location="???">???</url>
+ <url priority="-100" location="-100">-100</url>
+ <metaurl mediatype="???" priority="??" name="???">???</metaurl>
+ <metaurl mediatype="-100" priority="-100" name="-100">-100</metaurl>
+ </file>
+ <file name="test_file2.tar.bz2">
+ <!-- no size tag -->
+ <!-- no checksum -->
+ <!-- no maxconnections -->
+ <!-- no type -->
+ </file>
+ <!-- no name -->
+ <file>
+ <url>http://noname/file</url>
+ </file>
+ <!-- unsafe path -->
+ <file name="../unsafe">
+ <url priority="100">ftp://host/file</url>
+ </file>
+</metalink>
+
--
2.27.0

View File

@ -1,14 +1,15 @@
Name: libmetalink
Version: 0.1.3
Release: 9
Release: 10
Summary: Metalink library written in C
License: MIT
URL: https://launchpad.net/libmetalink
Source0: https://launchpad.net/libmetalink/trunk/%{name}-%{version}/+download/%{name}-%{version}.tar.bz2
BuildRequires: gcc expat-devel CUnit-devel
BuildRequires: gcc expat-devel libxml2-devel CUnit-devel autoconf automake libtool
Patch000:0001-fix-warning-_BSD_SOURCE-and-_SVID_SOURCE-are-depreca.patch
Patch001:0001-fix-warning-_BSD_SOURCE-and-_SVID_SOURCE-are-depreca.patch
Patch002:0002-add-libmetalink-testcases-for-api-and-error-handling.patch
%description
libmetalink is a Metalink library written in C language. It is intended to
@ -31,6 +32,9 @@ The %{name}-doc package contains document files.
%autosetup -p1
%build
autoreconf -i
automake --add-missing
autoconf
%configure --disable-static
%make_build
@ -60,6 +64,12 @@ make check
%{_mandir}/man3/*
%changelog
* Tue Mar 01 2022 Hu Bin <hubin73@huawei.com> - 0.1.3-10
- Type:enhancement
- ID:NA
- SUG:NA
- DESC:add libmetalink testcases for api and error handling, fix automake error
* Mon Jan 17 2022 Hu Bin <hubin57@huawei.com> - 0.1.3-9
- Type:bugfix
- ID:NA