blob: d1734cc4b43980deb17514c38a9d16f5a831d031 [file] [log] [blame] [edit]
// Copyright 2026 Google LLC
//
// 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 oempreloader
import (
"fmt"
"os"
"os/exec"
"strconv"
"testing"
)
func TestRunVeritysetup(t *testing.T) {
srcExt4 := "testdata/ext4.img"
targetExt4 := "target_ext4.img"
if err := copyFile(srcExt4, targetExt4); err != nil {
t.Fatalf("Failed to copy ext4 file: %v", err)
}
defer os.Remove(targetExt4)
fsSize := "1M" // 1M
size := int64(2 * 1024 * 1024) // 2M
if err := ExtendExt4(targetExt4, size); err != nil {
t.Fatalf("Failed to extend ext4 file: %v", err)
}
rootHash, salt, err := RunVeritysetup(targetExt4, fsSize)
if err != nil {
t.Fatalf("Failed to run verity setup: %v", err)
}
fsSize4K, err := sizeStringTo4K(fsSize)
if err != nil {
t.Fatalf("Failed to convert size string to 4K bytes: %v", err)
}
cmd := exec.Command(
"veritysetup",
"verify",
targetExt4,
targetExt4,
"--data-block-size=4096",
"--hash-block-size=4096",
fmt.Sprintf("--data-blocks=%s", strconv.FormatUint(fsSize4K, 10)),
fmt.Sprintf("--hash-offset=%s", strconv.FormatUint(fsSize4K<<12, 10)),
rootHash,
fmt.Sprintf("--salt=%s", salt),
"--no-superblock",
"--format=0",
)
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("verification failed: %q, err: %v", string(output), err)
}
}