Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
elf: add unit test for pkg_analyse_elf + initialization fixes
Baptiste Daroussin committed 1 year ago
commit 7fb71e99407f864bbb4740e6b78b0e07ef40997c
parent bbecfc2
6 files changed +85 -14
modified libpkg/pkg_abi.c
@@ -36,20 +36,10 @@

#include "private/pkg.h"
#include "private/event.h"
+
#include "private/binfmt.h"

#define _PATH_UNAME "/usr/bin/uname"

-
int pkg_get_myarch_elfparse(int fd, struct os_info *oi);
-
int pkg_analyse_init_elf(const char* stage);
-
int pkg_analyse_elf(const bool developer_mode, struct pkg *pkg, const char *fpath);
-
int pkg_analyse_close_elf();
-

-
int pkg_get_myarch_macho(int fd, struct os_info *oi);
-
int pkg_analyse_init_macho(const char* stage);
-
int pkg_analyse_macho(const bool developer_mode, struct pkg *pkg, const char *fpath);
-
int pkg_analyse_close_macho();
-

-

/* All possibilities on FreeBSD as of 5/26/2014 */
struct arch_trans {
	const char *elftype;
modified libpkg/pkg_elf.c
@@ -66,6 +66,7 @@
#include "private/event.h"
#include "private/elf_tables.h"
#include "private/ldconfig.h"
+
#include "private/binfmt.h"

#ifndef NT_ABI_TAG
#define NT_ABI_TAG 1
@@ -282,6 +283,12 @@ analyse_elf(struct pkg *pkg, const char *fpath)
		return (EPKG_FATAL);
	}

+
	if (elf_version(EV_CURRENT) == EV_NONE) {
+
		pkg_emit_error("ELF library initialization failed: %s",
+
		    elf_errmsg(-1));
+
		return (EPKG_FATAL);
+
	}
+

	if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
		ret = EPKG_FATAL;
		pkg_debug(1, "elf_begin() for %s failed: %s", fpath,
modified libpkg/pkg_macho.c
@@ -47,6 +47,7 @@
#include "pkg.h"
#include "private/pkg.h"
#include "private/event.h"
+
#include "private/binfmt.h"

static const char * const system_dylib_prefixes[] = {
	"/System/",
@@ -107,7 +108,7 @@ analyse_macho(struct pkg *pkg, const char *fpath,
			xasprintf(&libname, "%s.%s", march->mat_install_name, ai->name);
			pkg_addshlib_provided(pkg, libname);
			is_shlib = true;
-
		
+


		/* Now find all dependencies */
		for (macho_loadcmd_t *cmd = march->mat_loadcmds; cmd != NULL; cmd = cmd->next) {
modified tests/Makefile.autosetup
@@ -13,7 +13,8 @@ TESTS= \
	ssh \
	utils \
	metalog \
-
	vec
+
	vec \
+
	pkg_elf

TESTS_SH= \
	frontend/pkg.sh \
@@ -94,6 +95,7 @@ ssh_OBJS= lib/ssh.o
utils_OBJS=	lib/utils.o
metalog_OBJS=	lib/metalog.o
vec_OBJS=	lib/vec.o
+
pkg_elf_OBJS=	lib/pkg_elf.o

SRCS=	\
	$(packing_OBJS:.o=.c) \
@@ -108,7 +110,8 @@ SRCS= \
	$(ssh_OBJS:.o=.c) \
	$(utils_OBJS:.o=.c) \
	$(metalog_OBJS:.o=.c) \
-
	$(vec_OBJS:.o=.c)
+
	$(vec_OBJS:.o=.c) \
+
	$(pkg_elf_OBJS:.o=.c)

include $(MK)/common.mk

added tests/frontend/libtestfbsd.so.1
added tests/lib/pkg_elf.c
@@ -0,0 +1,70 @@
+
/*-
+
 * Copyright(c) 2024 Baptiste Daroussin <bapt@FreeBSD.org>
+
 *
+
 * SPDX-License-Identifier: BSD-2-Clause
+
 */
+

+
#include <sys/types.h>
+

+
#include <string.h>
+

+
#include <atf-c.h>
+
#include <private/pkg.h>
+
#include <private/binfmt.h>
+
#include <xstring.h>
+
#include <tllist.h>
+
#include <pkg.h>
+

+
#ifndef __unused
+
# ifdef __GNUC__
+
# define __unused __attribute__ ((__unused__))
+
# else
+
# define __unused
+
# endif
+
#endif
+

+
xstring *msg;
+

+
ATF_TC_WITHOUT_HEAD(analyse_elf);
+

+
int
+
event_callback(void *data __unused, struct pkg_event *ev)
+
{
+
	switch (ev->type) {
+
	case PKG_EVENT_ERROR:
+
		xstring_reset(msg);
+
		fprintf(msg->fp, "%s", ev->e_pkg_error.msg);
+
		break;
+
	default:
+
		/* IGNORE */
+
		break;
+
	}
+

+
	return (0);
+
}
+

+
ATF_TC_BODY(analyse_elf, tc)
+
{
+
	struct pkg *p = NULL;
+
	char *binpath = NULL;
+

+
	xasprintf(&binpath, "%s/frontend/libtestfbsd.so.1", atf_tc_get_config_var(tc, "srcdir"));
+

+
	ATF_REQUIRE_EQ(EPKG_OK, pkg_new(&p, PKG_INSTALLED));
+
	ATF_REQUIRE(p != NULL);
+

+
	ATF_REQUIRE_EQ(tll_length(p->shlibs_provided), 0);
+
	ATF_REQUIRE_EQ(pkg_analyse_elf(false, p, binpath), EPKG_OK);
+
	ATF_REQUIRE_EQ(tll_length(p->shlibs_provided), 1);
+
	ATF_REQUIRE_STREQ(tll_front(p->shlibs_provided), "libtestfbsd.so.1");
+

+
	free(binpath);
+

+
}
+

+
ATF_TP_ADD_TCS(tp)
+
{
+
	ATF_TP_ADD_TC(tp, analyse_elf);
+

+
	return (atf_no_error());
+
}