Merge branch 'tizen' of ssh://review.tizen.org:29418/platform/core/appfw/pkgmgr-info
[platform/core/appfw/pkgmgr-info.git] / test / unit_tests / mock / file_mock.c
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "file_mock.h"
18
19 #include <stdio.h>
20 #include <stdbool.h>
21
22 extern FILE *__real_fopen(const char *filename, const char *modes);
23 extern FILE *__real_fopen64(const char *filename, const char *modes);
24 extern char *__real_fgets(char *s, int n, FILE * stream);
25 extern int __real_fclose(FILE *stream);
26
27 static bool flag_ = false;
28
29 void fopen_mock_setup(bool flag) {
30   flag_ = flag;
31 }
32
33 FILE* __wrap_fopen(const char *filename, const char *modes) {
34   if (!flag_)
35     return __real_fopen(filename, modes);
36
37   return (FILE *)1;
38 }
39
40 FILE* __wrap_fopen64(const char *filename, const char *modes) {
41   if (!flag_)
42     return __real_fopen64(filename, modes);
43
44   return (FILE *)1;
45 }
46
47 char * __wrap_fgets(char *s, int n, FILE *stream) {
48   if (!flag_)
49     return __real_fgets(s, n, stream);
50
51   char buf[1024] = "30005";
52   printf("wrap_fopen called\n");
53   snprintf(s, n, "%s", buf);
54
55   return s;
56 }
57
58 int __wrap_fclose(FILE *stream) {
59   if (!flag_)
60     return __real_fclose(stream);
61
62   return 0;
63 }