1 /*
2  * Copyright (C) 2024 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 #include "ringtone_mimetype_utils_test.h"
17 
18 #include <algorithm>
19 
20 #include "ringtone_errno.h"
21 #include "ringtone_log.h"
22 #include "ringtone_mimetype_utils.h"
23 #include "ringtone_type.h"
24 
25 using std::string;
26 using namespace testing::ext;
27 
28 namespace OHOS {
29 namespace Media {
30 const std::map<string, string> g_testExt2MimeType = {
31     { "wrf", "video/x-webex" },
32     { "mov", "video/quicktime" },
33     { "m4v", "video/mp4" },
34     { "f4v", "video/mp4" },
35     { "mp4v", "video/mp4" },
36     { "mpeg4", "video/mp4" },
37     { "mp4", "video/mp4" },
38     { "m2ts", "video/mp2t" },
39     { "mts", "video/mp2t" },
40     { "3gpp2", "video/3gpp2" },
41     { "3gp2", "video/3gpp2" },
42     { "3g2", "video/3gpp2" },
43     { "3gpp", "video/3gpp" },
44     { "3gp", "video/3gpp" },
45     { "vcf", "text/x-vcard" },
46     { "cpp", "text/x-c++src" },
47     { "c++", "text/x-c++src" },
48     { "cxx", "text/x-c++src" },
49     { "cc", "text/x-c++src" },
50     { "hpp", "text/x-c++hdr" },
51     { "h++", "text/x-c++hdr" },
52     { "hxx", "text/x-c++hdr" },
53     { "hh", "text/x-c++hdr" },
54     { "html", "text/html" },
55     { "htm", "text/html" },
56     { "shtml", "text/html" },
57     { "md", "text/markdown" },
58     { "markdown", "text/markdown" },
59     { "java", "text/x-java" },
60     { "py", "text/x-python" },
61     { "ts", "video/mp2ts" },
62     { "rtf", "text/rtf" },
63     { "pef", "image/x-pentax-pef" },
64     { "nrw", "image/x-nikon-nrw" },
65     { "raf", "image/x-fuji-raf" },
66     { "jpg", "image/jpeg" },
67     { "jpeg", "image/jpeg" },
68     { "jpe", "image/jpeg"},
69     { "raw", "image/x-dcraw" },
70     { "cur", "image/ico" },
71     { "heif", "image/heif" },
72     { "hif", "image/heif" },
73     { "prc", "application/x-mobipocket-ebook" },
74     { "mobi", "application/x-mobipocket-ebook" },
75     { "bmp", "image/bmp" },
76     { "bm", "image/bmp" },
77     { "srt", "application/x-subrip" },
78     { "phps", "text/text" },
79     { "m3u", "text/text" },
80     { "m3u8", "text/text" },
81     { "css", "text/css" },
82     { "webarchivexml", "application/x-webarchive-xml" },
83     { "stl", "application/vnd.ms-pki.stl" },
84     { "pcf", "application/x-font" },
85     { "imy", "audio/imelody" },
86     { "avif", "image/avif" },
87     { "vor", "application/vnd.stardivision.writer" },
88     { "pot", "application/vnd.ms-powerpoint" },
89     { "csv", "text/comma-separated-values" },
90     { "webarchive", "application/x-webarchive" },
91     { "png", "image/png" },
92     { "ttml", "application/ttml+xml" },
93     { "dfxp", "application/ttml+xml" },
94     { "webp", "image/webp" },
95     { "pgp", "application/pgp-signature" },
96     { "dng", "image/x-adobe-dng" },
97     { "p12", "application/x-pkcs12" },
98     { "pfx", "application/x-pkcs12" },
99     { "mka", "audio/x-matroska" },
100     { "wpl", "application/vnd.ms-wpl" },
101     { "webm", "video/webm" },
102     { "sdp", "application/sdp" },
103     { "ra", "audio/x-pn-realaudio" },
104     { "gif", "image/gif" },
105     { "smf", "audio/sp-midi" },
106     { "ogg", "audio/ogg" },
107     { "mp3", "audio/mpeg" },
108     { "mp2", "audio/mpeg" },
109     { "mp1", "audio/mpeg" },
110     { "mpa", "audio/mpeg" },
111     { "m4r", "audio/mpeg" },
112     { "lrc", "application/lrc" },
113     { "crt", "application/x-x509-ca-cert" },
114     { "der", "application/x-x509-ca-cert" },
115     { "heics", "image/heic-sequence" },
116     { "heifs", "image/heic-sequence" },
117     { "flac", "audio/flac" },
118     { "epub", "application/epub+zip" },
119     { "3ga", "audio/3gpp" },
120     { "mxmf", "audio/mobile-xmf" },
121     { "rss", "application/rss+xml" },
122     { "h264", "video/H264" },
123     { "heic", "image/heic" },
124     { "wav", "audio/wav" },
125     { "aac", "audio/aac" },
126     { "adts", "audio/aac" },
127     { "adt", "audio/aac" },
128     { "snd", "audio/basic" },
129     { "xml", "text/xml" },
130     { "rtttl", "audio/midi" },
131     { "xmf", "audio/midi" },
132     { "rtx", "audio/midi" },
133     { "yt", "video/vnd.youtube.yt" },
134     { "arw", "image/x-sony-arw" },
135     { "ico", "image/x-icon" },
136     { "m3u", "audio/mpegurl" },
137     { "smil", "application/smil+xml" },
138     { "mpeg", "video/mpeg" },
139     { "mpeg2", "video/mpeg" },
140     { "mpv2", "video/mpeg" },
141     { "mp2v", "video/mpeg" },
142     { "m2v", "video/mpeg" },
143     { "m2t", "video/mpeg" },
144     { "mpeg1", "video/mpeg" },
145     { "mpv1", "video/mpeg" },
146     { "mp1v", "video/mpeg" },
147     { "m1v", "video/mpeg" },
148     { "mpg", "video/mpeg" },
149     { "amr", "audio/amr" },
150     { "mkv", "video/x-matroska" },
151     { "mp3", "audio/x-mpeg" },
152     { "rw2", "image/x-panasonic-rw2" },
153     { "svg", "image/svg+xml" },
154     { "ac3", "audio/ac3" },
155     { "a52", "audio/ac3" },
156     { "m4a", "audio/mp4" },
157     { "m4b", "audio/mp4" },
158     { "m4p", "audio/mp4" },
159     { "f4a", "audio/mp4" },
160     { "f4b", "audio/mp4" },
161     { "f4p", "audio/mp4" },
162     { "diff", "text/plain" },
163     { "po", "text/plain" },
164     { "txt", "text/plain" },
165     { "srw", "image/x-samsung-srw" },
166     { "pem", "application/x-pem-file" },
167     { "cer", "application/pkix-cert" },
168     { "json", "application/json" },
169     { "js", "application/javascript" },
170     { "rar", "application/rar" },
171     { "zip", "application/zip" },
172     { "pdf", "application/pdf" },
173     { "doc", "application/msword" },
174     { "xls", "application/ms-excel" },
175     { "ppt", "application/vnd.ms-powerpoint" },
176     { "docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document" },
177     { "xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" },
178     { "pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation" },
179     {"ringtonelib_GetMimeTypeFromExtension_test", "application/octet-stream"}
180 };
181 
182 const std::map<string, RingtoneMediaType> g_testMimeType2MediaType = {
183     { "application/epub+zip", RINGTONE_MEDIA_TYPE_INVALID },
184     { "application/lrc", RINGTONE_MEDIA_TYPE_INVALID },
185     { "application/pkix-cert", RINGTONE_MEDIA_TYPE_INVALID },
186     { "application/rss+xml", RINGTONE_MEDIA_TYPE_INVALID },
187     { "application/sdp", RINGTONE_MEDIA_TYPE_INVALID },
188     { "application/smil+xml", RINGTONE_MEDIA_TYPE_INVALID },
189     { "application/ttml+xml", RINGTONE_MEDIA_TYPE_INVALID },
190     { "application/vnd.ms-pki.stl", RINGTONE_MEDIA_TYPE_INVALID },
191     { "application/vnd.ms-powerpoint", RINGTONE_MEDIA_TYPE_INVALID },
192     { "application/vnd.ms-wpl", RINGTONE_MEDIA_TYPE_INVALID },
193     { "application/vnd.stardivision.writer", RINGTONE_MEDIA_TYPE_INVALID },
194     { "application/x-font", RINGTONE_MEDIA_TYPE_INVALID },
195     { "application/x-mobipocket-ebook", RINGTONE_MEDIA_TYPE_INVALID },
196     { "application/x-pem-file", RINGTONE_MEDIA_TYPE_INVALID },
197     { "application/x-pkcs12", RINGTONE_MEDIA_TYPE_INVALID },
198     { "application/x-subrip", RINGTONE_MEDIA_TYPE_INVALID },
199     { "application/x-webarchive", RINGTONE_MEDIA_TYPE_INVALID },
200     { "application/x-webarchive-xml", RINGTONE_MEDIA_TYPE_INVALID },
201     { "application/pgp-signature", RINGTONE_MEDIA_TYPE_INVALID },
202     { "application/x-x509-ca-cert", RINGTONE_MEDIA_TYPE_INVALID },
203     { "audio/3gpp", RINGTONE_MEDIA_TYPE_AUDIO },
204     { "audio/ac3", RINGTONE_MEDIA_TYPE_AUDIO },
205     { "audio/amr", RINGTONE_MEDIA_TYPE_AUDIO },
206     { "audio/imelody", RINGTONE_MEDIA_TYPE_AUDIO },
207     { "audio/midi", RINGTONE_MEDIA_TYPE_AUDIO },
208     { "audio/mobile-xmf", RINGTONE_MEDIA_TYPE_AUDIO },
209     { "audio/mp4", RINGTONE_MEDIA_TYPE_AUDIO },
210     { "audio/mpegurl", RINGTONE_MEDIA_TYPE_AUDIO },
211     { "audio/sp-midi", RINGTONE_MEDIA_TYPE_AUDIO },
212     { "audio/x-matroska", RINGTONE_MEDIA_TYPE_AUDIO },
213     { "audio/x-pn-realaudio", RINGTONE_MEDIA_TYPE_AUDIO },
214     { "audio/x-mpeg", RINGTONE_MEDIA_TYPE_AUDIO },
215     { "audio/aac", RINGTONE_MEDIA_TYPE_AUDIO },
216     { "audio/basic", RINGTONE_MEDIA_TYPE_AUDIO },
217     { "audio/flac", RINGTONE_MEDIA_TYPE_AUDIO },
218     { "audio/mpeg", RINGTONE_MEDIA_TYPE_AUDIO },
219     { "audio/wav", RINGTONE_MEDIA_TYPE_AUDIO },
220     { "audio/ogg", RINGTONE_MEDIA_TYPE_AUDIO },
221     { "image/gif", RINGTONE_MEDIA_TYPE_INVALID },
222     { "image/heic", RINGTONE_MEDIA_TYPE_INVALID },
223     { "image/heic-sequence", RINGTONE_MEDIA_TYPE_INVALID },
224     { "image/bmp", RINGTONE_MEDIA_TYPE_INVALID },
225     { "image/heif", RINGTONE_MEDIA_TYPE_INVALID },
226     { "image/avif", RINGTONE_MEDIA_TYPE_INVALID },
227     { "image/ico", RINGTONE_MEDIA_TYPE_INVALID },
228     { "image/webp", RINGTONE_MEDIA_TYPE_INVALID },
229     { "image/x-adobe-dng", RINGTONE_MEDIA_TYPE_INVALID },
230     { "image/x-fuji-raf", RINGTONE_MEDIA_TYPE_INVALID },
231     { "image/x-icon", RINGTONE_MEDIA_TYPE_INVALID },
232     { "image/x-nikon-nrw", RINGTONE_MEDIA_TYPE_INVALID },
233     { "image/x-panasonic-rw2", RINGTONE_MEDIA_TYPE_INVALID },
234     { "image/x-pentax-pef", RINGTONE_MEDIA_TYPE_INVALID },
235     { "image/x-samsung-srw", RINGTONE_MEDIA_TYPE_INVALID },
236     { "image/x-sony-arw", RINGTONE_MEDIA_TYPE_INVALID },
237     { "image/x-dcraw", RINGTONE_MEDIA_TYPE_INVALID},
238     { "image/jpeg", RINGTONE_MEDIA_TYPE_INVALID },
239     { "image/png", RINGTONE_MEDIA_TYPE_INVALID },
240     { "image/svg+xml", RINGTONE_MEDIA_TYPE_INVALID },
241     { "video/3gpp2", RINGTONE_MEDIA_TYPE_INVALID },
242     { "video/3gpp", RINGTONE_MEDIA_TYPE_INVALID },
243     { "video/mp4", RINGTONE_MEDIA_TYPE_INVALID },
244     { "video/mp2t", RINGTONE_MEDIA_TYPE_INVALID },
245     { "video/mp2ts", RINGTONE_MEDIA_TYPE_INVALID },
246     { "video/x-webex", RINGTONE_MEDIA_TYPE_INVALID },
247     { "video/mpeg", RINGTONE_MEDIA_TYPE_INVALID },
248     { "video/quicktime", RINGTONE_MEDIA_TYPE_INVALID },
249     { "video/x-matroska", RINGTONE_MEDIA_TYPE_INVALID },
250     { "video/webm", RINGTONE_MEDIA_TYPE_INVALID },
251     { "video/H264", RINGTONE_MEDIA_TYPE_INVALID },
252     { "text/comma-separated-values", RINGTONE_MEDIA_TYPE_INVALID },
253     { "text/plain", RINGTONE_MEDIA_TYPE_INVALID },
254     { "text/rtf", RINGTONE_MEDIA_TYPE_INVALID },
255     { "text/text", RINGTONE_MEDIA_TYPE_INVALID },
256     { "text/xml", RINGTONE_MEDIA_TYPE_INVALID },
257     { "text/x-vcard", RINGTONE_MEDIA_TYPE_INVALID },
258     { "text/x-c++hdr", RINGTONE_MEDIA_TYPE_INVALID },
259     { "text/x-c++src", RINGTONE_MEDIA_TYPE_INVALID },
260     { "", RINGTONE_MEDIA_TYPE_INVALID }
261 };
262 
SetUpTestCase()263 void RingtoneMimeTypeTest::SetUpTestCase() {}
264 
TearDownTestCase()265 void RingtoneMimeTypeTest::TearDownTestCase() {}
SetUp()266 void RingtoneMimeTypeTest::SetUp() {}
TearDown(void)267 void RingtoneMimeTypeTest::TearDown(void) {}
268 
269 HWTEST_F(RingtoneMimeTypeTest, ringtoneMimeType_InitMimeTypeMap_Test_001, TestSize.Level0)
270 {
271     auto ret = RingtoneMimeTypeUtils::InitMimeTypeMap();
272     ASSERT_EQ(ret, E_OK);
273 }
274 
275 HWTEST_F(RingtoneMimeTypeTest, ringtoneMimeType_GetMimeTypeFromExtension_Test_001, TestSize.Level0)
276 {
277     auto ret = RingtoneMimeTypeUtils::InitMimeTypeMap();
278     ASSERT_EQ(ret, E_OK);
279     for (const auto &item : g_testExt2MimeType) {
280         auto mimeType = RingtoneMimeTypeUtils::GetMimeTypeFromExtension(item.first);
281         if (item.first == "yt") {
282             ASSERT_EQ(mimeType == "application/vnd.youtube.yt" || mimeType == "video/vnd.youtube.yt", true);
283         } else {
284             ASSERT_EQ(mimeType, item.second);
285         }
286         string upperExtension = item.first;
287         std::transform(upperExtension.begin(), upperExtension.end(), upperExtension.begin(), ::toupper);
288         mimeType = RingtoneMimeTypeUtils::GetMimeTypeFromExtension(upperExtension);
289         if (item.first == "yt") {
290             ASSERT_EQ(mimeType == "application/vnd.youtube.yt" || mimeType == "video/vnd.youtube.yt", true);
291         } else {
292             ASSERT_EQ(mimeType, item.second);
293         }
294     }
295 }
296 
297 HWTEST_F(RingtoneMimeTypeTest, ringtoneMimeType_GetMediaTypeFromMimeType_Test_001, TestSize.Level0)
298 {
299     auto ret = RingtoneMimeTypeUtils::InitMimeTypeMap();
300     ASSERT_EQ(ret, E_OK);
301     for (const auto &item : g_testMimeType2MediaType) {
302         auto mediaType = RingtoneMimeTypeUtils::GetMediaTypeFromMimeType(item.first);
303         ASSERT_EQ(mediaType, item.second);
304     }
305 }
306 } // namespace Media
307 } // namespace OHOS
308