Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Librem5
lurch
Commits
f496b3ac
Commit
f496b3ac
authored
Aug 23, 2019
by
Richard Bayerle
Browse files
Test 'lurch-fp-list' signal handler
parent
cfd358d9
Changes
3
Hide whitespace changes
Inline
Side-by-side
Makefile
View file @
f496b3ac
...
...
@@ -173,6 +173,7 @@ $(BDIR)/test_lurch_api: $(OBJECTS_W_COVERAGE) $(VENDOR_LIBS) $(BDIR)/test_lurch_
-Wl
,--wrap
=
omemo_storage_chatlist_delete
\
-Wl
,--wrap
=
omemo_storage_chatlist_save
\
-Wl
,--wrap
=
axc_key_load_public_own
\
-Wl
,--wrap
=
axc_key_load_public_addr
\
-Wl
,--wrap
=
lurch_util_fp_get_printable
bash
-c
"set -o pipefail;
$@
2>&1 | grep -Ev "
.
*
CRITICAL.
*
" | tr -s '
\n
'"
# filter annoying and irrelevant glib output
...
...
src/lurch_api.h
View file @
f496b3ac
...
...
@@ -80,4 +80,14 @@ void lurch_api_disable_im_handler(PurpleAccount * acc_p, const char * contact_ba
*
* Gets the this device's fingerprint in a printable format.
*/
void
lurch_api_fp_get_handler
(
PurpleAccount
*
acc_p
,
void
(
*
cb
)(
int32_t
err
,
const
char
*
fp_printable
,
void
*
user_data_p
),
void
*
user_data_p
);
\ No newline at end of file
void
lurch_api_fp_get_handler
(
PurpleAccount
*
acc_p
,
void
(
*
cb
)(
int32_t
err
,
const
char
*
fp_printable
,
void
*
user_data_p
),
void
*
user_data_p
);
/**
* SIGNAL: lurch-fp-list
*
* Gets the fingerprints of all devices belonging to the specified account and creates a device-ID-to-fingerprint table.
* This is based on sessions, so if there is an entry in the OMEMO devicelist, but no libsignal session yet, the value will be NULL.
* If the whole devicelist is empty, i.e. the account is not an OMEMO user, the whole table will be NULL.
* Watch out as this is not a valid GHashTable.
*/
void
lurch_api_fp_list_handler
(
PurpleAccount
*
acc_p
,
void
(
*
cb
)(
int32_t
err
,
GHashTable
*
id_fp_table
,
void
*
user_data_p
),
void
*
user_data_p
);
\ No newline at end of file
test/test_lurch_api.c
View file @
f496b3ac
...
...
@@ -32,12 +32,14 @@ int __wrap_omemo_storage_user_devicelist_retrieve(const char * user, const char
int
__wrap_axc_get_device_id
(
axc_context
*
ctx_p
,
uint32_t
*
id_p
)
{
uint32_t
id
;
int
ret_val
;
id
=
mock_type
(
uint32_t
);
*
id_p
=
id
;
return
EXIT_SUCCESS
;
ret_val
=
mock_type
(
int
);
return
ret_val
;
}
void
__wrap_jabber_pep_publish
(
JabberStream
*
js_p
,
xmlnode
*
publish_node_p
)
{
...
...
@@ -51,7 +53,7 @@ void __wrap_jabber_pep_publish(JabberStream * js_p, xmlnode * publish_node_p) {
check_expected_ptr
(
device_node_p
->
next
);
}
PurpleConnection
*
__wrap_purple_account_get_connection
(
PurpleAccount
*
acc_p
)
{
void
__wrap_purple_account_get_connection
(
PurpleAccount
*
acc_p
)
{
function_called
();
}
...
...
@@ -71,6 +73,41 @@ int __wrap_omemo_storage_chatlist_save(const char * chat, const char * db_fn) {
return
ret_val
;
}
int
__wrap_axc_key_load_public_own
(
axc_context
*
ctx_p
,
axc_buf
**
pubkey_data_pp
)
{
void
*
pubkey_data_p
;
int
ret_val
;
pubkey_data_p
=
mock_ptr_type
(
void
*
);
*
pubkey_data_pp
=
pubkey_data_p
;
ret_val
=
mock_type
(
int
);
return
ret_val
;
}
int
__wrap_axc_key_load_public_addr
(
const
char
*
name
,
uint32_t
device_id
,
axc_context
*
ctx_p
,
axc_buf
**
pubkey_data_pp
)
{
void
*
pubkey_data_p
;
int
ret_val
;
check_expected
(
name
);
check_expected
(
device_id
);
pubkey_data_p
=
mock_ptr_type
(
void
*
);
*
pubkey_data_pp
=
pubkey_data_p
;
ret_val
=
mock_type
(
int
);
return
ret_val
;
}
char
*
__wrap_lurch_util_fp_get_printable
(
axc_buf
*
key_buf_p
)
{
function_called
();
check_expected
(
key_buf_p
);
char
*
ret_val
;
ret_val
=
mock_ptr_type
(
char
*
);
return
ret_val
;
}
void
lurch_api_id_list_handler_cb_mock
(
int32_t
err
,
GList
*
id_list
,
void
*
user_data_p
)
{
check_expected
(
err
);
...
...
@@ -117,6 +154,7 @@ static void test_lurch_api_id_list_handler(void ** state) {
uint32_t
test_own_id
=
1337
;
will_return
(
__wrap_axc_get_device_id
,
test_own_id
);
will_return
(
__wrap_axc_get_device_id
,
EXIT_SUCCESS
);
expect_value
(
lurch_api_id_list_handler_cb_mock
,
err
,
0
);
expect_value
(
lurch_api_id_list_handler_cb_mock
,
first_id
,
test_own_id
);
...
...
@@ -308,27 +346,6 @@ static void test_lurch_api_disable_im_handler_err(void ** state) {
lurch_api_disable_im_handler
((
void
*
)
"ignored"
,
contact_bare_jid
,
lurch_api_disable_im_handler_cb_mock
,
test_user_data
);
}
int
__wrap_axc_key_load_public_own
(
axc_context
*
ctx_p
,
axc_buf
**
pubkey_data_pp
)
{
void
*
pubkey_data_p
;
int
ret_val
;
pubkey_data_p
=
mock_ptr_type
(
void
*
);
*
pubkey_data_pp
=
pubkey_data_p
;
ret_val
=
mock_type
(
int
);
return
ret_val
;
}
char
*
__wrap_lurch_util_fp_get_printable
(
axc_buf
*
key_buf_p
)
{
function_called
();
check_expected
(
key_buf_p
);
char
*
ret_val
;
ret_val
=
mock_ptr_type
(
char
*
);
return
ret_val
;
}
void
lurch_api_fp_get_handler_cb_mock
(
int32_t
err
,
const
char
*
fp_printable
,
void
*
user_data_p
)
{
check_expected
(
err
);
check_expected
(
fp_printable
);
...
...
@@ -344,18 +361,18 @@ static void test_lurch_api_fp_get_handler(void ** state) {
const
char
*
test_jid
=
"me-testing@test.org/resource"
;
will_return
(
__wrap_purple_account_get_username
,
test_jid
);
axc_buf
*
mock_key_
data_
buf_
p
=
axc_buf_create
(
NULL
,
0
);
will_return
(
__wrap_axc_key_load_public_own
,
mock_key_
data_
buf_
p
);
axc_buf
*
mock_key_buf_
own
=
axc_buf_create
(
NULL
,
0
);
will_return
(
__wrap_axc_key_load_public_own
,
mock_key_buf_
own
);
will_return
(
__wrap_axc_key_load_public_own
,
EXIT_SUCCESS
);
expect_function_call
(
__wrap_lurch_util_fp_get_printable
);
expect_value
(
__wrap_lurch_util_fp_get_printable
,
key_buf_p
,
mock_key_
data_
buf_
p
);
char
*
mock_fp
=
g_strdup
(
"MOCK FINGERPRINT"
);
will_return
(
__wrap_lurch_util_fp_get_printable
,
mock_fp
);
expect_value
(
__wrap_lurch_util_fp_get_printable
,
key_buf_p
,
mock_key_buf_
own
);
char
*
mock_fp
_own
=
g_strdup
(
"MOCK FINGERPRINT"
);
will_return
(
__wrap_lurch_util_fp_get_printable
,
mock_fp
_own
);
char
*
test_user_data
=
"TEST USER DATA"
;
expect_value
(
lurch_api_fp_get_handler_cb_mock
,
err
,
EXIT_SUCCESS
);
expect_string
(
lurch_api_fp_get_handler_cb_mock
,
fp_printable
,
mock_fp
);
expect_string
(
lurch_api_fp_get_handler_cb_mock
,
fp_printable
,
mock_fp
_own
);
expect_value
(
lurch_api_fp_get_handler_cb_mock
,
user_data_p
,
test_user_data
);
lurch_api_fp_get_handler
(
NULL
,
lurch_api_fp_get_handler_cb_mock
,
test_user_data
);
...
...
@@ -381,6 +398,129 @@ static void test_lurch_api_fp_get_handler_err(void ** state) {
lurch_api_fp_get_handler
(
NULL
,
lurch_api_fp_get_handler_cb_mock
,
test_user_data
);
}
/**
* Mock callback that checks whether the given table is the same as the expected table passed through the user data.
*/
static
void
lurch_api_fp_list_handler_cb_mock
(
int32_t
err
,
GHashTable
*
id_fp_table
,
void
*
user_data_p
)
{
check_expected
(
err
);
if
(
err
)
{
return
;
}
GHashTable
*
expected_table
=
(
GHashTable
*
)
user_data_p
;
GHashTableIter
iter
;
gpointer
key
,
value
;
char
*
generated_fp
=
(
void
*
)
0
;
g_hash_table_iter_init
(
&
iter
,
expected_table
);
while
(
g_hash_table_iter_next
(
&
iter
,
&
key
,
&
value
))
{
generated_fp
=
g_hash_table_lookup
(
id_fp_table
,
key
);
// assert_non_null(generated_fp);
assert_ptr_equal
(
generated_fp
,
value
);
generated_fp
=
(
void
*
)
0
;
}
}
/**
* Loads the devicelist, then each device's public key, then gets their fingerprints, and finally puts everything
* into a single table of device ID to fingerprint.
*/
static
void
test_lurch_api_fp_list_handler
(
void
**
state
)
{
(
void
)
state
;
const
char
*
bare_jid
=
"me-testing@test.org"
;
const
char
*
test_jid
=
"me-testing@test.org/resource"
;
will_return_always
(
__wrap_purple_account_get_username
,
test_jid
);
char
*
devicelist
=
"<items node='urn:xmpp:omemo:0:devicelist'>"
"<item>"
"<list xmlns='urn:xmpp:omemo:0'>"
"<device id='4223' />"
"<device id='1337' />"
"<device id='1338' />"
"</list>"
"</item>"
"</items>"
;
omemo_devicelist
*
dl_p
;
omemo_devicelist_import
(
devicelist
,
test_jid
,
&
dl_p
);
will_return
(
__wrap_omemo_storage_user_devicelist_retrieve
,
dl_p
);
will_return
(
__wrap_omemo_storage_user_devicelist_retrieve
,
EXIT_SUCCESS
);
uint32_t
test_own_id
=
1337
;
will_return
(
__wrap_axc_get_device_id
,
test_own_id
);
will_return
(
__wrap_axc_get_device_id
,
EXIT_SUCCESS
);
int
id_4223
=
4223
;
expect_string
(
__wrap_axc_key_load_public_addr
,
name
,
bare_jid
);
expect_value
(
__wrap_axc_key_load_public_addr
,
device_id
,
id_4223
);
char
*
mock_key_4223
=
"FAKE KEY 4223"
;
axc_buf
*
mock_key_buf_4223
=
axc_buf_create
((
unsigned
char
*
)
mock_key_4223
,
strlen
(
mock_key_4223
));
will_return
(
__wrap_axc_key_load_public_addr
,
mock_key_buf_4223
);
will_return
(
__wrap_axc_key_load_public_addr
,
1
);
expect_function_call
(
__wrap_lurch_util_fp_get_printable
);
expect_value
(
__wrap_lurch_util_fp_get_printable
,
key_buf_p
,
mock_key_buf_4223
);
char
*
mock_fp_4223
=
g_strdup
(
"FAKE FP 4223"
);
will_return
(
__wrap_lurch_util_fp_get_printable
,
mock_fp_4223
);
int
id_1338
=
1338
;
expect_string
(
__wrap_axc_key_load_public_addr
,
name
,
bare_jid
);
expect_value
(
__wrap_axc_key_load_public_addr
,
device_id
,
id_1338
);
char
*
mock_key_1338
=
"FAKE KEY 1338"
;
axc_buf
*
mock_key_buf_1338
=
axc_buf_create
((
unsigned
char
*
)
mock_key_1338
,
strlen
(
mock_key_1338
));
will_return
(
__wrap_axc_key_load_public_addr
,
mock_key_buf_1338
);
will_return
(
__wrap_axc_key_load_public_addr
,
0
);
axc_buf
*
mock_key_buf_own
=
axc_buf_create
(
NULL
,
0
);
will_return
(
__wrap_axc_key_load_public_own
,
mock_key_buf_own
);
will_return
(
__wrap_axc_key_load_public_own
,
EXIT_SUCCESS
);
char
*
mock_fp_own
=
g_strdup
(
"MOCK FINGERPRINT"
);
expect_function_call
(
__wrap_lurch_util_fp_get_printable
);
expect_value
(
__wrap_lurch_util_fp_get_printable
,
key_buf_p
,
mock_key_buf_own
);
will_return
(
__wrap_lurch_util_fp_get_printable
,
mock_fp_own
);
GHashTable
*
expected_table
=
g_hash_table_new
(
g_int_hash
,
g_int_equal
);
g_hash_table_insert
(
expected_table
,
&
test_own_id
,
mock_fp_own
);
g_hash_table_insert
(
expected_table
,
&
id_4223
,
mock_fp_4223
);
g_hash_table_insert
(
expected_table
,
&
id_1338
,
NULL
);
expect_value
(
lurch_api_fp_list_handler_cb_mock
,
err
,
EXIT_SUCCESS
);
lurch_api_fp_list_handler
(
NULL
,
lurch_api_fp_list_handler_cb_mock
,
expected_table
);
}
/**
* Calls the callback with the return code in case of an error.
*/
static
void
test_lurch_api_fp_list_handler_err
(
void
**
state
)
{
(
void
)
state
;
const
char
*
bare_jid
=
"me-testing@test.org"
;
const
char
*
test_jid
=
"me-testing@test.org/resource"
;
will_return_always
(
__wrap_purple_account_get_username
,
test_jid
);
char
*
devicelist
=
"<items node='urn:xmpp:omemo:0:devicelist'>"
"<item>"
"<list xmlns='urn:xmpp:omemo:0'>"
"</list>"
"</item>"
"</items>"
;
omemo_devicelist
*
dl_p
;
omemo_devicelist_import
(
devicelist
,
test_jid
,
&
dl_p
);
will_return
(
__wrap_omemo_storage_user_devicelist_retrieve
,
dl_p
);
will_return
(
__wrap_omemo_storage_user_devicelist_retrieve
,
EXIT_SUCCESS
);
uint32_t
id_which_does_not_matter
=
123
;
will_return
(
__wrap_axc_get_device_id
,
id_which_does_not_matter
);
will_return
(
__wrap_axc_get_device_id
,
EXIT_FAILURE
);
expect_value
(
lurch_api_fp_list_handler_cb_mock
,
err
,
EXIT_FAILURE
);
lurch_api_fp_list_handler
(
NULL
,
lurch_api_fp_list_handler_cb_mock
,
NULL
);
}
int
main
(
void
)
{
const
struct
CMUnitTest
tests
[]
=
{
cmocka_unit_test
(
test_lurch_api_id_list_handler
),
...
...
@@ -392,7 +532,9 @@ int main(void) {
cmocka_unit_test
(
test_lurch_api_disable_im_handler
),
cmocka_unit_test
(
test_lurch_api_disable_im_handler_err
),
cmocka_unit_test
(
test_lurch_api_fp_get_handler
),
cmocka_unit_test
(
test_lurch_api_fp_get_handler_err
)
cmocka_unit_test
(
test_lurch_api_fp_get_handler_err
),
cmocka_unit_test
(
test_lurch_api_fp_list_handler
),
cmocka_unit_test
(
test_lurch_api_fp_list_handler_err
)
};
return
cmocka_run_group_tests_name
(
"lurch_api"
,
tests
,
NULL
,
NULL
);
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment