From 14ca1a0edd45db55da888b817c66249b2e15a12e Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 13 Apr 2021 11:02:54 +0200 Subject: Test libcontacts_list_contacts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- test.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/test.c b/test.c index fd3c3db..8b694d3 100644 --- a/test.c +++ b/test.c @@ -12,11 +12,46 @@ } while (0) +static void +touch(const char *path) +{ + int fd; + TEST((fd = open(path, O_WRONLY | O_CREAT, 0600)) >= 0); + TEST(!close(fd)); +} + +static int +strpcmp(const void *av, const void *bv) +{ + const char *const *a = av; + const char *const *b = bv; + return strcmp(*a, *b); +} + +static size_t +sort_contact_ids(char **ids) +{ + size_t n; + for (n = 0; ids[n]; n++); + qsort(ids, n, sizeof(*ids), strpcmp); + return n; +} + +static void +free_contact_ids(char **ids) +{ + char **idp; + for (idp = ids; *idp; idp++) + free(*idp); + free(ids); +} + + int main(void) { struct passwd user; - char *s; + char *s, **ids; TEST(libcontacts_get_path("", NULL) == NULL && errno == EINVAL); memset(&user, 0, sizeof(user)); @@ -33,9 +68,32 @@ main(void) /* TODO test ENOMEM case */ user.pw_dir = ".testdir"; - TEST(!mkdir(user.pw_dir, 0777)); + TEST(!mkdir(".testdir", 0700)); + TEST(!mkdir(".testdir/.config", 0700)); + TEST(!mkdir(".testdir/.config/contacts", 0700)); + + touch(".testdir/.config/contacts/alpha"); + touch(".testdir/.config/contacts/.me"); + touch(".testdir/.config/contacts/.groups"); + touch(".testdir/.config/contacts/beta"); + touch(".testdir/.config/contacts/.exclude"); + touch(".testdir/.config/contacts/gamma"); + + TEST(!libcontacts_list_contacts(&ids, &user, 0)); + TEST(sort_contact_ids(ids) == 3); + TEST(!strcmp(ids[0], "alpha")); + TEST(!strcmp(ids[1], "beta")); + TEST(!strcmp(ids[2], "gamma")); + free_contact_ids(ids); + TEST(!libcontacts_list_contacts(&ids, &user, 1)); + TEST(sort_contact_ids(ids) == 4); + TEST(!strcmp(ids[0], ".me")); + TEST(!strcmp(ids[1], "alpha")); + TEST(!strcmp(ids[2], "beta")); + TEST(!strcmp(ids[3], "gamma")); + free_contact_ids(ids); + /* TODO test error cases */ - /* TODO test libcontacts_list_contacts */ /* TODO test libcontacts_parse_contact */ /* TODO test libcontacts_load_contact */ /* TODO test libcontacts_load_contacts */ @@ -43,5 +101,6 @@ main(void) /* TODO test libcontacts_save_contact */ /* TODO check for memory leaks */ + /* TODO check for file descriptor leaks */ return 0; } -- cgit v1.2.3-70-g09d2