blob: 0ea3141db79146a93d2136fd91cfc0b5810cda93 [file] [log] [blame]
Jooyung Han3557e482020-12-24 05:11:15 +09001/*
2 * Copyright (C) 2020, 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#include "diagnostics.h"
17
18#include <gmock/gmock.h>
19#include <gtest/gtest.h>
20#include <string>
21#include <vector>
22
23#include "aidl.h"
24#include "parser.h"
25#include "tests/fake_io_delegate.h"
26
27using android::aidl::AidlError;
28using android::aidl::AidlTypenames;
29using android::aidl::DiagnosticID;
Jooyung Han3557e482020-12-24 05:11:15 +090030using android::aidl::Options;
31using android::aidl::internals::load_and_validate_aidl;
32using android::aidl::test::FakeIoDelegate;
33using testing::internal::CaptureStderr;
34using testing::internal::GetCapturedStderr;
35
36struct DiagnosticsTest : testing::Test {
37 void ParseFiles(std::vector<std::pair<std::string, std::string>>&& files) {
38 ASSERT_TRUE(files.size() > 0);
39 const std::string main = files.begin()->first;
40 for (const auto& [file, contents] : files) {
41 io.SetFileContents(file, contents);
42 }
43 // emit diagnostics as warnings.
44 // "java" has no specific meaning here because we're testing CheckValid()
Jooyung Han1d78a362021-02-10 14:01:07 +090045 const Options options =
46 Options::From("aidl " + optional_args + " -I . --lang java -o out -Weverything " + main);
Jooyung Han3557e482020-12-24 05:11:15 +090047 CaptureStderr();
Jiyong Parkd9113322020-12-31 03:20:33 +090048 load_and_validate_aidl(main, options, io, &typenames, nullptr);
Jooyung Han3557e482020-12-24 05:11:15 +090049 const std::string err = GetCapturedStderr();
50 if (expect_diagnostics.empty()) {
51 EXPECT_EQ("", err);
52 } else {
53 for (const auto id : expect_diagnostics) {
Jooyung Han7be55792020-12-29 14:50:13 +090054 EXPECT_THAT(err, testing::HasSubstr("-W" + to_string(id)));
Jooyung Han3557e482020-12-24 05:11:15 +090055 }
56 }
57 }
58
59 AidlTypenames typenames;
60 FakeIoDelegate io;
Jooyung Han1d78a362021-02-10 14:01:07 +090061 std::string optional_args;
Jooyung Han3557e482020-12-24 05:11:15 +090062 std::vector<DiagnosticID> expect_diagnostics;
63};
64
Jooyung Hanfe298df2020-12-27 02:13:29 +090065TEST_F(DiagnosticsTest, const_name_ForEnumerator) {
66 expect_diagnostics = {DiagnosticID::const_name};
67 ParseFiles({{"Foo.aidl", "enum Foo { foo }"}});
68}
69
70TEST_F(DiagnosticsTest, const_name_ForConstants) {
71 expect_diagnostics = {DiagnosticID::const_name};
72 ParseFiles({{"IFoo.aidl", "interface IFoo { const int foo = 1; }"}});
73}
74
Jooyung Han3557e482020-12-24 05:11:15 +090075TEST_F(DiagnosticsTest, interface_name) {
76 expect_diagnostics = {DiagnosticID::interface_name};
77 ParseFiles({{"Foo.aidl", "interface Foo { }"}});
78}
79
Jooyung Han7ab35ff2021-01-23 10:34:02 +090080TEST_F(DiagnosticsTest, enum_explicit_default) {
81 expect_diagnostics = {DiagnosticID::enum_explicit_default};
82 ParseFiles({{"Foo.aidl", "parcelable Foo { E e; }"}, {"E.aidl", "enum E { A }"}});
83}
84
Jooyung Han3557e482020-12-24 05:11:15 +090085TEST_F(DiagnosticsTest, inout_parameter) {
86 expect_diagnostics = {DiagnosticID::inout_parameter};
87 ParseFiles({{"IFoo.aidl", "interface IFoo { void foo(inout Bar bar); }"},
88 {"Bar.aidl", "parcelable Bar {}"}});
89}
Jiyong Parkd9113322020-12-31 03:20:33 +090090
91TEST_F(DiagnosticsTest, inout_parameter_SuppressAtMethodLevel) {
92 expect_diagnostics = {};
93 ParseFiles({
94 {"IFoo.aidl",
95 "interface IFoo { @SuppressWarnings(value={\"inout-parameter\"}) void foo(inout Bar b); }"},
96 {"Bar.aidl", "parcelable Bar {}"},
97 });
98}
99
100TEST_F(DiagnosticsTest, inout_parameter_SuppressAtDeclLevel) {
101 expect_diagnostics = {};
102 ParseFiles({
103 {"IFoo.aidl",
104 "@SuppressWarnings(value={\"inout-parameter\"}) interface IFoo { void foo(inout Bar b); }"},
105 {"Bar.aidl", "parcelable Bar {}"},
106 });
107}
108
109TEST_F(DiagnosticsTest, UnknownWarning) {
110 expect_diagnostics = {DiagnosticID::unknown_warning};
111 ParseFiles({
112 {"IFoo.aidl", "@SuppressWarnings(value={\"blahblah\"}) interface IFoo { void foo(); }"},
113 });
114}
115
116TEST_F(DiagnosticsTest, CantSuppressUnknownWarning) {
117 expect_diagnostics = {DiagnosticID::unknown_warning};
118 ParseFiles({
119 {"IFoo.aidl",
120 "@SuppressWarnings(value={\"unknown-warning\"})\n"
121 "interface IFoo { @SuppressWarnings(value={\"blah-blah\"}) void foo(); }"},
122 });
123}
Jooyung Han209c2862020-12-31 10:59:43 +0900124
125TEST_F(DiagnosticsTest, DontMixOnewayWithTwowayMethods) {
126 expect_diagnostics = {DiagnosticID::mixed_oneway};
127 ParseFiles({
128 {"IFoo.aidl", "interface IFoo { void foo(); oneway void bar(); }"},
129 });
Jooyung Hanc9b0c802020-12-31 11:50:21 +0900130}
131
Jooyung Han1d78a362021-02-10 14:01:07 +0900132TEST_F(DiagnosticsTest, OnewayInterfaceIsOkayWithSyntheticMethods) {
133 optional_args = "--version 2"; // will add getInterfaceVersion() synthetic method
134 expect_diagnostics = {};
135 ParseFiles({
136 {"IFoo.aidl", "oneway interface IFoo { void foo(); }"},
137 });
138}
139
Jooyung Hanc9b0c802020-12-31 11:50:21 +0900140TEST_F(DiagnosticsTest, ArraysAsOutputParametersConsideredHarmful) {
141 expect_diagnostics = {DiagnosticID::out_array};
142 ParseFiles({
143 {"IFoo.aidl", "interface IFoo { void foo(out String[] ret); }"},
144 });
Jooyung Han377795f2021-01-03 11:37:55 +0900145}
146
147TEST_F(DiagnosticsTest, file_descriptor) {
148 expect_diagnostics = {DiagnosticID::file_descriptor};
149 ParseFiles({{"IFoo.aidl",
Jooyung Hanecfd2882021-03-02 16:52:36 +0900150 "interface IFoo {\n"
Jooyung Han377795f2021-01-03 11:37:55 +0900151 " void foo(in FileDescriptor fd);\n"
152 "}"}});
153}
Jooyung Hanecfd2882021-03-02 16:52:36 +0900154
155TEST_F(DiagnosticsTest, out_nullable) {
156 expect_diagnostics = {DiagnosticID::out_nullable};
157 ParseFiles({{"IFoo.aidl",
158 "interface IFoo {\n"
159 " void foo(out @nullable Bar bar);\n"
160 "}"},
161 {"Bar.aidl", "parcelable Bar {}"}});
162}
163
164TEST_F(DiagnosticsTest, inout_nullable) {
165 expect_diagnostics = {DiagnosticID::out_nullable};
166 ParseFiles({{"IFoo.aidl",
167 "interface IFoo {\n"
168 " void foo(inout @nullable Bar bar);\n"
169 "}"},
170 {"Bar.aidl", "parcelable Bar {}"}});
171}
172
173TEST_F(DiagnosticsTest, out_nullable_OkayForArrays) {
174 expect_diagnostics = {DiagnosticID::out_array}; // not triggering out_nullable
175 ParseFiles({{"IFoo.aidl",
176 "interface IFoo {\n"
177 " void foo(inout @nullable Bar[] bar1, out @nullable Bar[] bar2);\n"
178 "}"},
179 {"Bar.aidl", "parcelable Bar {}"}});
180}