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

Commit 769cee8e authored by Max Loh's avatar Max Loh
Browse files

ASL allow DataType to be easily compared (for lint annotation logic)

For Android Studio annotations checking, the linter can parse both the annotations and human-readable XML into corresponding ASL java objects (the human-readable XML parsing logic already exists in this java library). The linter can then nestedly iterate through the map of data categories and map of data types; if corresponding entries don't exist in the XML-parsed ASL or the DataType are not equal, it can be flagged to the developer.

Bug: 329902686
Test: Unit tests.
Change-Id: I3263157c57489dd782e70a385bc6f364b47655b4
parent f7caa4df
Loading
Loading
Loading
Loading
+39 −0
Original line number Diff line number Diff line
@@ -21,7 +21,9 @@ import com.android.asllib.util.XmlUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
@@ -76,6 +78,43 @@ public class DataType implements AslMarshallable {
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null || obj.getClass() != this.getClass()) {
            return false;
        }
        DataType objAsDataType = (DataType) obj;
        return Objects.equals(this.mDataTypeName, objAsDataType.mDataTypeName)
                && Objects.equals(
                        new HashSet<>(this.mPurposes), new HashSet<>(objAsDataType.mPurposes))
                && Objects.equals(this.mIsCollectionOptional, objAsDataType.mIsCollectionOptional)
                && Objects.equals(this.mIsSharingOptional, objAsDataType.mIsSharingOptional)
                && Objects.equals(this.mEphemeral, objAsDataType.mEphemeral);
    }

    @Override
    public int hashCode() {
        int result = 1;
        int prime = 31;
        result =
                (prime * result) + (this.mDataTypeName != null ? this.mDataTypeName.hashCode() : 0);
        result =
                (prime * result)
                        + (this.mPurposes != null ? new HashSet<>(this.mPurposes).hashCode() : 0);
        result =
                (prime * result)
                        + (this.mIsCollectionOptional != null
                                ? this.mIsCollectionOptional.hashCode()
                                : 0);
        result =
                (prime * result)
                        + (this.mIsSharingOptional != null
                                ? this.mIsSharingOptional.hashCode()
                                : 0);
        result = (prime * result) + (this.mEphemeral != null ? this.mEphemeral.hashCode() : 0);
        return result;
    }

    private final String mDataTypeName;

    private final List<Purpose> mPurposes;
+2 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ 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.DataTypeEqualityTest;
import com.android.asllib.marshallable.DeveloperInfoTest;
import com.android.asllib.marshallable.SafetyLabelsTest;
import com.android.asllib.marshallable.SecurityLabelsTest;
@@ -37,6 +38,7 @@ import org.junit.runners.Suite;
    AppInfoTest.class,
    DataCategoryTest.class,
    DataLabelsTest.class,
    DataTypeEqualityTest.class,
    DeveloperInfoTest.class,
    SafetyLabelsTest.class,
    SecurityLabelsTest.class,
+170 −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.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RunWith(JUnit4.class)
public class DataTypeEqualityTest {

    public static final List<String> OPTIONAL_FIELD_NAMES =
            List.of("isDataDeletable", "isDataEncrypted");
    public static final List<String> OPTIONAL_FIELD_NAMES_OD =
            List.of("is_data_deletable", "is_data_encrypted");

    /** 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 equality different order. */
    @Test
    public void testEqualityDifferentOrder() throws Exception {
        System.out.println("starting testEqualityDifferentOrder.");
        DataType dataType1 =
                new DataType(
                        "datatype1",
                        Arrays.asList(
                                DataType.Purpose.ADVERTISING, DataType.Purpose.PERSONALIZATION),
                        true,
                        false,
                        null);
        DataType dataType2 =
                new DataType(
                        "datatype1",
                        Arrays.asList(
                                DataType.Purpose.PERSONALIZATION, DataType.Purpose.ADVERTISING),
                        true,
                        false,
                        null);
        assertEquals(dataType1, dataType2);
        assertEquals(dataType2, dataType1);
    }

    /** Test for contains different order. */
    @Test
    public void testContainsDifferentOrder() throws Exception {
        System.out.println("starting testContainsDifferentOrder.");
        DataType dataType1 =
                new DataType(
                        "datatype1",
                        Arrays.asList(
                                DataType.Purpose.ADVERTISING, DataType.Purpose.PERSONALIZATION),
                        true,
                        false,
                        null);
        DataType dataType2 =
                new DataType(
                        "datatype1",
                        Arrays.asList(
                                DataType.Purpose.PERSONALIZATION, DataType.Purpose.ADVERTISING),
                        true,
                        false,
                        null);
        Set<DataType> set = new HashSet<>();
        set.add(dataType1);
        assertTrue(set.contains(dataType2));
    }

    /** Test for inequality. */
    @Test
    public void testInequality() throws Exception {
        System.out.println("starting testInequality.");
        DataType dataType1 =
                new DataType(
                        "datatype1",
                        Arrays.asList(
                                DataType.Purpose.ADVERTISING, DataType.Purpose.PERSONALIZATION),
                        true,
                        false,
                        null);
        DataType dataType2 =
                new DataType(
                        "datatype1",
                        Arrays.asList(DataType.Purpose.PERSONALIZATION),
                        true,
                        false,
                        null);
        assertNotEquals(dataType1, dataType2);
        assertNotEquals(dataType2, dataType1);
    }

    /** Test for inequality bool. */
    @Test
    public void testInequalityBool() throws Exception {
        System.out.println("starting testInequalityBool.");
        DataType dataType1 =
                new DataType(
                        "datatype1",
                        Arrays.asList(
                                DataType.Purpose.ADVERTISING, DataType.Purpose.PERSONALIZATION),
                        true,
                        false,
                        null);
        DataType dataType2 =
                new DataType(
                        "datatype1",
                        Arrays.asList(
                                DataType.Purpose.ADVERTISING, DataType.Purpose.PERSONALIZATION),
                        true,
                        false,
                        true);
        assertNotEquals(dataType1, dataType2);
        assertNotEquals(dataType2, dataType1);
    }

    /** Test for does not contain. */
    @Test
    public void testDoesNotContain() throws Exception {
        System.out.println("starting testDoesNotContain.");
        System.out.println("starting testContainsDifferentOrder.");
        DataType dataType1 =
                new DataType(
                        "datatype1",
                        Arrays.asList(
                                DataType.Purpose.ADVERTISING, DataType.Purpose.PERSONALIZATION),
                        true,
                        false,
                        null);
        DataType dataType2 =
                new DataType(
                        "datatype1",
                        Arrays.asList(DataType.Purpose.PERSONALIZATION),
                        true,
                        false,
                        null);
        Set<DataType> set = new HashSet<>();
        set.add(dataType1);
        assertFalse(set.contains(dataType2));
    }
}