1 /*
2  * Copyright (C) 2017 The Android Open Source Project
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 <gmock/gmock.h>
18 
19 #include <vintf/Apex.h>
20 #include <vintf/ObjectFactory.h>
21 #include <vintf/PropertyFetcher.h>
22 #include "utils.h"
23 
24 using ::testing::_;
25 using ::testing::AtLeast;
26 using ::testing::Invoke;
27 using ::testing::Return;
28 
29 namespace android {
30 namespace vintf {
31 namespace details {
32 
33 class MockFileSystem : public FileSystem {
34    public:
MockFileSystem()35     MockFileSystem() {}
36 
37     MOCK_CONST_METHOD2(fetch, status_t(const std::string& path, std::string& fetched));
38     MOCK_CONST_METHOD3(listFiles,
39                        status_t(const std::string&, std::vector<std::string>*, std::string*));
40     MOCK_CONST_METHOD3(modifiedTime, status_t(const std::string&, timespec*, std::string*));
41 
fetch(const std::string & path,std::string * fetched,std::string *)42     status_t fetch(const std::string& path, std::string* fetched, std::string*) const override {
43         // Call the mocked function
44         return fetch(path, *fetched);
45     }
46    private:
47     FileSystemImpl mImpl;
48 };
49 
50 class MockFileSystemWithError : public FileSystem {
51    public:
52     MOCK_METHOD(status_t, fetch, (const std::string&, std::string*, std::string*),
53                 (const override));
54     MOCK_METHOD(status_t, listFiles, (const std::string&, std::vector<std::string>*, std::string*),
55                 (const override));
56     MOCK_METHOD(status_t, modifiedTime, (const std::string&, timespec*, std::string*),
57                 (const override));
58 };
59 
60 class MockRuntimeInfo : public RuntimeInfo {
61    public:
62     MockRuntimeInfo();
63     MOCK_METHOD1(fetchAllInformation, status_t(RuntimeInfo::FetchFlags));
64     status_t doFetch(RuntimeInfo::FetchFlags flags);
failNextFetch()65     void failNextFetch() { failNextFetch_ = true; }
66     void setNextFetchKernelInfo(KernelVersion&& v, std::map<std::string, std::string>&& configs);
67     void setNextFetchKernelInfo(const KernelVersion& v,
68                                 const std::map<std::string, std::string>& configs);
69     void setNextFetchKernelLevel(Level level);
70 
71    private:
72     bool failNextFetch_ = false;
73     // KernelInfo returned in next fetch.
74     KernelInfo kernel_info_;
75 };
76 class MockRuntimeInfoFactory : public ObjectFactory<RuntimeInfo> {
77    public:
MockRuntimeInfoFactory(const std::shared_ptr<MockRuntimeInfo> & info)78     MockRuntimeInfoFactory(const std::shared_ptr<MockRuntimeInfo>& info) { object_ = info; }
make_shared()79     std::shared_ptr<RuntimeInfo> make_shared() const override { return object_; }
getInfo()80     std::shared_ptr<MockRuntimeInfo> getInfo() const { return object_; }
81 
82    private:
83     std::shared_ptr<MockRuntimeInfo> object_;
84 };
85 
86 class MockPropertyFetcher : public PropertyFetcher {
87    public:
88     MockPropertyFetcher() = default;
89     MOCK_CONST_METHOD2(getProperty, std::string(const std::string&, const std::string&));
90     MOCK_CONST_METHOD2(getBoolProperty, bool(const std::string&, bool));
91     MOCK_CONST_METHOD3(getUintProperty, uint64_t(const std::string&, uint64_t, uint64_t));
92 };
93 
94 }  // namespace details
95 }  // namespace vintf
96 }  // namespace android
97