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/util/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 #include "test/core/test_util/test_config.h"
26
27 static constexpr absl::string_view kCrlDirectory =
28 "test/core/tsi/test_creds/crl_data/crls/";
29
30 namespace grpc_core {
31 namespace testing {
32 namespace {
33
TEST(DirectoryReader,CanListFiles)34 TEST(DirectoryReader, CanListFiles) {
35 auto reader = MakeDirectoryReader(kCrlDirectory);
36 std::vector<std::string> contents;
37 absl::Status status = reader->ForEach([&](absl::string_view filename) {
38 contents.push_back(std::string(filename));
39 });
40 ASSERT_TRUE(status.ok()) << status;
41 // IsSupersetOf() is needed instead of UnorderedElementsAre() because some
42 // builds/OS combinations will include the BUILD file in this directory when
43 // the tests are run
44 EXPECT_THAT(contents,
45 ::testing::IsSupersetOf({"ab06acdd.r0", "b9322cac.r0",
46 "current.crl", "intermediate.crl"}));
47 }
48
TEST(DirectoryReader,NonexistentDirectory)49 TEST(DirectoryReader, NonexistentDirectory) {
50 auto reader = MakeDirectoryReader("DOES_NOT_EXIST");
51 absl::Status status = reader->ForEach([](absl::string_view) {});
52 ASSERT_FALSE(status.ok()) << status;
53 }
54
55 } // namespace
56 } // namespace testing
57 } // namespace grpc_core
58
main(int argc,char ** argv)59 int main(int argc, char** argv) {
60 grpc::testing::TestEnvironment env(&argc, argv);
61 ::testing::InitGoogleTest(&argc, argv);
62 return RUN_ALL_TESTS();
63 }
64