• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
27 using android::aidl::AidlError;
28 using android::aidl::AidlTypenames;
29 using android::aidl::DiagnosticID;
30 using android::aidl::Options;
31 using android::aidl::internals::load_and_validate_aidl;
32 using android::aidl::test::FakeIoDelegate;
33 using testing::internal::CaptureStderr;
34 using testing::internal::GetCapturedStderr;
35 
36 struct DiagnosticsTest : testing::Test {
ParseFilesDiagnosticsTest37   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()
45     const Options options =
46         Options::From("aidl " + optional_args + " -I . --lang java -o out -Weverything " + main);
47     CaptureStderr();
48     load_and_validate_aidl(main, options, io, &typenames, nullptr);
49     const std::string err = GetCapturedStderr();
50     if (expect_diagnostics.empty()) {
51       EXPECT_EQ("", err);
52     } else {
53       for (const auto id : expect_diagnostics) {
54         EXPECT_THAT(err, testing::HasSubstr("-W" + to_string(id)));
55       }
56     }
57   }
58 
59   AidlTypenames typenames;
60   FakeIoDelegate io;
61   std::string optional_args;
62   std::vector<DiagnosticID> expect_diagnostics;
63 };
64 
TEST_F(DiagnosticsTest,const_name_ForEnumerator)65 TEST_F(DiagnosticsTest, const_name_ForEnumerator) {
66   expect_diagnostics = {DiagnosticID::const_name};
67   ParseFiles({{"Foo.aidl", "enum Foo { foo }"}});
68 }
69 
TEST_F(DiagnosticsTest,const_name_ForConstants)70 TEST_F(DiagnosticsTest, const_name_ForConstants) {
71   expect_diagnostics = {DiagnosticID::const_name};
72   ParseFiles({{"IFoo.aidl", "interface IFoo { const int foo = 1; }"}});
73 }
74 
TEST_F(DiagnosticsTest,interface_name)75 TEST_F(DiagnosticsTest, interface_name) {
76   expect_diagnostics = {DiagnosticID::interface_name};
77   ParseFiles({{"Foo.aidl", "interface Foo { }"}});
78 }
79 
TEST_F(DiagnosticsTest,enum_explicit_default)80 TEST_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 
TEST_F(DiagnosticsTest,inout_parameter)85 TEST_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 }
90 
TEST_F(DiagnosticsTest,inout_parameter_SuppressAtMethodLevel)91 TEST_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 
TEST_F(DiagnosticsTest,inout_parameter_SuppressAtDeclLevel)100 TEST_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 
TEST_F(DiagnosticsTest,UnknownWarning)109 TEST_F(DiagnosticsTest, UnknownWarning) {
110   expect_diagnostics = {DiagnosticID::unknown_warning};
111   ParseFiles({
112       {"IFoo.aidl", "@SuppressWarnings(value={\"blahblah\"}) interface IFoo { void foo(); }"},
113   });
114 }
115 
TEST_F(DiagnosticsTest,CantSuppressUnknownWarning)116 TEST_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 }
124 
TEST_F(DiagnosticsTest,DontMixOnewayWithTwowayMethods)125 TEST_F(DiagnosticsTest, DontMixOnewayWithTwowayMethods) {
126   expect_diagnostics = {DiagnosticID::mixed_oneway};
127   ParseFiles({
128       {"IFoo.aidl", "interface IFoo { void foo(); oneway void bar(); }"},
129   });
130 }
131 
TEST_F(DiagnosticsTest,OnewayInterfaceIsOkayWithSyntheticMethods)132 TEST_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 
TEST_F(DiagnosticsTest,ArraysAsOutputParametersConsideredHarmful)140 TEST_F(DiagnosticsTest, ArraysAsOutputParametersConsideredHarmful) {
141   expect_diagnostics = {DiagnosticID::out_array};
142   ParseFiles({
143       {"IFoo.aidl", "interface IFoo { void foo(out String[] ret); }"},
144   });
145 }
146 
TEST_F(DiagnosticsTest,file_descriptor)147 TEST_F(DiagnosticsTest, file_descriptor) {
148   expect_diagnostics = {DiagnosticID::file_descriptor};
149   ParseFiles({{"IFoo.aidl",
150                "interface IFoo {\n"
151                "  void foo(in FileDescriptor fd);\n"
152                "}"}});
153 }
154 
TEST_F(DiagnosticsTest,out_nullable)155 TEST_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 
TEST_F(DiagnosticsTest,inout_nullable)164 TEST_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 
TEST_F(DiagnosticsTest,out_nullable_OkayForArrays)173 TEST_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 }
181