generate_cpp_unittest.cpp revision 90be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6
1/* 2 * Copyright (C) 2015, 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 <string> 18 19#include <gtest/gtest.h> 20 21#include "aidl.h" 22#include "aidl_language.h" 23#include "ast_cpp.h" 24#include "code_writer.h" 25#include "generate_cpp.h" 26#include "tests/fake_io_delegate.h" 27#include "type_cpp.h" 28 29using android::aidl::test::FakeIoDelegate; 30using std::string; 31using std::unique_ptr; 32 33namespace android { 34namespace aidl { 35namespace cpp { 36namespace { 37 38const char kTrivialInterfaceAIDL[] = 39R"( 40package android.os; 41 42interface IPingResponder { 43 int Ping(String token); 44})"; 45 46const char kExpectedTrivialClientSourceOutput[] = 47R"(#include <android/os/BpPingResponder.h> 48#include <binder/Parcel.h> 49 50namespace android { 51 52namespace os { 53 54BpPingResponder::BpPingResponder(const android::sp<android::IBinder>& impl) 55 : BpInterface<IPingResponder>(impl){ 56} 57 58android::status_t BpPingResponder::Ping(android::String16 token, int32_t* _aidl_return) { 59android::Parcel data; 60android::Parcel reply; 61android::status_t status; 62status = data.writeString16(token); 63if (status != android::OK) { return status; } 64status = remote()->transact(IPingResponder::PING, data, &reply); 65if (status != android::OK) { return status; } 66status = reply.readInt32(_aidl_return); 67if (status != android::OK) { return status; } 68return status; 69} 70 71} // namespace os 72 73} // namespace android 74)"; 75 76const char kExpectedTrivialServerHeaderOutput[] = 77R"(#ifndef AIDL_GENERATED_ANDROID_OS_BN_PING_RESPONDER_H_ 78#define AIDL_GENERATED_ANDROID_OS_BN_PING_RESPONDER_H_ 79 80#include <binder/IInterface.h> 81#include <android/os/IPingResponder.h> 82 83namespace android { 84 85namespace os { 86 87class BnPingResponder : public android::BnInterface<IPingResponder> { 88public: 89android::status_t onTransact(uint32_t code, const android::Parcel& data, android::Parcel* reply, uint32_t flags = 0) override; 90}; // class BnPingResponder 91 92} // namespace os 93 94} // namespace android 95 96#endif // AIDL_GENERATED_ANDROID_OS_BN_PING_RESPONDER_H_)"; 97 98const char kExpectedTrivialServerSourceOutput[] = 99R"(#include <android/os/BnPingResponder.h> 100#include <binder/Parcel.h> 101 102namespace android { 103 104namespace os { 105 106android::status_t BnPingResponder::onTransact(uint32_t code, const android::Parcel& data, android::Parcel* reply, uint32_t flags) { 107android::status_t status; 108switch (code) { 109case Call::PING: 110{ 111android::String16 in_token; 112int32_t _aidl_return; 113status = data.readString16(&in_token); 114if (status != android::OK) { break; } 115status = Ping(in_token, &_aidl_return); 116if (status != android::OK) { break; } 117status = reply->writeInt32(_aidl_return); 118if (status != android::OK) { break; } 119} 120break; 121default: 122{ 123status = android::BBinder::onTransact(code, data, reply, flags); 124} 125break; 126} 127return status; 128} 129 130} // namespace os 131 132} // namespace android 133)"; 134 135const char kExpectedTrivialClientHeaderOutput[] = 136R"(#ifndef AIDL_GENERATED_ANDROID_OS_BP_PING_RESPONDER_H_ 137#define AIDL_GENERATED_ANDROID_OS_BP_PING_RESPONDER_H_ 138 139#include <binder/IBinder.h> 140#include <binder/IInterface.h> 141#include <utils/Errors.h> 142#include <android/os/IPingResponder.h> 143 144namespace android { 145 146namespace os { 147 148class BpPingResponder : public android::BpInterface<IPingResponder> { 149public: 150explicit BpPingResponder(const android::sp<android::IBinder>& impl); 151virtual ~BpPingResponder() = default; 152android::status_t Ping(android::String16 token, int32_t* _aidl_return) override; 153}; // class BpPingResponder 154 155} // namespace os 156 157} // namespace android 158 159#endif // AIDL_GENERATED_ANDROID_OS_BP_PING_RESPONDER_H_)"; 160 161const char kExpectedTrivialInterfaceHeaderOutput[] = 162R"(#ifndef AIDL_GENERATED_ANDROID_OS_I_PING_RESPONDER_H_ 163#define AIDL_GENERATED_ANDROID_OS_I_PING_RESPONDER_H_ 164 165#include <binder/IBinder.h> 166#include <binder/IInterface.h> 167#include <cstdint> 168#include <utils/String16.h> 169 170namespace android { 171 172namespace os { 173 174class IPingResponder : public android::IInterface { 175public: 176DECLARE_META_INTERFACE(PingResponder); 177virtual android::status_t Ping(android::String16 token, int32_t* _aidl_return) = 0; 178enum Call { 179 PING = android::IBinder::FIRST_CALL_TRANSACTION + 0, 180}; 181}; // class IPingResponder 182 183} // namespace os 184 185} // namespace android 186 187#endif // AIDL_GENERATED_ANDROID_OS_I_PING_RESPONDER_H_)"; 188 189const char kExpectedTrivialInterfaceSourceOutput[] = 190R"(#include <android/os/IPingResponder.h> 191#include <android/os/BpPingResponder.h> 192 193namespace android { 194 195namespace os { 196 197IMPLEMENT_META_INTERFACE(PingResponder, "android.os.IPingResponder"); 198 199} // namespace os 200 201} // namespace android 202)"; 203 204} // namespace 205 206class TrivialInterfaceASTTest : public ::testing::Test { 207 protected: 208 unique_ptr<AidlInterface> Parse() { 209 FakeIoDelegate io_delegate; 210 io_delegate.SetFileContents("android/os/IPingResponder.aidl", kTrivialInterfaceAIDL); 211 212 cpp::TypeNamespace types; 213 unique_ptr<AidlInterface> ret; 214 std::vector<std::unique_ptr<AidlImport>> imports; 215 int err = ::android::aidl::internals::load_and_validate_aidl( 216 {}, // no preprocessed files 217 {}, // no import paths 218 "android/os/IPingResponder.aidl", 219 io_delegate, 220 &types, 221 &ret, 222 &imports); 223 224 if (err) 225 return nullptr; 226 227 return ret; 228 } 229 230 void Compare(Document* doc, const char* expected) { 231 string output; 232 unique_ptr<CodeWriter> cw = GetStringWriter(&output); 233 234 doc->Write(cw.get()); 235 236 EXPECT_EQ(expected, output); 237 } 238}; 239 240TEST_F(TrivialInterfaceASTTest, GeneratesClientHeader) { 241 unique_ptr<AidlInterface> interface = Parse(); 242 ASSERT_NE(interface, nullptr); 243 TypeNamespace types; 244 unique_ptr<Document> doc = internals::BuildClientHeader(types, *interface); 245 Compare(doc.get(), kExpectedTrivialClientHeaderOutput); 246} 247 248TEST_F(TrivialInterfaceASTTest, GeneratesClientSource) { 249 unique_ptr<AidlInterface> interface = Parse(); 250 ASSERT_NE(interface, nullptr); 251 TypeNamespace types; 252 unique_ptr<Document> doc = internals::BuildClientSource(types, *interface); 253 Compare(doc.get(), kExpectedTrivialClientSourceOutput); 254} 255 256TEST_F(TrivialInterfaceASTTest, GeneratesServerHeader) { 257 unique_ptr<AidlInterface> interface = Parse(); 258 ASSERT_NE(interface, nullptr); 259 TypeNamespace types; 260 unique_ptr<Document> doc = internals::BuildServerHeader(types, *interface); 261 Compare(doc.get(), kExpectedTrivialServerHeaderOutput); 262} 263 264TEST_F(TrivialInterfaceASTTest, GeneratesServerSource) { 265 unique_ptr<AidlInterface> interface = Parse(); 266 ASSERT_NE(interface, nullptr); 267 TypeNamespace types; 268 unique_ptr<Document> doc = internals::BuildServerSource(types, *interface); 269 Compare(doc.get(), kExpectedTrivialServerSourceOutput); 270} 271 272TEST_F(TrivialInterfaceASTTest, GeneratesInterfaceHeader) { 273 unique_ptr<AidlInterface> interface = Parse(); 274 ASSERT_NE(interface, nullptr); 275 TypeNamespace types; 276 unique_ptr<Document> doc = internals::BuildInterfaceHeader(types, *interface); 277 Compare(doc.get(), kExpectedTrivialInterfaceHeaderOutput); 278} 279 280TEST_F(TrivialInterfaceASTTest, GeneratesInterfaceSource) { 281 unique_ptr<AidlInterface> interface = Parse(); 282 ASSERT_NE(interface, nullptr); 283 TypeNamespace types; 284 unique_ptr<Document> doc = internals::BuildInterfaceSource(types, *interface); 285 Compare(doc.get(), kExpectedTrivialInterfaceSourceOutput); 286} 287 288} // namespace cpp 289} // namespace aidl 290} // namespace android 291