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

Commit 7f539408 authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge "add aconfig device test util" into main am: 3f86fd53

parents 00d74d02 3f86fd53
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
@@ -73,3 +73,23 @@ java_library_host {
    name: "aconfig_host_device_paths_java",
    srcs: [":libaconfig_java_host_device_paths_src"],
}

genrule {
    name: "java_device_paths_test_util_src",
    srcs: ["src/DeviceProtosTestUtilTemplate.java"],
    out: ["DeviceProtosTestUtil.java"],
    tool_files: ["partition_aconfig_flags_paths.txt"],
    cmd: "sed -e '/TEMPLATE/{r$(location partition_aconfig_flags_paths.txt)' -e 'd}' $(in) > $(out)",
}

java_library {
    name: "aconfig_device_paths_java_util",
    srcs: [":java_device_paths_test_util_src"],
    static_libs: [
        "libaconfig_java_proto_nano",
    ],
    sdk_version: "core_platform",
    apex_available: [
        "//apex_available:platform",
    ],
}
+99 −0
Original line number Diff line number Diff line
/*
 * 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 android.aconfig;

import android.aconfig.nano.Aconfig.parsed_flag;
import android.aconfig.nano.Aconfig.parsed_flags;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/** @hide */
public class DeviceProtosTestUtil {
    public static final String[] PATHS = {
        TEMPLATE
    };

    private static final String APEX_DIR = "/apex/";
    private static final String APEX_ACONFIG_PATH_SUFFIX = "/etc/aconfig_flags.pb";
    private static final String SYSTEM_APEX_DIR = "/system/apex";

    /**
     * Returns a list of all on-device aconfig protos.
     *
     * <p>May throw an exception if the protos can't be read at the call site. For example, some of
     * the protos are in the apex/ partition, which is mounted somewhat late in the boot process.
     *
     * @throws IOException if we can't read one of the protos yet
     * @return a list of all on-device aconfig protos
     */
    public static List<parsed_flag> loadAndParseFlagProtos() throws IOException {
        ArrayList<parsed_flag> result = new ArrayList();

        for (String path : parsedFlagsProtoPaths()) {
            try (FileInputStream inputStream = new FileInputStream(path)) {
                parsed_flags parsedFlags = parsed_flags.parseFrom(inputStream.readAllBytes());
                for (parsed_flag flag : parsedFlags.parsedFlag) {
                    result.add(flag);
                }
            }
        }

        return result;
    }

    /**
     * Returns the list of all on-device aconfig protos paths.
     *
     * @hide
     */
    public static List<String> parsedFlagsProtoPaths() {
        ArrayList<String> paths = new ArrayList(Arrays.asList(PATHS));

        File apexDirectory = new File(SYSTEM_APEX_DIR);
        if (!apexDirectory.isDirectory()) {
            return paths;
        }

        File[] subdirs = apexDirectory.listFiles();
        if (subdirs == null) {
            return paths;
        }

        for (File prefix : subdirs) {
            // For each mainline modules, there are two directories, one <modulepackage>/,
            // and one <modulepackage>@<versioncode>/. Just read the former.
            if (!prefix.getAbsolutePath().endsWith(".apex")) {
                continue;
            }

            String apexName = prefix.getName();
            apexName = apexName.substring(0, apexName.length() - 5);

            File protoPath = new File(APEX_DIR + apexName + APEX_ACONFIG_PATH_SUFFIX);
            if (!protoPath.exists()) {
                continue;
            }

            paths.add(protoPath.getAbsolutePath());
        }
        return paths;
    }
}
+35 −0
Original line number Diff line number Diff line
// 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 {
    default_team: "trendy_team_android_core_experiments",
    default_applicable_licenses: ["Android-Apache-2.0"],
}

android_test {
    name: "aconfig_device_paths_java_test",
    srcs: [
        "src/**/*.java",
    ],
    static_libs: [
        "androidx.test.runner",
        "junit",
        "aconfig_device_paths_java_util",
    ],
    test_suites: [
        "general-tests",
    ],
    platform_apis: true,
    certificate: "platform",
}
+27 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
  ~ 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.
  -->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="android.aconfig.storage.test">
    <application>
        <uses-library android:name="android.test.runner" />
    </application>

    <instrumentation android:name="androidx.test.runner.AndroidJUnitRunner"
                     android:targetPackage="android.aconfig.storage.test" />

</manifest>
+52 −0
Original line number Diff line number Diff line
/*
 * 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 android.aconfig.test;

import static org.junit.Assert.assertTrue;

import android.aconfig.DeviceProtosTestUtil;
import android.aconfig.nano.Aconfig.parsed_flag;

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

import java.util.List;
import java.util.Set;

@RunWith(JUnit4.class)
public class DeviceProtosTestUtilTest {

    private static final Set<String> PLATFORM_CONTAINERS = Set.of("system", "vendor", "product");

    @Test
    public void testDeviceProtos_loadAndParseFlagProtos() throws Exception {
        List<parsed_flag> flags = DeviceProtosTestUtil.loadAndParseFlagProtos();
        int platformFlags = 0;
        int mainlineFlags = 0;
        for (parsed_flag pf : flags) {
            if (PLATFORM_CONTAINERS.contains(pf.container)) {
                platformFlags++;
            } else {
                mainlineFlags++;
            }
        }

        assertTrue(platformFlags > 0);
        assertTrue(mainlineFlags > 0);
    }
}