blob: decf5ec8ef5492ed7da22fb449041330f4f25f73 [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
2// SPDX-License-Identifier: Apache-2.0
3
Lorenz Brunee17d832022-10-18 12:02:45 +00004package gpt
5
6import (
7 "os"
8 "testing"
9
10 "github.com/google/uuid"
Lorenz Brunad131882023-06-28 16:42:20 +020011
Tim Windelschmidt9f21f532024-05-07 15:14:20 +020012 "source.monogon.dev/osbase/blockdev"
Lorenz Brunee17d832022-10-18 12:02:45 +000013)
14
15var testUUID = uuid.MustParse("85c0b60f-caf9-40dd-86fa-f8797e26238d")
16
17func TestKernelInterop(t *testing.T) {
18 if os.Getenv("IN_KTEST") != "true" {
19 t.Skip("Not in ktest")
20 }
Lorenz Brunad131882023-06-28 16:42:20 +020021 ram0, err := blockdev.Open("/dev/ram0")
Lorenz Brunee17d832022-10-18 12:02:45 +000022 if err != nil {
23 panic(err)
24 }
25 g := Table{
Lorenz Brunad131882023-06-28 16:42:20 +020026 ID: uuid.NewSHA1(testUUID, []byte("test")),
27 BootCode: []byte("just some test code"),
Lorenz Brunee17d832022-10-18 12:02:45 +000028 Partitions: []*Partition{
29 nil,
30 // This emoji is very complex and exercises UTF16 surrogate encoding
31 // and composing.
32 {Name: "Test 🏃‍♂️", FirstBlock: 10, LastBlock: 19, Type: PartitionTypeEFISystem, ID: uuid.NewSHA1(testUUID, []byte("test1")), Attributes: AttrNoBlockIOProto},
33 nil,
34 {Name: "Test2", FirstBlock: 20, LastBlock: 49, Type: PartitionTypeEFISystem, ID: uuid.NewSHA1(testUUID, []byte("test2")), Attributes: 0},
35 },
Lorenz Brunad131882023-06-28 16:42:20 +020036 b: ram0,
Lorenz Brunee17d832022-10-18 12:02:45 +000037 }
Lorenz Brunad131882023-06-28 16:42:20 +020038 if err := g.Write(); err != nil {
Lorenz Brunee17d832022-10-18 12:02:45 +000039 t.Fatalf("Failed to write GPT: %v", err)
40 }
Lorenz Brunad131882023-06-28 16:42:20 +020041 if err := ram0.RefreshPartitionTable(); err != nil {
42 t.Fatalf("Failed to refresh partition table: %v", err)
Lorenz Brunee17d832022-10-18 12:02:45 +000043 }
44 if _, err := os.Stat("/sys/block/ram0/ram0p2"); err != nil {
45 t.Errorf("Expected ram0p2 to exist, got %v", err)
46 }
47 if _, err := os.Stat("/sys/block/ram0/ram0p4"); err != nil {
48 t.Errorf("Expected ram0p4 to exist, got %v", err)
49 }
50 if _, err := os.Stat("/sys/block/ram0/ram0p1"); err == nil {
51 t.Error("Expected ram0p1 not to exist, but it exists")
52 }
53}