/*
 * Copyright (C) 2022 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.ondevicepersonalization.services.util;

import static org.junit.Assert.assertArrayEquals;

import android.content.Context;
import android.os.ParcelFileDescriptor;

import androidx.test.core.app.ApplicationProvider;

import org.junit.Test;

import java.nio.ByteBuffer;

public class IoUtilsTest {
    @Test
    public void testIoApis_success() {
        Context context = ApplicationProvider.getApplicationContext();
        String fileName = "model";
        byte[] data = new byte[] {20};

        // Write data to file.
        String modelFile = IoUtils.writeToFile(context, fileName, data);
        // Create file descriptor.
        ParcelFileDescriptor fd =
                IoUtils.createFileDescriptor(modelFile, ParcelFileDescriptor.MODE_READ_ONLY);
        // Read file content through file descriptor.
        ByteBuffer buffer = IoUtils.getByteBufferFromFd(fd);

        // Verify the file content is same as input data.
        byte[] array = new byte[buffer.remaining()];
        buffer.get(array);
        assertArrayEquals(array, data);
    }
}
