From c054bb479fa134ff458782d92056b68c983047d4 Mon Sep 17 00:00:00 2001 From: Xavrax Date: Fri, 6 Oct 2023 00:11:07 +0200 Subject: [PATCH] all ut works! <3 --- core/pbcc_crypto.c | 5 ----- core/pbcc_crypto.h | 4 ++-- core/pubnub_ccore_pubsub.c | 6 ++++++ core/pubnub_ccore_pubsub.h | 3 ++- core/pubnub_crypto.h | 2 ++ core/pubnub_crypto_unit_tests.c | 14 +++++++------- core/pubnub_pubsubapi.c | 2 ++ core/test/pubnub_test_mocks.c | 6 +++--- core/test/pubnub_test_mocks.h | 6 +++--- 9 files changed, 27 insertions(+), 21 deletions(-) diff --git a/core/pbcc_crypto.c b/core/pbcc_crypto.c index 7d9a6348..5f498e2e 100644 --- a/core/pbcc_crypto.c +++ b/core/pbcc_crypto.c @@ -1,7 +1,5 @@ /* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */ -//#ifdef PUBNUB_CRYPTO_API - #include #include #include "pbbase64.h" @@ -282,6 +280,3 @@ pubnub_bymebl_t pbcc_base64_decode(const char* buffer) { return pbbase64_decode_alloc_std_str(buffer); } - - -//#endif // PUBNUB_CRYPTO_API diff --git a/core/pbcc_crypto.h b/core/pbcc_crypto.h index d4676dd7..7aa985af 100644 --- a/core/pbcc_crypto.h +++ b/core/pbcc_crypto.h @@ -1,6 +1,6 @@ /* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */ -//#ifdef PUBNUB_CRYPTO_API +#ifdef PUBNUB_CRYPTO_API #ifndef PBCC_CRYPTO_H #define PBCC_CRYPTO_H @@ -274,4 +274,4 @@ const char* pbcc_base64_encode(pubnub_bymebl_t buffer); pubnub_bymebl_t pbcc_base64_decode(const char* buffer); #endif /* PBCC_CRYPTO_H */ -//#endif /* PUBNUB_CRYPTO_API */ +#endif /* PUBNUB_CRYPTO_API */ diff --git a/core/pubnub_ccore_pubsub.c b/core/pubnub_ccore_pubsub.c index e6281518..fb485f00 100644 --- a/core/pubnub_ccore_pubsub.c +++ b/core/pubnub_ccore_pubsub.c @@ -66,10 +66,12 @@ void pbcc_deinit(struct pbcc_context* p) } #endif /* PUBNUB_RECEIVE_GZIP_RESPONSE */ #endif /* PUBNUB_DYNAMIC_REPLY_BUFFER */ +#if PUBNUB_CRYPTO_API if (NULL != p->crypto_module) { free(p->crypto_module); p->crypto_module = NULL; } +#endif /* PUBNUB_CRYPTO_API */ } @@ -113,6 +115,7 @@ char const* pbcc_get_msg(struct pbcc_context* pb) char const* rslt = pb->http_reply + pb->msg_ofs; pb->msg_ofs += strlen(rslt); if (pb->msg_ofs++ <= pb->msg_end) { +#if PUBNUB_CRYPTO_API if (NULL != pb->crypto_module) { pubnub_bymebl_t encrypted = pbcc_base64_decode(rslt); @@ -130,6 +133,7 @@ char const* pbcc_get_msg(struct pbcc_context* pb) rslt = (char*)rslt_block.ptr; } +#endif // PUBNUB_CRYPTO_API return rslt; } @@ -528,6 +532,7 @@ enum pubnub_res pbcc_publish_prep(struct pbcc_context* pb, APPEND_URL_ENCODED_M(pb, channel); APPEND_URL_LITERAL_M(pb, "/0"); +#if PUBNUB_CRYPTO_API if (NULL != pb->crypto_module) { pubnub_bymebl_t message_block = { .ptr = (uint8_t*)message, .size = strlen(message) }; pubnub_bymebl_t encrypted = pb->crypto_module->encrypt(pb->crypto_module, message_block); @@ -548,6 +553,7 @@ enum pubnub_res pbcc_publish_prep(struct pbcc_context* pb, return PNR_INTERNAL_ERROR; } } +#endif // PUBNUB_CRYPTO_API if (pubnubSendViaGET == method) { pb->http_buf[pb->http_buf_len++] = '/'; diff --git a/core/pubnub_ccore_pubsub.h b/core/pubnub_ccore_pubsub.h index d56d7933..6d1d2d7e 100644 --- a/core/pubnub_ccore_pubsub.h +++ b/core/pubnub_ccore_pubsub.h @@ -115,9 +115,10 @@ struct pbcc_context { #if PUBNUB_CRYPTO_API /** Secret key to use for encryption/decryption */ char const* secret_key; -#endif + /** Crypto module for encryption and decryption */ struct pubnub_crypto_provider_t *crypto_module; +#endif // PUBNUB_CRYPTO_API }; diff --git a/core/pubnub_crypto.h b/core/pubnub_crypto.h index 85041431..8b3084af 100644 --- a/core/pubnub_crypto.h +++ b/core/pubnub_crypto.h @@ -166,6 +166,7 @@ char* pn_pam_hmac_sha256_sign(char const* key, char const* message); enum pubnub_res pn_gen_pam_v2_sign(pubnub_t* p, char const* qs_to_sign, char const* partial_url, char* signature); enum pubnub_res pn_gen_pam_v3_sign(pubnub_t* p, char const* qs_to_sign, char const* partial_url, char const* msg, char* signature); +#if PUBNUB_CRYPTO_API /** Prepare the aes cbc crypto module for use. @@ -222,6 +223,7 @@ struct pubnub_crypto_provider_t *pubnub_crypto_module_init(struct pubnub_cryptor @param crypto_provider Pointer to the crypto provider to use. */ void pubnub_set_crypto_module(pubnub_t *pubnub, struct pubnub_crypto_provider_t *crypto_provider); +#endif /* PUBNUB_CRYPTO_API */ #endif /* defined INC_PUBNUB_CRYPTO */ diff --git a/core/pubnub_crypto_unit_tests.c b/core/pubnub_crypto_unit_tests.c index 0e361e2e..e7545f70 100644 --- a/core/pubnub_crypto_unit_tests.c +++ b/core/pubnub_crypto_unit_tests.c @@ -94,9 +94,9 @@ Ensure(crypto_api, module_should_properly_select_algorythm_for_decryption) { Ensure(crypto_api, client_should_use_cryptors_for_publish) { pubnub_set_crypto_module(pbp, pubnub_crypto_module_init(&x_cryptor, &y_cryptor, 1)); - expect_have_dns_for_pubnub_origin(pbp); + expect_have_dns_for_pubnub_origin_on(pbp); - expect_outgoing_with_url_no_params(pbp, + expect_outgoing_with_url_no_params_on(pbp, "/publish/pub_key/sub_key/0/jarak/0/UE5FRAF4eHh4BG1ldGF4eHh4"); incoming("HTTP/1.1 200\r\nContent-Length: " "30\r\n\r\n[1,\"Sent\",\"14178940800777403\"]", @@ -113,8 +113,8 @@ Ensure(crypto_api, client_should_use_cryptors_for_subscribe) { pubnub_set_crypto_module(pbp, pubnub_crypto_module_init(&x_cryptor, &y_cryptor, 1)); assert_that(pubnub_last_time_token(pbp), is_equal_to_string("0")); - expect_have_dns_for_pubnub_origin(pbp); - expect_outgoing_with_url_no_params(pbp, + expect_have_dns_for_pubnub_origin_on(pbp); + expect_outgoing_with_url_no_params_on(pbp, "/subscribe/sub_key/health/0/0"); incoming("HTTP/1.1 200\r\nContent-Length: " "26\r\n\r\n[[],\"1516014978925123457\"]", @@ -131,7 +131,7 @@ Ensure(crypto_api, client_should_use_cryptors_for_subscribe) { expect(pbntf_enqueue_for_processing, when(pb, is_equal_to(pbp)), will_return(0)); expect(pbntf_got_socket, when(pb, is_equal_to(pbp)), will_return(0)); - expect_outgoing_with_url_no_params(pbp, "/subscribe/sub_key/health/0/" + expect_outgoing_with_url_no_params_on(pbp, "/subscribe/sub_key/health/0/" "1516014978925123457"); incoming("HTTP/1.1 200\r\nContent-Length: " "50\r\n\r\n[[UE5FRAF4eHh4BG1ldGF4eHh4]," @@ -151,9 +151,9 @@ Ensure(crypto_api, client_should_use_cryptors_for_subscribe) { Ensure(crypto_api, client_should_use_cryptors_for_history) { pubnub_set_crypto_module(pbp, pubnub_crypto_module_init(&x_cryptor, &y_cryptor, 1)); - expect_have_dns_for_pubnub_origin(pbp); + expect_have_dns_for_pubnub_origin_on(pbp); - expect_outgoing_with_url_no_params(pbp, "/v2/history/sub-key/sub_key/channel/" + expect_outgoing_with_url_no_params_on(pbp, "/v2/history/sub-key/sub_key/channel/" "ch"); incoming("HTTP/1.1 200\r\nContent-Length: " "26\r\n\r\n[UE5FRAF4eHh4BG1ldGF4eHh4]", diff --git a/core/pubnub_pubsubapi.c b/core/pubnub_pubsubapi.c index 4408034d..972d5063 100644 --- a/core/pubnub_pubsubapi.c +++ b/core/pubnub_pubsubapi.c @@ -85,7 +85,9 @@ pubnub_t* pubnub_init(pubnub_t* p, const char* publish_key, const char* subscrib #if PUBNUB_RECEIVE_GZIP_RESPONSE p->data_compressed = compressionNONE; #endif +#if PUBNUB_CRYPTO_API p->core.crypto_module = NULL; +#endif pubnub_mutex_unlock(p->monitor); diff --git a/core/test/pubnub_test_mocks.c b/core/test/pubnub_test_mocks.c index 2720b2d4..e6d80700 100644 --- a/core/test/pubnub_test_mocks.c +++ b/core/test/pubnub_test_mocks.c @@ -564,7 +564,7 @@ void pubnub_cleanup_mocks(pubnub_t* pbp) free_m_msgs(m_string_msg_array); } -void expect_have_dns_for_pubnub_origin(pubnub_t* pbp) +void expect_have_dns_for_pubnub_origin_on(pubnub_t* pbp) { expect(pbntf_enqueue_for_processing, when(pb, equals(pbp)), returns(0)); expect(pbpal_resolv_and_connect, @@ -573,7 +573,7 @@ void expect_have_dns_for_pubnub_origin(pubnub_t* pbp) expect(pbntf_got_socket, when(pb, equals(pbp)), returns(0)); } -void expect_outgoing_with_url(pubnub_t* pbp, char const* url) +void expect_outgoing_with_url_on(pubnub_t* pbp, char const* url) { expect(pbpal_send_str, when(s, streqs("GET ")), returns(0)); expect(pbpal_send_status, returns(0)); @@ -592,7 +592,7 @@ void expect_outgoing_with_url(pubnub_t* pbp, char const* url) expect(pbntf_watch_in_events, when(pb, equals(pbp)), returns(0)); } -void expect_outgoing_with_url_no_params(pubnub_t* pbp, char const* url) +void expect_outgoing_with_url_no_params_on(pubnub_t* pbp, char const* url) { expect(pbpal_send_str, when(s, streqs("GET ")), returns(0)); expect(pbpal_send_status, returns(0)); diff --git a/core/test/pubnub_test_mocks.h b/core/test/pubnub_test_mocks.h index a1db5ec3..d6020a61 100644 --- a/core/test/pubnub_test_mocks.h +++ b/core/test/pubnub_test_mocks.h @@ -34,10 +34,10 @@ void pubnub_setup_mocks(pubnub_t** pbp); void pubnub_cleanup_mocks(pubnub_t* pbp); -void expect_have_dns_for_pubnub_origin(pubnub_t* pbp); +void expect_have_dns_for_pubnub_origin_on(pubnub_t* pbp); -void expect_outgoing_with_url(pubnub_t* pbp, char const* url); +void expect_outgoing_with_url_on(pubnub_t* pbp, char const* url); -void expect_outgoing_with_url_no_params(pubnub_t* pbp, char const* url); +void expect_outgoing_with_url_no_params_on(pubnub_t* pbp, char const* url); #endif // INC_PUBNUB_TEST_MOCKS