Skip to content
Snippets Groups Projects
Commit ba7eba9c authored by Max Loh's avatar Max Loh Committed by Android (Google) Code Review
Browse files

Merge "aslgen implement security labels and third party verification" into main

parents 159d436b 58f7d34b
No related branches found
No related tags found
No related merge requests found
Showing
with 443 additions and 4 deletions
......@@ -28,10 +28,18 @@ public class SafetyLabels implements AslMarshallable {
private final Long mVersion;
private final DataLabels mDataLabels;
private final SecurityLabels mSecurityLabels;
private final ThirdPartyVerification mThirdPartyVerification;
public SafetyLabels(Long version, DataLabels dataLabels) {
public SafetyLabels(
Long version,
DataLabels dataLabels,
SecurityLabels securityLabels,
ThirdPartyVerification thirdPartyVerification) {
this.mVersion = version;
this.mDataLabels = dataLabels;
this.mSecurityLabels = securityLabels;
this.mThirdPartyVerification = thirdPartyVerification;
}
/** Returns the data label for the safety label */
......@@ -54,6 +62,12 @@ public class SafetyLabels implements AslMarshallable {
if (mDataLabels != null) {
XmlUtils.appendChildren(safetyLabelsEle, mDataLabels.toOdDomElements(doc));
}
if (mSecurityLabels != null) {
XmlUtils.appendChildren(safetyLabelsEle, mSecurityLabels.toOdDomElements(doc));
}
if (mThirdPartyVerification != null) {
XmlUtils.appendChildren(safetyLabelsEle, mThirdPartyVerification.toOdDomElements(doc));
}
return XmlUtils.listOf(safetyLabelsEle);
}
}
......@@ -44,6 +44,22 @@ public class SafetyLabelsFactory implements AslMarshallableFactory<SafetyLabels>
safetyLabelsEle,
XmlUtils.HR_TAG_DATA_LABELS,
false)));
return new SafetyLabels(version, dataLabels);
SecurityLabels securityLabels =
new SecurityLabelsFactory()
.createFromHrElements(
XmlUtils.listOf(
XmlUtils.getSingleChildElement(
safetyLabelsEle,
XmlUtils.HR_TAG_SECURITY_LABELS,
false)));
ThirdPartyVerification thirdPartyVerification =
new ThirdPartyVerificationFactory()
.createFromHrElements(
XmlUtils.listOf(
XmlUtils.getSingleChildElement(
safetyLabelsEle,
XmlUtils.HR_TAG_THIRD_PARTY_VERIFICATION,
false)));
return new SafetyLabels(version, dataLabels, securityLabels, thirdPartyVerification);
}
}
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.asllib.marshallable;
import com.android.asllib.util.XmlUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.util.List;
/** Security Labels representation */
public class SecurityLabels implements AslMarshallable {
private final Boolean mIsDataDeletable;
private final Boolean mIsDataEncrypted;
public SecurityLabels(Boolean isDataDeletable, Boolean isDataEncrypted) {
this.mIsDataDeletable = isDataDeletable;
this.mIsDataEncrypted = isDataEncrypted;
}
/** Creates an on-device DOM element from the {@link SecurityLabels}. */
@Override
public List<Element> toOdDomElements(Document doc) {
Element ele = XmlUtils.createPbundleEleWithName(doc, XmlUtils.OD_NAME_SECURITY_LABELS);
if (mIsDataDeletable != null) {
ele.appendChild(
XmlUtils.createOdBooleanEle(
doc, XmlUtils.OD_NAME_IS_DATA_DELETABLE, mIsDataDeletable));
}
if (mIsDataEncrypted != null) {
ele.appendChild(
XmlUtils.createOdBooleanEle(
doc, XmlUtils.OD_NAME_IS_DATA_ENCRYPTED, mIsDataEncrypted));
}
return XmlUtils.listOf(ele);
}
}
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.asllib.marshallable;
import com.android.asllib.util.AslgenUtil;
import com.android.asllib.util.MalformedXmlException;
import com.android.asllib.util.XmlUtils;
import org.w3c.dom.Element;
import java.util.List;
public class SecurityLabelsFactory implements AslMarshallableFactory<SecurityLabels> {
/** Creates a {@link SecurityLabels} from the human-readable DOM element. */
@Override
public SecurityLabels createFromHrElements(List<Element> elements)
throws MalformedXmlException {
Element ele = XmlUtils.getSingleElement(elements);
if (ele == null) {
AslgenUtil.logI("No SecurityLabels found in hr format.");
return null;
}
Boolean isDataDeletable =
XmlUtils.getBoolAttr(ele, XmlUtils.HR_ATTR_IS_DATA_DELETABLE, false);
Boolean isDataEncrypted =
XmlUtils.getBoolAttr(ele, XmlUtils.HR_ATTR_IS_DATA_ENCRYPTED, false);
return new SecurityLabels(isDataDeletable, isDataEncrypted);
}
}
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.asllib.marshallable;
import com.android.asllib.util.XmlUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.util.List;
/** ThirdPartyVerification representation. */
public class ThirdPartyVerification implements AslMarshallable {
private final String mUrl;
public ThirdPartyVerification(String url) {
this.mUrl = url;
}
/** Creates an on-device DOM element from the {@link ThirdPartyVerification}. */
@Override
public List<Element> toOdDomElements(Document doc) {
Element ele =
XmlUtils.createPbundleEleWithName(doc, XmlUtils.OD_NAME_THIRD_PARTY_VERIFICATION);
ele.appendChild(XmlUtils.createOdStringEle(doc, XmlUtils.OD_NAME_URL, mUrl));
return XmlUtils.listOf(ele);
}
}
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.asllib.marshallable;
import com.android.asllib.util.AslgenUtil;
import com.android.asllib.util.MalformedXmlException;
import com.android.asllib.util.XmlUtils;
import org.w3c.dom.Element;
import java.util.List;
public class ThirdPartyVerificationFactory
implements AslMarshallableFactory<ThirdPartyVerification> {
/** Creates a {@link ThirdPartyVerification} from the human-readable DOM element. */
@Override
public ThirdPartyVerification createFromHrElements(List<Element> elements)
throws MalformedXmlException {
Element ele = XmlUtils.getSingleElement(elements);
if (ele == null) {
AslgenUtil.logI("No ThirdPartyVerification found in hr format.");
return null;
}
String url = XmlUtils.getStringAttr(ele, XmlUtils.HR_ATTR_URL);
return new ThirdPartyVerification(url);
}
}
......@@ -32,6 +32,8 @@ public class XmlUtils {
public static final String HR_TAG_DEVELOPER_INFO = "developer-info";
public static final String HR_TAG_APP_INFO = "app-info";
public static final String HR_TAG_DATA_LABELS = "data-labels";
public static final String HR_TAG_SECURITY_LABELS = "security-labels";
public static final String HR_TAG_THIRD_PARTY_VERIFICATION = "third-party-verification";
public static final String HR_TAG_DATA_ACCESSED = "data-accessed";
public static final String HR_TAG_DATA_COLLECTED = "data-collected";
public static final String HR_TAG_DATA_SHARED = "data-shared";
......@@ -46,6 +48,8 @@ public class XmlUtils {
public static final String HR_ATTR_DATA_TYPE = "dataType";
public static final String HR_ATTR_IS_COLLECTION_OPTIONAL = "isCollectionOptional";
public static final String HR_ATTR_IS_SHARING_OPTIONAL = "isSharingOptional";
public static final String HR_ATTR_IS_DATA_DELETABLE = "isDataDeletable";
public static final String HR_ATTR_IS_DATA_ENCRYPTED = "isDataEncrypted";
public static final String HR_ATTR_EPHEMERAL = "ephemeral";
public static final String HR_ATTR_PURPOSES = "purposes";
public static final String HR_ATTR_VERSION = "version";
......@@ -98,6 +102,8 @@ public class XmlUtils {
public static final String OD_NAME_VERSION = "version";
public static final String OD_NAME_URL = "url";
public static final String OD_NAME_SYSTEM_APP_SAFETY_LABEL = "system_app_safety_label";
public static final String OD_NAME_SECURITY_LABELS = "security_labels";
public static final String OD_NAME_THIRD_PARTY_VERIFICATION = "third_party_verification";
public static final String OD_NAME_DATA_LABELS = "data_labels";
public static final String OD_NAME_DATA_ACCESSED = "data_accessed";
public static final String OD_NAME_DATA_COLLECTED = "data_collected";
......@@ -105,6 +111,8 @@ public class XmlUtils {
public static final String OD_NAME_PURPOSES = "purposes";
public static final String OD_NAME_IS_COLLECTION_OPTIONAL = "is_collection_optional";
public static final String OD_NAME_IS_SHARING_OPTIONAL = "is_sharing_optional";
public static final String OD_NAME_IS_DATA_DELETABLE = "is_data_deletable";
public static final String OD_NAME_IS_DATA_ENCRYPTED = "is_data_encrypted";
public static final String OD_NAME_EPHEMERAL = "ephemeral";
public static final String TRUE_STR = "true";
......
......@@ -17,9 +17,15 @@
package com.android.asllib;
import com.android.asllib.marshallable.AndroidSafetyLabelTest;
import com.android.asllib.marshallable.AppInfoTest;
import com.android.asllib.marshallable.DataCategoryTest;
import com.android.asllib.marshallable.DataLabelsTest;
import com.android.asllib.marshallable.DeveloperInfoTest;
import com.android.asllib.marshallable.SafetyLabelsTest;
import com.android.asllib.marshallable.SecurityLabelsTest;
import com.android.asllib.marshallable.SystemAppSafetyLabelTest;
import com.android.asllib.marshallable.ThirdPartyVerificationTest;
import com.android.asllib.marshallable.TransparencyInfoTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
......@@ -28,8 +34,14 @@ import org.junit.runners.Suite;
@Suite.SuiteClasses({
AslgenTests.class,
AndroidSafetyLabelTest.class,
DeveloperInfoTest.class,
AppInfoTest.class,
DataCategoryTest.class,
DataLabelsTest.class,
DeveloperInfoTest.class,
SafetyLabelsTest.class,
SecurityLabelsTest.class,
SystemAppSafetyLabelTest.class,
ThirdPartyVerificationTest.class,
TransparencyInfoTest.class
})
public class AllTests {}
......@@ -34,7 +34,8 @@ import java.util.List;
@RunWith(JUnit4.class)
public class AslgenTests {
private static final String VALID_MAPPINGS_PATH = "com/android/asllib/validmappings";
private static final List<String> VALID_MAPPINGS_SUBDIRS = List.of("location", "contacts");
private static final List<String> VALID_MAPPINGS_SUBDIRS =
List.of("location", "contacts", "general");
private static final String HR_XML_FILENAME = "hr.xml";
private static final String OD_XML_FILENAME = "od.xml";
......
......@@ -32,6 +32,9 @@ public class SafetyLabelsTest {
private static final String MISSING_VERSION_FILE_NAME = "missing-version.xml";
private static final String VALID_EMPTY_FILE_NAME = "valid-empty.xml";
private static final String WITH_DATA_LABELS_FILE_NAME = "with-data-labels.xml";
private static final String WITH_SECURITY_LABELS_FILE_NAME = "with-security-labels.xml";
private static final String WITH_THIRD_PARTY_VERIFICATION_FILE_NAME =
"with-third-party-verification.xml";
private Document mDoc = null;
......@@ -62,6 +65,20 @@ public class SafetyLabelsTest {
testHrToOdSafetyLabels(WITH_DATA_LABELS_FILE_NAME);
}
/** Test for safety labels with security labels. */
@Test
public void testSafetyLabelsWithSecurityLabels() throws Exception {
System.out.println("starting testSafetyLabelsWithSecurityLabels.");
testHrToOdSafetyLabels(WITH_SECURITY_LABELS_FILE_NAME);
}
/** Test for safety labels with third party verification. */
@Test
public void testSafetyLabelsWithThirdPartyVerification() throws Exception {
System.out.println("starting testSafetyLabelsWithThirdPartyVerification.");
testHrToOdSafetyLabels(WITH_THIRD_PARTY_VERIFICATION_FILE_NAME);
}
private void hrToOdExpectException(String fileName) {
TestUtils.hrToOdExpectException(new SafetyLabelsFactory(), SAFETY_LABELS_HR_PATH, fileName);
}
......
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.asllib.marshallable;
import com.android.asllib.testutils.TestUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.w3c.dom.Document;
import java.nio.file.Paths;
import java.util.List;
@RunWith(JUnit4.class)
public class SecurityLabelsTest {
private static final String SECURITY_LABELS_HR_PATH = "com/android/asllib/securitylabels/hr";
private static final String SECURITY_LABELS_OD_PATH = "com/android/asllib/securitylabels/od";
public static final List<String> OPTIONAL_FIELD_NAMES =
List.of("isDataDeletable", "isDataEncrypted");
private static final String ALL_FIELDS_VALID_FILE_NAME = "all-fields-valid.xml";
private Document mDoc = null;
/** Logic for setting up tests (empty if not yet needed). */
public static void main(String[] params) throws Exception {}
@Before
public void setUp() throws Exception {
System.out.println("set up.");
mDoc = TestUtils.document();
}
/** Test for all fields valid. */
@Test
public void testAllFieldsValid() throws Exception {
System.out.println("starting testAllFieldsValid.");
testHrToOdSecurityLabels(ALL_FIELDS_VALID_FILE_NAME);
}
/** Tests missing optional fields passes. */
@Test
public void testMissingOptionalFields() throws Exception {
for (String optField : OPTIONAL_FIELD_NAMES) {
var ele =
TestUtils.getElementsFromResource(
Paths.get(SECURITY_LABELS_HR_PATH, ALL_FIELDS_VALID_FILE_NAME));
ele.get(0).removeAttribute(optField);
SecurityLabels securityLabels = new SecurityLabelsFactory().createFromHrElements(ele);
securityLabels.toOdDomElements(mDoc);
}
}
private void testHrToOdSecurityLabels(String fileName) throws Exception {
TestUtils.testHrToOd(
mDoc,
new SecurityLabelsFactory(),
SECURITY_LABELS_HR_PATH,
SECURITY_LABELS_OD_PATH,
fileName);
}
}
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.asllib.marshallable;
import com.android.asllib.testutils.TestUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.w3c.dom.Document;
@RunWith(JUnit4.class)
public class ThirdPartyVerificationTest {
private static final String THIRD_PARTY_VERIFICATION_HR_PATH =
"com/android/asllib/thirdpartyverification/hr";
private static final String THIRD_PARTY_VERIFICATION_OD_PATH =
"com/android/asllib/thirdpartyverification/od";
private static final String VALID_FILE_NAME = "valid.xml";
private static final String MISSING_URL_FILE_NAME = "missing-url.xml";
private Document mDoc = null;
/** Logic for setting up tests (empty if not yet needed). */
public static void main(String[] params) throws Exception {}
@Before
public void setUp() throws Exception {
System.out.println("set up.");
mDoc = TestUtils.document();
}
/** Test for valid. */
@Test
public void testValid() throws Exception {
System.out.println("starting testValid.");
testHrToOdThirdPartyVerification(VALID_FILE_NAME);
}
/** Tests missing url. */
@Test
public void testMissingUrl() throws Exception {
System.out.println("starting testMissingUrl.");
hrToOdExpectException(MISSING_URL_FILE_NAME);
}
private void hrToOdExpectException(String fileName) {
TestUtils.hrToOdExpectException(
new ThirdPartyVerificationFactory(), THIRD_PARTY_VERIFICATION_HR_PATH, fileName);
}
private void testHrToOdThirdPartyVerification(String fileName) throws Exception {
TestUtils.testHrToOd(
mDoc,
new ThirdPartyVerificationFactory(),
THIRD_PARTY_VERIFICATION_HR_PATH,
THIRD_PARTY_VERIFICATION_OD_PATH,
fileName);
}
}
<safety-labels version="12345">
<security-labels
isDataDeletable="true"
isDataEncrypted="false"
/>
</safety-labels>
\ No newline at end of file
<safety-labels version="12345">
<third-party-verification url="www.example.com">
</third-party-verification>
</safety-labels>
\ No newline at end of file
<pbundle_as_map name="safety_labels">
<long name="version" value="12345"/>
<pbundle_as_map name="security_labels">
<boolean name="is_data_deletable" value="true" />
<boolean name="is_data_encrypted" value="false" />
</pbundle_as_map>
</pbundle_as_map>
\ No newline at end of file
<pbundle_as_map name="safety_labels">
<long name="version" value="12345"/>
<pbundle_as_map name="third_party_verification">
<string name="url" value="www.example.com"/>
</pbundle_as_map>
</pbundle_as_map>
\ No newline at end of file
<security-labels
isDataDeletable="true"
isDataEncrypted="false">
</security-labels>
\ No newline at end of file
<pbundle_as_map name="security_labels">
<boolean name="is_data_deletable" value="true" />
<boolean name="is_data_encrypted" value="false" />
</pbundle_as_map>
\ No newline at end of file
<third-party-verification></third-party-verification>
\ No newline at end of file
<third-party-verification url="www.example.com"></third-party-verification>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment