Skip to content

Commit

Permalink
Iothub_client_init/deinit implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jebrando authored and Azure IoT Builder committed May 30, 2018
1 parent 1282b17 commit c4c843d
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 5 deletions.
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ if(${use_custom_heap})
add_definitions(-DGB_USE_CUSTOM_HEAP)
endif()

if (NOT ${use_amqp} AND NOT ${use_http} AND NOT ${use_mqtt})
message(FATAL_ERROR "CMAKE Failure: AMQP, HTTP & MQTT are all disable, iothub client must have one protocol enabled")
endif()

message(STATUS "Provisioning client ${use_prov_client}")
if (XCODE AND ${use_prov_client})
# The TPM module is not available on Mac, and Mac's <string.h> and <unistd.h> files collide as well
Expand Down Expand Up @@ -178,9 +182,11 @@ endif()
# do not add or build any tests of the dependencies
set(original_run_e2e_tests ${run_e2e_tests})
set(original_run_unittests ${run_unittests})
set(original_skip_samples ${skip_samples})

set(run_e2e_tests OFF)
set(run_unittests OFF)
set(skip_samples ON)

include("dependencies.cmake")

Expand All @@ -193,6 +199,7 @@ set_platform_files(${CMAKE_CURRENT_LIST_DIR}/c-utility)

set(run_e2e_tests ${original_run_e2e_tests})
set(run_unittests ${original_run_unittests})
set(skip_samples ${original_skip_samples})

enable_testing()

Expand Down Expand Up @@ -275,7 +282,7 @@ if (NOT ${use_amqp} OR NOT ${use_http})
message(STATUS "iothub_service_client build is disabled (AMQP and HTTP support are required)")
endif()

if (NOT ${use_http})
if (NOT ${use_http} AND ${use_prov_client})
set (build_provisioning_service_client OFF)
message(STATUS "provisioning_service_client build is disabled (HTTP support is required)")
endif()
Expand Down
8 changes: 4 additions & 4 deletions dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ if(${use_installed_dependencies})
endif ()

else ()
add_subdirectory(c-utility EXCLUDE_FROM_ALL)
add_subdirectory(c-utility)

if (${use_amqp})
add_subdirectory(uamqp EXCLUDE_FROM_ALL)
add_subdirectory(uamqp)
endif ()

if (${use_mqtt})
add_subdirectory(umqtt EXCLUDE_FROM_ALL)
add_subdirectory(umqtt)
endif ()

if (${use_http})
add_subdirectory(deps/uhttp EXCLUDE_FROM_ALL)
add_subdirectory(deps/uhttp)
endif ()
endif()

Expand Down
2 changes: 2 additions & 0 deletions iothub_client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ endif()
compileAsC99()

