diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | tests/Makefile | 15 | ||||
-rw-r--r-- | tests/consumer.c | 38 | ||||
-rw-r--r-- | tests/producer.c | 39 | ||||
-rw-r--r-- | tests/semconfig.h | 8 | ||||
-rw-r--r-- | tests/semtest.c | 42 |
7 files changed, 149 insertions, 1 deletions
@@ -1,2 +1,5 @@ /naskpass +/tests/producer +/tests/consumer +/tests/semtest *.swp @@ -1,7 +1,7 @@ CFLAGS ?= $(shell ncurses5-config --cflags) -Wall -D_GNU_SOURCE=1 DBGFLAGS = -g LDFLAGS ?= $(shell ncurses5-config --libs) -pthread -CC := gcc +CC ?= gcc INSTALL ?= install VERSION ?= $(shell if [ -d ./.git ]; then echo -n "git-"; git rev-parse --short HEAD; else echo "1.2a"; fi) BIN = naskpass @@ -11,9 +11,11 @@ all: $(BIN) $(BIN): $(SOURCES) $(CC) $(SOURCES) -D_VERSION=\"$(VERSION)\" $(CFLAGS) $(LDFLAGS) -o $(BIN) + $(MAKE) -C tests CC='$(CC)' CFLAGS='$(CFLAGS)' all debug: $(MAKE) CFLAGS='$(CFLAGS) $(DBGFLAGS)' + $(MAKE) -C tests CFLAGS='$(CFLAGS) $(DBGFLAGS)' install: $(INSTALL) -D -m 0755 $(BIN) $(DESTDIR)/lib/cryptsetup/naskpass @@ -30,6 +32,7 @@ uninstall: clean: rm -f $(BIN) + $(MAKE) -C tests clean source: -dh_make --createorig -p naskpass_$(VERSION) -s -y diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..22a5469 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,15 @@ +CFLAGS = -Wall +LDFLAGS = -lpthread +CC = gcc +SOURCES = $(wildcard *.c) +BINARIES = $(patsubst %.c,%,$(SOURCES)) + +all: $(BINARIES) + +%: %.c + $(CC) $(CFLAGS) $(LDFLAGS) $< -o $(patsubst %.c,%,$<) + +clean: + rm -f $(BINARIES) + +.PHONY: all install clean diff --git a/tests/consumer.c b/tests/consumer.c new file mode 100644 index 0000000..3f93d1e --- /dev/null +++ b/tests/consumer.c @@ -0,0 +1,38 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <semaphore.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <fcntl.h> +#include <assert.h> + +#include "semconfig.h" + +sem_t *mysem = NULL, *mycnt = NULL; +pid_t child; + + +int main(int argc, char **argv) { + int semval = 0; + + if ( (mysem = sem_open(TESTSEM, 0, S_IRUSR | S_IWUSR, 1)) != NULL && + (mycnt = sem_open(CNTSEM, 0, S_IRUSR | S_IWUSR, 1)) != NULL ) { + assert( sem_getvalue(mycnt, &semval) == 0 ); + printf("factory producing %d items\n", semval); + + while ( semval-- >= 0 ) { + usleep(250000); + LOG("consumer: -1"); + assert( sem_wait(mysem) == 0 ); + printf("remaining: %d\n", semval); + } + } else { + exit(1); + } + assert( sem_close(mysem) == 0 ); + + exit(0); +} + diff --git a/tests/producer.c b/tests/producer.c new file mode 100644 index 0000000..25af700 --- /dev/null +++ b/tests/producer.c @@ -0,0 +1,39 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <semaphore.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <fcntl.h> +#include <assert.h> + +#include "semconfig.h" + +sem_t *mysem = NULL, *mycnt = NULL; +pid_t child; + + +int main(int argc, char **argv) { + int semval = 10; + + if (argc == 2) { + semval = atoi(argv[1]); + } + + if ( (mysem = sem_open(TESTSEM, O_CREAT, S_IRUSR | S_IWUSR, 0)) != NULL && + (mycnt = sem_open(CNTSEM, O_CREAT, S_IRUSR | S_IWUSR, semval)) != NULL ) { + while (semval-- >= 0) { + sleep(1); + LOG("producer: +1"); + assert( sem_post(mysem) == 0 ); + printf("remaining: %d\n", semval); + } + assert( sem_close(mysem) == 0 && sem_close(mycnt) == 0 ); + assert( sem_unlink(TESTSEM) == 0 && sem_unlink(CNTSEM) == 0 ); + } else { + exit(1); + } + exit(0); +} + diff --git a/tests/semconfig.h b/tests/semconfig.h new file mode 100644 index 0000000..e34984d --- /dev/null +++ b/tests/semconfig.h @@ -0,0 +1,8 @@ +#ifndef CONFIG_H +#define CONFIG_H 1 + +#define LOG(text) fprintf(stderr, "%s\n", text); +#define TESTSEM "/testsem" +#define CNTSEM "/testcnt" + +#endif diff --git a/tests/semtest.c b/tests/semtest.c new file mode 100644 index 0000000..7968127 --- /dev/null +++ b/tests/semtest.c @@ -0,0 +1,42 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <semaphore.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <fcntl.h> + + +sem_t *mysem = NULL; +pid_t child; + +#define LOG(cmd) fprintf(stderr, "%s\n", cmd); + +int main(int argc, char **argv) { + sem_unlink("/mysem"); + if ( (mysem = sem_open("/mysem", O_CREAT, S_IRUSR | S_IWUSR, 1)) != NULL ) { + if ( (child = fork()) == 0 ) { + /* child */ + sleep(1); + LOG("child: sempost"); + sem_post(mysem); + LOG("child: done"); + sleep(1); + exit(0); + } else if (child > 0) { + /* parent */ + LOG("parent: semwait"); + sem_wait(mysem); + LOG("parent: waitpid"); + waitpid(child, NULL, 0); + } else if (child < 0) { + perror("fork"); + } + } else { + sem_close(mysem); + exit(1); + } + exit(0); +} + |