1 /*
2  * Copyright (C) 2021 The Android Open Source Project
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 "tests/bad_parcelable.h"
18 
19 #include <android-base/stringprintf.h>
20 #include <binder/Parcel.h>
21 #include <utils/String8.h>
22 
23 using android::base::StringPrintf;
24 
25 namespace android {
26 namespace aidl {
27 namespace tests {
28 
BadParcelable(bool bad,const std::string & name,int32_t number)29 BadParcelable::BadParcelable(bool bad, const std::string& name, int32_t number)
30     : bad_(bad), name_(name.c_str(), name.length()), number_(number) {}
31 
writeToParcel(Parcel * parcel) const32 status_t BadParcelable::writeToParcel(Parcel* parcel) const {
33   if (auto status = parcel->writeBool(bad_); status != OK) return status;
34   if (auto status = parcel->writeString16(name_); status != OK) return status;
35   if (auto status = parcel->writeInt32(number_); status != OK) return status;
36   // BAD! write superfluous data
37   if (bad_) parcel->writeInt32(42);
38   return OK;
39 }
40 
readFromParcel(const Parcel * parcel)41 status_t BadParcelable::readFromParcel(const Parcel* parcel) {
42   if (auto status = parcel->readBool(&bad_); status != OK) return status;
43   if (auto status = parcel->readString16(&name_); status != OK) return status;
44   if (auto status = parcel->readInt32(&number_); status != OK) return status;
45   return OK;
46 }
47 
toString() const48 std::string BadParcelable::toString() const {
49   return StringPrintf("BadParcelable{bad=%d,name=%s,number=%d}", bad_, String8(name_).c_str(),
50                       number_);
51 }
52 
53 }  // namespace tests
54 }  // namespace aidl
55 }  // namespace android
56