1 // Copyright (C) 2018 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <android/os/IncidentReportArgs.h>
16 #include <incident/incident_report.h>
17 
18 #include <gtest/gtest.h>
19 
20 #include <vector>
21 #include <string>
22 
23 using namespace std;
24 using namespace android::os;
25 
26 class IncidentReportRequest {
27 public:
IncidentReportRequest()28     inline IncidentReportRequest() {
29         mImpl = AIncidentReportArgs_init();
30     }
31 
IncidentReportRequest(const IncidentReportRequest & that)32     inline IncidentReportRequest(const IncidentReportRequest& that) {
33         mImpl = AIncidentReportArgs_clone(that.mImpl);
34     }
35 
~IncidentReportRequest()36     inline ~IncidentReportRequest() {
37         AIncidentReportArgs_delete(mImpl);
38     }
39 
getImpl()40     inline AIncidentReportArgs* getImpl() {
41         return mImpl;
42     }
43 
setAll(bool all)44     inline void setAll(bool all) {
45         AIncidentReportArgs_setAll(mImpl, all);
46     }
47 
setPrivacyPolicy(int privacyPolicy)48     inline void setPrivacyPolicy(int privacyPolicy) {
49         AIncidentReportArgs_setPrivacyPolicy(mImpl, privacyPolicy);
50     }
51 
addSection(int section)52     inline void addSection(int section) {
53         AIncidentReportArgs_addSection(mImpl, section);
54     }
55 
setReceiverPackage(const string & pkg)56     inline void setReceiverPackage(const string& pkg) {
57         AIncidentReportArgs_setReceiverPackage(mImpl, pkg.c_str());
58     };
59 
setReceiverClass(const string & cls)60     inline void setReceiverClass(const string& cls) {
61         AIncidentReportArgs_setReceiverClass(mImpl, cls.c_str());
62     };
63 
addHeader(const vector<uint8_t> & headerProto)64     inline void addHeader(const vector<uint8_t>& headerProto) {
65         AIncidentReportArgs_addHeader(mImpl, headerProto.data(), headerProto.size());
66     };
67 
addHeader(const uint8_t * buf,size_t size)68     inline void addHeader(const uint8_t* buf, size_t size) {
69         AIncidentReportArgs_addHeader(mImpl, buf, size);
70     };
71 
72     // returns a status_t
takeReport()73     inline int takeReport() {
74         return AIncidentReportArgs_takeReport(mImpl);
75     }
76 
77 private:
78     AIncidentReportArgs* mImpl;
79 };
80 
81 
TEST(IncidentReportRequestTest,testWrite)82 TEST(IncidentReportRequestTest, testWrite) {
83     IncidentReportRequest request;
84     request.setAll(0);
85     request.addSection(1000);
86     request.addSection(1001);
87 
88     vector<uint8_t> header1;
89     header1.push_back(0x1);
90     header1.push_back(0x2);
91     vector<uint8_t> header2;
92     header1.push_back(0x22);
93     header1.push_back(0x33);
94 
95     request.addHeader(header1);
96     request.addHeader(header2);
97 
98     request.setPrivacyPolicy(1);
99 
100     request.setReceiverPackage("com.android.os");
101     request.setReceiverClass("com.android.os.Receiver");
102 
103     IncidentReportArgs* args = reinterpret_cast<IncidentReportArgs*>(request.getImpl());
104 
105     EXPECT_EQ(0, args->all());
106     set<int> sections;
107     sections.insert(1000);
108     sections.insert(1001);
109     EXPECT_EQ(sections, args->sections());
110     EXPECT_EQ(1, args->getPrivacyPolicy());
111 
112     EXPECT_EQ(string("com.android.os"), args->receiverPkg());
113     EXPECT_EQ(string("com.android.os.Receiver"), args->receiverCls());
114 
115     vector<vector<uint8_t>> headers;
116     headers.push_back(header1);
117     headers.push_back(header2);
118     EXPECT_EQ(headers, args->headers());
119 }
120 
121