| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 1 | /* |
| 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 "java/ManifestClassGenerator.h" |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
| Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 19 | #include "test/Test.h" |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 20 | |
| 21 | namespace aapt { |
| 22 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 23 | static ::testing::AssertionResult GetManifestClassText(IAaptContext* context, |
| 24 | xml::XmlResource* res, |
| 25 | std::string* out_str) { |
| 26 | std::unique_ptr<ClassDefinition> manifest_class = |
| 27 | GenerateManifestClass(context->GetDiagnostics(), res); |
| 28 | if (!manifest_class) { |
| 29 | return ::testing::AssertionFailure() << "manifest_class == nullptr"; |
| 30 | } |
| Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 31 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 32 | std::stringstream out; |
| 33 | if (!manifest_class->WriteJavaFile(manifest_class.get(), "android", true, |
| 34 | &out)) { |
| 35 | return ::testing::AssertionFailure() << "failed to write java file"; |
| 36 | } |
| Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 37 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 38 | *out_str = out.str(); |
| 39 | return ::testing::AssertionSuccess(); |
| Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 42 | TEST(ManifestClassGeneratorTest, NameIsProperlyGeneratedFromSymbol) { |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 43 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 44 | std::unique_ptr<xml::XmlResource> manifest = test::BuildXmlDom(R"EOF( |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 45 | <manifest xmlns:android="http://schemas.android.com/apk/res/android"> |
| 46 | <permission android:name="android.permission.ACCESS_INTERNET" /> |
| 47 | <permission android:name="android.DO_DANGEROUS_THINGS" /> |
| 48 | <permission android:name="com.test.sample.permission.HUH" /> |
| 49 | <permission-group android:name="foo.bar.PERMISSION" /> |
| 50 | </manifest>)EOF"); |
| 51 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 52 | std::string actual; |
| 53 | ASSERT_TRUE(GetManifestClassText(context.get(), manifest.get(), &actual)); |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 54 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 55 | const size_t permission_class_pos = |
| 56 | actual.find("public static final class permission {"); |
| 57 | const size_t permission_croup_class_pos = |
| 58 | actual.find("public static final class permission_group {"); |
| 59 | ASSERT_NE(std::string::npos, permission_class_pos); |
| 60 | ASSERT_NE(std::string::npos, permission_croup_class_pos); |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 61 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 62 | // |
| 63 | // Make sure these permissions are in the permission class. |
| 64 | // |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 65 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 66 | size_t pos = actual.find( |
| 67 | "public static final String ACCESS_INTERNET=" |
| 68 | "\"android.permission.ACCESS_INTERNET\";"); |
| 69 | EXPECT_GT(pos, permission_class_pos); |
| 70 | EXPECT_LT(pos, permission_croup_class_pos); |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 71 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 72 | pos = actual.find( |
| 73 | "public static final String DO_DANGEROUS_THINGS=" |
| 74 | "\"android.DO_DANGEROUS_THINGS\";"); |
| 75 | EXPECT_GT(pos, permission_class_pos); |
| 76 | EXPECT_LT(pos, permission_croup_class_pos); |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 77 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 78 | pos = actual.find( |
| 79 | "public static final String HUH=\"com.test.sample.permission.HUH\";"); |
| 80 | EXPECT_GT(pos, permission_class_pos); |
| 81 | EXPECT_LT(pos, permission_croup_class_pos); |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 82 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 83 | // |
| 84 | // Make sure these permissions are in the permission_group class |
| 85 | // |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 86 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 87 | pos = actual.find( |
| 88 | "public static final String PERMISSION=" |
| 89 | "\"foo.bar.PERMISSION\";"); |
| 90 | EXPECT_GT(pos, permission_croup_class_pos); |
| 91 | EXPECT_LT(pos, std::string::npos); |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | TEST(ManifestClassGeneratorTest, CommentsAndAnnotationsArePresent) { |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 95 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
| 96 | std::unique_ptr<xml::XmlResource> manifest = test::BuildXmlDom(R"EOF( |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 97 | <manifest xmlns:android="http://schemas.android.com/apk/res/android"> |
| 98 | <!-- Required to access the internet. |
| 99 | Added in API 1. --> |
| 100 | <permission android:name="android.permission.ACCESS_INTERNET" /> |
| 101 | <!-- @deprecated This permission is for playing outside. --> |
| 102 | <permission android:name="android.permission.PLAY_OUTSIDE" /> |
| 103 | <!-- This is a private permission for system only! |
| 104 | @hide |
| 105 | @SystemApi --> |
| 106 | <permission android:name="android.permission.SECRET" /> |
| 107 | </manifest>)EOF"); |
| 108 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 109 | std::string actual; |
| 110 | ASSERT_TRUE(GetManifestClassText(context.get(), manifest.get(), &actual)); |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 111 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 112 | const char* expected_access_internet = |
| 113 | R"EOF( /** |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 114 | * Required to access the internet. |
| 115 | * Added in API 1. |
| 116 | */ |
| Adam Lesinski | 803c7c8 | 2016-04-06 16:09:43 -0700 | [diff] [blame] | 117 | public static final String ACCESS_INTERNET="android.permission.ACCESS_INTERNET";)EOF"; |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 118 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 119 | EXPECT_NE(std::string::npos, actual.find(expected_access_internet)); |
| Adam Lesinski | 803c7c8 | 2016-04-06 16:09:43 -0700 | [diff] [blame] | 120 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 121 | const char* expected_play_outside = |
| 122 | R"EOF( /** |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 123 | * @deprecated This permission is for playing outside. |
| 124 | */ |
| 125 | @Deprecated |
| Adam Lesinski | 803c7c8 | 2016-04-06 16:09:43 -0700 | [diff] [blame] | 126 | public static final String PLAY_OUTSIDE="android.permission.PLAY_OUTSIDE";)EOF"; |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 127 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 128 | EXPECT_NE(std::string::npos, actual.find(expected_play_outside)); |
| Adam Lesinski | 803c7c8 | 2016-04-06 16:09:43 -0700 | [diff] [blame] | 129 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 130 | const char* expected_secret = |
| 131 | R"EOF( /** |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 132 | * This is a private permission for system only! |
| 133 | * @hide |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 134 | */ |
| Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 135 | @android.annotation.SystemApi |
| Adam Lesinski | 803c7c8 | 2016-04-06 16:09:43 -0700 | [diff] [blame] | 136 | public static final String SECRET="android.permission.SECRET";)EOF"; |
| 137 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 138 | EXPECT_NE(std::string::npos, actual.find(expected_secret)); |
| Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 141 | } // namespace aapt |