1 /*
2 * Copyright (C) 2018 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 <binder/request_id.h>
18
19 #include <gtest/gtest.h>
20
21 using namespace iorap::binder; // NOLINT
22
23 using android::Parcel;
24
TEST(AutoParcelable,RequestIdRoundTrip)25 TEST(AutoParcelable, RequestIdRoundTrip) {
26 RequestId typical;
27 typical.request_id = 0xC0FFEE;
28
29 static constexpr auto NO_ERROR = android::NO_ERROR;
30 static constexpr int COMPARED_SAME = 0;
31
32 {
33 Parcel p1, p2;
34
35 p1.writeInt64(0x1234);
36 p2.writeInt64(0x1234);
37
38 EXPECT_EQ(COMPARED_SAME, p1.compareData(p2));
39 }
40
41 {
42 Parcel parcel_typical;
43
44 EXPECT_EQ(NO_ERROR, typical.writeToParcel(&parcel_typical));
45
46 Parcel parcel_manual;
47
48 parcel_manual.writeInt64(typical.request_id);
49
50 EXPECT_EQ(COMPARED_SAME, parcel_manual.compareData(parcel_typical));
51
52 parcel_typical.setDataPosition(0);
53 EXPECT_EQ(typical.request_id, parcel_typical.readInt64());
54 // Test that the 'writeToParcel' works correctly.
55 }
56
57 {
58 // Test that the 'readFromParcel' works correctly.
59 RequestId copy;
60 copy.request_id = 0;
61
62 Parcel parcel_typical;
63 parcel_typical.writeInt64(typical.request_id);
64 parcel_typical.setDataPosition(0);
65
66 EXPECT_EQ(NO_ERROR, copy.readFromParcel(&parcel_typical));
67 EXPECT_EQ(typical.request_id, copy.request_id);
68 }
69
70 {
71 // Test that write and then read from parcel works correctly.
72 RequestId copy;
73 copy.request_id = 0;
74
75 Parcel parcel{};
76
77 EXPECT_EQ(NO_ERROR, typical.writeToParcel(&parcel));
78 parcel.setDataPosition(0); // rewind after write before read.
79 EXPECT_EQ(NO_ERROR, copy.readFromParcel(&parcel));
80
81 // for each field, they should be equal.
82 EXPECT_EQ(typical.request_id, copy.request_id);
83 }
84 }
85