aidl_unittest.cpp revision 90be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6
190be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley/* 290be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley * Copyright (C) 2015, The Android Open Source Project 390be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley * 490be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley * Licensed under the Apache License, Version 2.0 (the "License"); 590be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley * you may not use this file except in compliance with the License. 690be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley * You may obtain a copy of the License at 790be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley * 890be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley * http://www.apache.org/licenses/LICENSE-2.0 990be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley * 1090be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley * Unless required by applicable law or agreed to in writing, software 1190be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley * distributed under the License is distributed on an "AS IS" BASIS, 1290be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1390be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley * See the License for the specific language governing permissions and 1490be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley * limitations under the License. 1590be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley */ 1690be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley 1790be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley#include <string> 1890be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley#include <memory> 1990be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley#include <vector> 2090be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley 2190be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley#include <gtest/gtest.h> 2290be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley 2390be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley#include "aidl.h" 2490be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley#include "aidl_language.h" 2590be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley#include "tests/fake_io_delegate.h" 2690be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley#include "type_cpp.h" 2790be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley#include "type_java.h" 2890be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley#include "type_namespace.h" 2990be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley 3090be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wileyusing android::aidl::test::FakeIoDelegate; 3190be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wileyusing std::string; 3290be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wileyusing std::unique_ptr; 3390be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley 3490be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wileynamespace android { 3590be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wileynamespace aidl { 3690be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley 3790be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wileyclass AidlTest : public ::testing::Test { 3890be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley protected: 3990be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley unique_ptr<AidlInterface> Parse(const string& path, 4090be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley const string& contents, 4190be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley TypeNamespace* types) { 4290be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley FakeIoDelegate io_delegate; 4390be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley io_delegate.SetFileContents(path, contents); 4490be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley unique_ptr<AidlInterface> ret; 4590be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley std::vector<std::unique_ptr<AidlImport>> imports; 4690be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley ::android::aidl::internals::load_and_validate_aidl( 4790be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley {}, // no preprocessed files 4890be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley {}, // no import paths 4990be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley path, 5090be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley io_delegate, 5190be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley types, 5290be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley &ret, 5390be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley &imports); 5490be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley return ret; 5590be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley } 5690be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley}; 5790be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley 5890be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher WileyTEST_F(AidlTest, JavaAcceptsMissingPackage) { 5990be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley java::JavaTypeNamespace types; 6090be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley EXPECT_NE(nullptr, Parse("IFoo.aidl", "interface IFoo { }", &types)); 6190be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley} 6290be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley 6390be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher WileyTEST_F(AidlTest, CppRejectsMissingPackage) { 6490be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley cpp::TypeNamespace types; 6590be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley EXPECT_EQ(nullptr, Parse("IFoo.aidl", "interface IFoo { }", &types)); 6690be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley EXPECT_NE(nullptr, 6790be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley Parse("a/IFoo.aidl", "package a; interface IFoo { }", &types)); 6890be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley} 6990be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley 7090be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher WileyTEST_F(AidlTest, RejectsOnewayOutParameters) { 7190be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley cpp::TypeNamespace cpp_types; 7290be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley java::JavaTypeNamespace java_types; 7390be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley string oneway_interface = 7490be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley "package a; oneway interface IFoo { void f(out int bar); }"; 7590be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley string oneway_method = 7690be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley "package a; interface IBar { oneway void f(out int bar); }"; 7790be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_interface, &cpp_types)); 7890be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_interface, &java_types)); 7990be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_method, &cpp_types)); 8090be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_method, &java_types)); 8190be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley} 8290be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley 8390be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher WileyTEST_F(AidlTest, RejectsOnewayNonVoidReturn) { 8490be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley cpp::TypeNamespace cpp_types; 8590be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley java::JavaTypeNamespace java_types; 8690be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley string oneway_method = "package a; interface IFoo { oneway int f(); }"; 8790be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, &cpp_types)); 8890be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, &java_types)); 8990be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley} 9090be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley 9190be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher WileyTEST_F(AidlTest, AcceptsOneway) { 9290be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley cpp::TypeNamespace cpp_types; 9390be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley java::JavaTypeNamespace java_types; 9490be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley string oneway_method = "package a; interface IFoo { oneway void f(int a); }"; 9590be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley string oneway_interface = 9690be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley "package a; oneway interface IBar { void f(int a); }"; 9790be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, &cpp_types)); 9890be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, &java_types)); 9990be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley EXPECT_NE(nullptr, Parse("a/IBar.aidl", oneway_interface, &cpp_types)); 10090be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley EXPECT_NE(nullptr, Parse("a/IBar.aidl", oneway_interface, &java_types)); 10190be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley} 10290be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley} // namespace aidl 10390be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley} // namespace android 104