From 7d584e936005d7ac6439457a155cc59609e22940 Mon Sep 17 00:00:00 2001 From: Paul Duffin Date: Fri, 23 Oct 2020 18:26:03 +0100 Subject: Retry: Make ConfiguredJarList immutable By making the Append and RemoveList methods return a new list instead of modifying the existing list it makes the ConfiguredJarList usages easier to reason about and safer to use, especially considering that they are primarily used in global configuration. Added some tests for Append/RemoveList to ensure that they work and do not modify the original or result in newly created lists sharing storage with the original which would lead to corruption. Bug: 171756871 Bug: 171479578 Test: m nothing EMMA_INSTRUMENT=true EMMA_INSTRUMENT_FRAMEWORK=true m nothing Change-Id: I541c4686ecdd45c6a0c8b1c93fedf0fcd5952e2b --- android/config_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'android/config_test.go') diff --git a/android/config_test.go b/android/config_test.go index 274d59fb6..68f68a0a5 100644 --- a/android/config_test.go +++ b/android/config_test.go @@ -91,3 +91,49 @@ func TestMissingVendorConfig(t *testing.T) { t.Errorf("Expected false") } } + +func assertStringEquals(t *testing.T, expected, actual string) { + if actual != expected { + t.Errorf("expected %q found %q", expected, actual) + } +} + +func TestConfiguredJarList(t *testing.T) { + list1 := CreateTestConfiguredJarList([]string{"apex1:jarA"}) + + t.Run("create", func(t *testing.T) { + assertStringEquals(t, "apex1:jarA", list1.String()) + }) + + list2 := list1.Append("apex2", "jarB") + t.Run("append", func(t *testing.T) { + assertStringEquals(t, "apex1:jarA,apex2:jarB", list2.String()) + }) + + t.Run("append does not modify", func(t *testing.T) { + assertStringEquals(t, "apex1:jarA", list1.String()) + }) + + // Make sure that two lists created by appending to the same list do not share storage. + list3 := list1.Append("apex3", "jarC") + t.Run("append does not share", func(t *testing.T) { + assertStringEquals(t, "apex1:jarA,apex2:jarB", list2.String()) + assertStringEquals(t, "apex1:jarA,apex3:jarC", list3.String()) + }) + + list4 := list3.RemoveList(list1) + t.Run("remove", func(t *testing.T) { + assertStringEquals(t, "apex3:jarC", list4.String()) + }) + + t.Run("remove does not modify", func(t *testing.T) { + assertStringEquals(t, "apex1:jarA,apex3:jarC", list3.String()) + }) + + // Make sure that two lists created by removing from the same list do not share storage. + list5 := list3.RemoveList(CreateTestConfiguredJarList([]string{"apex3:jarC"})) + t.Run("remove", func(t *testing.T) { + assertStringEquals(t, "apex3:jarC", list4.String()) + assertStringEquals(t, "apex1:jarA", list5.String()) + }) +} -- cgit v1.2.3-59-g8ed1b