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

Commit aa2bed96 authored by Eric Biggers's avatar Eric Biggers
Browse files

Make FsVerityTest explicitly use an uncompressed file

Fix a test failure on devices that have 'compress_extension=*' in their
fstab by explicitly setting the test file to uncompressed.

Bug: 440971743
Test: atest FsVerityTest # Cuttlefish with current fstab
Test: atest FsVerityTest # Cuttlefish with 'compress_extension=*' added to fstab
Test: atest FsVerityTest # Cuttlefish with 'fscompress' removed from fstab
Flag: EXEMPT TEST_ONLY
Change-Id: Ib8ce595be1dd75269a09c78ea866ac391f4f2d89
parent cb8e3d62
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -27,4 +27,12 @@ android_test_helper_app {
    manifest: "AndroidManifest.xml",
    srcs: ["src/**/*.java"],
    static_libs: ["compatibility-device-util-axt"],
    jni_libs: ["libfsveritytestapp_jni"],
}

cc_library {
    name: "libfsveritytestapp_jni",
    header_libs: ["jni_headers"],
    shared_libs: ["libnativehelper"],
    srcs: ["jni/*.cpp"],
}
+44 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.
 */

#include <errno.h>
#include <jni.h>
#include <linux/fs.h>
#include <nativehelper/JNIPlatformHelp.h>
#include <string.h>
#include <sys/ioctl.h>

#include <string>

extern "C" JNIEXPORT void JNICALL Java_com_android_fsverity_Helper_disableCompression(
        JNIEnv* env, [[maybe_unused]] jobject clazz, jobject fdObj) {
    int fd = jniGetFDFromFileDescriptor(env, fdObj);

    unsigned int flags = 0;
    if (ioctl(fd, FS_IOC_GETFLAGS, &flags) != 0) {
        std::string msg = std::string("FS_IOC_GETFLAGS failed: ") + strerror(errno);
        env->ThrowNew(env->FindClass("java/lang/RuntimeException"), msg.c_str());
        return;
    }
    flags &= ~FS_COMPR_FL;
    flags |= FS_NOCOMP_FL;
    if (ioctl(fd, FS_IOC_SETFLAGS, &flags) != 0 &&
        // EOPNOTSUPP is expected if the filesystem doesn't have the compression feature flag.
        errno != EOPNOTSUPP) {
        std::string msg = std::string("FS_IOC_SETFLAGS failed: ") + strerror(errno);
        env->ThrowNew(env->FindClass("java/lang/RuntimeException"), msg.c_str());
    }
}
+18 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import com.android.compatibility.common.util.AdoptShellPermissionsRule;
import org.junit.Rule;
import org.junit.Test;

import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
@@ -51,6 +52,10 @@ public class Helper {

    private static final String FILENAME = "test.file";

    static {
        System.loadLibrary("fsveritytestapp_jni");
    }

    @Rule
    public final AdoptShellPermissionsRule mAdoptShellPermissionsRule =
            new AdoptShellPermissionsRule(
@@ -72,6 +77,7 @@ public class Helper {
        byte[] bytes = new byte[8192];
        Arrays.fill(bytes, (byte) '1');
        try (FileOutputStream os = context.openFileOutput(basename, Context.MODE_PRIVATE)) {
            disableCompression(os.getFD());
            for (int i = 0; i < fileSize; i += bytes.length) {
                if (i + bytes.length > fileSize) {
                    os.write(bytes, 0, fileSize % bytes.length);
@@ -86,6 +92,18 @@ public class Helper {
        fim.setupFsVerity(context.getFileStreamPath(basename));
    }

    /**
     * Explicitly disables compression on the given file. This is needed in case automatic
     * compression is enabled filesystem-wide. The tests expect an uncompressed file.
     *
     * <p>This uses JNI because these flags are accessible only via ioctl().
     *
     * <p>Does nothing if the filesystem does not support compression.
     *
     * @throws RuntimeException if an unexpected error occurred
     */
    private static native void disableCompression(FileDescriptor fd);

    private static long getPageSize() {
        String arch = System.getProperty("os.arch");
        Log.d(TAG, "os.arch=" + arch);