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