Reject invalid input.

Ensure that apps don't provide negative estimated byte usage.

Bug: 141645789
Test: atest android.jobscheduler.cts.JobInfoTest
Test: atest android.jobscheduler.cts.JobWorkItemTest
Change-Id: I66485040de0cc105056349e308561f9ddd6ab91a
This commit is contained in:
Kweku Adams
2021-06-22 13:28:23 -07:00
parent 6fb5c88adb
commit 70f8e99c40
2 changed files with 14 additions and 1 deletions

View File

@@ -1250,6 +1250,11 @@ public class JobInfo implements Parcelable {
*/
public Builder setEstimatedNetworkBytes(@BytesLong long downloadBytes,
@BytesLong long uploadBytes) {
if ((downloadBytes != NETWORK_BYTES_UNKNOWN && downloadBytes < 0)
|| (uploadBytes != NETWORK_BYTES_UNKNOWN && uploadBytes < 0)) {
throw new IllegalArgumentException(
"Can't provide negative estimated network usage");
}
mNetworkDownloadBytes = downloadBytes;
mNetworkUploadBytes = uploadBytes;
return this;
@@ -1647,12 +1652,16 @@ public class JobInfo implements Parcelable {
/**
* @hide
*/
public void enforceValidity() {
public final void enforceValidity() {
// Check that network estimates require network type
if ((networkDownloadBytes > 0 || networkUploadBytes > 0) && networkRequest == null) {
throw new IllegalArgumentException(
"Can't provide estimated network usage without requiring a network");
}
if ((networkDownloadBytes != NETWORK_BYTES_UNKNOWN && networkDownloadBytes < 0)
|| (networkUploadBytes != NETWORK_BYTES_UNKNOWN && networkUploadBytes < 0)) {
throw new IllegalArgumentException("Can't provide negative estimated network usage");
}
// Check that a deadline was not set on a periodic job.
if (isPeriodic) {

View File

@@ -68,6 +68,10 @@ final public class JobWorkItem implements Parcelable {
* uploaded by this job work item, in bytes.
*/
public JobWorkItem(Intent intent, @BytesLong long downloadBytes, @BytesLong long uploadBytes) {
if ((downloadBytes != NETWORK_BYTES_UNKNOWN && downloadBytes < 0)
|| (uploadBytes != NETWORK_BYTES_UNKNOWN && uploadBytes < 0)) {
throw new IllegalArgumentException("Can't provide negative estimated network usage");
}
mIntent = intent;
mNetworkDownloadBytes = downloadBytes;
mNetworkUploadBytes = uploadBytes;