• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 APPSPAWN_LIB_FUNC_MOCK_H
17 #define APPSPAWN_LIB_FUNC_MOCK_H
18 
19 #include <gmock/gmock.h>
20 
21 #include <cstdio>
22 #include <fcntl.h>
23 #include <grp.h>
24 #include <memory>
25 #include <pwd.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 
30 #include "securec.h"
31 
32 int Fseeko(FILE*, off_t, int);
33 off_t Ftello(FILE*);
34 int Access(const char*, int);
35 int Mkdir(const char*, mode_t);
36 size_t Fread(void*, size_t, size_t, FILE*);
37 size_t Fwrite(const void*, size_t, size_t, FILE*);
38 char* Realpath(const char*, char*);
39 FILE* Fopen(const char*, const char*);
40 int Fclose(FILE*);
41 int Chmod(const char*, mode_t);
42 int Stat(const char*, struct stat*);
43 int Utime(const char*, const struct utimbuf*);
44 int Ferror(FILE*);
45 int Fflush(FILE*);
46 int Remove(const char*);
47 struct passwd* Getpwuid(uid_t);
48 struct group* Getgrgid(gid_t);
49 int Open(const char*, int, ...);
50 ssize_t Read(int, void*, size_t);
51 ssize_t Write(int, const void*, size_t);
52 int Close(int);
53 int Umount2(const char*, int);
54 int Rmdir(const char*);
55 
56 
57 namespace OHOS {
58 namespace AppSpawn {
59 class LibraryFunc {
60 public:
61     virtual ~LibraryFunc() = default;
62     virtual int fseeko(FILE*, off_t, int) = 0;
63     virtual off_t ftello(FILE*) = 0;
64     virtual int access(const char*, int) = 0;
65     virtual int mkdir(const char*, mode_t) = 0;
66     virtual size_t fread(void*, size_t, size_t, FILE*) = 0;
67     virtual size_t fwrite(const void*, size_t, size_t, FILE*) = 0;
68     virtual char* realpath(const char*, char*) = 0;
69     virtual FILE* fopen(const char*, const char*) = 0;
70     virtual int fclose(FILE*) = 0;
71     virtual int chmod(const char*, mode_t) = 0;
72     virtual int stat(const char*, struct stat*) = 0;
73     virtual int utime(const char*, const struct utimbuf*) = 0;
74     virtual int ferror(FILE*) = 0;
75     virtual int fflush(FILE*) = 0;
76     virtual int remove(const char*) = 0;
77     virtual struct passwd* getpwuid(uid_t) = 0;
78     virtual struct group* getgrgid(gid_t) = 0;
79     virtual int open(const char *, int) = 0;
80     virtual ssize_t read(int, void*, size_t) = 0;
81     virtual ssize_t write(int, const void*, size_t) = 0;
82     virtual int close(int) = 0;
83     virtual int umount2(const char*, int) = 0;
84     virtual int rmdir(const char*) = 0;
85 public:
86     static inline std::shared_ptr<LibraryFunc> libraryFunc_ = nullptr;
87 };
88 
89 class LibraryFuncMock : public LibraryFunc {
90 public:
91     MOCK_METHOD(int, fseeko, (FILE*, off_t, int));
92     MOCK_METHOD(off_t, ftello, (FILE*));
93     MOCK_METHOD(int, access, (const char*, int));
94     MOCK_METHOD(int, mkdir, (const char*, mode_t));
95     MOCK_METHOD(size_t, fread, (void*, size_t, size_t, FILE*));
96     MOCK_METHOD(size_t, fwrite, (const void*, size_t, size_t, FILE*));
97     MOCK_METHOD(char*, realpath, (const char*, char*));
98     MOCK_METHOD(FILE*, fopen, (const char*, const char*));
99     MOCK_METHOD(int, fclose, (FILE*));
100     MOCK_METHOD(int, chmod, (const char*, mode_t));
101     MOCK_METHOD(int, stat, (const char*, struct stat*));
102     MOCK_METHOD(int, utime, (const char*, const struct utimbuf*));
103     MOCK_METHOD(int, ferror, (FILE*));
104     MOCK_METHOD(int, fflush, (FILE*));
105     MOCK_METHOD(int, remove, (const char*));
106     MOCK_METHOD(passwd*, getpwuid, (uid_t));
107     MOCK_METHOD(group*, getgrgid, (gid_t));
108     MOCK_METHOD(int, open, (const char*, int));
109     MOCK_METHOD(ssize_t, read, (int, void*, size_t));
110     MOCK_METHOD(ssize_t, write, (int, const void*, size_t));
111     MOCK_METHOD(int, close, (int));
112     MOCK_METHOD(int, umount2, (const char*, int));
113     MOCK_METHOD(int, rmdir, (const char*));
114 };
115 } // namespace AppSpawn
116 } // namespace OHOS
117 
118 #endif // APPSPAWN_LIB_FUNC_MOCK_H