diff --git a/test/mock/lib_func_mock/lib_func_define.h b/test/mock/lib_func_mock/lib_func_define.h new file mode 100644 index 0000000000000000000000000000000000000000..cc361dc80cf77b370cdccd7a639c6bd94bfd21e8 --- /dev/null +++ b/test/mock/lib_func_mock/lib_func_define.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef APPSPAWN_LIB_FUNC_DEFINE_H +#define APPSPAWN_LIB_FUNC_DEFINE_H + +#define fseeko Fseeko +#define ftello Ftello +#define access Access +#define mkdir Mkdir +#define fread Fread +#define fwrite Fwrite +#define realpath Realpath +#define fopen Fopen +#define fclose Fclose +#define chmod Chmod +#define utime Utime +#define stat(pathname, statbuf) Stat(pathname, statbuf) +#define ferror Ferror +#define fflush Fflush +#define remove Remove +#define getpwuid Getpwuid +#define getgrgid Getgrgid +#define open Open +#define read Read +#define write Write +#define close Close +#define umount2 Umount2 +#define rmdir Rmdir + +#endif // APPSPAWN_LIB_FUNC_DEFINE_H \ No newline at end of file diff --git a/test/mock/lib_func_mock/lib_func_mock.cpp b/test/mock/lib_func_mock/lib_func_mock.cpp new file mode 100644 index 0000000000000000000000000000000000000000..06120b81acaba31ef8bd8ee9aae00c3ac6811695 --- /dev/null +++ b/test/mock/lib_func_mock/lib_func_mock.cpp @@ -0,0 +1,132 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "lib_func_mock.h" + +using namespace OHOS::AppSpawn; +int Fseeko(FILE *stream, off_t offset, int whence) +{ + return LibraryFunc::libraryFunc_->fseeko(stream, offset, whence); +} + +off_t Ftello(FILE *stream) +{ + return LibraryFunc::libraryFunc_->ftello(stream); +} + +int Access(const char *pathname, int mode) +{ + return LibraryFunc::libraryFunc_->access(pathname, mode); +} + +int Mkdir(const char *pathname, mode_t mode) +{ + return LibraryFunc::libraryFunc_->mkdir(pathname, mode); +} + +size_t Fread(void *ptr, size_t size, size_t nmemb, FILE *stream) +{ + return LibraryFunc::libraryFunc_->fread(ptr, size, nmemb, stream); +} + +size_t Fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) +{ + return LibraryFunc::libraryFunc_->fwrite(ptr, size, nmemb, stream); +} + +char *Realpath(const char *path, char *resolved_path) +{ + return LibraryFunc::libraryFunc_->realpath(path, resolved_path); +} + +FILE *Fopen(const char *pathname, const char *mode) +{ + return LibraryFunc::libraryFunc_->fopen(pathname, mode); +} + +int Fclose(FILE *stream) +{ + return LibraryFunc::libraryFunc_->fclose(stream); +} + +int Chmod(const char *pathname, mode_t mode) +{ + return LibraryFunc::libraryFunc_->chmod(pathname, mode); +} + +int Stat(const char *pathname, struct stat *statbuf) +{ + return LibraryFunc::libraryFunc_->stat(pathname, statbuf); +} + +int Utime(const char *filename, const struct utimbuf *times) +{ + return LibraryFunc::libraryFunc_->utime(filename, times); +} + +int Ferror(FILE *f) +{ + return LibraryFunc::libraryFunc_->ferror(f); +} + +int Fflush(FILE *f) +{ + return LibraryFunc::libraryFunc_->fflush(f); +} + +int Remove(const char *path) +{ + return LibraryFunc::libraryFunc_->remove(path); +} + +struct passwd *Getpwuid(uid_t uid) +{ + return LibraryFunc::libraryFunc_->getpwuid(uid); +} + +struct group *Getgrgid(gid_t gid) +{ + return LibraryFunc::libraryFunc_->getgrgid(gid); +} + +int Open(const char *filename, int flags, ...) +{ + return LibraryFunc::libraryFunc_->open(filename, flags); +} + +ssize_t Read(int fd, void *buf, size_t count) +{ + return LibraryFunc::libraryFunc_->read(fd, buf, count); +} + +ssize_t Write(int fd, const void *buf, size_t count) +{ + return LibraryFunc::libraryFunc_->write(fd, buf, count); +} + +int Close(int fd) +{ + return LibraryFunc::libraryFunc_->close(fd); +} + +int Umount2(const char *file, int flag) +{ + return LibraryFunc::libraryFunc_->umount2(file, flag); +} + +int Rmdir(const char *file) +{ + return LibraryFunc::libraryFunc_->rmdir(file); +} \ No newline at end of file diff --git a/test/mock/lib_func_mock/lib_func_mock.h b/test/mock/lib_func_mock/lib_func_mock.h new file mode 100644 index 0000000000000000000000000000000000000000..5cddc77255589d7ac19a669c68b42bb5fcefb109 --- /dev/null +++ b/test/mock/lib_func_mock/lib_func_mock.h @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef APPSPAWN_LIB_FUNC_MOCK_H +#define APPSPAWN_LIB_FUNC_MOCK_H + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "securec.h" + +int Fseeko(FILE*, off_t, int); +off_t Ftello(FILE*); +int Access(const char*, int); +int Mkdir(const char*, mode_t); +size_t Fread(void*, size_t, size_t, FILE*); +size_t Fwrite(const void*, size_t, size_t, FILE*); +char* Realpath(const char*, char*); +FILE* Fopen(const char*, const char*); +int Fclose(FILE*); +int Chmod(const char*, mode_t); +int Stat(const char*, struct stat*); +int Utime(const char*, const struct utimbuf*); +int Ferror(FILE*); +int Fflush(FILE*); +int Remove(const char*); +struct passwd* Getpwuid(uid_t); +struct group* Getgrgid(gid_t); +int Open(const char*, int, ...); +ssize_t Read(int, void*, size_t); +ssize_t Write(int, const void*, size_t); +int Close(int); +int Umount2(const char*, int); +int Rmdir(const char*); + + +namespace OHOS { +namespace AppSpawn { +class LibraryFunc { +public: + virtual ~LibraryFunc() = default; + virtual int fseeko(FILE*, off_t, int) = 0; + virtual off_t ftello(FILE*) = 0; + virtual int access(const char*, int) = 0; + virtual int mkdir(const char*, mode_t) = 0; + virtual size_t fread(void*, size_t, size_t, FILE*) = 0; + virtual size_t fwrite(const void*, size_t, size_t, FILE*) = 0; + virtual char* realpath(const char*, char*) = 0; + virtual FILE* fopen(const char*, const char*) = 0; + virtual int fclose(FILE*) = 0; + virtual int chmod(const char*, mode_t) = 0; + virtual int stat(const char*, struct stat*) = 0; + virtual int utime(const char*, const struct utimbuf*) = 0; + virtual int ferror(FILE*) = 0; + virtual int fflush(FILE*) = 0; + virtual int remove(const char*) = 0; + virtual struct passwd* getpwuid(uid_t) = 0; + virtual struct group* getgrgid(gid_t) = 0; + virtual int open(const char *, int) = 0; + virtual ssize_t read(int, void*, size_t) = 0; + virtual ssize_t write(int, const void*, size_t) = 0; + virtual int close(int) = 0; + virtual int umount2(const char*, int) = 0; + virtual int rmdir(const char*) = 0; +public: + static inline std::shared_ptr libraryFunc_ = nullptr; +}; + +class LibraryFuncMock : public LibraryFunc { +public: + MOCK_METHOD(int, fseeko, (FILE*, off_t, int)); + MOCK_METHOD(off_t, ftello, (FILE*)); + MOCK_METHOD(int, access, (const char*, int)); + MOCK_METHOD(int, mkdir, (const char*, mode_t)); + MOCK_METHOD(size_t, fread, (void*, size_t, size_t, FILE*)); + MOCK_METHOD(size_t, fwrite, (const void*, size_t, size_t, FILE*)); + MOCK_METHOD(char*, realpath, (const char*, char*)); + MOCK_METHOD(FILE*, fopen, (const char*, const char*)); + MOCK_METHOD(int, fclose, (FILE*)); + MOCK_METHOD(int, chmod, (const char*, mode_t)); + MOCK_METHOD(int, stat, (const char*, struct stat*)); + MOCK_METHOD(int, utime, (const char*, const struct utimbuf*)); + MOCK_METHOD(int, ferror, (FILE*)); + MOCK_METHOD(int, fflush, (FILE*)); + MOCK_METHOD(int, remove, (const char*)); + MOCK_METHOD(passwd*, getpwuid, (uid_t)); + MOCK_METHOD(group*, getgrgid, (gid_t)); + MOCK_METHOD(int, open, (const char*, int)); + MOCK_METHOD(ssize_t, read, (int, void*, size_t)); + MOCK_METHOD(ssize_t, write, (int, const void*, size_t)); + MOCK_METHOD(int, close, (int)); + MOCK_METHOD(int, umount2, (const char*, int)); + MOCK_METHOD(int, rmdir, (const char*)); +}; +} // namespace AppSpawn +} // namespace OHOS + +#endif // APPSPAWN_LIB_FUNC_MOCK_H \ No newline at end of file diff --git a/test/mock/lib_func_mock/lib_func_undef.h b/test/mock/lib_func_mock/lib_func_undef.h new file mode 100644 index 0000000000000000000000000000000000000000..3ceee3becde749b480146bc2cb1d1452acde6fd0 --- /dev/null +++ b/test/mock/lib_func_mock/lib_func_undef.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef APPSPAWN_LIB_FUNC_UNDEF_H +#define APPSPAWN_LIB_FUNC_UNDEF_H + +#define Fseeko fseeko +#define Ftello ftello +#define Access access +#define Mkdir mkdir +#define Fread fread +#define Fwrite fwrite +#define Realpath realpath +#define Fopen fopen +#define Fclose fclose +#define Chmod chmod +#define Utime utime +#define Stat(pathname, statbuf) stat(pathname, statbuf) +#define Ferror ferror +#define Fflush fflush +#define Remove remove +#define Getpwuid getpwuid +#define Getgrgid getgrgid +#define Open open +#define Read read +#define Write write +#define Close close +#define Umount2 umount2 +#define Rmdir rmdir + +#endif // APPSPAWN_LIB_FUNC_UNDEF_H \ No newline at end of file