1 // Copyright 2015 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "core/fpdfapi/font/cpdf_tounicodemap.h"
6 
7 #include "testing/gmock/include/gmock/gmock.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 
TEST(cpdf_tounicodemap,StringToCode)10 TEST(cpdf_tounicodemap, StringToCode) {
11   EXPECT_THAT(CPDF_ToUnicodeMap::StringToCode("<0001>"), testing::Optional(1u));
12   EXPECT_THAT(CPDF_ToUnicodeMap::StringToCode("<c2>"), testing::Optional(194u));
13   EXPECT_THAT(CPDF_ToUnicodeMap::StringToCode("<A2>"), testing::Optional(162u));
14   EXPECT_THAT(CPDF_ToUnicodeMap::StringToCode("<Af2>"),
15               testing::Optional(2802u));
16   EXPECT_THAT(CPDF_ToUnicodeMap::StringToCode("<FFFFFFFF>"),
17               testing::Optional(4294967295u));
18 
19   // Integer overflow
20   EXPECT_FALSE(CPDF_ToUnicodeMap::StringToCode("<100000000>").has_value());
21   EXPECT_FALSE(CPDF_ToUnicodeMap::StringToCode("<1abcdFFFF>").has_value());
22 
23   // Invalid string
24   EXPECT_FALSE(CPDF_ToUnicodeMap::StringToCode("").has_value());
25   EXPECT_FALSE(CPDF_ToUnicodeMap::StringToCode("<>").has_value());
26   EXPECT_FALSE(CPDF_ToUnicodeMap::StringToCode("12").has_value());
27   EXPECT_FALSE(CPDF_ToUnicodeMap::StringToCode("<12").has_value());
28   EXPECT_FALSE(CPDF_ToUnicodeMap::StringToCode("12>").has_value());
29   EXPECT_FALSE(CPDF_ToUnicodeMap::StringToCode("<1-7>").has_value());
30   EXPECT_FALSE(CPDF_ToUnicodeMap::StringToCode("00AB").has_value());
31   EXPECT_FALSE(CPDF_ToUnicodeMap::StringToCode("<00NN>").has_value());
32 }
33 
TEST(cpdf_tounicodemap,StringToWideString)34 TEST(cpdf_tounicodemap, StringToWideString) {
35   EXPECT_EQ(L"", CPDF_ToUnicodeMap::StringToWideString(""));
36   EXPECT_EQ(L"", CPDF_ToUnicodeMap::StringToWideString("1234"));
37   EXPECT_EQ(L"", CPDF_ToUnicodeMap::StringToWideString("<c2"));
38   EXPECT_EQ(L"", CPDF_ToUnicodeMap::StringToWideString("<c2D2"));
39   EXPECT_EQ(L"", CPDF_ToUnicodeMap::StringToWideString("c2ab>"));
40 
41   WideString res = L"\xc2ab";
42   EXPECT_EQ(res, CPDF_ToUnicodeMap::StringToWideString("<c2ab>"));
43   EXPECT_EQ(res, CPDF_ToUnicodeMap::StringToWideString("<c2abab>"));
44   EXPECT_EQ(res, CPDF_ToUnicodeMap::StringToWideString("<c2ab 1234>"));
45 
46   res += L"\xfaab";
47   EXPECT_EQ(res, CPDF_ToUnicodeMap::StringToWideString("<c2abFaAb>"));
48   EXPECT_EQ(res, CPDF_ToUnicodeMap::StringToWideString("<c2abFaAb12>"));
49 }
50