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

Commit 2237e8ca authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Remove BinaryFileOperations.java and its test from...

Merge "Remove BinaryFileOperations.java and its test from services/core/java/com/android/server/integrity/parser" into main
parents 6be50758 fe523054
Loading
Loading
Loading
Loading
+0 −78
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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.server.integrity.parser;

import static com.android.server.integrity.model.ComponentBitSize.IS_HASHED_BITS;
import static com.android.server.integrity.model.ComponentBitSize.VALUE_SIZE_BITS;

import android.content.integrity.IntegrityUtils;

import com.android.server.integrity.model.BitInputStream;

import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * Helper methods for reading standard data structures from {@link BitInputStream}.
 */
public class BinaryFileOperations {

    /**
     * Read an string value with the given size and hash status from a {@code BitInputStream}.
     *
     * If the value is hashed, get the hex-encoding of the value. Serialized values are in raw form.
     * All hashed values are hex-encoded.
     */
    public static String getStringValue(BitInputStream bitInputStream) throws IOException {
        boolean isHashedValue = bitInputStream.getNext(IS_HASHED_BITS) == 1;
        int valueSize = bitInputStream.getNext(VALUE_SIZE_BITS);
        return getStringValue(bitInputStream, valueSize, isHashedValue);
    }

    /**
     * Read an string value with the given size and hash status from a {@code BitInputStream}.
     *
     * If the value is hashed, get the hex-encoding of the value. Serialized values are in raw form.
     * All hashed values are hex-encoded.
     */
    public static String getStringValue(
            BitInputStream bitInputStream, int valueSize, boolean isHashedValue)
            throws IOException {
        if (!isHashedValue) {
            StringBuilder value = new StringBuilder();
            while (valueSize-- > 0) {
                value.append((char) bitInputStream.getNext(/* numOfBits= */ 8));
            }
            return value.toString();
        }
        ByteBuffer byteBuffer = ByteBuffer.allocate(valueSize);
        while (valueSize-- > 0) {
            byteBuffer.put((byte) (bitInputStream.getNext(/* numOfBits= */ 8) & 0xFF));
        }
        return IntegrityUtils.getHexDigest(byteBuffer.array());
    }

    /** Read an integer value from a {@code BitInputStream}. */
    public static int getIntValue(BitInputStream bitInputStream) throws IOException {
        return bitInputStream.getNext(/* numOfBits= */ 32);
    }

    /** Read an boolean value from a {@code BitInputStream}. */
    public static boolean getBooleanValue(BitInputStream bitInputStream) throws IOException {
        return bitInputStream.getNext(/* numOfBits= */ 1) == 1;
    }
}
+0 −116
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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.server.integrity.parser;

import static com.android.server.integrity.model.ComponentBitSize.VALUE_SIZE_BITS;
import static com.android.server.integrity.parser.BinaryFileOperations.getBooleanValue;
import static com.android.server.integrity.parser.BinaryFileOperations.getIntValue;
import static com.android.server.integrity.parser.BinaryFileOperations.getStringValue;
import static com.android.server.integrity.utils.TestUtils.getBits;
import static com.android.server.integrity.utils.TestUtils.getBytes;
import static com.android.server.integrity.utils.TestUtils.getValueBits;

import static com.google.common.truth.Truth.assertThat;

import android.content.integrity.IntegrityUtils;

import com.android.server.integrity.model.BitInputStream;

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

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@RunWith(JUnit4.class)
public class BinaryFileOperationsTest {

    private static final String IS_NOT_HASHED = "0";
    private static final String IS_HASHED = "1";
    private static final String PACKAGE_NAME = "com.test.app";
    private static final String APP_CERTIFICATE = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";

    @Test
    public void testGetStringValue() throws IOException {
        byte[] stringBytes =
                getBytes(
                        IS_NOT_HASHED
                                + getBits(PACKAGE_NAME.length(), VALUE_SIZE_BITS)
                                + getValueBits(PACKAGE_NAME));
        BitInputStream inputStream = new BitInputStream(new ByteArrayInputStream(stringBytes));

        String resultString = getStringValue(inputStream);

        assertThat(resultString).isEqualTo(PACKAGE_NAME);
    }

    @Test
    public void testGetHashedStringValue() throws IOException {
        byte[] ruleBytes =
                getBytes(
                        IS_HASHED
                                + getBits(APP_CERTIFICATE.length(), VALUE_SIZE_BITS)
                                + getValueBits(APP_CERTIFICATE));
        BitInputStream inputStream = new BitInputStream(new ByteArrayInputStream(ruleBytes));

        String resultString = getStringValue(inputStream);

        assertThat(resultString)
                .isEqualTo(IntegrityUtils.getHexDigest(
                        APP_CERTIFICATE.getBytes(StandardCharsets.UTF_8)));
    }

    @Test
    public void testGetStringValue_withSizeAndHashingInfo() throws IOException {
        byte[] ruleBytes = getBytes(getValueBits(PACKAGE_NAME));
        BitInputStream inputStream = new BitInputStream(new ByteArrayInputStream(ruleBytes));

        String resultString = getStringValue(inputStream,
                PACKAGE_NAME.length(), /* isHashedValue= */false);

        assertThat(resultString).isEqualTo(PACKAGE_NAME);
    }

    @Test
    public void testGetIntValue() throws IOException {
        int randomValue = 15;
        byte[] ruleBytes = getBytes(getBits(randomValue, /* numOfBits= */ 32));
        BitInputStream inputStream = new BitInputStream(new ByteArrayInputStream(ruleBytes));

        assertThat(getIntValue(inputStream)).isEqualTo(randomValue);
    }

    @Test
    public void testGetBooleanValue_true() throws IOException {
        String booleanValue = "1";
        byte[] ruleBytes = getBytes(booleanValue);
        BitInputStream inputStream = new BitInputStream(new ByteArrayInputStream(ruleBytes));

        assertThat(getBooleanValue(inputStream)).isEqualTo(true);
    }

    @Test
    public void testGetBooleanValue_false() throws IOException {
        String booleanValue = "0";
        byte[] ruleBytes = getBytes(booleanValue);
        BitInputStream inputStream = new BitInputStream(new ByteArrayInputStream(ruleBytes));

        assertThat(getBooleanValue(inputStream)).isEqualTo(false);
    }
}