blob: 6ced7c2204e49ba1425da623bc744b5b517e55d7 [file] [log] [blame]
Christopher Wiley90be4e32015-10-20 14:55:25 -07001/*
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#include <memory>
19#include <vector>
20
21#include <gtest/gtest.h>
22
23#include "aidl.h"
24#include "aidl_language.h"
25#include "tests/fake_io_delegate.h"
26#include "type_cpp.h"
27#include "type_java.h"
28#include "type_namespace.h"
29
30using android::aidl::test::FakeIoDelegate;
31using std::string;
32using std::unique_ptr;
33
34namespace android {
35namespace aidl {
36
37class AidlTest : public ::testing::Test {
38 protected:
39 unique_ptr<AidlInterface> Parse(const string& path,
40 const string& contents,
41 TypeNamespace* types) {
42 FakeIoDelegate io_delegate;
43 io_delegate.SetFileContents(path, contents);
44 unique_ptr<AidlInterface> ret;
45 std::vector<std::unique_ptr<AidlImport>> imports;
46 ::android::aidl::internals::load_and_validate_aidl(
47 {}, // no preprocessed files
48 {}, // no import paths
49 path,
50 io_delegate,
51 types,
52 &ret,
53 &imports);
54 return ret;
55 }
56};
57
58TEST_F(AidlTest, JavaAcceptsMissingPackage) {
59 java::JavaTypeNamespace types;
60 EXPECT_NE(nullptr, Parse("IFoo.aidl", "interface IFoo { }", &types));
61}
62
63TEST_F(AidlTest, CppRejectsMissingPackage) {
64 cpp::TypeNamespace types;
65 EXPECT_EQ(nullptr, Parse("IFoo.aidl", "interface IFoo { }", &types));
66 EXPECT_NE(nullptr,
67 Parse("a/IFoo.aidl", "package a; interface IFoo { }", &types));
68}
69
70TEST_F(AidlTest, RejectsOnewayOutParameters) {
71 cpp::TypeNamespace cpp_types;
72 java::JavaTypeNamespace java_types;
73 string oneway_interface =
74 "package a; oneway interface IFoo { void f(out int bar); }";
75 string oneway_method =
76 "package a; interface IBar { oneway void f(out int bar); }";
77 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_interface, &cpp_types));
78 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_interface, &java_types));
79 EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_method, &cpp_types));
80 EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_method, &java_types));
81}
82
83TEST_F(AidlTest, RejectsOnewayNonVoidReturn) {
84 cpp::TypeNamespace cpp_types;
85 java::JavaTypeNamespace java_types;
86 string oneway_method = "package a; interface IFoo { oneway int f(); }";
87 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, &cpp_types));
88 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, &java_types));
89}
90
91TEST_F(AidlTest, AcceptsOneway) {
92 cpp::TypeNamespace cpp_types;
93 java::JavaTypeNamespace java_types;
94 string oneway_method = "package a; interface IFoo { oneway void f(int a); }";
95 string oneway_interface =
96 "package a; oneway interface IBar { void f(int a); }";
97 EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, &cpp_types));
98 EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, &java_types));
99 EXPECT_NE(nullptr, Parse("a/IBar.aidl", oneway_interface, &cpp_types));
100 EXPECT_NE(nullptr, Parse("a/IBar.aidl", oneway_interface, &java_types));
101}
102} // namespace aidl
103} // namespace android