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 51 Uscript::UScriptInstructionFactory *factory_ = nullptr; 52 private: 53 bool isRetry_ = false; 54 }; 55 56 class TestPkgMgr : public Hpackage::TestScriptPkgManager { 57 public: ExtractFile(const std::string & fileId,Hpackage::PkgManager::StreamPtr output)58 int32_t ExtractFile(const std::string &fileId, Hpackage::PkgManager::StreamPtr output) override 59 { 60 return Hpackage::PKG_SUCCESS; 61 } GetFileInfo(const std::string & fileId)62 const Hpackage::FileInfo *GetFileInfo(const std::string &fileId) override 63 { 64 static Hpackage::FileInfo fileInfo {}; 65 if (fileId == "binary") { 66 return &fileInfo; 67 } 68 return nullptr; 69 } 70 }; 71 72 class TestPkgMgrStream1 : public Hpackage::TestScriptPkgManager { 73 public: CreatePkgStream(Hpackage::PkgManager::StreamPtr & stream,const std::string & fileName,size_t size,int32_t type)74 int32_t CreatePkgStream(Hpackage::PkgManager::StreamPtr &stream, const std::string &fileName, size_t size, 75 int32_t type) override 76 { 77 return Hpackage::PKG_ERROR_BASE; 78 } 79 }; 80 81 class TestPkgMgrStream2 : public Hpackage::TestScriptPkgManager { 82 public: CreatePkgStream(Hpackage::PkgManager::StreamPtr & stream,const std::string & fileName,size_t size,int32_t type)83 int32_t CreatePkgStream(Hpackage::PkgManager::StreamPtr &stream, const std::string &fileName, size_t size, 84 int32_t type) override 85 { 86 stream = nullptr; 87 return Hpackage::PKG_SUCCESS; 88 } 89 }; 90 91 class TestPkgMgrExtract1 : public Hpackage::TestScriptPkgManager { 92 public: ExtractFile(const std::string & fileId,Hpackage::PkgManager::StreamPtr output)93 int32_t ExtractFile(const std::string &fileId, Hpackage::PkgManager::StreamPtr output) override 94 { 95 return Hpackage::PKG_ERROR_BASE; 96 } 97 }; 98 } 99 #endif // UPDATE_BINARY_UNITTEST_H