test_string_bytes.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * Copyright (C) 2020 Embedded AMS B.V. - All Rights Reserved
  3. *
  4. * This file is part of Embedded Proto.
  5. *
  6. * Embedded Proto is open source software: you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as published
  8. * by the Free Software Foundation, version 3 of the license.
  9. *
  10. * Embedded Proto is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Embedded Proto. If not, see <https://www.gnu.org/licenses/>.
  17. *
  18. * For commercial and closed source application please visit:
  19. * <https://EmbeddedProto.com/license/>.
  20. *
  21. * Embedded AMS B.V.
  22. * Info:
  23. * info at EmbeddedProto dot com
  24. *
  25. * Postal address:
  26. * Johan Huizingalaan 763a
  27. * 1066 VH, Amsterdam
  28. * the Netherlands
  29. */
  30. #include "gtest/gtest.h"
  31. #include "gmock/gmock.h"
  32. #include <WireFormatter.h>
  33. #include <ReadBufferMock.h>
  34. #include <WriteBufferMock.h>
  35. #include <cstdint>
  36. #include <limits>
  37. // EAMS message definitions
  38. #include <string_bytes.h>
  39. using ::testing::_;
  40. using ::testing::InSequence;
  41. using ::testing::Return;
  42. using ::testing::SetArgReferee;
  43. using ::testing::ElementsAre;
  44. namespace test_EmbeddedAMS_string_bytes
  45. {
  46. TEST(FieldString, clear)
  47. {
  48. text<10> msg;
  49. // Clear the field specific.
  50. msg.mutable_txt() = "Foo Bar";
  51. EXPECT_EQ(7, msg.txt().get_length());
  52. msg.clear_txt();
  53. EXPECT_EQ(0, msg.txt().get_length());
  54. // Clear the whole message.
  55. msg.mutable_txt() = "Foo Bar";
  56. msg.clear();
  57. EXPECT_EQ(0, msg.txt().get_length());
  58. }
  59. TEST(FieldString, serialize)
  60. {
  61. InSequence s;
  62. text<10> msg;
  63. Mocks::WriteBufferMock buffer;
  64. msg.mutable_txt() = "Foo bar";
  65. EXPECT_CALL(buffer, get_available_size()).Times(1).WillOnce(Return(17));
  66. uint8_t expected[] = {0x0a, 0x07};
  67. for(auto e : expected)
  68. {
  69. EXPECT_CALL(buffer, push(e)).Times(1).WillOnce(Return(true));
  70. }
  71. EXPECT_CALL(buffer, push(_, 7)).Times(1).WillOnce(Return(true));
  72. EXPECT_EQ(::EmbeddedProto::Error::NO_ERRORS, msg.serialize(buffer));
  73. EXPECT_EQ(10, msg.txt().get_max_length());
  74. }
  75. TEST(FieldString, deserialize)
  76. {
  77. InSequence s;
  78. text<10> msg;
  79. Mocks::ReadBufferMock buffer;
  80. uint8_t referee[] = {0x0a, 0x07, 0x46, 0x6f, 0x6f, 0x20, 0x62, 0x61, 0x72};
  81. for(auto r: referee)
  82. {
  83. EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(r), Return(true)));
  84. }
  85. EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(Return(false));
  86. EXPECT_EQ(::EmbeddedProto::Error::NO_ERRORS, msg.deserialize(buffer));
  87. EXPECT_EQ(7, msg.txt().get_length());
  88. EXPECT_STREQ(msg.get_txt(), "Foo bar");
  89. }
  90. TEST(FieldString, deserialize_error_invalid_wiretype)
  91. {
  92. InSequence s;
  93. text<10> msg;
  94. Mocks::ReadBufferMock buffer;
  95. // The first byte is an invalid wiretype
  96. EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(0x09), Return(true)));
  97. EXPECT_EQ(::EmbeddedProto::Error::INVALID_WIRETYPE, msg.deserialize(buffer));
  98. EXPECT_EQ(0, msg.txt().get_length());
  99. }
  100. TEST(FieldString, oneof_serialize)
  101. {
  102. InSequence s;
  103. string_or_bytes<10, 10> msg;
  104. Mocks::WriteBufferMock buffer;
  105. msg.mutable_txt() = "Foo bar";
  106. EXPECT_CALL(buffer, get_available_size()).Times(1).WillOnce(Return(99));
  107. uint8_t expected[] = {0x0a, 0x07};
  108. for(auto e : expected)
  109. {
  110. EXPECT_CALL(buffer, push(e)).Times(1).WillOnce(Return(true));
  111. }
  112. EXPECT_CALL(buffer, push(_, 7)).Times(1).WillOnce(Return(true));
  113. EXPECT_EQ(::EmbeddedProto::Error::NO_ERRORS, msg.serialize(buffer));
  114. EXPECT_EQ(10, msg.txt().get_max_length());
  115. }
  116. TEST(FieldString, oneof_deserialize)
  117. {
  118. InSequence s;
  119. string_or_bytes<10, 10> msg;
  120. Mocks::ReadBufferMock buffer;
  121. uint8_t referee[] = {0x0a, 0x07, 0x46, 0x6f, 0x6f, 0x20, 0x62, 0x61, 0x72};
  122. for(auto r: referee)
  123. {
  124. EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(r), Return(true)));
  125. }
  126. EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(Return(false));
  127. EXPECT_EQ(::EmbeddedProto::Error::NO_ERRORS, msg.deserialize(buffer));
  128. EXPECT_EQ(7, msg.txt().get_length());
  129. EXPECT_STREQ(msg.get_txt(), "Foo bar");
  130. }
  131. TEST(FieldBytes, set_get)
  132. {
  133. raw_bytes<10> msg;
  134. msg.mutable_b()[0] = 1;
  135. EXPECT_EQ(1, msg.b().get_length());
  136. EXPECT_EQ(1, msg.b().get_const(0));
  137. msg.clear();
  138. EXPECT_EQ(0, msg.b().get_length());
  139. EXPECT_EQ(0, msg.b().get_const(0));
  140. msg.mutable_b()[1] = 2;
  141. EXPECT_EQ(2, msg.b().get_length());
  142. EXPECT_EQ(0, msg.b().get_const(0));
  143. EXPECT_EQ(2, msg.b().get_const(1));
  144. // Check index out of bound will return the last element.
  145. msg.mutable_b()[10] = 11; // max index should be 9.
  146. // The last element should be changed
  147. EXPECT_EQ(10, msg.b().get_length());
  148. EXPECT_EQ(11, msg.b().get_const(9));
  149. // Check this function out of bound aswell.
  150. EXPECT_EQ(11, msg.b().get_const(10));
  151. // Try to set more bytes compared to what will fit.
  152. uint8_t big_array[11] = {0};
  153. big_array[10] = 11;
  154. EXPECT_EQ(::EmbeddedProto::Error::ARRAY_FULL, msg.mutable_b().set(big_array, 11));
  155. }
  156. TEST(FieldBytes, clear)
  157. {
  158. raw_bytes<10> msg;
  159. const uint8_t array[2] = {1 ,2};
  160. // Clear the field specific.
  161. msg.mutable_b().set(array, 2);
  162. EXPECT_EQ(2, msg.b().get_length());
  163. msg.clear_b();
  164. EXPECT_EQ(0, msg.b().get_length());
  165. // Clear the whole message.
  166. msg.mutable_b().set(array, 2);
  167. msg.clear();
  168. EXPECT_EQ(0, msg.b().get_length());
  169. }
  170. TEST(FieldBytes, serialize)
  171. {
  172. InSequence s;
  173. raw_bytes<10> msg;
  174. Mocks::WriteBufferMock buffer;
  175. uint8_t bytes[] = {1u, 2u, 3u, 0u};
  176. msg.mutable_b().set(bytes, 4);
  177. EXPECT_CALL(buffer, get_available_size()).Times(1).WillOnce(Return(17));
  178. uint8_t expected[] = {0x0a, 0x04};
  179. for(auto e : expected)
  180. {
  181. EXPECT_CALL(buffer, push(e)).Times(1).WillOnce(Return(true));
  182. }
  183. EXPECT_CALL(buffer, push(_, 4)).Times(1).WillOnce(Return(true));
  184. EXPECT_EQ(::EmbeddedProto::Error::NO_ERRORS, msg.serialize(buffer));
  185. EXPECT_EQ(10, msg.b().get_max_length());
  186. }
  187. TEST(FieldBytes, deserialize)
  188. {
  189. InSequence s;
  190. raw_bytes<10> msg;
  191. Mocks::ReadBufferMock buffer;
  192. uint8_t referee[] = {0x0a, 0x04, 0x01, 0x02, 0x03, 0x00};
  193. for(auto r: referee)
  194. {
  195. EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(r), Return(true)));
  196. }
  197. EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(Return(false));
  198. EXPECT_EQ(::EmbeddedProto::Error::NO_ERRORS, msg.deserialize(buffer));
  199. EXPECT_EQ(4, msg.b().get_length());
  200. EXPECT_EQ(1, msg.b()[0]);
  201. EXPECT_EQ(2, msg.b()[1]);
  202. EXPECT_EQ(3, msg.b()[2]);
  203. EXPECT_EQ(0, msg.b()[3]);
  204. }
  205. TEST(FieldBytes, deserialize_error_invalid_wiretype)
  206. {
  207. InSequence s;
  208. raw_bytes<10> msg;
  209. Mocks::ReadBufferMock buffer;
  210. // The first byte is an invalid wiretype
  211. EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(0x09), Return(true)));
  212. EXPECT_EQ(::EmbeddedProto::Error::INVALID_WIRETYPE, msg.deserialize(buffer));
  213. EXPECT_EQ(0, msg.b().get_length());
  214. }
  215. TEST(FieldBytes, oneof_set_get)
  216. {
  217. string_or_bytes<10, 10> msg;
  218. msg.mutable_txt() = "Foo Bar";
  219. auto id = string_or_bytes<10, 10>::id::TXT;
  220. EXPECT_EQ(id, msg.get_which_s_or_b());
  221. EXPECT_STREQ(msg.get_txt(), "Foo Bar");
  222. // Switch to the array
  223. uint8_t array[] = {1, 2, 3, 4, 5};
  224. msg.mutable_b().set(array, 5);
  225. id = string_or_bytes<10, 10>::id::B;
  226. EXPECT_EQ(id, msg.get_which_s_or_b());
  227. for(uint8_t i = 0; i < 5; ++i)
  228. {
  229. EXPECT_EQ(i+1, msg.get_b()[i]);
  230. }
  231. }
  232. TEST(FieldBytes, oneof_clear)
  233. {
  234. raw_bytes<10> msg;
  235. const uint8_t array[2] = {1 ,2};
  236. // Clear the field specific.
  237. msg.mutable_b().set(array, 2);
  238. EXPECT_EQ(2, msg.b().get_length());
  239. msg.clear_b();
  240. EXPECT_EQ(0, msg.b().get_length());
  241. // Clear the whole message.
  242. msg.mutable_b().set(array, 2);
  243. msg.clear();
  244. EXPECT_EQ(0, msg.b().get_length());
  245. }
  246. TEST(FieldBytes, oneof_serialize)
  247. {
  248. InSequence s;
  249. string_or_bytes<10, 10> msg;
  250. Mocks::WriteBufferMock buffer;
  251. uint8_t bytes[] = {1u, 2u, 3u, 0u};
  252. msg.mutable_b().set(bytes, 4);
  253. EXPECT_CALL(buffer, get_available_size()).Times(1).WillOnce(Return(17));
  254. uint8_t expected[] = {0x12, 0x04};
  255. for(auto e : expected)
  256. {
  257. EXPECT_CALL(buffer, push(e)).Times(1).WillOnce(Return(true));
  258. }
  259. EXPECT_CALL(buffer, push(_, 4)).Times(1).WillOnce(Return(true));
  260. EXPECT_EQ(::EmbeddedProto::Error::NO_ERRORS, msg.serialize(buffer));
  261. EXPECT_EQ(10, msg.txt().get_max_length());
  262. }
  263. TEST(FieldBytes, oneof_deserialize)
  264. {
  265. InSequence s;
  266. string_or_bytes<10, 10> msg;
  267. Mocks::ReadBufferMock buffer;
  268. uint8_t referee[] = {0x12, 0x04, 0x01, 0x02, 0x03, 0x00};
  269. for(auto r: referee)
  270. {
  271. EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(r), Return(true)));
  272. }
  273. EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(Return(false));
  274. EXPECT_EQ(::EmbeddedProto::Error::NO_ERRORS, msg.deserialize(buffer));
  275. EXPECT_EQ(4, msg.b().get_length());
  276. EXPECT_EQ(1, msg.b()[0]);
  277. EXPECT_EQ(2, msg.b()[1]);
  278. EXPECT_EQ(3, msg.b()[2]);
  279. EXPECT_EQ(0, msg.b()[3]);
  280. }
  281. } // End of namespace test_EmbeddedAMS_string_bytes