2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
23 #include "parcel/parcel.hh"
25 using ::testing::AtLeast;
26 using namespace tizen_base;
28 class ParcelCppTest : public ::testing::Test {
30 virtual void SetUp() {
33 virtual void TearDown() {
45 class Student : public Parcelable {
47 Student(std::string name, uint32_t age)
48 : name_(std::move(name)), age_(age) {
52 virtual ~Student() = default;
54 const std::string& GetName() const {
58 uint32_t GetAge() const {
62 virtual void WriteToParcel(Parcel* parcel) const {
63 parcel->WriteString(name_);
64 parcel->WriteUInt32(age_);
67 virtual void ReadFromParcel(Parcel* parcel) {
68 name_ = parcel->ReadString();
69 parcel->ReadUInt32(&age_);
77 TEST_F(ParcelCppTest, Ctor_AND_Dtor) {
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]);
90 TEST_F(ParcelCppTest, WriteBool_AND_ReadBool) {
91 GetHandle().WriteBool(true);
93 int ret = GetHandle().ReadBool(&val);
94 ASSERT_EQ(ret, Parcel::Error::None);
98 TEST_F(ParcelCppTest, WriteByte_AND_ReadByte) {
99 GetHandle().WriteByte(1);
101 int ret = GetHandle().ReadByte(&val);
102 ASSERT_EQ(ret, Parcel::Error::None);
106 TEST_F(ParcelCppTest, WriteUInt16_AND_ReadUInt16) {
107 GetHandle().WriteUInt16(1);
109 int ret = GetHandle().ReadUInt16(&val);
110 ASSERT_EQ(ret, Parcel::Error::None);
114 TEST_F(ParcelCppTest, WriteUInt32_AND_ReadUInt32) {
115 GetHandle().WriteUInt32(1);
117 int ret = GetHandle().ReadUInt32(&val);
118 ASSERT_EQ(ret, Parcel::Error::None);
122 TEST_F(ParcelCppTest, WriteUInt64_AND_ReadUInt64) {
123 GetHandle().WriteUInt64(1);
125 int ret = GetHandle().ReadUInt64(&val);
126 ASSERT_EQ(ret, Parcel::Error::None);
130 TEST_F(ParcelCppTest, WriteInt16_AND_ReadInt16) {
131 GetHandle().WriteInt16(1);
133 int ret = GetHandle().ReadInt16(&val);
134 ASSERT_EQ(ret, Parcel::Error::None);
138 TEST_F(ParcelCppTest, WriteInt32_AND_ReadInt32) {
139 GetHandle().WriteInt32(1);
141 int ret = GetHandle().ReadInt32(&val);
142 ASSERT_EQ(ret, Parcel::Error::None);
146 TEST_F(ParcelCppTest, WriteInt64_AND_ReadInt64) {
147 GetHandle().WriteInt64(1);
149 int ret = GetHandle().ReadInt64(&val);
150 ASSERT_EQ(ret, Parcel::Error::None);
154 TEST_F(ParcelCppTest, WriteFloat_AND_ReadFloat) {
155 GetHandle().WriteFloat(0.1f);
157 int ret = GetHandle().ReadFloat(&val);
158 ASSERT_EQ(ret, Parcel::Error::None);
159 ASSERT_EQ(val, 0.1f);
162 TEST_F(ParcelCppTest, WriteDouble_AND_ReadDouble) {
163 GetHandle().WriteDouble(0.1f);
165 int ret = GetHandle().ReadDouble(&val);
166 ASSERT_EQ(ret, Parcel::Error::None);
167 ASSERT_EQ(val, 0.1f);
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");
177 TEST_F(ParcelCppTest, WriteCString_AND_ReadCString) {
178 GetHandle().WriteCString("TestCString");
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");
187 TEST_F(ParcelCppTest, ResetReader) {
188 GetHandle().WriteInt32(1);
189 GetHandle().WriteInt32(2);
191 int ret = GetHandle().ReadInt32(&val);
192 ASSERT_EQ(ret, Parcel::Error::None);
194 GetHandle().ResetReader();
196 ret = GetHandle().ReadInt32(&val);
197 ASSERT_EQ(ret, Parcel::Error::None);
201 TEST_F(ParcelCppTest, Clear) {
202 GetHandle().WriteInt32(1);
205 int ret = GetHandle().ReadInt32(&val);
206 ASSERT_NE(ret, Parcel::Error::None);
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]);
220 TEST_F(ParcelCppTest, WriteParcelable_AND_ReadParcelable) {
222 GetHandle().WriteParcelable(s1);
224 GetHandle().ReadParcelable(&s2);
225 ASSERT_EQ(s1.GetName(), s2.GetName());
226 ASSERT_EQ(s1.GetAge(), s2.GetAge());
229 TEST_F(ParcelCppTest, IsEmpty) {
230 ASSERT_EQ(GetHandle().IsEmpty(), true);
231 GetHandle().WriteInt32(1);
232 ASSERT_EQ(GetHandle().IsEmpty(), false);
235 TEST_F(ParcelCppTest, SetByteOrder) {
236 GetHandle().SetByteOrder(true);
237 GetHandle().WriteInt32(1);
239 int ret = GetHandle().ReadInt32(&val);
240 ASSERT_EQ(ret, Parcel::Error::None);
244 TEST_F(ParcelCppTest, GetData) {
245 uint8_t* data = GetHandle().GetData();
246 ASSERT_NE(data, nullptr);
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));
256 TEST_F(ParcelCppTest, GetDataCapacity) {
257 size_t size = GetHandle().GetDataCapacity();
261 TEST_F(ParcelCppTest, SetDataCapacity) {
262 GetHandle().SetDataCapacity(128);
263 size_t size = GetHandle().GetDataCapacity();
264 ASSERT_EQ(size, 128);
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));
276 TEST_F(ParcelCppTest, GetReader) {
277 GetHandle().WriteInt32(1);
278 GetHandle().WriteString("GetReader");
280 GetHandle().ReadInt32(&value);
281 uint32_t reader = GetHandle().GetReader();
282 ASSERT_EQ(reader, sizeof(int32_t));
285 TEST_F(ParcelCppTest, ToRaw) {
286 GetHandle().WriteInt32(0);
287 auto raw = GetHandle().ToRaw();
288 ASSERT_EQ(raw.size(), sizeof(int32_t));