set(iothub_client_c_files
./src/iothub.c
./src/iothub_client.c
./src/iothub_client_core.c
./src/iothub_client_core_ll.c
Expand All @@ -24,6 +25,7 @@ set(iothub_client_c_files
)

set(iothub_client_h_files
./inc/iothub.h
./inc/iothub_client_core.h
./inc/iothub_client_core_ll.h
./inc/iothub_client.h
Expand Down
33 changes: 33 additions & 0 deletions iothub_client/inc/iothub.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#ifndef IOTHUB_H
#define IOTHUB_H

#include "azure_c_shared_utility/umock_c_prod.h"

#ifdef __cplusplus
extern "C"
{
#else
#endif
/**
* @brief IoTHubClient_Init Initializes the IoTHub Client System.
*
* @param client_param Reserved for future use.
*
* @return int zero upon success, any other value upon failure.
*/
MOCKABLE_FUNCTION(, int, IoTHub_Init);

/**
* @brief IoTHubClient_Deinit Frees resources initialized in the IoTHubClient_Init function call.
*
*/
MOCKABLE_FUNCTION(, void, IoTHub_Deinit);

#ifdef __cplusplus
}
#endif

#endif /* IOTHUB_H */
29 changes: 29 additions & 0 deletions iothub_client/src/iothub.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#include <stdlib.h>

#include "azure_c_shared_utility/platform.h"
#include "azure_c_shared_utility/xlogging.h"
#include "azure_c_shared_utility/macro_utils.h"
#include "iothub.h"

int IoTHub_Init()
{
int result;
if (platform_init() != 0)
{
LogError("Platform initialization failed");
result = __FAILURE__;
}
else
{
result = 0;
}
return result;
}

void IoTHub_Deinit()
{
platform_deinit();
}
1 change: 1 addition & 0 deletions iothub_client/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function(addSupportedTransportsToTest whatExecutableIsBuilding)
endfunction()

#this is CMakeLists for iothub_client tests folder
add_unittest_directory(iothub_ut)
add_unittest_directory(iothub_client_authorization_ut)
add_unittest_directory(iothubclient_ll_ut)
add_unittest_directory(iothubclientcore_ll_ut)
Expand Down
23 changes: 23 additions & 0 deletions iothub_client/tests/iothub_ut/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#Copyright (c) Microsoft. All rights reserved.
#Licensed under the MIT license. See LICENSE file in the project root for full license information.

cmake_minimum_required(VERSION 2.8.11)

compileAsC11()

set(theseTestsName iothub_ut)

set(${theseTestsName}_test_files
${theseTestsName}.c
)

include_directories(${SHARED_UTIL_REAL_TEST_FOLDER})

set(${theseTestsName}_c_files
../../src/iothub.c
)

set(${theseTestsName}_h_files
)

build_c_test_artifacts(${theseTestsName} ON "tests/azure_iothub_client_tests")
97 changes: 97 additions & 0 deletions iothub_client/tests/iothub_ut/iothub_ut.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#ifdef __cplusplus
#include <cstdlib>
#include <cstddef>
#else
#include <stdlib.h>
#include <stddef.h>
#include <stdbool.h>
#endif

#include "testrunnerswitcher.h"
#include "azure_c_shared_utility/macro_utils.h"

#include "umock_c.h"
#include "umock_c_negative_tests.h"
#include "umocktypes_charptr.h"

#define ENABLE_MOCKS
#include "azure_c_shared_utility/platform.h"
#undef ENABLE_MOCKS

#include "iothub.h"

static TEST_MUTEX_HANDLE test_serialize_mutex;
static TEST_MUTEX_HANDLE g_dllByDll;
DEFINE_ENUM_STRINGS(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES)

static void on_umock_c_error(UMOCK_C_ERROR_CODE error_code)
{
char temp_str[256];
(void)snprintf(temp_str, sizeof(temp_str), "umock_c reported error :%s", ENUM_TO_STRING(UMOCK_C_ERROR_CODE, error_code));
ASSERT_FAIL(temp_str);
}

BEGIN_TEST_SUITE(iothub_ut)

TEST_SUITE_INITIALIZE(suite_init)
{
TEST_INITIALIZE_MEMORY_DEBUG(g_dllByDll);

test_serialize_mutex = TEST_MUTEX_CREATE();
ASSERT_IS_NOT_NULL(test_serialize_mutex);

umock_c_init(on_umock_c_error);

REGISTER_GLOBAL_MOCK_RETURN(platform_init, 0);
REGISTER_GLOBAL_MOCK_FAIL_RETURN(platform_init, __LINE__);
}

TEST_SUITE_CLEANUP(suite_cleanup)
{
umock_c_deinit();
TEST_MUTEX_DESTROY(test_serialize_mutex);
TEST_DEINITIALIZE_MEMORY_DEBUG(g_dllByDll);
}

TEST_FUNCTION(IoTHub_Init_succeed)
{
//arrange
STRICT_EXPECTED_CALL(platform_init());

//act
int result = IoTHub_Init();

//assert
ASSERT_ARE_EQUAL(int, 0, result);
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
}

TEST_FUNCTION(IoTHub_Init_fail)
{
//arrange
STRICT_EXPECTED_CALL(platform_init());

//act
int result = IoTHub_Init();

//assert
ASSERT_ARE_EQUAL(int, 0, result);
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
}

TEST_FUNCTION(IoTHub_Deinit_succeed)
{
//arrange
STRICT_EXPECTED_CALL(platform_deinit());

//act
IoTHub_Deinit();

//assert
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
}

END_TEST_SUITE(iothub_ut)
11 changes: 11 additions & 0 deletions iothub_client/tests/iothub_ut/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#include "testrunnerswitcher.h"

int main(void)
{
size_t failedTestCount = 0;
RUN_TEST_SUITE(iothub_ut, failedTestCount);
return failedTestCount;
}

0 comments on commit c4c843d

Please sign in to comment.