|
| 1 | +/** |
| 2 | + * Copyright (c) 2019-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#include "gloo/config.h" |
| 10 | +#if GLOO_HAVE_TRANSPORT_TCP_TLS |
| 11 | + |
| 12 | +#include <gmock/gmock.h> |
| 13 | + |
| 14 | +#include "gloo/test/multiproc_test.h" |
| 15 | +#include "gloo/test/openssl_utils.h" |
| 16 | + |
| 17 | +namespace gloo { |
| 18 | +namespace test { |
| 19 | +namespace { |
| 20 | + |
| 21 | +const char* kDefaultDevice = "localhost"; |
| 22 | + |
| 23 | +class TlsTcpTest : public BaseTest {}; |
| 24 | + |
| 25 | +TEST_F(TlsTcpTest, CreateDeviceWithAllEmptyFilePaths) { |
| 26 | + bool exception_thrown = false; |
| 27 | + try { |
| 28 | + ::gloo::rendezvous::HashStore store; |
| 29 | + auto device = ::gloo::transport::tcp::tls::CreateDevice( |
| 30 | + kDefaultDevice, "", "", "", ""); |
| 31 | + auto context = device->createContext(0, 1); |
| 32 | + } catch (::gloo::EnforceNotMet e) { |
| 33 | + exception_thrown = true; |
| 34 | + ASSERT_THAT( |
| 35 | + e.what(), |
| 36 | + ::testing::ContainsRegex( |
| 37 | + "Private key and certificate location must be specified")); |
| 38 | + } |
| 39 | + ASSERT_TRUE(exception_thrown); |
| 40 | +} |
| 41 | + |
| 42 | +TEST_F(TlsTcpTest, CreateDeviceWithCAEmptyFilePaths) { |
| 43 | + bool exception_thrown = false; |
| 44 | + try { |
| 45 | + ::gloo::rendezvous::HashStore store; |
| 46 | + auto device = ::gloo::transport::tcp::tls::CreateDevice( |
| 47 | + kDefaultDevice, pkey_file, cert_file, "", ""); |
| 48 | + auto context = device->createContext(0, 1); |
| 49 | + } catch (::gloo::EnforceNotMet e) { |
| 50 | + exception_thrown = true; |
| 51 | + ASSERT_THAT( |
| 52 | + e.what(), |
| 53 | + ::testing::ContainsRegex("CAfile or CApath must be specified")); |
| 54 | + } |
| 55 | + ASSERT_TRUE(exception_thrown); |
| 56 | +} |
| 57 | + |
| 58 | +TEST_F(TlsTcpTest, CreateDeviceWithUnknownCA) { |
| 59 | + auto device = ::gloo::transport::tcp::tls::CreateDevice( |
| 60 | + kDefaultDevice, pkey_file, cert_file, cert_file, ""); |
| 61 | + auto context = device->createContext(0, 2); |
| 62 | + auto& pair0 = context->createPair(0); |
| 63 | + auto addrBytes0 = pair0->address().bytes(); |
| 64 | + auto& pair1 = context->createPair(1); |
| 65 | + auto addrBytes1 = pair1->address().bytes(); |
| 66 | + |
| 67 | + bool exception_thrown = false; |
| 68 | + spawnThreads(2, [&](int rank) { |
| 69 | + try { |
| 70 | + if (rank == 0) { |
| 71 | + pair0->connect(addrBytes1); |
| 72 | + } else { |
| 73 | + pair1->connect(addrBytes0); |
| 74 | + } |
| 75 | + } catch (::gloo::IoException e) { |
| 76 | + exception_thrown = true; |
| 77 | + ASSERT_THAT(e.what(), ::testing::ContainsRegex("unknown ca")); |
| 78 | + } catch (::gloo::EnforceNotMet e) { |
| 79 | + exception_thrown = true; |
| 80 | + ASSERT_THAT( |
| 81 | + e.what(), ::testing::ContainsRegex("handshake was not succeeded")); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + ASSERT_TRUE(exception_thrown); |
| 86 | +} |
| 87 | + |
| 88 | +} // namespace |
| 89 | +} // namespace test |
| 90 | +} // namespace gloo |
| 91 | +#endif |
0 commit comments