Skip to content

Commit a143d17

Browse files
committed
Switch unnamed semaphores to named semaphores in unit tests
Unnamed semaphores, part of the POSIX standard, are not supported on macOS (apparently, because "if Apple implemented POSIX unnamed semaphores, how many X Serves would you buy?" [1]). Using a global resource looks ugly to me, but appears to be the simplest way to get the unit tests to compile and work on both Linux and macOS. [1] https://lists.apple.com/archives/darwin-kernel/2009/Apr/msg00010.html)
1 parent 3202e5b commit a143d17

File tree

1 file changed

+42
-22
lines changed

1 file changed

+42
-22
lines changed

tests/test_fsm.c

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include <pthread.h>
4343
#include <unistd.h>
4444
#include <semaphore.h>
45+
#include <fcntl.h>
4546

4647
// Defines
4748

@@ -92,8 +93,8 @@ typedef struct _LocalDataFsmPico {
9293
int cycles;
9394
FsmPico* pico;
9495
Queue queue;
95-
sem_t read_semaphore;
96-
sem_t connect_semaphore;
96+
sem_t * read_semaphore;
97+
sem_t * connect_semaphore;
9798
char global_data[GLOABL_DATA_LEN];
9899
int global_data_len;
99100
pthread_mutex_t global_data_mutex;
@@ -105,9 +106,9 @@ typedef struct _LocalDataFsmService {
105106
bool calledAuthenticated;
106107
Queue queue;
107108
Buffer * symmetricKey;
108-
sem_t read_semaphore;
109-
sem_t authenticated_semaphore;
110-
sem_t stop_semaphore;
109+
sem_t * read_semaphore;
110+
sem_t * authenticated_semaphore;
111+
sem_t * stop_semaphore;
111112
char global_data[GLOABL_DATA_LEN];
112113
int global_data_len;
113114
pthread_mutex_t global_data_mutex;
@@ -503,7 +504,7 @@ static bool channelOpenFsmPico(RVPChannel * channel) {
503504
void * user_data = channel_get_data(channel);
504505
LocalDataFsmPico * local = (LocalDataFsmPico *)user_data;
505506

506-
sem_wait(&local->connect_semaphore);
507+
sem_wait(local->connect_semaphore);
507508
push_connected(&local->queue, local->pico, NULL, currentTime);
508509

509510
return true;
@@ -539,7 +540,7 @@ static bool channelReadFsmPico(RVPChannel * channel, Buffer * buffer) {
539540

540541
bool result;
541542

542-
sem_wait(&local->read_semaphore);
543+
sem_wait(local->read_semaphore);
543544
result = true;
544545
pthread_mutex_lock(&local->global_data_mutex);
545546
if (local->global_data_len == -1) {
@@ -563,7 +564,7 @@ static void picoWriteFsmPico(char const * data, size_t length, void * user_data)
563564
memcpy(local->global_data, data, length);
564565
local->global_data_len = length;
565566
pthread_mutex_unlock(&local->global_data_mutex);
566-
sem_post(&local->read_semaphore);
567+
sem_post(local->read_semaphore);
567568
}
568569

569570
static void picoSetTimeoutFsmPico(int timeout, void * user_data) {
@@ -582,7 +583,7 @@ static void picoErrorFsmPico(void * user_data) {
582583
static void picoReconnectFsmPico(void * user_data) {
583584
LocalDataFsmPico * local = (LocalDataFsmPico *)user_data;
584585

585-
sem_post(&local->connect_semaphore);
586+
sem_post(local->connect_semaphore);
586587
}
587588

588589
static void picoDisconnectFsmPico(void * user_data) {
@@ -600,7 +601,7 @@ static void picoStatusUpdateFsmPico(FSMPICOSTATE state, void * user_data) {
600601
pthread_mutex_lock(&local->global_data_mutex);
601602
local->global_data_len = -1;
602603
pthread_mutex_unlock(&local->global_data_mutex);
603-
sem_post(&local->read_semaphore);
604+
sem_post(local->read_semaphore);
604605
}
605606
}
606607
}
@@ -631,8 +632,11 @@ START_TEST (fsm_pico_test) {
631632
local.global_data[0] = 0;
632633
local.global_data_len = 0;
633634
pthread_mutex_init(& local.global_data_mutex, NULL);
634-
sem_init(&local.read_semaphore, 0, 0);
635-
sem_init(&local.connect_semaphore, 0, 0);
635+
636+
local.read_semaphore = sem_open("picofsmpico_read", O_CREAT | O_EXCL, 0644, 0);
637+
ck_assert(local.read_semaphore != SEM_FAILED);
638+
local.connect_semaphore = sem_open("picofsmpico_connect", O_CREAT | O_EXCL, 0644, 0);
639+
ck_assert(local.connect_semaphore != SEM_FAILED);
636640

637641
picoShared = shared_new();
638642
shared_load_or_generate_pico_keys(picoShared, "testpicokey.pub", "testpicokey.priv");
@@ -658,7 +662,7 @@ START_TEST (fsm_pico_test) {
658662
// We have to duplicate the objects because fsmpico tries to delete them later
659663
cryptosupport_getprivateder(picoIdSKey, picoIdDer);
660664
fsmpico_start(local.pico, picoExtraData, EC_KEY_dup(servIdPKey), EC_KEY_dup(picoIdPKey), cryptosupport_read_buffer_private_key(picoIdDer));
661-
sem_post(&local.connect_semaphore);
665+
sem_post(local.connect_semaphore);
662666

663667
pthread_t prover_td;
664668
pthread_create(&prover_td, NULL, event_loop_thread, &local.queue);
@@ -719,6 +723,11 @@ START_TEST (fsm_pico_test) {
719723
buffer_delete(picoIdDer);
720724
users_delete(users);
721725
buffer_delete(symmetricKey);
726+
727+
sem_close(local.read_semaphore);
728+
sem_unlink("picofsmpico_read");
729+
sem_close(local.connect_semaphore);
730+
sem_unlink("picofsmpico_connect");
722731
}
723732
END_TEST
724733

@@ -767,7 +776,7 @@ static bool channelReadFsmService(RVPChannel * channel, Buffer * buffer) {
767776
LocalDataFsmService * local = (LocalDataFsmService *)user_data;
768777
bool result;
769778

770-
sem_wait(&local->read_semaphore);
779+
sem_wait(local->read_semaphore);
771780
result = true;
772781
pthread_mutex_lock(&local->global_data_mutex);
773782
if (local->global_data_len == -1) {
@@ -791,7 +800,7 @@ static void serviceWriteFsmService(char const * data, size_t length, void * user
791800
memcpy(local->global_data, data, length);
792801
local->global_data_len = length;
793802
pthread_mutex_unlock(&local->global_data_mutex);
794-
sem_post(&local->read_semaphore);
803+
sem_post(local->read_semaphore);
795804
}
796805

797806
static void serviceSetTimeoutFsmService(int timeout, void * user_data) {
@@ -825,7 +834,7 @@ static void serviceAuthenticatedFsmService(int status, void * user_data) {
825834
ck_assert(buffer_equals(receivedSymKey, local->symmetricKey));
826835
ck_assert(!strcmp(buffer_get_buffer(user), "Synchronous"));
827836
ck_assert(!strcmp(buffer_get_buffer(extraData), "p@ssword"));
828-
sem_post(&local->authenticated_semaphore);
837+
sem_post(local->authenticated_semaphore);
829838
}
830839

831840
static void serviceStatusUpdateFsmService(int state, void * user_data) {
@@ -835,7 +844,7 @@ static void serviceStatusUpdateFsmService(int state, void * user_data) {
835844
local->cycles++;
836845
if (local->cycles > 3) {
837846
push_stop(&local->queue, NULL, local->serv, currentTime);
838-
sem_post(&local->stop_semaphore);
847+
sem_post(local->stop_semaphore);
839848
}
840849
}
841850
}
@@ -861,9 +870,13 @@ START_TEST (fsm_service_test) {
861870
local.global_data[0] = 0;
862871
local.global_data_len = 0;
863872
pthread_mutex_init(& local.global_data_mutex, NULL);
864-
sem_init(&local.read_semaphore, 0, 0);
865-
sem_init(&local.authenticated_semaphore, 0, 0);
866-
sem_init(&local.stop_semaphore, 0, 0);
873+
874+
local.read_semaphore = sem_open("picofsmservice_read", O_CREAT | O_EXCL, 0644, 0);
875+
ck_assert(local.read_semaphore != SEM_FAILED);
876+
local.authenticated_semaphore = sem_open("picofsmservice_authenticate", O_CREAT | O_EXCL, 0644, 0);
877+
ck_assert(local.authenticated_semaphore != SEM_FAILED);
878+
local.stop_semaphore = sem_open("picofsmservice_stop", O_CREAT | O_EXCL, 0644, 0);
879+
ck_assert(local.stop_semaphore != SEM_FAILED);
867880

868881
picoShared = shared_new();
869882
shared_load_or_generate_pico_keys(picoShared, "testpicokey.pub", "testpicokey.priv");
@@ -899,7 +912,7 @@ START_TEST (fsm_service_test) {
899912
bool result = sigmaprover(picoShared, channel, picoExtraData, returnedExtraData);
900913
ck_assert(result);
901914

902-
sem_wait(&local.authenticated_semaphore);
915+
sem_wait(local.authenticated_semaphore);
903916
ck_assert(local.calledAuthenticated);
904917
ck_assert(!strcmp(buffer_get_buffer(returnedExtraData), "SERVICE EXTRA"));
905918

@@ -921,7 +934,7 @@ START_TEST (fsm_service_test) {
921934
ck_assert(result);
922935

923936
// Wait for the other thread to complete the cycles before stopping the loop
924-
sem_wait(&local.stop_semaphore);
937+
sem_wait(local.stop_semaphore);
925938

926939
event.type = STOP_LOOP;
927940
event.service = local.serv;
@@ -941,6 +954,13 @@ START_TEST (fsm_service_test) {
941954
buffer_delete(picoExtraData);
942955
users_delete(users);
943956
buffer_delete(local.symmetricKey);
957+
958+
sem_close(local.read_semaphore);
959+
sem_unlink("picofsmservice_read");
960+
sem_close(local.authenticated_semaphore);
961+
sem_unlink("picofsmservice_authenticate");
962+
sem_close(local.stop_semaphore);
963+
sem_unlink("picofsmservice_stop");
944964
}
945965
END_TEST
946966

0 commit comments

Comments
 (0)