Add file extension check before adding image file to set home or lock screen
[platform/core/api/system-settings.git] / tests / mocks / libc.c
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
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 #define _GNU_SOURCE
17 #include <string.h>
18 #include <unistd.h>
19 #include <dlfcn.h>
20 #include <dirent.h>
21 #include <sys/types.h>
22 #include "sst.h"
23 #include "sstt_mock.h"
24
25 static bool sstm_access_enable = true;
26 static bool sstm_readlink_enable = true;
27 static bool sstm_opendir_enable = true;
28 static bool sstm_dlopen_enable = true;
29
30 API void sstm_access_setup(bool enable)
31 {
32         sstm_access_enable = enable;
33 }
34
35 API void sstm_readlink_setup(bool enable)
36 {
37         sstm_readlink_enable = enable;
38 }
39
40 API void sstm_opendir_setup(bool enable)
41 {
42         sstm_opendir_enable = enable;
43 }
44
45 API void sstm_dlopen_setup(bool enable)
46 {
47         sstm_dlopen_enable = enable;
48 }
49
50 API int access(const char *pathname, int mode)
51 {
52         if (false == sstm_access_enable)
53                 return -1;
54
55         if (0 == strcmp(pathname, SYS_SETTINGS_TEST_DIR"/ringtone_sdk.mp3"))
56                 return 0;
57         if (0 == strcmp(pathname, SYS_SETTINGS_TEST_DIR"/Over the horizon.mp3"))
58                 return 0;
59         if (0 == strcmp(pathname, SYS_SETTINGS_TEST_DIR"/big_buck_bunny.mp3"))
60                 return 0;
61         if (0 == strcmp(pathname, SYS_SETTINGS_TEST_DIR"/daliTestVideo.mp3"))
62                 return 0;
63         if (0 == strcmp(pathname, SYS_SETTINGS_TEST_DIR"/sound_5.mp3"))
64                 return 0;
65         if (0 == strcmp(pathname, SYS_SETTINGS_TEST_DIR"/tct-content-tizen-tests_video.mp3"))
66                 return 0;
67         if (0 == strcmp(pathname, "/opt/usr/data/settings/Alerts/General notification_sdk.wav"))
68                 return 0;
69         if (0 == strcmp(pathname, "/opt/usr/data/settings/Ringtones/ringtone_sdk.mp3"))
70                 return 0;
71         if (0 == strcmp(pathname, SYS_SETTINGS_TEST_DIR"/bg_test.png"))
72                 return 0;
73         if (0 == strcmp(pathname, SYS_SETTINGS_TEST_DIR"/bg_test.mp4"))
74                 return 0;
75
76         int (*org_fn)(const char *, int);
77         org_fn = dlsym(RTLD_NEXT, "access");
78         return org_fn(pathname, mode);
79 }
80
81 API ssize_t readlink(const char *restrict path, char *restrict buf, size_t len)
82 {
83         const char *local = "/usr/share/zoneinfo/Asia/Seoul";
84
85         if (false == sstm_readlink_enable)
86                 return -1;
87
88         if (0 == strcmp(path, "/opt/etc/localtime")) {
89                 strncpy(buf, local, len);
90                 return (sizeof(local) - 1);
91         }
92
93         ssize_t(*org_fn)(const char *restrict, char *restrict, size_t);
94         org_fn = dlsym(RTLD_NEXT, "readlink");
95         return org_fn(path, buf, len);
96 }
97
98 API DIR *opendir(const char *name)
99 {
100         DIR *(*org_fn)(const char *);
101         if (false == sstm_opendir_enable)
102                 return NULL;
103
104         org_fn = dlsym(RTLD_NEXT, "opendir");
105
106         if (0 == strcmp(name, SST_RES_DIR"/Ringtones")) {
107                 org_fn = dlsym(RTLD_NEXT, "opendir");
108                 return org_fn("tests/res");
109         }
110
111         return org_fn(name);
112 }
113
114 API void* dlopen(const char *filename, int flag)
115 {
116         if (false == sstm_dlopen_enable)
117                 return NULL;
118
119         void *(*org_fn)(const char *filename, int flag);
120         org_fn = dlsym(RTLD_NEXT, "dlopen");
121         return org_fn(filename, flag);
122 }