/** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. */ #include #include #include static const char* ALLOCATION_TAG = "AWSImportExportEndpointProviderTests"; using ImportExportEndpointProvider = Aws::ImportExport::Endpoint::ImportExportEndpointProvider; using EndpointParameters = Aws::Vector; using ResolveEndpointOutcome = Aws::Endpoint::ResolveEndpointOutcome; using EpParam = Aws::Endpoint::EndpointParameter; using EpProp = Aws::Endpoint::EndpointParameter; // just a container to store test expectations using ExpEpProps = Aws::UnorderedMap>; using ExpEpHeaders = Aws::UnorderedMap>; class ImportExportEndpointProviderTests : public ::testing::TestWithParam {}; struct ImportExportEndpointProviderEndpointTestCase { using OperationParamsFromTest = EndpointParameters; struct Expect { struct Endpoint { Aws::String url; ExpEpProps properties; ExpEpHeaders headers; } endpoint; Aws::String error; }; struct OperationInput { Aws::String operationName; OperationParamsFromTest operationParams; OperationParamsFromTest builtinParams; OperationParamsFromTest clientParams; }; Aws::String documentation; // Specification tells us it is Client Initialization parameters // At the same time, specification tells us to test EndpointProvider not the client itself // Hence params here will be set as a client params (just like a dedicated field above). Aws::Vector params; Aws::Vector tags; Expect expect; // Aws::Vector operationInput; }; static const Aws::Vector TEST_CASES = { /*TEST CASE 0*/ {"For region aws-global with FIPS disabled and DualStack disabled", // documentation {EpParam("UseFIPS", false), EpParam("Region", "aws-global"), EpParam("UseDualStack", false)}, // params {}, // tags {{/*epUrl*/"https://importexport.amazonaws.com", {/*properties*/{"authSchemes", {EpProp("signingRegion", "us-east-1"), EpProp("name", "sigv4"), EpProp("signingName", "IngestionService")}}}, {/*headers*/}}, {/*No error*/}} // expect }, /*TEST CASE 1*/ {"For custom endpoint with fips disabled and dualstack disabled", // documentation {EpParam("UseFIPS", false), EpParam("Endpoint", "https://example.com"), EpParam("Region", "us-east-1"), EpParam("UseDualStack", false)}, // params {}, // tags {{/*epUrl*/"https://example.com", {/*properties*/}, {/*headers*/}}, {/*No error*/}} // expect }, /*TEST CASE 2*/ {"For custom endpoint with fips enabled and dualstack disabled", // documentation {EpParam("UseFIPS", true), EpParam("Endpoint", "https://example.com"), EpParam("Region", "us-east-1"), EpParam("UseDualStack", false)}, // params {}, // tags {{/*No endpoint expected*/}, /*error*/"Invalid Configuration: FIPS and custom endpoint are not supported"} // expect }, /*TEST CASE 3*/ {"For custom endpoint with fips disabled and dualstack enabled", // documentation {EpParam("UseFIPS", false), EpParam("Endpoint", "https://example.com"), EpParam("Region", "us-east-1"), EpParam("UseDualStack", true)}, // params {}, // tags {{/*No endpoint expected*/}, /*error*/"Invalid Configuration: Dualstack and custom endpoint are not supported"} // expect } }; Aws::String RulesToSdkSignerName(const Aws::String& rulesSignerName) { Aws::String sdkSigner = "NullSigner"; if (rulesSignerName == "sigv4") { sdkSigner = "SignatureV4"; } else if (rulesSignerName == "sigv4a") { sdkSigner = "AsymmetricSignatureV4"; } else if (rulesSignerName == "none") { sdkSigner = "NullSigner"; } else if (rulesSignerName == "bearer") { sdkSigner = "Bearer"; } else { sdkSigner = rulesSignerName; } return sdkSigner; } void ValidateOutcome(const ResolveEndpointOutcome& outcome, const ImportExportEndpointProviderEndpointTestCase::Expect& expect) { if(!expect.error.empty()) { ASSERT_FALSE(outcome.IsSuccess()) << "Expected failure with message:\n" << expect.error; ASSERT_EQ(outcome.GetError().GetMessage(), expect.error); } else { AWS_ASSERT_SUCCESS(outcome); ASSERT_EQ(outcome.GetResult().GetURL(), expect.endpoint.url); const auto expAuthSchemesIt = expect.endpoint.properties.find("authSchemes"); if (expAuthSchemesIt != expect.endpoint.properties.end()) { const auto& endpointResultAttrs = outcome.GetResult().GetAttributes(); ASSERT_TRUE(endpointResultAttrs) << "Expected non-empty EndpointAttributes (authSchemes)"; for (const auto& expProperty : expAuthSchemesIt->second) { if (expProperty.GetName() == "name") { ASSERT_TRUE(!endpointResultAttrs->authScheme.GetName().empty()); ASSERT_EQ(RulesToSdkSignerName(expProperty.GetStrValueNoCheck()), endpointResultAttrs->authScheme.GetName()); } else if (expProperty.GetName() == "signingName") { ASSERT_TRUE(endpointResultAttrs->authScheme.GetSigningName()); ASSERT_EQ(expProperty.GetStrValueNoCheck(), endpointResultAttrs->authScheme.GetSigningName().value()); } else if (expProperty.GetName() == "signingRegion") { ASSERT_TRUE(endpointResultAttrs->authScheme.GetSigningRegion()); ASSERT_EQ(expProperty.GetStrValueNoCheck(), endpointResultAttrs->authScheme.GetSigningRegion().value()); } else if (expProperty.GetName() == "signingRegionSet") { ASSERT_TRUE(endpointResultAttrs->authScheme.GetSigningRegionSet()); ASSERT_EQ(expProperty.GetStrValueNoCheck(), endpointResultAttrs->authScheme.GetSigningRegionSet().value()); } else if (expProperty.GetName() == "disableDoubleEncoding") { ASSERT_TRUE(endpointResultAttrs->authScheme.GetDisableDoubleEncoding()); ASSERT_EQ(expProperty.GetBoolValueNoCheck(), endpointResultAttrs->authScheme.GetDisableDoubleEncoding().value()); } else { FAIL() << "Unsupported Auth type property " << expProperty.GetName() << ". Need to update test."; } } } EXPECT_EQ(expect.endpoint.headers.empty(), outcome.GetResult().GetHeaders().empty()); for(const auto& expHeaderVec : expect.endpoint.headers) { const auto& retHeaderIt = outcome.GetResult().GetHeaders().find(expHeaderVec.first); ASSERT_TRUE(retHeaderIt != outcome.GetResult().GetHeaders().end()); auto retHeaderVec = Aws::Utils::StringUtils::Split(retHeaderIt->second, ';'); std::sort(retHeaderVec.begin(), retHeaderVec.end()); auto expHeaderVecSorted = expHeaderVec.second; std::sort(expHeaderVecSorted.begin(), expHeaderVecSorted.end()); ASSERT_EQ(expHeaderVecSorted, retHeaderVec); } } } TEST_P(ImportExportEndpointProviderTests, EndpointProviderTest) { const size_t TEST_CASE_IDX = GetParam(); ASSERT_LT(TEST_CASE_IDX, TEST_CASES.size()) << "Something is wrong with the test fixture itself."; const ImportExportEndpointProviderEndpointTestCase& TEST_CASE = TEST_CASES.at(TEST_CASE_IDX); SCOPED_TRACE(Aws::String("\nTEST CASE # ") + Aws::Utils::StringUtils::to_string(TEST_CASE_IDX) + ": " + TEST_CASE.documentation); std::shared_ptr endpointProvider = Aws::MakeShared(ALLOCATION_TAG); ASSERT_TRUE(endpointProvider) << "Failed to allocate/initialize ImportExportEndpointProvider"; EndpointParameters endpointParameters; for(const auto& param : TEST_CASE.params) { endpointParameters.emplace(endpointParameters.end(), Aws::Endpoint::EndpointParameter(param)); } auto resolvedEndpointOutcome = endpointProvider->ResolveEndpoint(endpointParameters); ValidateOutcome(resolvedEndpointOutcome, TEST_CASE.expect); #if 0 // temporarily disabled for(const auto& operation : TEST_CASE.operationInput) { /* * Most specific to least specific value locations: staticContextParams contextParam clientContextParams Built-In Bindings Built-in binding default values */ const Aws::Vector> operationInputParams = {std::cref(operation.builtinParams), std::cref(operation.clientParams), std::cref(operation.operationParams)}; for(const auto& paramSource : operationInputParams) { for(const auto& param : paramSource.get()) { endpointParameters.emplace(endpointParameters.end(), Aws::Endpoint::EndpointParameter(param)); } } auto resolvedEndpointOutcomePerOperation = endpointProvider->ResolveEndpoint(endpointParameters); ValidateOutcome(resolvedEndpointOutcomePerOperation, TEST_CASE.expect); } #endif } INSTANTIATE_TEST_SUITE_P(EndpointTestsFromModel, ImportExportEndpointProviderTests, ::testing::Range((size_t) 0u, TEST_CASES.size()));