Jooyung Han | 3557e48 | 2020-12-24 05:11:15 +0900 | [diff] [blame] | 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; |
Jooyung Han | 3557e48 | 2020-12-24 05:11:15 +0900 | [diff] [blame] | 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 { |
| 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 Han | 1d78a36 | 2021-02-10 14:01:07 +0900 | [diff] [blame] | 45 | const Options options = |
| 46 | Options::From("aidl " + optional_args + " -I . --lang java -o out -Weverything " + main); |
Jooyung Han | 3557e48 | 2020-12-24 05:11:15 +0900 | [diff] [blame] | 47 | CaptureStderr(); |
Jiyong Park | d911332 | 2020-12-31 03:20:33 +0900 | [diff] [blame] | 48 | load_and_validate_aidl(main, options, io, &typenames, nullptr); |
Jooyung Han | 3557e48 | 2020-12-24 05:11:15 +0900 | [diff] [blame] | 49 | const std::string err = GetCapturedStderr(); |
| 50 | if (expect_diagnostics.empty()) { |
| 51 | EXPECT_EQ("", err); |
| 52 | } else { |
| 53 | for (const auto id : expect_diagnostics) { |
Jooyung Han | 7be5579 | 2020-12-29 14:50:13 +0900 | [diff] [blame] | 54 | EXPECT_THAT(err, testing::HasSubstr("-W" + to_string(id))); |
Jooyung Han | 3557e48 | 2020-12-24 05:11:15 +0900 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | AidlTypenames typenames; |
| 60 | FakeIoDelegate io; |
Jooyung Han | 1d78a36 | 2021-02-10 14:01:07 +0900 | [diff] [blame] | 61 | std::string optional_args; |
Jooyung Han | 3557e48 | 2020-12-24 05:11:15 +0900 | [diff] [blame] | 62 | std::vector<DiagnosticID> expect_diagnostics; |
| 63 | }; |
| 64 | |
Jooyung Han | fe298df | 2020-12-27 02:13:29 +0900 | [diff] [blame] | 65 | TEST_F(DiagnosticsTest, const_name_ForEnumerator) { |
| 66 | expect_diagnostics = {DiagnosticID::const_name}; |
| 67 | ParseFiles({{"Foo.aidl", "enum Foo { foo }"}}); |
| 68 | } |
| 69 | |
| 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 | |
Jooyung Han | 3557e48 | 2020-12-24 05:11:15 +0900 | [diff] [blame] | 75 | TEST_F(DiagnosticsTest, interface_name) { |
| 76 | expect_diagnostics = {DiagnosticID::interface_name}; |
| 77 | ParseFiles({{"Foo.aidl", "interface Foo { }"}}); |
| 78 | } |
| 79 | |
Jooyung Han | 7ab35ff | 2021-01-23 10:34:02 +0900 | [diff] [blame] | 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 | |
Jooyung Han | 3557e48 | 2020-12-24 05:11:15 +0900 | [diff] [blame] | 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 | } |
Jiyong Park | d911332 | 2020-12-31 03:20:33 +0900 | [diff] [blame] | 90 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | } |
Jooyung Han | 209c286 | 2020-12-31 10:59:43 +0900 | [diff] [blame] | 124 | |
| 125 | TEST_F(DiagnosticsTest, DontMixOnewayWithTwowayMethods) { |
| 126 | expect_diagnostics = {DiagnosticID::mixed_oneway}; |
| 127 | ParseFiles({ |
| 128 | {"IFoo.aidl", "interface IFoo { void foo(); oneway void bar(); }"}, |
| 129 | }); |
Jooyung Han | c9b0c80 | 2020-12-31 11:50:21 +0900 | [diff] [blame] | 130 | } |
| 131 | |
Jooyung Han | 1d78a36 | 2021-02-10 14:01:07 +0900 | [diff] [blame] | 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 | |
Jooyung Han | c9b0c80 | 2020-12-31 11:50:21 +0900 | [diff] [blame] | 140 | TEST_F(DiagnosticsTest, ArraysAsOutputParametersConsideredHarmful) { |
| 141 | expect_diagnostics = {DiagnosticID::out_array}; |
| 142 | ParseFiles({ |
| 143 | {"IFoo.aidl", "interface IFoo { void foo(out String[] ret); }"}, |
| 144 | }); |
Jooyung Han | 377795f | 2021-01-03 11:37:55 +0900 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | TEST_F(DiagnosticsTest, file_descriptor) { |
| 148 | expect_diagnostics = {DiagnosticID::file_descriptor}; |
| 149 | ParseFiles({{"IFoo.aidl", |
Jooyung Han | ecfd288 | 2021-03-02 16:52:36 +0900 | [diff] [blame^] | 150 | "interface IFoo {\n" |
Jooyung Han | 377795f | 2021-01-03 11:37:55 +0900 | [diff] [blame] | 151 | " void foo(in FileDescriptor fd);\n" |
| 152 | "}"}}); |
| 153 | } |
Jooyung Han | ecfd288 | 2021-03-02 16:52:36 +0900 | [diff] [blame^] | 154 | |
| 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 | |
| 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 | |
| 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 | } |