1*65c59e02SInna Palant /*
2*65c59e02SInna Palant * Copyright (c) Facebook, Inc. and its affiliates.
3*65c59e02SInna Palant *
4*65c59e02SInna Palant * Licensed under the Apache License, Version 2.0 (the "License");
5*65c59e02SInna Palant * you may not use this file except in compliance with the License.
6*65c59e02SInna Palant * You may obtain a copy of the License at
7*65c59e02SInna Palant *
8*65c59e02SInna Palant * http://www.apache.org/licenses/LICENSE-2.0
9*65c59e02SInna Palant *
10*65c59e02SInna Palant * Unless required by applicable law or agreed to in writing, software
11*65c59e02SInna Palant * distributed under the License is distributed on an "AS IS" BASIS,
12*65c59e02SInna Palant * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*65c59e02SInna Palant * See the License for the specific language governing permissions and
14*65c59e02SInna Palant * limitations under the License.
15*65c59e02SInna Palant */
16*65c59e02SInna Palant
17*65c59e02SInna Palant #include <gtest/gtest.h>
18*65c59e02SInna Palant
19*65c59e02SInna Palant #include <fbjni/detail/utf8.h>
20*65c59e02SInna Palant
21*65c59e02SInna Palant #include <vector>
22*65c59e02SInna Palant
23*65c59e02SInna Palant using namespace std;
24*65c59e02SInna Palant using namespace facebook::jni;
25*65c59e02SInna Palant
testPair(const vector<uint8_t> & utf8,const vector<uint8_t> & modified,bool test_out_eq)26*65c59e02SInna Palant void testPair(
27*65c59e02SInna Palant const vector<uint8_t>& utf8,
28*65c59e02SInna Palant const vector<uint8_t>& modified,
29*65c59e02SInna Palant bool test_out_eq) {
30*65c59e02SInna Palant // utf8 -> modified utf8
31*65c59e02SInna Palant
32*65c59e02SInna Palant string utf8str(reinterpret_cast<const char*>(utf8.data()), utf8.size());
33*65c59e02SInna Palant
34*65c59e02SInna Palant size_t modlen = detail::modifiedLength(utf8str);
35*65c59e02SInna Palant EXPECT_EQ(modlen, modified.size());
36*65c59e02SInna Palant vector<uint8_t> out(modlen + 1);
37*65c59e02SInna Palant detail::utf8ToModifiedUTF8(utf8.data(), utf8.size(), out.data(), out.size());
38*65c59e02SInna Palant // we expect utf8ToModified to return null terminated string, but vector in
39*65c59e02SInna Palant // modified will not have \0 at the end, therefore we crop out buffer before
40*65c59e02SInna Palant // comparing
41*65c59e02SInna Palant EXPECT_EQ('\0', out[modlen]);
42*65c59e02SInna Palant out.resize(modlen);
43*65c59e02SInna Palant if (test_out_eq) {
44*65c59e02SInna Palant EXPECT_EQ(out, modified);
45*65c59e02SInna Palant }
46*65c59e02SInna Palant
47*65c59e02SInna Palant // modified utf8 -> utf8
48*65c59e02SInna Palant
49*65c59e02SInna Palant string s = detail::modifiedUTF8ToUTF8(modified.data(), modified.size());
50*65c59e02SInna Palant if (test_out_eq) {
51*65c59e02SInna Palant EXPECT_EQ(s, utf8str);
52*65c59e02SInna Palant }
53*65c59e02SInna Palant }
54*65c59e02SInna Palant
testFailForTooShortBuffer(const vector<uint8_t> & utf8,int out_len)55*65c59e02SInna Palant void testFailForTooShortBuffer(const vector<uint8_t>& utf8, int out_len) {
56*65c59e02SInna Palant const char* message =
57*65c59e02SInna Palant #ifdef __ANDROID__
58*65c59e02SInna Palant "output buffer is too short";
59*65c59e02SInna Palant #else
60*65c59e02SInna Palant "";
61*65c59e02SInna Palant #endif
62*65c59e02SInna Palant
63*65c59e02SInna Palant ASSERT_DEATH(
64*65c59e02SInna Palant {
65*65c59e02SInna Palant vector<uint8_t> out(out_len);
66*65c59e02SInna Palant detail::utf8ToModifiedUTF8(
67*65c59e02SInna Palant utf8.data(), utf8.size(), out.data(), out.size());
68*65c59e02SInna Palant },
69*65c59e02SInna Palant message);
70*65c59e02SInna Palant }
71*65c59e02SInna Palant
vector_append(vector<uint8_t> & target,const vector<uint8_t> & source)72*65c59e02SInna Palant void vector_append(vector<uint8_t>& target, const vector<uint8_t>& source) {
73*65c59e02SInna Palant target.insert(target.end(), source.begin(), source.end());
74*65c59e02SInna Palant }
75*65c59e02SInna Palant
TEST(ModifiedUTF8Test,pairs)76*65c59e02SInna Palant TEST(ModifiedUTF8Test, pairs) {
77*65c59e02SInna Palant vector<uint8_t> zero = {0}; // U+0000
78*65c59e02SInna Palant vector<uint8_t> zero_modified = {0xc0, 0x80};
79*65c59e02SInna Palant vector<uint8_t> one_byte = {'a'}; // U+0041
80*65c59e02SInna Palant vector<uint8_t> two_byte = {0xd8, 0xa1}; // U+00E1 small a with acute
81*65c59e02SInna Palant vector<uint8_t> three_byte = {0xe4, 0xba, 0xba}; // U+4EBA unihan ren
82*65c59e02SInna Palant vector<uint8_t> four_byte = {
83*65c59e02SInna Palant 0xf0, 0x9f, 0x98, 0xb8}; // U+1F638 grinning cat face with smiling eyes
84*65c59e02SInna Palant vector<uint8_t> four_byte_modified = {0xed, 0xa0, 0xbd, 0xed, 0xb8, 0xb8};
85*65c59e02SInna Palant vector<uint8_t> four_byte_truncated(
86*65c59e02SInna Palant four_byte.begin(), four_byte.begin() + (four_byte.size() - 1));
87*65c59e02SInna Palant vector<uint8_t> modified_truncated(
88*65c59e02SInna Palant four_byte_modified.begin(),
89*65c59e02SInna Palant four_byte_modified.begin() + (four_byte_modified.size() - 1));
90*65c59e02SInna Palant
91*65c59e02SInna Palant testPair(zero, zero_modified, true);
92*65c59e02SInna Palant testPair(one_byte, one_byte, true);
93*65c59e02SInna Palant testPair(two_byte, two_byte, true);
94*65c59e02SInna Palant testPair(three_byte, three_byte, true);
95*65c59e02SInna Palant testPair(four_byte, four_byte_modified, true);
96*65c59e02SInna Palant
97*65c59e02SInna Palant // The output is not strictly defined, but should not crash.
98*65c59e02SInna Palant testPair(four_byte_truncated, four_byte_truncated, false);
99*65c59e02SInna Palant testPair(modified_truncated, modified_truncated, false);
100*65c59e02SInna Palant
101*65c59e02SInna Palant vector<uint8_t> all;
102*65c59e02SInna Palant vector_append(all, zero);
103*65c59e02SInna Palant vector_append(all, one_byte);
104*65c59e02SInna Palant vector_append(all, two_byte);
105*65c59e02SInna Palant vector_append(all, three_byte);
106*65c59e02SInna Palant vector_append(all, four_byte);
107*65c59e02SInna Palant vector_append(all, zero);
108*65c59e02SInna Palant vector_append(all, one_byte);
109*65c59e02SInna Palant vector_append(all, two_byte);
110*65c59e02SInna Palant vector_append(all, three_byte);
111*65c59e02SInna Palant vector_append(all, four_byte);
112*65c59e02SInna Palant
113*65c59e02SInna Palant vector<uint8_t> all_modified;
114*65c59e02SInna Palant vector_append(all_modified, zero_modified);
115*65c59e02SInna Palant vector_append(all_modified, one_byte);
116*65c59e02SInna Palant vector_append(all_modified, two_byte);
117*65c59e02SInna Palant vector_append(all_modified, three_byte);
118*65c59e02SInna Palant vector_append(all_modified, four_byte_modified);
119*65c59e02SInna Palant vector_append(all_modified, zero_modified);
120*65c59e02SInna Palant vector_append(all_modified, one_byte);
121*65c59e02SInna Palant vector_append(all_modified, two_byte);
122*65c59e02SInna Palant vector_append(all_modified, three_byte);
123*65c59e02SInna Palant vector_append(all_modified, four_byte_modified);
124*65c59e02SInna Palant
125*65c59e02SInna Palant testPair(all, all_modified, true);
126*65c59e02SInna Palant }
127*65c59e02SInna Palant
TEST(ModifiedUTF8Test,conversionFailForTooShortBuffer)128*65c59e02SInna Palant TEST(ModifiedUTF8Test, conversionFailForTooShortBuffer) {
129*65c59e02SInna Palant vector<uint8_t> zero = {0};
130*65c59e02SInna Palant vector<uint8_t> one_byte = {'a'};
131*65c59e02SInna Palant vector<uint8_t> four_byte = {0xf0, 0x9f, 0x98, 0xb8};
132*65c59e02SInna Palant
133*65c59e02SInna Palant testFailForTooShortBuffer(zero, 1);
134*65c59e02SInna Palant testFailForTooShortBuffer(zero, 2);
135*65c59e02SInna Palant testFailForTooShortBuffer(one_byte, 1);
136*65c59e02SInna Palant testFailForTooShortBuffer(four_byte, 2);
137*65c59e02SInna Palant testFailForTooShortBuffer(four_byte, 4);
138*65c59e02SInna Palant }
139*65c59e02SInna Palant
main(int argc,char ** argv)140*65c59e02SInna Palant int main(int argc, char** argv) {
141*65c59e02SInna Palant testing::InitGoogleTest(&argc, argv);
142*65c59e02SInna Palant
143*65c59e02SInna Palant return RUN_ALL_TESTS();
144*65c59e02SInna Palant }
145