make an enum

pull/5318/head
yang-g 9 years ago
parent 7d2a3e1917
commit 5152cd29f8
  1. 62
      test/cpp/end2end/end2end_test.cc
  2. 28
      test/cpp/util/test_credentials_provider.cc
  3. 13
      test/cpp/util/test_credentials_provider.h

@ -191,14 +191,14 @@ class TestServiceImplDupPkg
class TestScenario { class TestScenario {
public: public:
TestScenario(bool proxy, const grpc::string& creds_type) TestScenario(bool proxy, TestCredentialsType type)
: use_proxy(proxy), credentials_type(creds_type) {} : use_proxy(proxy), credentials_type(type) {}
void Log() const { void Log() const {
gpr_log(GPR_INFO, "Scenario: proxy %d, credentials %s", use_proxy, gpr_log(GPR_INFO, "Scenario: proxy %d, credentials %s", use_proxy,
credentials_type.c_str()); TestCredentialsTypeToString(credentials_type).c_str());
} }
bool use_proxy; bool use_proxy;
const grpc::string credentials_type; TestCredentialsType credentials_type;
}; };
class End2endTest : public ::testing::TestWithParam<TestScenario> { class End2endTest : public ::testing::TestWithParam<TestScenario> {
@ -223,7 +223,7 @@ class End2endTest : public ::testing::TestWithParam<TestScenario> {
// Setup server // Setup server
ServerBuilder builder; ServerBuilder builder;
auto server_creds = GetServerCredentials(GetParam().credentials_type); auto server_creds = GetServerCredentials(GetParam().credentials_type);
if (GetParam().credentials_type != kInsecureCredentialsType) { if (GetParam().credentials_type != INSECURE_CREDENTIALS) {
server_creds->SetAuthMetadataProcessor(processor); server_creds->SetAuthMetadataProcessor(processor);
} }
builder.AddListeningPort(server_address_.str(), server_creds); builder.AddListeningPort(server_address_.str(), server_creds);
@ -933,7 +933,7 @@ TEST_P(End2endTest, ChannelState) {
// Takes 10s. // Takes 10s.
TEST_P(End2endTest, ChannelStateTimeout) { TEST_P(End2endTest, ChannelStateTimeout) {
if (GetParam().credentials_type != kInsecureCredentialsType) { if (GetParam().credentials_type != INSECURE_CREDENTIALS) {
return; return;
} }
int port = grpc_pick_unused_port_or_die(); int port = grpc_pick_unused_port_or_die();
@ -1142,7 +1142,7 @@ class SecureEnd2endTest : public End2endTest {
protected: protected:
SecureEnd2endTest() { SecureEnd2endTest() {
GPR_ASSERT(!GetParam().use_proxy); GPR_ASSERT(!GetParam().use_proxy);
GPR_ASSERT(GetParam().credentials_type != kInsecureCredentialsType); GPR_ASSERT(GetParam().credentials_type != INSECURE_CREDENTIALS);
} }
}; };
@ -1365,25 +1365,43 @@ TEST_P(SecureEnd2endTest, ClientAuthContext) {
EXPECT_EQ("*.test.youtube.com", ToString(auth_ctx->GetPeerIdentity()[2])); EXPECT_EQ("*.test.youtube.com", ToString(auth_ctx->GetPeerIdentity()[2]));
} }
INSTANTIATE_TEST_CASE_P( std::vector<TestScenario> CreateTestScenarios(bool use_proxy,
End2end, End2endTest, bool test_insecure,
::testing::Values(TestScenario(false, kInsecureCredentialsType), bool test_secure) {
TestScenario(false, kTlsCredentialsType))); std::vector<TestScenario> scenarios;
for (int i = INSECURE_CREDENTIALS; i < MAX_CREDENTIALS_TYPE; i++) {
if (i == INSECURE_CREDENTIALS && !test_insecure) {
continue;
}
if (i != INSECURE_CREDENTIALS && !test_secure) {
continue;
}
if (use_proxy) {
scenarios.push_back(
TestScenario(true, static_cast<TestCredentialsType>(i)));
}
scenarios.push_back(
TestScenario(false, static_cast<TestCredentialsType>(i)));
}
GPR_ASSERT(!scenarios.empty());
return scenarios;
}
INSTANTIATE_TEST_CASE_P(End2end, End2endTest,
::testing::ValuesIn(CreateTestScenarios(false, true,
true)));
INSTANTIATE_TEST_CASE_P( INSTANTIATE_TEST_CASE_P(End2endServerTryCancel, End2endServerTryCancelTest,
End2endServerTryCancel, End2endServerTryCancelTest, ::testing::ValuesIn(CreateTestScenarios(false, true,
::testing::Values(TestScenario(false, kInsecureCredentialsType))); false)));
INSTANTIATE_TEST_CASE_P( INSTANTIATE_TEST_CASE_P(ProxyEnd2end, ProxyEnd2endTest,
ProxyEnd2end, ProxyEnd2endTest, ::testing::ValuesIn(CreateTestScenarios(true, true,
::testing::Values(TestScenario(false, kInsecureCredentialsType), true)));
TestScenario(false, kTlsCredentialsType),
TestScenario(true, kInsecureCredentialsType),
TestScenario(true, kTlsCredentialsType)));
INSTANTIATE_TEST_CASE_P(SecureEnd2end, SecureEnd2endTest, INSTANTIATE_TEST_CASE_P(SecureEnd2end, SecureEnd2endTest,
::testing::Values(TestScenario(false, ::testing::ValuesIn(CreateTestScenarios(false, false,
kTlsCredentialsType))); true)));
} // namespace } // namespace
} // namespace testing } // namespace testing

@ -40,24 +40,24 @@ namespace grpc {
namespace testing { namespace testing {
std::shared_ptr<ChannelCredentials> GetChannelCredentials( std::shared_ptr<ChannelCredentials> GetChannelCredentials(
const grpc::string& type, ChannelArguments* args) { TestCredentialsType type, ChannelArguments* args) {
if (type == kInsecureCredentialsType) { if (type == INSECURE_CREDENTIALS) {
return InsecureChannelCredentials(); return InsecureChannelCredentials();
} else if (type == kTlsCredentialsType) { } else if (type == TLS_CREDENTIALS) {
SslCredentialsOptions ssl_opts = {test_root_cert, "", ""}; SslCredentialsOptions ssl_opts = {test_root_cert, "", ""};
args->SetSslTargetNameOverride("foo.test.google.fr"); args->SetSslTargetNameOverride("foo.test.google.fr");
return SslCredentials(ssl_opts); return SslCredentials(ssl_opts);
} else { } else {
gpr_log(GPR_ERROR, "Unsupported credentials type %s.", type.c_str()); gpr_log(GPR_ERROR, "Unsupported credentials type %d.", type);
} }
return nullptr; return nullptr;
} }
std::shared_ptr<ServerCredentials> GetServerCredentials( std::shared_ptr<ServerCredentials> GetServerCredentials(
const grpc::string& type) { TestCredentialsType type) {
if (type == kInsecureCredentialsType) { if (type == INSECURE_CREDENTIALS) {
return InsecureServerCredentials(); return InsecureServerCredentials();
} else if (type == kTlsCredentialsType) { } else if (type == TLS_CREDENTIALS) {
SslServerCredentialsOptions::PemKeyCertPair pkcp = {test_server1_key, SslServerCredentialsOptions::PemKeyCertPair pkcp = {test_server1_key,
test_server1_cert}; test_server1_cert};
SslServerCredentialsOptions ssl_opts; SslServerCredentialsOptions ssl_opts;
@ -65,10 +65,22 @@ std::shared_ptr<ServerCredentials> GetServerCredentials(
ssl_opts.pem_key_cert_pairs.push_back(pkcp); ssl_opts.pem_key_cert_pairs.push_back(pkcp);
return SslServerCredentials(ssl_opts); return SslServerCredentials(ssl_opts);
} else { } else {
gpr_log(GPR_ERROR, "Unsupported credentials type %s.", type.c_str()); gpr_log(GPR_ERROR, "Unsupported credentials type %d.", type);
} }
return nullptr; return nullptr;
} }
grpc::string TestCredentialsTypeToString(TestCredentialsType type) {
switch (type) {
case INSECURE_CREDENTIALS:
return "INSECURE_CREDENTIALS";
case TLS_CREDENTIALS:
return "TLS_CREDENTIALS";
default:
break;
}
return "UNKNOWN";
}
} // namespace testing } // namespace testing
} // namespace grpc } // namespace grpc

@ -43,17 +43,22 @@
namespace grpc { namespace grpc {
namespace testing { namespace testing {
const char kInsecureCredentialsType[] = "INSECURE_CREDENTIALS"; enum TestCredentialsType {
const char kTlsCredentialsType[] = "TLS_CREDENTIALS"; INSECURE_CREDENTIALS = 0,
TLS_CREDENTIALS,
MAX_CREDENTIALS_TYPE
};
// Provide channel credentials according to the given type. Alter the channel // Provide channel credentials according to the given type. Alter the channel
// arguments if needed. // arguments if needed.
std::shared_ptr<ChannelCredentials> GetChannelCredentials( std::shared_ptr<ChannelCredentials> GetChannelCredentials(
const grpc::string& type, ChannelArguments* args); TestCredentialsType type, ChannelArguments* args);
// Provide server credentials according to the given type. // Provide server credentials according to the given type.
std::shared_ptr<ServerCredentials> GetServerCredentials( std::shared_ptr<ServerCredentials> GetServerCredentials(
const grpc::string& type); TestCredentialsType type);
grpc::string TestCredentialsTypeToString(TestCredentialsType type);
} // namespace testing } // namespace testing
} // namespace grpc } // namespace grpc

Loading…
Cancel
Save