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
22namespace UpdaterUt {
23class UTestBinaryEnv : public Uscript::UScriptEnv {
24public:
25    explicit UTestBinaryEnv(Hpackage::PkgManager::PkgManagerPtr pkgManager) : Uscript::UScriptEnv(pkgManager) {}
26
27    ~UTestBinaryEnv() = default;
28
29    void PostMessage(const std::string &cmd, std::string content) {}
30
31    Uscript::UScriptInstructionFactoryPtr GetInstructionFactory()
32    {
33        return nullptr;
34    }
35
36    const std::vector<std::string> GetInstructionNames() const
37    {
38        return {};
39    }
40
41    bool IsRetry() const
42    {
43        return isRetry_;
44    }
45
46    bool SetRetry(bool retry)
47    {
48        return isRetry_ = retry;
49    }
50
51    virtual Updater::PostMessageFunction GetPostmsgFunc()
52    {
53        return nullptr;
54    }
55
56    Uscript::UScriptInstructionFactory *factory_ = nullptr;
57private:
58    bool isRetry_ = false;
59};
60
61class TestPkgMgr : public Hpackage::TestScriptPkgManager {
62public:
63    int32_t ExtractFile(const std::string &fileId, Hpackage::PkgManager::StreamPtr output) override
64    {
65        return Hpackage::PKG_SUCCESS;
66    }
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
77class TestPkgMgrStream1 : public Hpackage::TestScriptPkgManager {
78public:
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
86class TestPkgMgrStream2 : public Hpackage::TestScriptPkgManager {
87public:
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
96class TestPkgMgrExtract1 : public Hpackage::TestScriptPkgManager {
97public:
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