1 //
2 // Copyright 2023 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "src/core/lib/gprpp/directory_reader.h"
18
19 #include <string>
20 #include <vector>
21
22 #include "absl/strings/string_view.h"
23 #include "gmock/gmock.h"
24 #include "gtest/gtest.h"
25
26 #include "test/core/util/test_config.h"
27
28 static constexpr absl::string_view kCrlDirectory =
29 "test/core/tsi/test_creds/crl_data/crls/";
30
31 namespace grpc_core {
32 namespace testing {
33 namespace {
34
TEST(DirectoryReader,CanListFiles)35 TEST(DirectoryReader, CanListFiles) {
36 auto reader = MakeDirectoryReader(kCrlDirectory);
37 std::vector<std::string> contents;
38 absl::Status status = reader->ForEach([&](absl::string_view filename) {
39 contents.push_back(std::string(filename));
40 });
41 ASSERT_TRUE(status.ok()) << status;
42 // IsSupersetOf() is needed instead of UnorderedElementsAre() because some
43 // builds/OS combinations will include the BUILD file in this directory when
44 // the tests are run
45 EXPECT_THAT(contents,
46 ::testing::IsSupersetOf({"ab06acdd.r0", "b9322cac.r0",
47 "current.crl", "intermediate.crl"}));
48 }
49
TEST(DirectoryReader,NonexistentDirectory)50 TEST(DirectoryReader, NonexistentDirectory) {
51 auto reader = MakeDirectoryReader("DOES_NOT_EXIST");
52 absl::Status status = reader->ForEach([](absl::string_view) {});
53 ASSERT_FALSE(status.ok()) << status;
54 }
55
56 } // namespace
57 } // namespace testing
58 } // namespace grpc_core
59
main(int argc,char ** argv)60 int main(int argc, char** argv) {
61 grpc::testing::TestEnvironment env(&argc, argv);
62 ::testing::InitGoogleTest(&argc, argv);
63 return RUN_ALL_TESTS();
64 }
65