Use malloc instead of std::vector
[platform/core/base/bundle.git] / tests / parcel_unittests / test_parcel_cpp.cc
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
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
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19
20 #include <iostream>
21 #include <stdexcept>
22
23 #include "parcel/parcel.hh"
24
25 using ::testing::AtLeast;
26 using namespace tizen_base;
27
28 class ParcelCppTest : public ::testing::Test {
29  public:
30   virtual void SetUp() {
31   }
32
33   virtual void TearDown() {
34     parcel_.Clear();
35   }
36
37   Parcel& GetHandle() {
38     return parcel_;
39   }
40
41  private:
42   Parcel parcel_;
43 };
44
45 class Student : public Parcelable {
46  public:
47   Student(std::string name, uint32_t age)
48       : name_(std::move(name)), age_(age) {
49   }
50
51   Student() = default;
52   virtual ~Student() = default;
53
54   const std::string& GetName() const {
55     return name_;
56   }
57
58   uint32_t GetAge() const {
59     return age_;
60   }
61
62   virtual void WriteToParcel(Parcel* parcel) const {
63     parcel->WriteString(name_);
64     parcel->WriteUInt32(age_);
65   }
66
67   virtual void ReadFromParcel(Parcel* parcel) {
68     name_ = parcel->ReadString();
69     parcel->ReadUInt32(&age_);
70   }
71
72  private:
73   std::string name_;
74   uint32_t age_;
75 };
76
77 TEST_F(ParcelCppTest, Ctor_AND_Dtor) {
78   Parcel parcel;
79 }
80
81 TEST_F(ParcelCppTest, Write_AND_Read) {
82   char buf[1024] = { 'P', };
83   GetHandle().Write(buf, sizeof(buf));
84   char read_buf[1024] = { 0, };
85   int ret = GetHandle().Read(read_buf, sizeof(read_buf));
86   ASSERT_EQ(ret, Parcel::Error::None);
87   ASSERT_EQ(buf[0], read_buf[0]);
88 }
89
90 TEST_F(ParcelCppTest, WriteBool_AND_ReadBool) {
91   GetHandle().WriteBool(true);
92   bool val = false;
93   int ret = GetHandle().ReadBool(&val);
94   ASSERT_EQ(ret, Parcel::Error::None);
95   ASSERT_EQ(val, true);
96 }
97
98 TEST_F(ParcelCppTest, WriteByte_AND_ReadByte) {
99   GetHandle().WriteByte(1);
100   char val = 0;
101   int ret = GetHandle().ReadByte(&val);
102   ASSERT_EQ(ret, Parcel::Error::None);
103   ASSERT_EQ(val, 1);
104 }
105
106 TEST_F(ParcelCppTest, WriteUInt16_AND_ReadUInt16) {
107   GetHandle().WriteUInt16(1);
108   uint16_t val = 0;
109   int ret = GetHandle().ReadUInt16(&val);
110   ASSERT_EQ(ret, Parcel::Error::None);
111   ASSERT_EQ(val, 1);
112 }
113
114 TEST_F(ParcelCppTest, WriteUInt32_AND_ReadUInt32) {
115   GetHandle().WriteUInt32(1);
116   uint32_t val = 0;
117   int ret = GetHandle().ReadUInt32(&val);
118   ASSERT_EQ(ret, Parcel::Error::None);
119   ASSERT_EQ(val, 1);
120 }
121
122 TEST_F(ParcelCppTest, WriteUInt64_AND_ReadUInt64) {
123   GetHandle().WriteUInt64(1);
124   uint64_t val = 0;
125   int ret = GetHandle().ReadUInt64(&val);
126   ASSERT_EQ(ret, Parcel::Error::None);
127   ASSERT_EQ(val, 1);
128 }
129
130 TEST_F(ParcelCppTest, WriteInt16_AND_ReadInt16) {
131   GetHandle().WriteInt16(1);
132   int16_t val = 0;
133   int ret = GetHandle().ReadInt16(&val);
134   ASSERT_EQ(ret, Parcel::Error::None);
135   ASSERT_EQ(val, 1);
136 }
137
138 TEST_F(ParcelCppTest, WriteInt32_AND_ReadInt32) {
139   GetHandle().WriteInt32(1);
140   int32_t val = 0;
141   int ret = GetHandle().ReadInt32(&val);
142   ASSERT_EQ(ret, Parcel::Error::None);
143   ASSERT_EQ(val, 1);
144 }
145
146 TEST_F(ParcelCppTest, WriteInt64_AND_ReadInt64) {
147   GetHandle().WriteInt64(1);
148   int64_t val = 0;
149   int ret = GetHandle().ReadInt64(&val);
150   ASSERT_EQ(ret, Parcel::Error::None);
151   ASSERT_EQ(val, 1);
152 }
153
154 TEST_F(ParcelCppTest, WriteFloat_AND_ReadFloat) {
155   GetHandle().WriteFloat(0.1f);
156   float val = 0.0f;
157   int ret = GetHandle().ReadFloat(&val);
158   ASSERT_EQ(ret, Parcel::Error::None);
159   ASSERT_EQ(val, 0.1f);
160 }
161
162 TEST_F(ParcelCppTest, WriteDouble_AND_ReadDouble) {
163   GetHandle().WriteDouble(0.1f);
164   double val = 0.0f;
165   int ret = GetHandle().ReadDouble(&val);
166   ASSERT_EQ(ret, Parcel::Error::None);
167   ASSERT_EQ(val, 0.1f);
168 }
169
170 TEST_F(ParcelCppTest, WriteString_AND_ReadString) {
171   GetHandle().WriteString("TestString");
172   auto val = GetHandle().ReadString();
173   ASSERT_EQ(get_last_result(), Parcel::Error::None);
174   ASSERT_EQ(val, "TestString");
175 }
176
177 TEST_F(ParcelCppTest, WriteCString_AND_ReadCString) {
178   GetHandle().WriteCString("TestCString");
179   char* str = nullptr;
180   int ret = GetHandle().ReadCString(&str);
181   auto ptr = std::unique_ptr<char, decltype(std::free)*>(str, std::free);
182   ASSERT_EQ(ret, Parcel::Error::None);
183   ASSERT_NE(str, nullptr);
184   ASSERT_EQ(std::string(str), "TestCString");
185 }
186
187 TEST_F(ParcelCppTest, ResetReader) {
188   GetHandle().WriteInt32(1);
189   GetHandle().WriteInt32(2);
190   int32_t val = 0;
191   int ret = GetHandle().ReadInt32(&val);
192   ASSERT_EQ(ret, Parcel::Error::None);
193   ASSERT_EQ(val, 1);
194   GetHandle().ResetReader();
195   val = 0;
196   ret = GetHandle().ReadInt32(&val);
197   ASSERT_EQ(ret, Parcel::Error::None);
198   ASSERT_EQ(val, 1);
199 }
200
201 TEST_F(ParcelCppTest, Clear) {
202   GetHandle().WriteInt32(1);
203   GetHandle().Clear();
204   int32_t val = 0;
205   int ret = GetHandle().ReadInt32(&val);
206   ASSERT_NE(ret, Parcel::Error::None);
207   ASSERT_NE(val, 1);
208 }
209
210 TEST_F(ParcelCppTest, Reset) {
211   GetHandle().WriteByte(1);
212   char buf[1024] = { 'P', };
213   GetHandle().Reset(buf, sizeof(buf));
214   char read_buf[1024] = { 0, };
215   int ret = GetHandle().Read(read_buf, sizeof(read_buf));
216   ASSERT_EQ(ret, Parcel::Error::None);
217   ASSERT_EQ(buf[0], read_buf[0]);
218 }
219
220 TEST_F(ParcelCppTest, WriteParcelable_AND_ReadParcelable) {
221   Student s1("s1", 8);
222   GetHandle().WriteParcelable(s1);
223   Student s2;
224   GetHandle().ReadParcelable(&s2);
225   ASSERT_EQ(s1.GetName(), s2.GetName());
226   ASSERT_EQ(s1.GetAge(), s2.GetAge());
227 }
228
229 TEST_F(ParcelCppTest, IsEmpty) {
230   ASSERT_EQ(GetHandle().IsEmpty(), true);
231   GetHandle().WriteInt32(1);
232   ASSERT_EQ(GetHandle().IsEmpty(), false);
233 }
234
235 TEST_F(ParcelCppTest, SetByteOrder) {
236   GetHandle().SetByteOrder(true);
237   GetHandle().WriteInt32(1);
238   int32_t val = 0;
239   int ret = GetHandle().ReadInt32(&val);
240   ASSERT_EQ(ret, Parcel::Error::None);
241   ASSERT_EQ(val, 1);
242 }
243
244 TEST_F(ParcelCppTest, GetData) {
245   uint8_t* data = GetHandle().GetData();
246   ASSERT_NE(data, nullptr);
247 }
248
249 TEST_F(ParcelCppTest, GetDataSize) {
250   GetHandle().WriteInt32(1);
251   ASSERT_EQ(GetHandle().GetDataSize(), sizeof(int32_t));
252   GetHandle().WriteInt32(2);
253   ASSERT_EQ(GetHandle().GetDataSize(), sizeof(int32_t) + sizeof(int32_t));
254 }
255
256 TEST_F(ParcelCppTest, GetDataCapacity) {
257   size_t size = GetHandle().GetDataCapacity();
258   ASSERT_NE(size, 0);
259 }
260
261 TEST_F(ParcelCppTest, SetDataCapacity) {
262   GetHandle().SetDataCapacity(128);
263   size_t size = GetHandle().GetDataCapacity();
264   ASSERT_EQ(size, 128);
265 }
266
267 TEST_F(ParcelCppTest, Detach) {
268   GetHandle().WriteInt32(1);
269   size_t data_size = 0;
270   uint8_t* data = GetHandle().Detach(&data_size);
271   std::unique_ptr<uint8_t, decltype(std::free)*> data_auto(data, std::free);
272   ASSERT_NE(data, nullptr);
273   ASSERT_EQ(data_size, sizeof(int32_t));
274 }
275
276 TEST_F(ParcelCppTest, GetReader) {
277   GetHandle().WriteInt32(1);
278   GetHandle().WriteString("GetReader");
279   int32_t value = 0;
280   GetHandle().ReadInt32(&value);
281   uint32_t reader = GetHandle().GetReader();
282   ASSERT_EQ(reader, sizeof(int32_t));
283 }
284
285 TEST_F(ParcelCppTest, ToRaw) {
286   GetHandle().WriteInt32(0);
287   auto raw = GetHandle().ToRaw();
288   ASSERT_EQ(raw.size(), sizeof(int32_t));
289 }