IOTHUB_ACCOUNT_INFO_HANDLE IoTHubAccount_Init_With_Config()

in testtools/iothub_test/src/iothub_account.c [703:830]


IOTHUB_ACCOUNT_INFO_HANDLE IoTHubAccount_Init_With_Config(IOTHUB_ACCOUNT_CONFIG* config, bool testingModules)
{
    IOTHUB_ACCOUNT_INFO* iothub_account_info;
    int result;

    if (config == NULL)
    {
        LogError("[IoTHubAccount] invalid configuration (NULL)");
        result = MU_FAILURE;
        iothub_account_info = NULL;
    }
    else
    {
        if ((iothub_account_info = calloc(1, sizeof(IOTHUB_ACCOUNT_INFO))) == NULL)
        {
            LogError("[IoTHubAccount] Failed allocating IOTHUB_ACCOUNT_INFO.");
            result = MU_FAILURE;
        }
        else if ((iothub_account_info->sasDevices = (IOTHUB_PROVISIONED_DEVICE**)malloc(sizeof(IOTHUB_PROVISIONED_DEVICE*) * config->number_of_sas_devices)) == NULL)
        {
            LogError("[IoTHubAccount] Failed allocating array for SAS devices");
            result = MU_FAILURE;
        }
        else
        {
            char* base64_cert;
            char* base64_key;
            char* tempThumb;

            iothub_account_info->number_of_sas_devices = config->number_of_sas_devices;
            memset(iothub_account_info->sasDevices, 0, sizeof(IOTHUB_PROVISIONED_DEVICE*) * iothub_account_info->number_of_sas_devices);

            iothub_account_info->connString = getenv("IOTHUB_CONNECTION_STRING");
            iothub_account_info->eventhubConnString = getenv("IOTHUB_EVENTHUB_CONNECTION_STRING");
            base64_cert = getenv("IOTHUB_E2E_X509_CERT_BASE64");
            base64_key = getenv("IOTHUB_E2E_X509_PRIVATE_KEY_BASE64");
            tempThumb = getenv("IOTHUB_E2E_X509_THUMBPRINT");

            if (iothub_account_info->connString == NULL)
            {
                LogError("Failure retrieving IoT Hub connection string from the environment.\r\n");
                result = MU_FAILURE;
            }
            else if (iothub_account_info->eventhubConnString == NULL)
            {
                LogError("Failure retrieving Event Hub connection string from the environment.\r\n");
                result = MU_FAILURE;
            }
            else if (base64_cert == NULL)
            {
                LogError("Failure retrieving x509 certificate from the environment.\r\n");
                result = MU_FAILURE;
            }
            else if (base64_key == NULL)
            {
                LogError("Failure retrieving x509 private key from the environment.\r\n");
                result = MU_FAILURE;
            }
            else if (tempThumb == NULL)
            {
                LogError("Failure retrieving x509 certificate thumbprint from the environment.\r\n");
                result = MU_FAILURE;
            }
            else if ((iothub_account_info->x509Certificate = convert_base64_to_string(base64_cert)) == NULL)
            {
                LogError("Failure allocating x509 certificate from the environment.\r\n");
                result = MU_FAILURE;
            }
            else if ((iothub_account_info->x509PrivateKey = convert_base64_to_string(base64_key)) == NULL)
            {
                LogError("Failure allocating x509 key from the environment.\r\n");
                result = MU_FAILURE;
            }
            else if (mallocAndStrcpy_s(&iothub_account_info->x509Thumbprint, tempThumb) != 0)
            {
                LogError("Failure allocating x509 thumbprint from the environment.\r\n");
                result = MU_FAILURE;
            }
            else if (retrieveConnStringInfo(iothub_account_info) != 0)
            {
                LogError("retrieveConnStringInfo failed.\r\n");
                result = MU_FAILURE;
            }
            else if ((iothub_account_info->iothub_service_client_auth_handle = IoTHubServiceClientAuth_CreateFromConnectionString(iothub_account_info->connString)) == NULL)
            {
                LogError("IoTHubServiceClientAuth_CreateFromConnectionString failed.\r\n");
                result = MU_FAILURE;
            }
            else if ((iothub_account_info->iothub_messaging_handle = IoTHubMessaging_LL_Create(iothub_account_info->iothub_service_client_auth_handle)) == NULL)
            {
                LogError("IoTHubMessaging_LL_Create failed\r\n");
                result = MU_FAILURE;
            }
            else if ((iothub_account_info->iothub_registrymanager_handle = IoTHubRegistryManager_Create(iothub_account_info->iothub_service_client_auth_handle)) == NULL)
            {
                LogError("IoTHubRegistryManager_Create failed\r\n");
                result = MU_FAILURE;
            }
            else if (provisionDevices(iothub_account_info, IOTHUB_ACCOUNT_AUTH_CONNSTRING, iothub_account_info->sasDevices, iothub_account_info->number_of_sas_devices) != 0)
            {
                LogError("Failed to create the SAS device(s)\r\n");
                result = MU_FAILURE;
            }
            else if (provisionDevice(iothub_account_info, IOTHUB_ACCOUNT_AUTH_X509, &iothub_account_info->x509Device) != 0)
            {
                LogError("Failed to create the X509 device\r\n");
                result = MU_FAILURE;
            }
            else if (testingModules && provisionModule(iothub_account_info, iothub_account_info->sasDevices[0]))
            {
                LogError("Failed to create the module\r\n");
                result = MU_FAILURE;
            }
            else 
            {
                result = 0;
            }
        }
    }

    if (result != 0)
    {
        IoTHubAccount_deinit(iothub_account_info);
        iothub_account_info = NULL;
    }

    return (IOTHUB_ACCOUNT_INFO_HANDLE)iothub_account_info;
}