// Copyright 2021 yuyezhong@gmail.com
//
// 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 bootimg

import cfig.bootimg.cpio.AndroidCpio
import cfig.bootimg.cpio.AndroidCpioEntry
import cfig.helper.Helper
import org.junit.Assert.assertTrue
import org.junit.Test
import java.io.File

class AndroidCpioEntryTest {
    @Test
    fun dirEntry() {
        run {//dir, fileMode 040755
            val entry1 = AndroidCpioEntry(name = "acct", statMode = 0x41ed, data = byteArrayOf(), ino = 300000)
            val exp =
                Helper.fromHexString("3037303730313030303439336530303030303431656430303030303030303030303030303030303030303030303130303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030353030303030303030616363740000")
            assertTrue(entry1.encode().contentEquals(exp))
            assertTrue(entry1.encode2().contentEquals(exp))
        }

        run {//dir, fileMode 040755
            val entry2 = AndroidCpioEntry(name = "apex", statMode = 0x41ed, data = byteArrayOf(), ino = 300001)
            val exp2 =
                Helper.fromHexString("3037303730313030303439336531303030303431656430303030303030303030303030303030303030303030303130303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030353030303030303030617065780000")
            assertTrue(entry2.encode().contentEquals(exp2))
            assertTrue(entry2.encode2().contentEquals(exp2))
        }
    }

    @Test
    fun linkEntry() {
        run {//link, fileMode 0120777
            val entry3 =
                AndroidCpioEntry(name = "bin", statMode = 0xa1a4, data = "/system/bin".toByteArray(), ino = 300002)
            val exp3 =
                Helper.fromHexString("303730373031303030343933653230303030613161343030303030303030303030303030303030303030303030313030303030303030303030303030306230303030303030303030303030303030303030303030303030303030303030303030303030303034303030303030303062696e0000002f73797374656d2f62696e00")
            entry3.encode()
            entry3.encode2()
            entry3.encode()
            entry3.encode2()
            assertTrue(exp3.contentEquals(entry3.encode()))
            assertTrue(exp3.contentEquals(entry3.encode2()))
        }
    }

    @Test
    fun fileEntry() {
        //init.environ.rc
        val initrc =
            "[encoded data, 4558 characters]"
        println("<" + String(Helper.fromHexString(initrc)) + ">")
        val entry = AndroidCpioEntry(
            name = "/init.environ.rc",
            statMode = java.lang.Long.valueOf("100644", 8),
            data = Helper.fromHexString(initrc),
            ino = 300003
        )
        assertTrue(entry.encode().contentEquals(entry.encode2()))
    }

    @Test
    fun specialFilePermTest() {
        val rd = "src/test/resources/filemode_000_ramdisk.img"
        AndroidCpio.decompressCPIO(rd, "temp", null)
        File("temp").deleteRecursively()
    }

    fun packTest() {
        val dir = "/home/yu/work/boot/build/unzip_boot/root"
        val oF = "/home/yu/work/boot/root.cpio"
        val acpio = AndroidCpio()
        acpio.pack(dir, oF, "/home/yu/work/boot/build/unzip_boot/ramdisk_filelist.txt")
    }
}
