Flutter Engine
The Flutter Engine
ApplicationInfoLoaderTest.java
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package io.flutter.embedding.engine.loader;
6
7import static org.junit.Assert.assertEquals;
8import static org.junit.Assert.assertNotNull;
9import static org.junit.Assert.assertNull;
10import static org.mockito.Mockito.any;
11import static org.mockito.Mockito.anyBoolean;
12import static org.mockito.Mockito.anyInt;
13import static org.mockito.Mockito.anyString;
14import static org.mockito.Mockito.doAnswer;
15import static org.mockito.Mockito.mock;
16import static org.mockito.Mockito.when;
17
18import android.content.Context;
19import android.content.pm.ApplicationInfo;
20import android.content.pm.PackageManager;
21import android.content.res.Resources;
22import android.content.res.XmlResourceParser;
23import android.os.Bundle;
24import androidx.test.core.app.ApplicationProvider;
25import androidx.test.ext.junit.runners.AndroidJUnit4;
26import java.io.StringReader;
27import org.junit.Test;
28import org.junit.runner.RunWith;
29import org.mockito.stubbing.Answer;
30import org.robolectric.annotation.Config;
31import org.xmlpull.v1.XmlPullParser;
32import org.xmlpull.v1.XmlPullParserFactory;
33
34@Config(manifest = Config.NONE)
35@RunWith(AndroidJUnit4.class)
37
38 @Test
41 ApplicationInfoLoader.load(ApplicationProvider.getApplicationContext());
42 assertNotNull(info);
43 assertEquals("libapp.so", info.aotSharedLibraryName);
44 assertEquals("vm_snapshot_data", info.vmSnapshotData);
45 assertEquals("isolate_snapshot_data", info.isolateSnapshotData);
46 assertEquals("flutter_assets", info.flutterAssetsDir);
47 assertEquals("", info.domainNetworkPolicy);
48 assertNull(info.nativeLibraryDir);
49 }
50
51 @SuppressWarnings("deprecation")
52 // getApplicationInfo
53 private Context generateMockContext(Bundle metadata, String networkPolicyXml) throws Exception {
54 Context context = mock(Context.class);
55 PackageManager packageManager = mock(PackageManager.class);
56 ApplicationInfo applicationInfo = mock(ApplicationInfo.class);
57 applicationInfo.metaData = metadata;
58 Resources resources = mock(Resources.class);
59 when(context.getPackageManager()).thenReturn(packageManager);
60 when(context.getResources()).thenReturn(resources);
61 when(context.getPackageName()).thenReturn("");
62 when(packageManager.getApplicationInfo(anyString(), anyInt())).thenReturn(applicationInfo);
63 if (networkPolicyXml != null) {
65 doAnswer(invocationOnMock -> createMockResourceParser(networkPolicyXml))
66 .when(resources)
67 .getXml(5);
68 }
69 return context;
70 }
71
72 @Test
74 Bundle bundle = new Bundle();
75 bundle.putString(ApplicationInfoLoader.PUBLIC_AOT_SHARED_LIBRARY_NAME, "testaot");
76 bundle.putString(ApplicationInfoLoader.PUBLIC_VM_SNAPSHOT_DATA_KEY, "testvmsnapshot");
77 bundle.putString(ApplicationInfoLoader.PUBLIC_ISOLATE_SNAPSHOT_DATA_KEY, "testisolatesnapshot");
78 bundle.putString(ApplicationInfoLoader.PUBLIC_FLUTTER_ASSETS_DIR_KEY, "testassets");
79 Context context = generateMockContext(bundle, null);
81 assertNotNull(info);
82 assertEquals("testaot", info.aotSharedLibraryName);
83 assertEquals("testvmsnapshot", info.vmSnapshotData);
84 assertEquals("testisolatesnapshot", info.isolateSnapshotData);
85 assertEquals("testassets", info.flutterAssetsDir);
86 assertNull(info.nativeLibraryDir);
87 assertEquals("", info.domainNetworkPolicy);
88 }
89
90 @Test
91 public void itGeneratesCorrectNetworkPolicy() throws Exception {
92 Bundle bundle = new Bundle();
93 String networkPolicyXml =
94 "<network-security-config>"
95 + "<domain-config cleartextTrafficPermitted=\"false\">"
96 + "<domain includeSubdomains=\"true\">secure.example.com</domain>"
97 + "</domain-config>"
98 + "</network-security-config>";
99 Context context = generateMockContext(bundle, networkPolicyXml);
101 assertNotNull(info);
102 assertEquals("[[\"secure.example.com\",true,false]]", info.domainNetworkPolicy);
103 }
104
105 @Test
106 public void itHandlesBogusInformationInNetworkPolicy() throws Exception {
107 Bundle bundle = new Bundle();
108 String networkPolicyXml =
109 "<network-security-config>"
110 + "<domain-config cleartextTrafficPermitted=\"false\">"
111 + "<domain includeSubdomains=\"true\">secure.example.com</domain>"
112 + "<pin-set expiration=\"2018-01-01\">"
113 + "<pin digest=\"SHA-256\">7HIpactkIAq2Y49orFOOQKurWxmmSFZhBCoQYcRhJ3Y=</pin>"
114 + "<!-- backup pin -->"
115 + "<pin digest=\"SHA-256\">fwza0LRMXouZHRC8Ei+4PyuldPDcf3UKgO/04cDM1oE=</pin>"
116 + "</pin-set>"
117 + "</domain-config>"
118 + "</network-security-config>";
119 Context context = generateMockContext(bundle, networkPolicyXml);
121 assertNotNull(info);
122 assertEquals("[[\"secure.example.com\",true,false]]", info.domainNetworkPolicy);
123 }
124
125 @Test
126 public void itHandlesNestedSubDomains() throws Exception {
127 Bundle bundle = new Bundle();
128 String networkPolicyXml =
129 "<network-security-config>"
130 + "<domain-config cleartextTrafficPermitted=\"true\">"
131 + "<domain includeSubdomains=\"true\">example.com</domain>"
132 + "<domain-config>"
133 + "<domain includeSubdomains=\"true\">insecure.example.com</domain>"
134 + "</domain-config>"
135 + "<domain-config cleartextTrafficPermitted=\"false\">"
136 + "<domain includeSubdomains=\"true\">secure.example.com</domain>"
137 + "</domain-config>"
138 + "</domain-config>"
139 + "</network-security-config>";
140 Context context = generateMockContext(bundle, networkPolicyXml);
142 assertNotNull(info);
143 assertEquals(
144 "[[\"example.com\",true,true],[\"insecure.example.com\",true,true],[\"secure.example.com\",true,false]]",
145 info.domainNetworkPolicy);
146 }
147
148 // The following ridiculousness is needed because Android gives no way for us
149 // to customize XmlResourceParser. We have to mock it and tie each method
150 // we use to an actual Xml parser.
151 private XmlResourceParser createMockResourceParser(String xml) throws Exception {
152 final XmlPullParser xpp = XmlPullParserFactory.newInstance().newPullParser();
153 xpp.setInput(new StringReader(xml));
154 XmlResourceParser resourceParser = mock(XmlResourceParser.class);
155 final Answer<Object> invokeMethodOnRealParser =
156 invocation -> invocation.getMethod().invoke(xpp, invocation.getArguments());
157 when(resourceParser.next()).thenAnswer(invokeMethodOnRealParser);
158 when(resourceParser.getName()).thenAnswer(invokeMethodOnRealParser);
159 when(resourceParser.getEventType()).thenAnswer(invokeMethodOnRealParser);
160 when(resourceParser.getText()).thenAnswer(invokeMethodOnRealParser);
161 when(resourceParser.getAttributeCount()).thenAnswer(invokeMethodOnRealParser);
162 when(resourceParser.getAttributeName(anyInt())).thenAnswer(invokeMethodOnRealParser);
163 when(resourceParser.getAttributeValue(anyInt())).thenAnswer(invokeMethodOnRealParser);
164 when(resourceParser.getAttributeValue(anyString(), anyString()))
165 .thenAnswer(invokeMethodOnRealParser);
166 when(resourceParser.getAttributeBooleanValue(any(), anyString(), anyBoolean()))
167 .thenAnswer(
168 invocation -> {
169 Object[] args = invocation.getArguments();
170 String result = xpp.getAttributeValue((String) args[0], (String) args[1]);
171 if (result == null) {
172 return (Boolean) args[2];
173 }
174 return Boolean.parseBoolean(result);
175 });
176 return resourceParser;
177 }
178}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
static FlutterApplicationInfo load(@NonNull Context applicationContext)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
GAsyncResult * result
SIT bool any(const Vec< 1, T > &x)
Definition: SkVx.h:530