1 /******************************************************************************
2  *
3  * Copyright (C) 2022 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************
18  */
19 #include <fuzzer/FuzzedDataProvider.h>
20 
21 #include "LocaleListCache.h"
22 #include "minikin/LocaleList.h"
23 
24 using namespace minikin;
25 
26 const SubtagBits subtangBits[] = {SubtagBits::EMPTY,  SubtagBits::LANGUAGE, SubtagBits::SCRIPT,
27                                   SubtagBits::REGION, SubtagBits::VARIANT,  SubtagBits::EMOJI,
28                                   SubtagBits::ALL};
29 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)30 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
31     FuzzedDataProvider fdp(data, size);
32 
33     uint32_t id = registerLocaleList(fdp.ConsumeRandomLengthString());
34     uint32_t localeListId = fdp.ConsumeIntegralInRange<uint32_t>(0, id);
35     const LocaleList& locales = LocaleListCache::getById(localeListId);
36     std::string langTag = getLocaleString(localeListId);
37 
38     for (size_t i = 0; i < locales.size(); i++) {
39         locales[i].getPartialLocale(fdp.PickValueInArray(subtangBits));
40         locales[i].supportsScript(fdp.ConsumeIntegral<uint32_t>());
41         locales[i].calcScoreFor(locales);
42     }
43 
44     BufferWriter fakeWriter(nullptr);
45     LocaleListCache::writeTo(&fakeWriter, LocaleListCache::getId(langTag));
46     std::vector<uint8_t> buffer(fakeWriter.size());
47     BufferWriter writer(buffer.data());
48     LocaleListCache::writeTo(&writer, LocaleListCache::getId(langTag));
49     BufferReader reader(buffer.data());
50     LocaleListCache::readFrom(&reader);
51 
52     return 0;
53 }
54