blob: 19ac519b48053a4b5f48bf9cbd8a20cfbe1d27cc [file] [log] [blame]
Lorenz Brunee17d832022-10-18 12:02:45 +00001package gpt
2
3import (
4 "os"
5 "testing"
6
7 "github.com/google/uuid"
8 "golang.org/x/sys/unix"
9)
10
11var testUUID = uuid.MustParse("85c0b60f-caf9-40dd-86fa-f8797e26238d")
12
13func TestKernelInterop(t *testing.T) {
14 if os.Getenv("IN_KTEST") != "true" {
15 t.Skip("Not in ktest")
16 }
17 ram0, err := os.OpenFile("/dev/ram0", os.O_RDWR, 0)
18 if err != nil {
19 panic(err)
20 }
21 g := Table{
22 ID: uuid.NewSHA1(testUUID, []byte("test")),
23 BlockSize: 512,
24 BlockCount: 2048,
25 BootCode: []byte("just some test code"),
26 Partitions: []*Partition{
27 nil,
28 // This emoji is very complex and exercises UTF16 surrogate encoding
29 // and composing.
30 {Name: "Test 🏃‍♂️", FirstBlock: 10, LastBlock: 19, Type: PartitionTypeEFISystem, ID: uuid.NewSHA1(testUUID, []byte("test1")), Attributes: AttrNoBlockIOProto},
31 nil,
32 {Name: "Test2", FirstBlock: 20, LastBlock: 49, Type: PartitionTypeEFISystem, ID: uuid.NewSHA1(testUUID, []byte("test2")), Attributes: 0},
33 },
34 }
35 if err := Write(ram0, &g); err != nil {
36 t.Fatalf("Failed to write GPT: %v", err)
37 }
38
39 if err := unix.IoctlSetInt(int(ram0.Fd()), unix.BLKRRPART, 0); err != nil {
40 t.Fatalf("Failed to reread partition table: %v", err)
41 }
42 if _, err := os.Stat("/sys/block/ram0/ram0p2"); err != nil {
43 t.Errorf("Expected ram0p2 to exist, got %v", err)
44 }
45 if _, err := os.Stat("/sys/block/ram0/ram0p4"); err != nil {
46 t.Errorf("Expected ram0p4 to exist, got %v", err)
47 }
48 if _, err := os.Stat("/sys/block/ram0/ram0p1"); err == nil {
49 t.Error("Expected ram0p1 not to exist, but it exists")
50 }
51}