objectivec_helpers_unittest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2014 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #include <google/protobuf/compiler/objectivec/objectivec_helpers.h>
  31. #include <google/protobuf/testing/googletest.h>
  32. #include <gtest/gtest.h>
  33. namespace google {
  34. namespace protobuf {
  35. namespace compiler {
  36. namespace objectivec {
  37. namespace {
  38. TEST(ObjCHelper, TextFormatDecodeData_DecodeDataForString_RawStrings) {
  39. string input_for_decode("abcdefghIJ");
  40. string desired_output_for_decode;
  41. string expected;
  42. string result;
  43. // Different data, can't transform.
  44. desired_output_for_decode = "zbcdefghIJ";
  45. expected = string("\0zbcdefghIJ\0", 12);
  46. result = TextFormatDecodeData::DecodeDataForString(input_for_decode,
  47. desired_output_for_decode);
  48. EXPECT_EQ(expected, result);
  49. desired_output_for_decode = "abcdezghIJ";
  50. expected = string("\0abcdezghIJ\0", 12);
  51. result = TextFormatDecodeData::DecodeDataForString(input_for_decode,
  52. desired_output_for_decode);
  53. EXPECT_EQ(expected, result);
  54. // Shortened data, can't transform.
  55. desired_output_for_decode = "abcdefghI";
  56. expected = string("\0abcdefghI\0", 11);
  57. result = TextFormatDecodeData::DecodeDataForString(input_for_decode,
  58. desired_output_for_decode);
  59. EXPECT_EQ(expected, result);
  60. // Extra data, can't transform.
  61. desired_output_for_decode = "abcdefghIJz";
  62. expected = string("\0abcdefghIJz\0", 13);
  63. result = TextFormatDecodeData::DecodeDataForString(input_for_decode,
  64. desired_output_for_decode);
  65. EXPECT_EQ(expected, result);
  66. }
  67. TEST(ObjCHelper, TextFormatDecodeData_DecodeDataForString_ByteCodes) {
  68. string input_for_decode("abcdefghIJ");
  69. string desired_output_for_decode;
  70. string expected;
  71. string result;
  72. desired_output_for_decode = "abcdefghIJ";
  73. expected = string("\x0A\x0", 2);
  74. result = TextFormatDecodeData::DecodeDataForString(input_for_decode,
  75. desired_output_for_decode);
  76. EXPECT_EQ(expected, result);
  77. desired_output_for_decode = "_AbcdefghIJ";
  78. expected = string("\xCA\x0", 2);
  79. result = TextFormatDecodeData::DecodeDataForString(input_for_decode,
  80. desired_output_for_decode);
  81. EXPECT_EQ(expected, result);
  82. desired_output_for_decode = "ABCD__EfghI_j";
  83. expected = string("\x64\x80\xC5\xA1\x0", 5);
  84. result = TextFormatDecodeData::DecodeDataForString(input_for_decode,
  85. desired_output_for_decode);
  86. EXPECT_EQ(expected, result);
  87. // Long name so multiple decode ops are needed.
  88. input_for_decode =
  89. "longFieldNameIsLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong1000";
  90. desired_output_for_decode =
  91. "long_field_name_is_looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong_1000";
  92. expected = string("\x04\xA5\xA4\xA2\xBF\x1F\x0E\x84\x0", 9);
  93. result = TextFormatDecodeData::DecodeDataForString(input_for_decode,
  94. desired_output_for_decode);
  95. EXPECT_EQ(expected, result);
  96. }
  97. // Death tests do not work on Windows as of yet.
  98. #ifdef PROTOBUF_HAS_DEATH_TEST
  99. TEST(ObjCHelperDeathTest, TextFormatDecodeData_DecodeDataForString_Failures) {
  100. // Empty inputs.
  101. EXPECT_EXIT(TextFormatDecodeData::DecodeDataForString("", ""),
  102. ::testing::KilledBySignal(SIGABRT),
  103. "error: got empty string for making TextFormat data, input:");
  104. EXPECT_EXIT(TextFormatDecodeData::DecodeDataForString("a", ""),
  105. ::testing::KilledBySignal(SIGABRT),
  106. "error: got empty string for making TextFormat data, input:");
  107. EXPECT_EXIT(TextFormatDecodeData::DecodeDataForString("", "a"),
  108. ::testing::KilledBySignal(SIGABRT),
  109. "error: got empty string for making TextFormat data, input:");
  110. // Null char in the string.
  111. string str_with_null_char("ab\0c", 4);
  112. EXPECT_EXIT(
  113. TextFormatDecodeData::DecodeDataForString(str_with_null_char, "def"),
  114. ::testing::KilledBySignal(SIGABRT),
  115. "error: got a null char in a string for making TextFormat data, input:");
  116. EXPECT_EXIT(
  117. TextFormatDecodeData::DecodeDataForString("def", str_with_null_char),
  118. ::testing::KilledBySignal(SIGABRT),
  119. "error: got a null char in a string for making TextFormat data, input:");
  120. }
  121. #endif // PROTOBUF_HAS_DEATH_TEST
  122. TEST(ObjCHelper, TextFormatDecodeData_RawStrings) {
  123. TextFormatDecodeData decode_data;
  124. // Different data, can't transform.
  125. decode_data.AddString(1, "abcdefghIJ", "zbcdefghIJ");
  126. decode_data.AddString(3, "abcdefghIJ", "abcdezghIJ");
  127. // Shortened data, can't transform.
  128. decode_data.AddString(2, "abcdefghIJ", "abcdefghI");
  129. // Extra data, can't transform.
  130. decode_data.AddString(4, "abcdefghIJ", "abcdefghIJz");
  131. EXPECT_EQ(4, decode_data.num_entries());
  132. uint8 expected_data[] = {
  133. 0x4,
  134. 0x1, 0x0, 'z', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'I', 'J', 0x0,
  135. 0x3, 0x0, 'a', 'b', 'c', 'd', 'e', 'z', 'g', 'h', 'I', 'J', 0x0,
  136. 0x2, 0x0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'I', 0x0,
  137. 0x4, 0x0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'I', 'J', 'z', 0x0,
  138. };
  139. string expected((const char*)expected_data, sizeof(expected_data));
  140. EXPECT_EQ(expected, decode_data.Data());
  141. }
  142. TEST(ObjCHelper, TextFormatDecodeData_ByteCodes) {
  143. TextFormatDecodeData decode_data;
  144. decode_data.AddString(1, "abcdefghIJ", "abcdefghIJ");
  145. decode_data.AddString(3, "abcdefghIJ", "_AbcdefghIJ");
  146. decode_data.AddString(2, "abcdefghIJ", "Abcd_EfghIJ");
  147. decode_data.AddString(4, "abcdefghIJ", "ABCD__EfghI_j");
  148. decode_data.AddString(1000,
  149. "longFieldNameIsLooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong1000",
  150. "long_field_name_is_looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong_1000");
  151. EXPECT_EQ(5, decode_data.num_entries());
  152. uint8 expected_data[] = {
  153. 0x5,
  154. // All as is (00 op)
  155. 0x1, 0x0A, 0x0,
  156. // Underscore, upper + 9 (10 op)
  157. 0x3, 0xCA, 0x0,
  158. // Upper + 3 (10 op), underscore, upper + 5 (10 op)
  159. 0x2, 0x44, 0xC6, 0x0,
  160. // All Upper for 4 (11 op), underscore, underscore, upper + 5 (10 op),
  161. // underscore, lower + 0 (01 op)
  162. 0x4, 0x64, 0x80, 0xC5, 0xA1, 0x0,
  163. // 2 byte key: as is + 3 (00 op), underscore, lower + 4 (01 op),
  164. // underscore, lower + 3 (01 op), underscore, lower + 1 (01 op),
  165. // underscore, lower + 30 (01 op), as is + 30 (00 op), as is + 13 (00
  166. // op),
  167. // underscore, as is + 3 (00 op)
  168. 0xE8, 0x07, 0x04, 0xA5, 0xA4, 0xA2, 0xBF, 0x1F, 0x0E, 0x84, 0x0,
  169. };
  170. string expected((const char*)expected_data, sizeof(expected_data));
  171. EXPECT_EQ(expected, decode_data.Data());
  172. }
  173. // Death tests do not work on Windows as of yet.
  174. #ifdef PROTOBUF_HAS_DEATH_TEST
  175. TEST(ObjCHelperDeathTest, TextFormatDecodeData_Failures) {
  176. TextFormatDecodeData decode_data;
  177. // Empty inputs.
  178. EXPECT_EXIT(decode_data.AddString(1, "", ""),
  179. ::testing::KilledBySignal(SIGABRT),
  180. "error: got empty string for making TextFormat data, input:");
  181. EXPECT_EXIT(decode_data.AddString(1, "a", ""),
  182. ::testing::KilledBySignal(SIGABRT),
  183. "error: got empty string for making TextFormat data, input:");
  184. EXPECT_EXIT(decode_data.AddString(1, "", "a"),
  185. ::testing::KilledBySignal(SIGABRT),
  186. "error: got empty string for making TextFormat data, input:");
  187. // Null char in the string.
  188. string str_with_null_char("ab\0c", 4);
  189. EXPECT_EXIT(
  190. decode_data.AddString(1, str_with_null_char, "def"),
  191. ::testing::KilledBySignal(SIGABRT),
  192. "error: got a null char in a string for making TextFormat data, input:");
  193. EXPECT_EXIT(
  194. decode_data.AddString(1, "def", str_with_null_char),
  195. ::testing::KilledBySignal(SIGABRT),
  196. "error: got a null char in a string for making TextFormat data, input:");
  197. // Duplicate keys
  198. decode_data.AddString(1, "abcdefghIJ", "abcdefghIJ");
  199. decode_data.AddString(3, "abcdefghIJ", "_AbcdefghIJ");
  200. decode_data.AddString(2, "abcdefghIJ", "Abcd_EfghIJ");
  201. EXPECT_EXIT(decode_data.AddString(2, "xyz", "x_yz"),
  202. ::testing::KilledBySignal(SIGABRT),
  203. "error: duplicate key \\(2\\) making TextFormat data, input:");
  204. }
  205. #endif // PROTOBUF_HAS_DEATH_TEST
  206. // TODO(thomasvl): Should probably add some unittests for all the special cases
  207. // of name mangling (class name, field name, enum names). Rather than doing
  208. // this with an ObjC test in the objectivec directory, we should be able to
  209. // use src/google/protobuf/compiler/importer* (like other tests) to support a
  210. // virtual file system to feed in protos, once we have the Descriptor tree, the
  211. // tests could use the helper methods for generating names and validate the
  212. // right things are happening.
  213. } // namespace
  214. } // namespace objectivec
  215. } // namespace compiler
  216. } // namespace protobuf
  217. } // namespace google