From 2adf7222ce021748b0edc4a9c5a32375a5b0d124 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Thu, 8 Apr 2021 17:45:01 +0200 Subject: Add test for libcontacts_get_path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- .gitignore | 2 ++ Makefile | 15 ++++++++++++--- TODO | 1 - libcontacts.h | 2 +- test.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 test.c diff --git a/.gitignore b/.gitignore index bf35bd2..6d6e6bf 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ *.lo *.dll *.dylib +/test +/.testdir diff --git a/Makefile b/Makefile index 72f191b..4a543c8 100644 --- a/Makefile +++ b/Makefile @@ -43,8 +43,9 @@ HDR =\ LOBJ = $(OBJ:.o=.lo) -all: libcontacts.a libcontacts.$(LIBEXT) +all: libcontacts.a libcontacts.$(LIBEXT) test $(OBJ): $($@:.o=.c) $(HDR) +test.o: test.c $(HDR) libcontacts.a: $(OBJ) -rm -f -- $@ @@ -60,6 +61,14 @@ libcontacts.$(LIBEXT): $(LOBJ) .c.lo: $(CC) -fPIC -c -o $@ $< $(CFLAGS) $(CPPFLAGS) +test: test.o libcontacts.a + $(CC) -o $@ test.o libcontacts.a $(LDFLAGS) + +check: test + @rm -rf -- .testdir + ./test + @rm -rf -- .testdir + install: libcontacts.a libcontacts.$(LIBEXT) mkdir -p -- "$(DESTDIR)$(PREFIX)/lib" mkdir -p -- "$(DESTDIR)$(PREFIX)/include" @@ -77,9 +86,9 @@ uninstall: -rm -f -- "$(DESTDIR)$(PREFIX)/include/libcontacts.h" clean: - -rm -f -- *.o *.a *.lo *.so *.dylib *.dll *.su + -rm -rf -- *.o *.a *.lo *.so *.dylib *.dll *.su test .testdir .SUFFIXES: .SUFFIXES: .c .o .lo -.PHONY: all install uninstall clean +.PHONY: all check install uninstall clean diff --git a/TODO b/TODO index 9ff27fd..2faa6d5 100644 --- a/TODO +++ b/TODO @@ -1,3 +1,2 @@ Add support for different calendars Add man pages -Add tests diff --git a/libcontacts.h b/libcontacts.h index 6eb4054..877840d 100644 --- a/libcontacts.h +++ b/libcontacts.h @@ -295,7 +295,7 @@ int libcontacts_list_contacts(char ***, const struct passwd *); int libcontacts_load_contact(const char *, struct libcontacts_contact *, const struct passwd *); /* errno = 0 if malformatted */ int libcontacts_load_contacts(struct libcontacts_contact ***, const struct passwd *); int libcontacts_save_contact(struct libcontacts_contact *, const struct passwd *); -int libcontacts_same_number(const char *, const char *, const char *, const char *); +int libcontacts_same_number(const char *, const char *, const char *, const char *); /* might be removed in the future */ char *libcontacts_get_path(const char *, const struct passwd *); int libcontacts_parse_contact(char *, struct libcontacts_contact *); /* does not load .id, not stored in file, but is the filename */ diff --git a/test.c b/test.c new file mode 100644 index 0000000..fd3c3db --- /dev/null +++ b/test.c @@ -0,0 +1,47 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +#define TEST(EXPR)\ + do {\ + errno = 0;\ + if (EXPR)\ + break;\ + fprintf(stderr, "Failure at line %i, with errno = %i (%s): %s\n", __LINE__, errno, strerror(errno), #EXPR);\ + exit(1);\ + } while (0) + + +int +main(void) +{ + struct passwd user; + char *s; + + TEST(libcontacts_get_path("", NULL) == NULL && errno == EINVAL); + memset(&user, 0, sizeof(user)); + TEST(libcontacts_get_path("", &user) == NULL && errno == EINVAL); + user.pw_dir = ""; + TEST(libcontacts_get_path("", &user) == NULL && errno == EINVAL); + user.pw_dir = "/var/empty"; + TEST(libcontacts_get_path(NULL, &user) == NULL && errno == EINVAL); + user.pw_dir = "/var/empty"; + TEST((s = libcontacts_get_path("", &user)) && !strcmp(s, "/var/empty/.config/contacts/")); + free(s); + TEST((s = libcontacts_get_path("someone", &user)) && !strcmp(s, "/var/empty/.config/contacts/someone")); + free(s); + /* TODO test ENOMEM case */ + + user.pw_dir = ".testdir"; + TEST(!mkdir(user.pw_dir, 0777)); + + /* TODO test libcontacts_list_contacts */ + /* TODO test libcontacts_parse_contact */ + /* TODO test libcontacts_load_contact */ + /* TODO test libcontacts_load_contacts */ + /* TODO test libcontacts_format_contact */ + /* TODO test libcontacts_save_contact */ + + /* TODO check for memory leaks */ + return 0; +} -- cgit v1.2.3-70-g09d2