Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5ae5413a authored by Max Loh's avatar Max Loh
Browse files

aslgen versioning logic part 2 (improving unit test coverage)

Bug: 350566067
Test: Unit tests
Flag: NONE (Command-line tool which doesn't affect Android functionality)
Change-Id: I62e486bf7bc559041b66540bf53285704227316b
parent 3f83d163
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -115,6 +115,9 @@ public class AppInfoFactory implements AslMarshallableFactory<AppInfo> {
    /** Creates a {@link AppInfo} from the human-readable DOM element. */
    public AppInfo createFromHrElement(Element appInfoEle, long version)
            throws MalformedXmlException {
        if (appInfoEle == null) {
            return null;
        }
        XmlUtils.throwIfExtraneousAttributes(
                appInfoEle, XmlUtils.getMostRecentVersion(mRecognizedHrAttrs, version));
        XmlUtils.throwIfExtraneousChildrenHr(
@@ -184,6 +187,9 @@ public class AppInfoFactory implements AslMarshallableFactory<AppInfo> {
    /** Creates an {@link AslMarshallableFactory} from on-device DOM elements */
    public AppInfo createFromOdElement(Element appInfoEle, long version)
            throws MalformedXmlException {
        if (appInfoEle == null) {
            return null;
        }
        XmlUtils.throwIfExtraneousChildrenOd(
                appInfoEle, XmlUtils.getMostRecentVersion(mRecognizedOdEleNames, version));
        var requiredOdEles = XmlUtils.getMostRecentVersion(mRequiredOdEles, version);
+7 −1
Original line number Diff line number Diff line
@@ -20,8 +20,11 @@ import com.android.asllib.marshallable.AndroidSafetyLabelTest;
import com.android.asllib.marshallable.AppInfoTest;
import com.android.asllib.marshallable.DataLabelsTest;
import com.android.asllib.marshallable.DataTypeEqualityTest;
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;
@@ -36,6 +39,9 @@ import org.junit.runners.Suite;
    DataTypeEqualityTest.class,
    SafetyLabelsTest.class,
    SystemAppSafetyLabelTest.class,
    TransparencyInfoTest.class
    TransparencyInfoTest.class,
    DeveloperInfoTest.class,
    SecurityLabelsTest.class,
    ThirdPartyVerificationTest.class
})
public class AllTests {}
+39 −6
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import org.junit.runners.JUnit4;
import org.w3c.dom.Element;

import java.nio.file.Paths;
import java.util.List;

@RunWith(JUnit4.class)
public class AndroidSafetyLabelTest {
@@ -37,12 +38,16 @@ public class AndroidSafetyLabelTest {
            "com/android/asllib/androidsafetylabel/od";

    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 VALID_V2_FILE_NAME = "valid-empty.xml";
    private static final String VALID_V1_FILE_NAME = "valid-v1.xml";
    private static final String WITH_SAFETY_LABELS_FILE_NAME = "with-safety-labels.xml";
    private static final String WITH_SYSTEM_APP_SAFETY_LABEL_FILE_NAME =
            "with-system-app-safety-label.xml";
    private static final String WITH_TRANSPARENCY_INFO_FILE_NAME = "with-transparency-info.xml";

    public static final List<String> REQUIRED_FIELD_NAMES_OD_V2 =
            List.of("system_app_safety_label", "transparency_info");

    @Before
    public void setUp() throws Exception {
        System.out.println("set up.");
@@ -56,12 +61,12 @@ public class AndroidSafetyLabelTest {
        odToHrExpectException(MISSING_VERSION_FILE_NAME);
    }

    /** Test for android safety label valid empty. */
    /** Test for android safety label valid v2. */
    @Test
    public void testAndroidSafetyLabelValidEmptyFile() throws Exception {
        System.out.println("starting testAndroidSafetyLabelValidEmptyFile.");
        testHrToOdAndroidSafetyLabel(VALID_EMPTY_FILE_NAME);
        testOdToHrAndroidSafetyLabel(VALID_EMPTY_FILE_NAME);
    public void testAndroidSafetyLabelValidV2File() throws Exception {
        System.out.println("starting testAndroidSafetyLabelValidV2File.");
        testHrToOdAndroidSafetyLabel(VALID_V2_FILE_NAME);
        testOdToHrAndroidSafetyLabel(VALID_V2_FILE_NAME);
    }

    /** Test for android safety label with safety labels. */
@@ -72,6 +77,34 @@ public class AndroidSafetyLabelTest {
        testOdToHrAndroidSafetyLabel(WITH_SAFETY_LABELS_FILE_NAME);
    }

    /** Tests missing required fields fails, V2. */
    @Test
    public void testMissingRequiredFieldsOdV2() throws Exception {
        for (String reqField : REQUIRED_FIELD_NAMES_OD_V2) {
            System.out.println("testing missing required field od v2: " + reqField);
            var ele =
                    TestUtils.getElementFromResource(
                            Paths.get(ANDROID_SAFETY_LABEL_OD_PATH, VALID_V2_FILE_NAME));
            TestUtils.removeOdChildEleWithName(ele, reqField);
            assertThrows(
                    MalformedXmlException.class,
                    () -> new AndroidSafetyLabelFactory().createFromOdElement(ele));
        }
    }

    /** Tests missing optional fields succeeds, V1. */
    @Test
    public void testMissingOptionalFieldsOdV1() throws Exception {
        for (String reqField : REQUIRED_FIELD_NAMES_OD_V2) {
            System.out.println("testing missing optional field od v1: " + reqField);
            var ele =
                    TestUtils.getElementFromResource(
                            Paths.get(ANDROID_SAFETY_LABEL_OD_PATH, VALID_V1_FILE_NAME));
            TestUtils.removeOdChildEleWithName(ele, reqField);
            var unused = new AndroidSafetyLabelFactory().createFromOdElement(ele);
        }
    }

    private void hrToOdExpectException(String fileName) {
        assertThrows(
                MalformedXmlException.class,
+0 −2
Original line number Diff line number Diff line
@@ -35,8 +35,6 @@ import javax.xml.parsers.ParserConfigurationException;

@RunWith(JUnit4.class)
public class DataLabelsTest {
    private static final long DEFAULT_VERSION = 2L;

    private static final String DATA_LABELS_HR_PATH = "com/android/asllib/datalabels/hr";
    private static final String DATA_LABELS_OD_PATH = "com/android/asllib/datalabels/od";

+139 −0
Original line number Diff line number Diff line
/*
 * 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 static org.junit.Assert.assertThrows;

import com.android.asllib.testutils.TestUtils;
import com.android.asllib.util.MalformedXmlException;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.w3c.dom.Element;

import java.nio.file.Paths;
import java.util.List;

@RunWith(JUnit4.class)
public class DeveloperInfoTest {
    private static final String DEVELOPER_INFO_HR_PATH = "com/android/asllib/developerinfo/hr";
    private static final String DEVELOPER_INFO_OD_PATH = "com/android/asllib/developerinfo/od";
    public static final List<String> REQUIRED_FIELD_NAMES =
            List.of("address", "countryRegion", "email", "name", "relationship");
    public static final List<String> REQUIRED_FIELD_NAMES_OD =
            List.of("address", "country_region", "email", "name", "relationship");
    public static final List<String> OPTIONAL_FIELD_NAMES = List.of("website", "registryId");
    public static final List<String> OPTIONAL_FIELD_NAMES_OD =
            List.of("website", "app_developer_registry_id");

    private static final String ALL_FIELDS_VALID_FILE_NAME = "all-fields-valid.xml";

    /** 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.");
    }

    /** Test for all fields valid. */
    @Test
    public void testAllFieldsValid() throws Exception {
        System.out.println("starting testAllFieldsValid.");
        testHrToOdDeveloperInfo(ALL_FIELDS_VALID_FILE_NAME);
        testOdToHrDeveloperInfo(ALL_FIELDS_VALID_FILE_NAME);
    }

    /** Tests missing required fields fails. */
    @Test
    public void testMissingRequiredFields() throws Exception {
        System.out.println("Starting testMissingRequiredFields");
        for (String reqField : REQUIRED_FIELD_NAMES) {
            System.out.println("testing missing required field: " + reqField);
            var developerInfoEle =
                    TestUtils.getElementFromResource(
                            Paths.get(DEVELOPER_INFO_HR_PATH, ALL_FIELDS_VALID_FILE_NAME));
            developerInfoEle.removeAttribute(reqField);

            assertThrows(
                    MalformedXmlException.class,
                    () -> new DeveloperInfoFactory().createFromHrElement(developerInfoEle));
        }

        for (String reqField : REQUIRED_FIELD_NAMES_OD) {
            System.out.println("testing missing required field od: " + reqField);
            var developerInfoEle =
                    TestUtils.getElementFromResource(
                            Paths.get(DEVELOPER_INFO_OD_PATH, ALL_FIELDS_VALID_FILE_NAME));
            TestUtils.removeOdChildEleWithName(developerInfoEle, reqField);

            assertThrows(
                    MalformedXmlException.class,
                    () -> new DeveloperInfoFactory().createFromOdElement(developerInfoEle));
        }
    }

    /** Tests missing optional fields passes. */
    @Test
    public void testMissingOptionalFields() throws Exception {
        for (String optField : OPTIONAL_FIELD_NAMES) {
            var developerInfoEle =
                    TestUtils.getElementFromResource(
                            Paths.get(DEVELOPER_INFO_HR_PATH, ALL_FIELDS_VALID_FILE_NAME));
            developerInfoEle.removeAttribute(optField);
            DeveloperInfo developerInfo =
                    new DeveloperInfoFactory().createFromHrElement(developerInfoEle);
            developerInfo.toOdDomElement(TestUtils.document());
        }

        for (String optField : OPTIONAL_FIELD_NAMES_OD) {
            var developerInfoEle =
                    TestUtils.getElementFromResource(
                            Paths.get(DEVELOPER_INFO_OD_PATH, ALL_FIELDS_VALID_FILE_NAME));
            TestUtils.removeOdChildEleWithName(developerInfoEle, optField);
            DeveloperInfo developerInfo =
                    new DeveloperInfoFactory().createFromOdElement(developerInfoEle);
            developerInfo.toHrDomElement(TestUtils.document());
        }
    }

    private void testHrToOdDeveloperInfo(String fileName) throws Exception {
        var doc = TestUtils.document();
        DeveloperInfo developerInfo =
                new DeveloperInfoFactory()
                        .createFromHrElement(
                                TestUtils.getElementFromResource(
                                        Paths.get(DEVELOPER_INFO_HR_PATH, fileName)));
        Element developerInfoEle = developerInfo.toOdDomElement(doc);
        doc.appendChild(developerInfoEle);
        TestUtils.testFormatToFormat(doc, Paths.get(DEVELOPER_INFO_OD_PATH, fileName));
    }

    private void testOdToHrDeveloperInfo(String fileName) throws Exception {
        var doc = TestUtils.document();
        DeveloperInfo developerInfo =
                new DeveloperInfoFactory()
                        .createFromOdElement(
                                TestUtils.getElementFromResource(
                                        Paths.get(DEVELOPER_INFO_OD_PATH, fileName)));
        Element developerInfoEle = developerInfo.toHrDomElement(doc);
        doc.appendChild(developerInfoEle);
        TestUtils.testFormatToFormat(doc, Paths.get(DEVELOPER_INFO_HR_PATH, fileName));
    }
}
Loading