blob: 923b0810d1772f9de7fadb2e49cb0eaf73ee14cd [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 partutil
import "testing"
func TestParseGPTAttrs_Success(t *testing.T) {
tests := []struct {
name string
attrs string
want uint64
}{
{
name: "Empty",
attrs: "",
want: 0,
},
{
name: "RequiredPartition",
attrs: "RequiredPartition",
want: 1,
},
{
name: "NoBlockIOProtocol",
attrs: "NoBlockIOProtocol",
want: 2,
},
{
name: "LegacyBIOSBootable",
attrs: "LegacyBIOSBootable",
want: 4,
},
{
name: "GUID",
attrs: "GUID:48,50,52",
want: (1 << 48) | (1 << 50) | (1 << 52),
},
{
name: "All",
attrs: "RequiredPartition NoBlockIOProtocol LegacyBIOSBootable GUID:48,50,52",
want: (1 << 0) | (1 << 1) | (1 << 2) | (1 << 48) | (1 << 50) | (1 << 52),
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
got, err := ParseGPTAttrs(test.attrs)
if err != nil {
t.Fatalf("ParseGPTAttrs(%q) = %v; want nil", test.attrs, err)
}
if got != test.want {
t.Errorf("ParseGPTAttrs(%q) = %d; want %d", test.attrs, got, test.want)
}
})
}
}
func TestParseGPTAttrs_Failure(t *testing.T) {
tests := []struct {
name string
attrs string
}{
{
name: "Unknown",
attrs: "UnknownAttr",
},
{
name: "MixedUnknown",
attrs: "LegacyBIOSBootable Unknown GUID:48,50,52",
},
{
name: "BadGUIDBit",
attrs: "GUID:a,50,52",
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
got, err := ParseGPTAttrs(test.attrs)
if err == nil {
t.Fatalf("ParseGPTAttrs(%q) = %d; expected error", test.attrs, got)
}
})
}
}
func TestGPTPriority(t *testing.T) {
tests := []struct {
name string
attrs string
want uint64
}{
{
name: "Empty",
attrs: "",
want: 0,
},
{
name: "NonEmpty",
attrs: "GUID:48,49,50",
want: 7,
},
{
name: "ExtraWide",
attrs: "GUID:48,52,53,54,55",
want: 1,
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
got, err := GPTPriority(test.attrs)
if err != nil {
t.Fatalf("GPTPriority(%q) = %v, want nil", test.attrs, err)
}
if got != test.want {
t.Errorf("GPTPriority(%q) = %d, want %d", test.attrs, got, test.want)
}
})
}
}