5package io.flutter.embedding.engine.loader;
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;
19import android.content.pm.ApplicationInfo;
20import android.content.pm.PackageManager;
21import android.content.res.Resources;
22import android.content.res.XmlResourceParser;
24import androidx.test.core.app.ApplicationProvider;
25import androidx.test.ext.junit.runners.AndroidJUnit4;
26import java.io.StringReader;
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;
35@RunWith(AndroidJUnit4.class)
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);
51 @SuppressWarnings(
"deprecation")
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))
74 Bundle bundle =
new Bundle();
79 Context context = generateMockContext(bundle,
null);
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);
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>"
98 +
"</network-security-config>";
99 Context context = generateMockContext(bundle, networkPolicyXml);
102 assertEquals(
"[[\"secure.example.com\",true,false]]",
info.domainNetworkPolicy);
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>"
118 +
"</network-security-config>";
119 Context context = generateMockContext(bundle, networkPolicyXml);
122 assertEquals(
"[[\"secure.example.com\",true,false]]",
info.domainNetworkPolicy);
127 Bundle bundle =
new Bundle();
128 String networkPolicyXml =
129 "<network-security-config>"
130 +
"<domain-config cleartextTrafficPermitted=\"true\">"
131 +
"<domain includeSubdomains=\"true\">example.com</domain>"
133 +
"<domain includeSubdomains=\"true\">insecure.example.com</domain>"
135 +
"<domain-config cleartextTrafficPermitted=\"false\">"
136 +
"<domain includeSubdomains=\"true\">secure.example.com</domain>"
139 +
"</network-security-config>";
140 Context context = generateMockContext(bundle, networkPolicyXml);
144 "[[\"example.com\",true,true],[\"insecure.example.com\",true,true],[\"secure.example.com\",true,false]]",
145 info.domainNetworkPolicy);
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()))
169 Object[]
args = invocation.getArguments();
170 String
result = xpp.getAttributeValue((String)
args[0], (String)
args[1]);
172 return (Boolean)
args[2];
174 return Boolean.parseBoolean(
result);
176 return resourceParser;
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
void itGeneratesCorrectApplicationInfoWithCustomValues()
void itGeneratesCorrectApplicationInfoWithDefaultManifest()
void itHandlesNestedSubDomains()
void itGeneratesCorrectNetworkPolicy()
void itHandlesBogusInformationInNetworkPolicy()
static final String PUBLIC_AOT_SHARED_LIBRARY_NAME
static final String PUBLIC_VM_SNAPSHOT_DATA_KEY
static final String PUBLIC_FLUTTER_ASSETS_DIR_KEY
static final String PUBLIC_ISOLATE_SNAPSHOT_DATA_KEY
static final String NETWORK_POLICY_METADATA_KEY
static FlutterApplicationInfo load(@NonNull Context applicationContext)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
SIT bool any(const Vec< 1, T > &x)