Skip to content

Commit 3c7aa49

Browse files
committed
Tests: Use common mutex helper
Also add glib-based mutex implementation
1 parent b7a03fa commit 3c7aa49

File tree

7 files changed

+73
-61
lines changed

7 files changed

+73
-61
lines changed

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ set(LIBS ${LIBS}
4343

4444
set(common_SRCS
4545
test_common.c
46+
test_common_pthread.c
4647
test_common.h
4748
)
4849

tests/test_common.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,9 @@ void setup_test_sender_key_store(signal_protocol_store_context *context, signal_
9090
void srand_deterministic(unsigned int seed);
9191
#endif
9292

93+
void test_global_mutex_lock(void *user_data);
94+
void test_global_mutex_unlock(void *user_data);
95+
void test_global_mutex_setup();
96+
void test_global_mutex_teardown();
97+
9398
#endif /* TEST_COMMON_H */

tests/test_common_glib.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <glib.h>
2+
3+
GRecMutex global_mutex;
4+
5+
void test_global_mutex_lock(void *user_data)
6+
{
7+
g_rec_mutex_lock(&global_mutex);
8+
}
9+
10+
void test_global_mutex_unlock(void *user_data)
11+
{
12+
g_rec_mutex_unlock(&global_mutex);
13+
}
14+
15+
void test_global_mutex_setup()
16+
{
17+
g_rec_mutex_init(&global_mutex);
18+
}
19+
20+
void test_global_mutex_teardown()
21+
{
22+
g_rec_mutex_clear(&global_mutex);
23+
}

tests/test_common_pthread.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <pthread.h>
2+
3+
pthread_mutex_t global_mutex;
4+
pthread_mutexattr_t global_mutex_attr;
5+
6+
void test_global_mutex_lock(void *user_data)
7+
{
8+
pthread_mutex_lock(&global_mutex);
9+
}
10+
11+
void test_global_mutex_unlock(void *user_data)
12+
{
13+
pthread_mutex_unlock(&global_mutex);
14+
}
15+
16+
void test_global_mutex_setup()
17+
{
18+
pthread_mutexattr_init(&global_mutex_attr);
19+
pthread_mutexattr_settype(&global_mutex_attr, PTHREAD_MUTEX_RECURSIVE);
20+
pthread_mutex_init(&global_mutex, &global_mutex_attr);
21+
}
22+
23+
void test_global_mutex_teardown()
24+
{
25+
pthread_mutex_destroy(&global_mutex);
26+
pthread_mutexattr_destroy(&global_mutex_attr);
27+
}

tests/test_session_builder.c

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33
#include <check.h>
4-
#include <pthread.h>
4+
#include <time.h>
55

66
#include "../src/signal_protocol.h"
77
#include "session_record.h"
@@ -23,46 +23,31 @@ static signal_protocol_address bob_address = {
2323
};
2424

2525
signal_context *global_context;
26-
pthread_mutex_t global_mutex;
27-
pthread_mutexattr_t global_mutex_attr;
2826

2927
void run_interaction(signal_protocol_store_context *alice_store, signal_protocol_store_context *bob_store, uint32_t version);
3028
int test_basic_pre_key_v3_decrypt_callback(session_cipher *cipher, signal_buffer *plaintext, void *decrypt_context);
3129

32-
void test_lock(void *user_data)
33-
{
34-
pthread_mutex_lock(&global_mutex);
35-
}
36-
37-
void test_unlock(void *user_data)
38-
{
39-
pthread_mutex_unlock(&global_mutex);
40-
}
4130

4231
void test_setup()
4332
{
4433
int result;
45-
46-
pthread_mutexattr_init(&global_mutex_attr);
47-
pthread_mutexattr_settype(&global_mutex_attr, PTHREAD_MUTEX_RECURSIVE);
48-
pthread_mutex_init(&global_mutex, &global_mutex_attr);
34+
test_global_mutex_setup();
4935

5036
result = signal_context_create(&global_context, 0);
5137
ck_assert_int_eq(result, 0);
5238
signal_context_set_log_function(global_context, test_log);
5339

5440
setup_test_crypto_provider(global_context);
5541

56-
result = signal_context_set_locking_functions(global_context, test_lock, test_unlock);
42+
result = signal_context_set_locking_functions(global_context, test_global_mutex_lock, test_global_mutex_unlock);
5743
ck_assert_int_eq(result, 0);
5844
}
5945

6046
void test_teardown()
6147
{
6248
signal_context_destroy(global_context);
6349

64-
pthread_mutex_destroy(&global_mutex);
65-
pthread_mutexattr_destroy(&global_mutex_attr);
50+
test_global_mutex_teardown();
6651
}
6752

6853
START_TEST(test_basic_pre_key_v2)
@@ -221,7 +206,7 @@ START_TEST(test_basic_pre_key_v3)
221206
loaded_record_state = 0;
222207

223208
/* Encrypt an outgoing message to send to Bob */
224-
static const char original_message[] = "L'homme est condamné à être libre";
209+
static const char original_message[] = "L'homme est condamn� � �tre libre";
225210
size_t original_message_len = sizeof(original_message) - 1;
226211
session_cipher *alice_session_cipher = 0;
227212
result = session_cipher_create(&alice_session_cipher, alice_store, &bob_address, global_context);
@@ -603,7 +588,7 @@ START_TEST(test_basic_pre_key_omemo)
603588
loaded_record_state = 0;
604589

605590
/* Encrypt an outgoing message to send to Bob */
606-
static const char original_message[] = "L'homme est condamné à être libre";
591+
static const char original_message[] = "L'homme est condamn� � �tre libre";
607592
size_t original_message_len = sizeof(original_message) - 1;
608593
session_cipher *alice_session_cipher = 0;
609594
result = session_cipher_create(&alice_session_cipher, alice_store, &bob_address, global_context);
@@ -1246,7 +1231,7 @@ START_TEST(test_repeat_bundle_message_v3)
12461231
ck_assert_int_eq(result, 0);
12471232

12481233
/* Initialize Alice's session cipher */
1249-
static const char original_message[] = "L'homme est condamné à être libre";
1234+
static const char original_message[] = "L'homme est condamn� � �tre libre";
12501235
size_t original_message_len = sizeof(original_message) - 1;
12511236
session_cipher *alice_session_cipher = 0;
12521237
result = session_cipher_create(&alice_session_cipher, alice_store, &bob_address, global_context);
@@ -1466,7 +1451,7 @@ START_TEST(test_bad_message_bundle)
14661451
ck_assert_int_eq(result, 0);
14671452

14681453
/* Encrypt an outgoing message to send to Bob */
1469-
static const char original_message[] = "L'homme est condamné à être libre";
1454+
static const char original_message[] = "L'homme est condamn� � �tre libre";
14701455
size_t original_message_len = sizeof(original_message) - 1;
14711456
session_cipher *alice_session_cipher = 0;
14721457
result = session_cipher_create(&alice_session_cipher, alice_store, &bob_address, global_context);
@@ -1634,7 +1619,7 @@ START_TEST(test_optional_one_time_pre_key)
16341619
ck_assert_int_eq(session_state_get_session_version(state), 3);
16351620
SIGNAL_UNREF(record);
16361621

1637-
static const char original_message[] = "L'homme est condamné à être libre";
1622+
static const char original_message[] = "L'homme est condamn� � �tre libre";
16381623
size_t original_message_len = sizeof(original_message) - 1;
16391624

16401625
/* Create Alice's session cipher */

tests/test_session_cipher.c

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33
#include <check.h>
4-
#include <pthread.h>
4+
#include <time.h>
55

66
#include "../src/signal_protocol.h"
77
#include "session_record.h"
@@ -13,43 +13,28 @@
1313
#include "test_common.h"
1414

1515
signal_context *global_context;
16-
pthread_mutex_t global_mutex;
17-
pthread_mutexattr_t global_mutex_attr;
18-
19-
void test_lock(void *user_data)
20-
{
21-
pthread_mutex_lock(&global_mutex);
22-
}
23-
24-
void test_unlock(void *user_data)
25-
{
26-
pthread_mutex_unlock(&global_mutex);
27-
}
2816

2917
void test_setup()
3018
{
3119
int result;
3220

33-
pthread_mutexattr_init(&global_mutex_attr);
34-
pthread_mutexattr_settype(&global_mutex_attr, PTHREAD_MUTEX_RECURSIVE);
35-
pthread_mutex_init(&global_mutex, &global_mutex_attr);
21+
test_global_mutex_setup();
3622

3723
result = signal_context_create(&global_context, 0);
3824
ck_assert_int_eq(result, 0);
3925
signal_context_set_log_function(global_context, test_log);
4026

4127
setup_test_crypto_provider(global_context);
4228

43-
result = signal_context_set_locking_functions(global_context, test_lock, test_unlock);
29+
result = signal_context_set_locking_functions(global_context, test_global_mutex_lock, test_global_mutex_unlock);
4430
ck_assert_int_eq(result, 0);
4531
}
4632

4733
void test_teardown()
4834
{
4935
signal_context_destroy(global_context);
5036

51-
pthread_mutex_destroy(&global_mutex);
52-
pthread_mutexattr_destroy(&global_mutex_attr);
37+
test_global_mutex_teardown();
5338
}
5439

5540
void initialize_sessions_v3(session_state *alice_state, session_state *bob_state);

tests/test_simultaneous_initiate.c

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33
#include <check.h>
4-
#include <pthread.h>
4+
#include <time.h>
55

66
#include "../src/signal_protocol.h"
77
#include "curve.h"
@@ -21,8 +21,6 @@ static signal_protocol_address bob_address = {
2121
};
2222

2323
signal_context *global_context;
24-
pthread_mutex_t global_mutex;
25-
pthread_mutexattr_t global_mutex_attr;
2624

2725
ec_key_pair *alice_signed_pre_key;
2826
ec_key_pair *bob_signed_pre_key;
@@ -34,31 +32,20 @@ int current_session_version(signal_protocol_store_context *store, const signal_p
3432
session_pre_key_bundle *create_alice_pre_key_bundle(signal_protocol_store_context *store);
3533
session_pre_key_bundle *create_bob_pre_key_bundle(signal_protocol_store_context *store);
3634

37-
void test_lock(void *user_data)
38-
{
39-
pthread_mutex_lock(&global_mutex);
40-
}
41-
42-
void test_unlock(void *user_data)
43-
{
44-
pthread_mutex_unlock(&global_mutex);
45-
}
4635

4736
void test_setup()
4837
{
4938
int result;
5039

51-
pthread_mutexattr_init(&global_mutex_attr);
52-
pthread_mutexattr_settype(&global_mutex_attr, PTHREAD_MUTEX_RECURSIVE);
53-
pthread_mutex_init(&global_mutex, &global_mutex_attr);
40+
test_global_mutex_setup();
5441

5542
result = signal_context_create(&global_context, 0);
5643
ck_assert_int_eq(result, 0);
5744
signal_context_set_log_function(global_context, test_log);
5845

5946
setup_test_crypto_provider(global_context);
6047

61-
result = signal_context_set_locking_functions(global_context, test_lock, test_unlock);
48+
result = signal_context_set_locking_functions(global_context, test_global_mutex_lock, test_global_mutex_unlock);
6249
ck_assert_int_eq(result, 0);
6350

6451
result = curve_generate_key_pair(global_context, &alice_signed_pre_key);
@@ -77,8 +64,7 @@ void test_teardown()
7764
SIGNAL_UNREF(bob_signed_pre_key);
7865
signal_context_destroy(global_context);
7966

80-
pthread_mutex_destroy(&global_mutex);
81-
pthread_mutexattr_destroy(&global_mutex_attr);
67+
test_global_mutex_teardown();
8268
}
8369

8470
START_TEST(test_basic_simultaneous_initiate)

0 commit comments

Comments
 (0)