1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef UPDATE_BINARY_UNITTEST_H
17 #define UPDATE_BINARY_UNITTEST_H
18 
19 #include "script_instruction.h"
20 #include "script_unittest.h"
21 
22 namespace UpdaterUt {
23 class UTestBinaryEnv : public Uscript::UScriptEnv {
24 public:
UTestBinaryEnv(Hpackage::PkgManager::PkgManagerPtr pkgManager)25     explicit UTestBinaryEnv(Hpackage::PkgManager::PkgManagerPtr pkgManager) : Uscript::UScriptEnv(pkgManager) {}
26 
27     ~UTestBinaryEnv() = default;
28 
PostMessage(const std::string & cmd,std::string content)29     void PostMessage(const std::string &cmd, std::string content) {}
30 
GetInstructionFactory()31     Uscript::UScriptInstructionFactoryPtr GetInstructionFactory()
32     {
33         return nullptr;
34     }
35 
GetInstructionNames()36     const std::vector<std::string> GetInstructionNames() const
37     {
38         return {};
39     }
40 
IsRetry()41     bool IsRetry() const
42     {
43         return isRetry_;
44     }
45 
SetRetry(bool retry)46     bool SetRetry(bool retry)
47     {
48         return isRetry_ = retry;
49     }
50 
GetPostmsgFunc()51     virtual Updater::PostMessageFunction GetPostmsgFunc()
52     {
53         return nullptr;
54     }
55 
56     Uscript::UScriptInstructionFactory *factory_ = nullptr;
57 private:
58     bool isRetry_ = false;
59 };
60 
61 class TestPkgMgr : public Hpackage::TestScriptPkgManager {
62 public:
ExtractFile(const std::string & fileId,Hpackage::PkgManager::StreamPtr output)63     int32_t ExtractFile(const std::string &fileId, Hpackage::PkgManager::StreamPtr output) override
64     {
65         return Hpackage::PKG_SUCCESS;
66     }
GetFileInfo(const std::string & fileId)67     const Hpackage::FileInfo *GetFileInfo(const std::string &fileId) override
68     {
69         static Hpackage::FileInfo fileInfo {};
70         if (fileId == "binary") {
71             return &fileInfo;
72         }
73         return nullptr;
74     }
75 };
76 
77 class TestPkgMgrStream1 : public Hpackage::TestScriptPkgManager {
78 public:
CreatePkgStream(Hpackage::PkgManager::StreamPtr & stream,const std::string & fileName,size_t size,int32_t type)79     int32_t CreatePkgStream(Hpackage::PkgManager::StreamPtr &stream, const std::string &fileName, size_t size,
80         int32_t type) override
81     {
82         return Hpackage::PKG_ERROR_BASE;
83     }
84 };
85 
86 class TestPkgMgrStream2 : public Hpackage::TestScriptPkgManager {
87 public:
CreatePkgStream(Hpackage::PkgManager::StreamPtr & stream,const std::string & fileName,size_t size,int32_t type)88     int32_t CreatePkgStream(Hpackage::PkgManager::StreamPtr &stream, const std::string &fileName, size_t size,
89         int32_t type) override
90     {
91         stream = nullptr;
92         return Hpackage::PKG_SUCCESS;
93     }
94 };
95 
96 class TestPkgMgrExtract1 : public Hpackage::TestScriptPkgManager {
97 public:
ExtractFile(const std::string & fileId,Hpackage::PkgManager::StreamPtr output)98     int32_t ExtractFile(const std::string &fileId, Hpackage::PkgManager::StreamPtr output) override
99     {
100         return Hpackage::PKG_ERROR_BASE;
101     }
102 };
103 }
104 #endif // UPDATE_BINARY_UNITTEST_H