xref: /aosp_15_r20/external/fbjni/test/jni/modified_utf8_test.cpp (revision 65c59e023c5336bbd4a23be7af78407e3d80e7e7)
1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
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 <gtest/gtest.h>
18 
19 #include <fbjni/detail/utf8.h>
20 
21 #include <vector>
22 
23 using namespace std;
24 using namespace facebook::jni;
25 
testPair(const vector<uint8_t> & utf8,const vector<uint8_t> & modified,bool test_out_eq)26 void testPair(
27     const vector<uint8_t>& utf8,
28     const vector<uint8_t>& modified,
29     bool test_out_eq) {
30   // utf8 -> modified utf8
31 
32   string utf8str(reinterpret_cast<const char*>(utf8.data()), utf8.size());
33 
34   size_t modlen = detail::modifiedLength(utf8str);
35   EXPECT_EQ(modlen, modified.size());
36   vector<uint8_t> out(modlen + 1);
37   detail::utf8ToModifiedUTF8(utf8.data(), utf8.size(), out.data(), out.size());
38   // we expect utf8ToModified to return null terminated string, but vector in
39   // modified will not have \0 at the end, therefore we crop out buffer before
40   // comparing
41   EXPECT_EQ('\0', out[modlen]);
42   out.resize(modlen);
43   if (test_out_eq) {
44     EXPECT_EQ(out, modified);
45   }
46 
47   // modified utf8 -> utf8
48 
49   string s = detail::modifiedUTF8ToUTF8(modified.data(), modified.size());
50   if (test_out_eq) {
51     EXPECT_EQ(s, utf8str);
52   }
53 }
54 
testFailForTooShortBuffer(const vector<uint8_t> & utf8,int out_len)55 void testFailForTooShortBuffer(const vector<uint8_t>& utf8, int out_len) {
56   const char* message =
57 #ifdef __ANDROID__
58       "output buffer is too short";
59 #else
60       "";
61 #endif
62 
63   ASSERT_DEATH(
64       {
65         vector<uint8_t> out(out_len);
66         detail::utf8ToModifiedUTF8(
67             utf8.data(), utf8.size(), out.data(), out.size());
68       },
69       message);
70 }
71 
vector_append(vector<uint8_t> & target,const vector<uint8_t> & source)72 void vector_append(vector<uint8_t>& target, const vector<uint8_t>& source) {
73   target.insert(target.end(), source.begin(), source.end());
74 }
75 
TEST(ModifiedUTF8Test,pairs)76 TEST(ModifiedUTF8Test, pairs) {
77   vector<uint8_t> zero = {0}; // U+0000
78   vector<uint8_t> zero_modified = {0xc0, 0x80};
79   vector<uint8_t> one_byte = {'a'}; // U+0041
80   vector<uint8_t> two_byte = {0xd8, 0xa1}; // U+00E1  small a with acute
81   vector<uint8_t> three_byte = {0xe4, 0xba, 0xba}; // U+4EBA  unihan ren
82   vector<uint8_t> four_byte = {
83       0xf0, 0x9f, 0x98, 0xb8}; // U+1F638  grinning cat face with smiling eyes
84   vector<uint8_t> four_byte_modified = {0xed, 0xa0, 0xbd, 0xed, 0xb8, 0xb8};
85   vector<uint8_t> four_byte_truncated(
86       four_byte.begin(), four_byte.begin() + (four_byte.size() - 1));
87   vector<uint8_t> modified_truncated(
88       four_byte_modified.begin(),
89       four_byte_modified.begin() + (four_byte_modified.size() - 1));
90 
91   testPair(zero, zero_modified, true);
92   testPair(one_byte, one_byte, true);
93   testPair(two_byte, two_byte, true);
94   testPair(three_byte, three_byte, true);
95   testPair(four_byte, four_byte_modified, true);
96 
97   // The output is not strictly defined, but should not crash.
98   testPair(four_byte_truncated, four_byte_truncated, false);
99   testPair(modified_truncated, modified_truncated, false);
100 
101   vector<uint8_t> all;
102   vector_append(all, zero);
103   vector_append(all, one_byte);
104   vector_append(all, two_byte);
105   vector_append(all, three_byte);
106   vector_append(all, four_byte);
107   vector_append(all, zero);
108   vector_append(all, one_byte);
109   vector_append(all, two_byte);
110   vector_append(all, three_byte);
111   vector_append(all, four_byte);
112 
113   vector<uint8_t> all_modified;
114   vector_append(all_modified, zero_modified);
115   vector_append(all_modified, one_byte);
116   vector_append(all_modified, two_byte);
117   vector_append(all_modified, three_byte);
118   vector_append(all_modified, four_byte_modified);
119   vector_append(all_modified, zero_modified);
120   vector_append(all_modified, one_byte);
121   vector_append(all_modified, two_byte);
122   vector_append(all_modified, three_byte);
123   vector_append(all_modified, four_byte_modified);
124 
125   testPair(all, all_modified, true);
126 }
127 
TEST(ModifiedUTF8Test,conversionFailForTooShortBuffer)128 TEST(ModifiedUTF8Test, conversionFailForTooShortBuffer) {
129   vector<uint8_t> zero = {0};
130   vector<uint8_t> one_byte = {'a'};
131   vector<uint8_t> four_byte = {0xf0, 0x9f, 0x98, 0xb8};
132 
133   testFailForTooShortBuffer(zero, 1);
134   testFailForTooShortBuffer(zero, 2);
135   testFailForTooShortBuffer(one_byte, 1);
136   testFailForTooShortBuffer(four_byte, 2);
137   testFailForTooShortBuffer(four_byte, 4);
138 }
139 
main(int argc,char ** argv)140 int main(int argc, char** argv) {
141   testing::InitGoogleTest(&argc, argv);
142 
143   return RUN_ALL_TESTS();
144 }
145