aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2021-04-13 11:02:54 +0200
committerMattias Andrée <maandree@kth.se>2021-04-13 11:02:54 +0200
commit14ca1a0edd45db55da888b817c66249b2e15a12e (patch)
tree55551e74f0c1da8478a75fb3fd702e623b7ebe87
parentFix parsing of block data (diff)
downloadlibcontacts-14ca1a0edd45db55da888b817c66249b2e15a12e.tar.gz
libcontacts-14ca1a0edd45db55da888b817c66249b2e15a12e.tar.bz2
libcontacts-14ca1a0edd45db55da888b817c66249b2e15a12e.tar.xz
Test libcontacts_list_contacts
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r--test.c65
1 files 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;
}