Skip to content

Commit

Permalink
all ut works! <3
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavrax committed Oct 5, 2023
1 parent c5563d7 commit c054bb4
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 21 deletions.
5 changes: 0 additions & 5 deletions core/pbcc_crypto.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */

//#ifdef PUBNUB_CRYPTO_API

#include <stdint.h>
#include <stdio.h>
#include "pbbase64.h"
Expand Down Expand Up @@ -282,6 +280,3 @@ pubnub_bymebl_t pbcc_base64_decode(const char* buffer) {
return pbbase64_decode_alloc_std_str(buffer);
}



//#endif // PUBNUB_CRYPTO_API
4 changes: 2 additions & 2 deletions core/pbcc_crypto.h
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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 */
6 changes: 6 additions & 0 deletions core/pubnub_ccore_pubsub.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
}


Expand Down Expand Up @@ -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);

Expand All @@ -130,6 +133,7 @@ char const* pbcc_get_msg(struct pbcc_context* pb)

rslt = (char*)rslt_block.ptr;
}
#endif // PUBNUB_CRYPTO_API

return rslt;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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++] = '/';
Expand Down
3 changes: 2 additions & 1 deletion core/pubnub_ccore_pubsub.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};


Expand Down
2 changes: 2 additions & 0 deletions core/pubnub_crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 */
14 changes: 7 additions & 7 deletions core/pubnub_crypto_unit_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -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\"]",
Expand All @@ -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\"]",
Expand All @@ -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],"
Expand All @@ -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]",
Expand Down
2 changes: 2 additions & 0 deletions core/pubnub_pubsubapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions core/test/pubnub_test_mocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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));
Expand All @@ -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));
Expand Down
6 changes: 3 additions & 3 deletions core/test/pubnub_test_mocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c054bb4

Please sign in to comment.