0

Add flag to determine the source of the cronet build

Currently, there's no way to distinguish whether we're building
CronetEngine or HttpEngine (Cronet in AOSP). This meant that we had
to use hacks (eg: look at classloader) which may break at any time.

The tests depended on a special xml file that is only fed to the test
APK, we couldn't do the same for prod.

This new flag will help remove the last bits of divergence that
is leftover in AOSP (UserAgent).

Bug: 412608685
Change-Id: Ie0cf7fac0493ac123e728e2ed5c138b9af8c46d9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6479291
Reviewed-by: Andrew Grieve <agrieve@chromium.org>
Commit-Queue: Mohannad Farrag <aymanm@google.com>
Auto-Submit: Mohannad Farrag <aymanm@google.com>
Cr-Commit-Position: refs/heads/main@{#1451099}
This commit is contained in:
Mohannad Farrag
2025-04-24 06:22:07 -07:00
committed by Chromium LUCI CQ
parent 916bf0b549
commit e28d07aba4
9 changed files with 22 additions and 39 deletions

@ -77,6 +77,12 @@ public class BuildConfig {
public static boolean IS_CRONET_BUILD;
#endif
#if defined(_CRONET_FOR_AOSP_BUILD)
public static boolean CRONET_FOR_AOSP_BUILD = true;
#else
public static boolean CRONET_FOR_AOSP_BUILD;
#endif
#if defined(_WRITE_CLANG_PROFILING_DATA)
public static boolean WRITE_CLANG_PROFILING_DATA = true;
#else

@ -1992,6 +1992,9 @@ if (!is_robolectric && enable_java_templates) {
if (is_cronet_build) {
defines += [ "_IS_CRONET_BUILD" ]
defines += [ "_LOGTAG_PREFIX=cn_" ]
if (is_cronet_for_aosp_build) {
defines += [ "_CRONET_FOR_AOSP_BUILD" ]
}
} else {
defines += [ "_LOGTAG_PREFIX=cr_" ]
}

@ -1157,7 +1157,6 @@ android_resources("cronet_test_apk_resources") {
# TODO(crbug.com/40283951): Modify quicroot.pem to become a build time copy of
# net/data/ssl/certificates/cronet-quic-chain.pem.
"test/res/raw/quicroot.pem",
"test/res/values/cronet-test-rule-configuration.xml",
"test/res/values/strings.xml",
"test/res/xml/network_security_config.xml",
]

@ -7,6 +7,7 @@ package org.chromium.net.impl;
import android.content.Context;
import android.os.SystemClock;
import org.chromium.build.BuildConfig;
import org.chromium.net.ExperimentalCronetEngine;
import org.chromium.net.ICronetEngineBuilder;
import org.chromium.net.impl.CronetLogger.CronetSource;
@ -31,7 +32,7 @@ public class NativeCronetEngineBuilderImpl extends CronetEngineBuilderImpl {
private static CronetSource computeCronetSource() {
ClassLoader implClassLoader = CronetEngineBuilderImpl.class.getClassLoader();
if (implClassLoader.toString().startsWith("java.lang.BootClassLoader")) {
if (BuildConfig.CRONET_FOR_AOSP_BUILD) {
return CronetSource.CRONET_SOURCE_PLATFORM;
}

@ -29,6 +29,7 @@ import org.junit.runner.RunWith;
import org.chromium.base.Log;
import org.chromium.base.test.util.DoNotBatch;
import org.chromium.base.test.util.RequiresRestart;
import org.chromium.build.BuildConfig;
import org.chromium.net.CronetTestRule.BoolFlag;
import org.chromium.net.CronetTestRule.CronetImplementation;
import org.chromium.net.CronetTestRule.Flags;
@ -1009,7 +1010,7 @@ public class BidirectionalStreamTest {
var oldMessage = "Invalid header header:name=headervalue";
var newMessage = "Invalid header with headername: header:name";
if (mTestRule.implementationUnderTest() == CronetImplementation.AOSP_PLATFORM
&& !mTestRule.isRunningInAOSP()) {
&& !BuildConfig.CRONET_FOR_AOSP_BUILD) {
// We may be running against an HttpEngine backed by an old version of Cronet, so accept
// both the old and new variants of the message.
assertThat(e).hasMessageThat().isAnyOf(oldMessage, newMessage);
@ -1031,7 +1032,7 @@ public class BidirectionalStreamTest {
var oldMessage = "Invalid header headername=bad header\r\nvalue";
var newMessage = "Invalid header with headername: headername";
if (mTestRule.implementationUnderTest() == CronetImplementation.AOSP_PLATFORM
&& !mTestRule.isRunningInAOSP()) {
&& !BuildConfig.CRONET_FOR_AOSP_BUILD) {
// We may be running against an HttpEngine backed by an old version of Cronet, so accept
// both the old and new variants of the message.
assertThat(e).hasMessageThat().isAnyOf(oldMessage, newMessage);

@ -30,6 +30,7 @@ import org.chromium.base.ContextUtils;
import org.chromium.base.Log;
import org.chromium.base.PathUtils;
import org.chromium.base.test.util.DoNotBatch;
import org.chromium.build.BuildConfig;
import org.chromium.net.httpflags.FlagValue;
import org.chromium.net.httpflags.HttpFlagsInterceptor;
import org.chromium.net.impl.CronetLibraryLoader;
@ -236,7 +237,8 @@ public class CronetTestRule implements TestRule {
if (packageName.startsWith("org.chromium.net")) {
for (CronetImplementation implementation : implementationsUnderTest) {
if (isRunningInAOSP() && implementation.equals(CronetImplementation.FALLBACK)) {
if (BuildConfig.CRONET_FOR_AOSP_BUILD
&& implementation.equals(CronetImplementation.FALLBACK)) {
// Skip executing tests for JavaCronetEngine.
continue;
}
@ -309,33 +311,6 @@ public class CronetTestRule implements TestRule {
return flagsBuilder.build();
}
/**
* This method only returns the value of the `is_running_in_aosp` flag which for Chromium can be
* found inside components/cronet/android/test/res/values/cronet-test-rule-configuration.xml for
* which it should be equal to false. However, on AOSP, we ship a different value which is equal
* to true.
*
* <p>This distinction between where the tests are being executed is crucial because we don't
* want to run JavaCronetEngine tests in AOSP.
*
* @return True if the tests are being executed in AOSP.
*/
@SuppressWarnings("DiscouragedApi")
public boolean isRunningInAOSP() {
int resId =
ApplicationProvider.getApplicationContext()
.getResources()
.getIdentifier(
"is_running_in_aosp",
"bool",
ApplicationProvider.getApplicationContext().getPackageName());
if (resId == 0) {
throw new IllegalStateException(
"Could not find any value for `is_running_in_aosp` boolean entry.");
}
return ApplicationProvider.getApplicationContext().getResources().getBoolean(resId);
}
private void evaluateWithFramework(
Statement statement, String testName, boolean netLogEnabled, Description desc)
throws Throwable {

@ -34,6 +34,7 @@ import org.chromium.base.ApkInfo;
import org.chromium.base.DeviceInfo;
import org.chromium.base.Log;
import org.chromium.base.test.util.DoNotBatch;
import org.chromium.build.BuildConfig;
import org.chromium.net.CronetTestRule.BoolFlag;
import org.chromium.net.CronetTestRule.CronetImplementation;
import org.chromium.net.CronetTestRule.Flags;
@ -645,7 +646,7 @@ public class CronetUrlRequestTest {
var oldMessage = "Invalid header header:name=headervalue";
var newMessage = "Invalid header with headername: header:name";
if (mTestRule.implementationUnderTest() == CronetImplementation.AOSP_PLATFORM
&& !mTestRule.isRunningInAOSP()) {
&& !BuildConfig.CRONET_FOR_AOSP_BUILD) {
// We may be running against an HttpEngine backed by an old version of Cronet, so accept
// both the old and new variants of the message.
assertThat(e).hasMessageThat().isAnyOf(oldMessage, newMessage);
@ -688,7 +689,7 @@ public class CronetUrlRequestTest {
var oldMessage = "Invalid header headername=bad header\r\nvalue";
var newMessage = "Invalid header with headername: headername";
if (mTestRule.implementationUnderTest() == CronetImplementation.AOSP_PLATFORM
&& !mTestRule.isRunningInAOSP()) {
&& !BuildConfig.CRONET_FOR_AOSP_BUILD) {
// We may be running against an HttpEngine backed by an old version of Cronet, so accept
// both the old and new variants of the message.
assertThat(e).hasMessageThat().isAnyOf(oldMessage, newMessage);

@ -21,6 +21,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.test.util.DoNotBatch;
import org.chromium.build.BuildConfig;
import org.chromium.net.CronetTestRule.CronetImplementation;
import org.chromium.net.CronetTestRule.IgnoreFor;
import org.chromium.net.CronetTestRule.RequiresMinApi;
@ -459,7 +460,7 @@ public class RequestFinishedInfoTest {
var oldMessage = "Invalid header =";
var newMessage = "Invalid header with headername: ";
if (mTestRule.implementationUnderTest() == CronetImplementation.AOSP_PLATFORM
&& !mTestRule.isRunningInAOSP()) {
&& !BuildConfig.CRONET_FOR_AOSP_BUILD) {
// We may be running against an HttpEngine backed by an old version of Cronet, so accept
// both the old and new variants of the message.
assertThat(e).hasMessageThat().isAnyOf(oldMessage, newMessage);

@ -1,4 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="is_running_in_aosp">false</bool>
</resources>