#include <gtest/gtest.h>
#include <unistd.h>
#include <webauthn.h>
+#include <thread>
+using std::thread;
#define WEBAUTHN_CODE_DESCRIBE(name) case name: return #name
+wauthn_client_data_s *client_data = nullptr;
+wauthn_pubkey_cred_creation_options_s *mc_options = nullptr;
+wauthn_pubkey_cred_request_options_s * ga_options = nullptr;
+
+wauthn_client_data_s *client_data2 = nullptr;
+wauthn_pubkey_cred_creation_options_s *mc_options2 = nullptr;
+wauthn_pubkey_cred_request_options_s * ga_options2 = nullptr;
+
class WebAuthnTest : public ::testing::Test {
protected:
void SetUp() override {
}
}
};
+void test_cb_display_qrcode(const char *qr_contents, void *user_data)
+{
+ std::cout << "QR Code: " << std::string(qr_contents) << std::endl;
+ if (user_data == nullptr)
+ std::cout << "user_data is null" << std::endl;
+}
+void test_cb_mc_on_response(const wauthn_pubkey_credential_attestaion_s *pubkey_cred,
+ wauthn_error_e result,
+ void *user_data)
+{
+ std::cout << "mc response, result: " << wauthn_error_to_string(result) << std::endl;
+ if (pubkey_cred == nullptr)
+ std::cout << "pubkey_cred is null" << std::endl;
+ if (user_data == nullptr)
+ std::cout << "user_data is null" << std::endl;
+ if (client_data != nullptr)
+ free(client_data);
+ if (mc_options != nullptr)
+ free(mc_options);
+ if (mc_options2 != nullptr)
+ free(mc_options2);
+
+}
-TEST_F(WebAuthnTest, testMakeCredential)
+void test_cb_ga_on_response(const wauthn_pubkey_credential_assertion_s *pubkey_cred,
+ wauthn_error_e result,
+ void *user_data)
{
+ std::cout << "ga response, result: " << wauthn_error_to_string(result) << std::endl;
+ if (pubkey_cred == nullptr)
+ std::cout << "pubkey_cred is null" << std::endl;
+ if (user_data == nullptr)
+ std::cout << "user_data is null" << std::endl;
+
+ if (client_data != nullptr)
+ free(client_data);
+ if (ga_options != nullptr)
+ free(ga_options);
+ if (ga_options2 != nullptr)
+ free(ga_options2);
+}
+
+void mc_func1()
+{
+ std::cout << "mc_func1\n";
int ret = WAUTHN_ERROR_NONE;
- ret = wauthn_make_credential(NULL, NULL, NULL);
+ wauthn_mc_callbacks_s *callbacks = nullptr;
+ callbacks = (wauthn_mc_callbacks_s*) calloc(1, sizeof(wauthn_mc_callbacks_s));
+ callbacks->qrcode_callback = test_cb_display_qrcode;
+ callbacks->response_callback = test_cb_mc_on_response;
+
+ client_data = (wauthn_client_data_s *) calloc(1, sizeof(wauthn_client_data_s));
+ mc_options = (wauthn_pubkey_cred_creation_options_s *) calloc(1, sizeof(wauthn_pubkey_cred_creation_options_s));
+
+ ret = wauthn_make_credential(client_data, mc_options, callbacks);
+
EXPECT_EQ(ret, WAUTHN_ERROR_NONE)
<< "[wauthn_make_credential] failed. " << "ret=" << wauthn_error_to_string(ret) << std::endl;
+ std::cout << "wauthn_make_credential ret=" << wauthn_error_to_string(ret) << std::endl;
+}
+void ga_func1()
+{
+ std::cout << "ga_func1\n";
+ int ret = WAUTHN_ERROR_NONE;
+
+ wauthn_ga_callbacks_s *callbacks = nullptr;
+ callbacks = (wauthn_ga_callbacks_s*) calloc(1, sizeof(wauthn_ga_callbacks_s));
+ callbacks->qrcode_callback = test_cb_display_qrcode;
+ callbacks->response_callback = test_cb_ga_on_response;
+
+ client_data = (wauthn_client_data_s *) calloc(1, sizeof(wauthn_client_data_s));
+ ga_options = (wauthn_pubkey_cred_request_options_s *) calloc(1, sizeof(wauthn_pubkey_cred_request_options_s));
+
+ ret = wauthn_get_assertion(client_data, ga_options, callbacks);
+ EXPECT_EQ(ret, WAUTHN_ERROR_NONE)
+ << "[wauthn_get_assertion] failed. " << "ret=" << wauthn_error_to_string(ret) << std::endl;
+ std::cout << "wauthn_get_assertion ret=" << wauthn_error_to_string(ret) << std::endl;
+
+}
+
+TEST_F(WebAuthnTest, testMCRequest)
+{
+ thread t1(mc_func1);
+ t1.join();
+ sleep(15);
+}
+
+TEST_F(WebAuthnTest, testGARequest)
+{
+ thread t1(ga_func1);
+ t1.join();
+ sleep(15);
+}
+
+
+TEST_F(WebAuthnTest, testNotAllowedByBusy)
+{
+ int ret = WAUTHN_ERROR_NONE;
+
+ wauthn_mc_callbacks_s *mc_callbacks = nullptr;
+ mc_callbacks = (wauthn_mc_callbacks_s*) calloc(1, sizeof(wauthn_mc_callbacks_s));
+ mc_callbacks->qrcode_callback = test_cb_display_qrcode;
+ mc_callbacks->response_callback = test_cb_mc_on_response;
+
+
+ client_data = (wauthn_client_data_s *) calloc(1, sizeof(wauthn_client_data_s));
+ mc_options = (wauthn_pubkey_cred_creation_options_s *) calloc(1, sizeof(wauthn_pubkey_cred_creation_options_s));
+
+ ret = wauthn_make_credential(client_data, mc_options, mc_callbacks);
+
+ EXPECT_EQ(ret, WAUTHN_ERROR_NONE)
+ << "[wauthn_make_credential] failed. " << "ret=" << wauthn_error_to_string(ret) << std::endl;
std::cout << "wauthn_make_credential ret=" << wauthn_error_to_string(ret) << std::endl;
+
+ sleep(1);
+
+ wauthn_ga_callbacks_s *ga_callbacks = nullptr;
+ ga_callbacks = (wauthn_ga_callbacks_s*) calloc(1, sizeof(wauthn_ga_callbacks_s));
+ ga_callbacks->qrcode_callback = test_cb_display_qrcode;
+ ga_callbacks->response_callback = test_cb_ga_on_response;
+
+ client_data2 = (wauthn_client_data_s *) calloc(1, sizeof(wauthn_client_data_s));
+ ga_options = (wauthn_pubkey_cred_request_options_s *) calloc(1, sizeof(wauthn_pubkey_cred_request_options_s));
+
+ ret = wauthn_get_assertion(client_data2, ga_options, ga_callbacks);
+ EXPECT_EQ(ret, WAUTHN_ERROR_NOT_ALLOWED)
+ << "[wauthn_get_assertion] failed. " << "ret=" << wauthn_error_to_string(ret) << std::endl;
+ std::cout << "wauthn_get_assertion ret=" << wauthn_error_to_string(ret) << std::endl;
+ sleep(15);
}
+
+TEST_F(WebAuthnTest, testCancelOK)
+{
+ int ret = WAUTHN_ERROR_NONE;
+
+ wauthn_mc_callbacks_s *mc_callbacks = nullptr;
+ mc_callbacks = (wauthn_mc_callbacks_s*) calloc(1, sizeof(wauthn_mc_callbacks_s));
+ mc_callbacks->qrcode_callback = test_cb_display_qrcode;
+ mc_callbacks->response_callback = test_cb_mc_on_response;
+
+ client_data = (wauthn_client_data_s *) calloc(1, sizeof(wauthn_client_data_s));
+ mc_options = (wauthn_pubkey_cred_creation_options_s *) calloc(1, sizeof(wauthn_pubkey_cred_creation_options_s));
+ ret = wauthn_make_credential(client_data, mc_options, mc_callbacks);
+
+ EXPECT_EQ(ret, WAUTHN_ERROR_NONE)
+ << "[wauthn_make_credential] failed. " << "ret=" << wauthn_error_to_string(ret) << std::endl;
+ std::cout << "wauthn_make_credential ret=" << wauthn_error_to_string(ret) << std::endl;
+
+ sleep(1);
+
+ ret = wauthn_cancel();
+ EXPECT_EQ(ret, WAUTHN_ERROR_NONE)
+ << "[wauthn_cancel] failed. " << "ret=" << wauthn_error_to_string(ret) << std::endl;
+ std::cout << "wauthn_cancel ret=" << wauthn_error_to_string(ret) << std::endl;
+ sleep(15);
+}
+