Unicode_test.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (C) 2010 The Android Open Source Project
  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. #define LOG_TAG "Unicode_test"
  17. #include <sys/mman.h>
  18. #include <unistd.h>
  19. #include <log/log.h>
  20. #include <utils/Unicode.h>
  21. #include <gtest/gtest.h>
  22. namespace android {
  23. class UnicodeTest : public testing::Test {
  24. protected:
  25. virtual void SetUp() {
  26. }
  27. virtual void TearDown() {
  28. }
  29. char16_t const * const kSearchString = u"I am a leaf on the wind.";
  30. };
  31. TEST_F(UnicodeTest, UTF8toUTF16ZeroLength) {
  32. ssize_t measured;
  33. const uint8_t str[] = { };
  34. measured = utf8_to_utf16_length(str, 0);
  35. EXPECT_EQ(0, measured)
  36. << "Zero length input should return zero length output.";
  37. }
  38. TEST_F(UnicodeTest, UTF8toUTF16ASCIILength) {
  39. ssize_t measured;
  40. // U+0030 or ASCII '0'
  41. const uint8_t str[] = { 0x30 };
  42. measured = utf8_to_utf16_length(str, sizeof(str));
  43. EXPECT_EQ(1, measured)
  44. << "ASCII glyphs should have a length of 1 char16_t";
  45. }
  46. TEST_F(UnicodeTest, UTF8toUTF16Plane1Length) {
  47. ssize_t measured;
  48. // U+2323 SMILE
  49. const uint8_t str[] = { 0xE2, 0x8C, 0xA3 };
  50. measured = utf8_to_utf16_length(str, sizeof(str));
  51. EXPECT_EQ(1, measured)
  52. << "Plane 1 glyphs should have a length of 1 char16_t";
  53. }
  54. TEST_F(UnicodeTest, UTF8toUTF16SurrogateLength) {
  55. ssize_t measured;
  56. // U+10000
  57. const uint8_t str[] = { 0xF0, 0x90, 0x80, 0x80 };
  58. measured = utf8_to_utf16_length(str, sizeof(str));
  59. EXPECT_EQ(2, measured)
  60. << "Surrogate pairs should have a length of 2 char16_t";
  61. }
  62. TEST_F(UnicodeTest, UTF8toUTF16TruncatedUTF8) {
  63. ssize_t measured;
  64. // Truncated U+2323 SMILE
  65. // U+2323 SMILE
  66. const uint8_t str[] = { 0xE2, 0x8C };
  67. measured = utf8_to_utf16_length(str, sizeof(str));
  68. EXPECT_EQ(-1, measured)
  69. << "Truncated UTF-8 should return -1 to indicate invalid";
  70. }
  71. TEST_F(UnicodeTest, UTF8toUTF16Normal) {
  72. const uint8_t str[] = {
  73. 0x30, // U+0030, 1 UTF-16 character
  74. 0xC4, 0x80, // U+0100, 1 UTF-16 character
  75. 0xE2, 0x8C, 0xA3, // U+2323, 1 UTF-16 character
  76. 0xF0, 0x90, 0x80, 0x80, // U+10000, 2 UTF-16 character
  77. };
  78. char16_t output[1 + 1 + 1 + 2 + 1]; // Room for NULL
  79. utf8_to_utf16(str, sizeof(str), output, sizeof(output) / sizeof(output[0]));
  80. EXPECT_EQ(0x0030, output[0])
  81. << "should be U+0030";
  82. EXPECT_EQ(0x0100, output[1])
  83. << "should be U+0100";
  84. EXPECT_EQ(0x2323, output[2])
  85. << "should be U+2323";
  86. EXPECT_EQ(0xD800, output[3])
  87. << "should be first half of surrogate U+10000";
  88. EXPECT_EQ(0xDC00, output[4])
  89. << "should be second half of surrogate U+10000";
  90. EXPECT_EQ(NULL, output[5])
  91. << "should be NULL terminated";
  92. }
  93. TEST_F(UnicodeTest, strstr16EmptyTarget) {
  94. EXPECT_EQ(strstr16(kSearchString, u""), kSearchString)
  95. << "should return the original pointer";
  96. }
  97. TEST_F(UnicodeTest, strstr16EmptyTarget_bug) {
  98. // In the original code when target is an empty string strlen16() would
  99. // start reading the memory until a "terminating null" (that is, zero)
  100. // character is found. This happens because "*target++" in the original
  101. // code would increment the pointer beyond the actual string.
  102. void* memptr;
  103. const size_t alignment = sysconf(_SC_PAGESIZE);
  104. const size_t size = 2 * alignment;
  105. ASSERT_EQ(posix_memalign(&memptr, alignment, size), 0);
  106. // Fill allocated memory.
  107. memset(memptr, 'A', size);
  108. // Create a pointer to an "empty" string on the first page.
  109. char16_t* const emptyString = (char16_t* const)((char*)memptr + alignment - 4);
  110. *emptyString = (char16_t)0;
  111. // Protect the second page to show that strstr16() violates that.
  112. ASSERT_EQ(mprotect((char*)memptr + alignment, alignment, PROT_NONE), 0);
  113. // Test strstr16(): when bug is present a segmentation fault is raised.
  114. ASSERT_EQ(strstr16((char16_t*)memptr, emptyString), (char16_t*)memptr)
  115. << "should not read beyond the first char16_t.";
  116. // Reset protection of the second page
  117. ASSERT_EQ(mprotect((char*)memptr + alignment, alignment, PROT_READ | PROT_WRITE), 0);
  118. // Free allocated memory.
  119. free(memptr);
  120. }
  121. TEST_F(UnicodeTest, strstr16SameString) {
  122. const char16_t* result = strstr16(kSearchString, kSearchString);
  123. EXPECT_EQ(kSearchString, result)
  124. << "should return the original pointer";
  125. }
  126. TEST_F(UnicodeTest, strstr16TargetStartOfString) {
  127. const char16_t* result = strstr16(kSearchString, u"I am");
  128. EXPECT_EQ(kSearchString, result)
  129. << "should return the original pointer";
  130. }
  131. TEST_F(UnicodeTest, strstr16TargetEndOfString) {
  132. const char16_t* result = strstr16(kSearchString, u"wind.");
  133. EXPECT_EQ(kSearchString+19, result);
  134. }
  135. TEST_F(UnicodeTest, strstr16TargetWithinString) {
  136. const char16_t* result = strstr16(kSearchString, u"leaf");
  137. EXPECT_EQ(kSearchString+7, result);
  138. }
  139. TEST_F(UnicodeTest, strstr16TargetNotPresent) {
  140. const char16_t* result = strstr16(kSearchString, u"soar");
  141. EXPECT_EQ(nullptr, result);
  142. }
  143. // http://b/29267949
  144. // Test that overreading in utf8_to_utf16_length is detected
  145. TEST_F(UnicodeTest, InvalidUtf8OverreadDetected) {
  146. // An utf8 char starting with \xc4 is two bytes long.
  147. // Add extra zeros so no extra memory is read in case the code doesn't
  148. // work as expected.
  149. static char utf8[] = "\xc4\x00\x00\x00";
  150. ASSERT_DEATH(utf8_to_utf16_length((uint8_t *) utf8, strlen(utf8),
  151. true /* overreadIsFatal */), "" /* regex for ASSERT_DEATH */);
  152. }
  153. }