diff options
| author | 2021-02-01 20:55:14 +0000 | |
|---|---|---|
| committer | 2021-02-01 20:55:14 +0000 | |
| commit | 969c49681c4565912f43cf694412d0455b19e7d4 (patch) | |
| tree | 5e13158b1997d442f342cec86c821f4b3c1ba617 | |
| parent | db07cc3032b139bb84f0d16f6a250cb5fa027b0f (diff) | |
| parent | d8bd3028e3e94c8bb6c37ddb2a4d87abe7747c66 (diff) | |
Merge "Document Signature equals/hash should not include flags" into sc-dev
| -rw-r--r-- | core/java/android/content/pm/Signature.java | 4 | ||||
| -rw-r--r-- | core/tests/coretests/src/android/content/pm/SignatureTest.java | 26 |
2 files changed, 30 insertions, 0 deletions
diff --git a/core/java/android/content/pm/Signature.java b/core/java/android/content/pm/Signature.java index 02fb06b808a2..bce4b872b8a6 100644 --- a/core/java/android/content/pm/Signature.java +++ b/core/java/android/content/pm/Signature.java @@ -256,6 +256,8 @@ public class Signature implements Parcelable { try { if (obj != null) { Signature other = (Signature)obj; + // Note, some classes, such as PackageParser.SigningDetails, rely on equals + // only comparing the mSignature arrays without the flags. return this == other || Arrays.equals(mSignature, other.mSignature); } } catch (ClassCastException e) { @@ -268,6 +270,8 @@ public class Signature implements Parcelable { if (mHaveHashCode) { return mHashCode; } + // Note, similar to equals some classes rely on the hash code not including + // the flags for Set membership checks. mHashCode = Arrays.hashCode(mSignature); mHaveHashCode = true; return mHashCode; diff --git a/core/tests/coretests/src/android/content/pm/SignatureTest.java b/core/tests/coretests/src/android/content/pm/SignatureTest.java index f0b4af6fc44f..19458da796e8 100644 --- a/core/tests/coretests/src/android/content/pm/SignatureTest.java +++ b/core/tests/coretests/src/android/content/pm/SignatureTest.java @@ -54,6 +54,32 @@ public class SignatureTest extends TestCase { assertFalse(Signature.areEffectiveMatch(asArray(A, M), asArray(A, B))); } + public void testHashCode_doesNotIncludeFlags() throws Exception { + // Some classes rely on the hash code not including the flags / capabilities for the signer + // to verify Set membership. This test verifies two signers with the same signature but + // different flags have the same hash code. + Signature signatureAWithAllCaps = new Signature(A.toCharsString()); + // There are currently 5 capabilities that can be assigned to a previous signer, although + // for the purposes of this test all that matters is that the two flag values are distinct. + signatureAWithAllCaps.setFlags(31); + Signature signatureAWithNoCaps = new Signature(A.toCharsString()); + signatureAWithNoCaps.setFlags(0); + + assertEquals(signatureAWithAllCaps.hashCode(), signatureAWithNoCaps.hashCode()); + } + + public void testEquals_doesNotIncludeFlags() throws Exception { + // Similar to above some classes rely on equals only comparing the signature arrays + // for equality without including the flags. This test verifies two signers with the + // same signature but different flags are still considered equal. + Signature signatureAWithAllCaps = new Signature(A.toCharsString()); + signatureAWithAllCaps.setFlags(31); + Signature signatureAWithNoCaps = new Signature(A.toCharsString()); + signatureAWithNoCaps.setFlags(0); + + assertEquals(signatureAWithAllCaps, signatureAWithNoCaps); + } + private static Signature[] asArray(Signature... s) { return s; } |