Debian Codegraph @

launcher/internal/gpu/attestation_test.go

raw
1 package gpu
2
3 import (
4 "testing"
5
6 "cos.googlesource.com/cos/tools.git/src/cmd/cos_gpu_installer/deviceinfo"
7 "github.com/confidentsecurity/go-nvtrust/pkg/gonvtrust/gpu"
8
9 attestationpb "github.com/GoogleCloudPlatform/confidential-space/server/proto/gen/attestation"
10 )
11
12 func TestCollectAttestationEvidence(t *testing.T) {
13 handler := &gpu.NVMLHandlerMock{}
14
15 testCases := []struct {
16 name string
17 nonce []byte
18 gpuType deviceinfo.GPUType
19 wantPass bool
20 wantSPT bool
21 wantMPT bool
22 }{
23 {
24 name: "success w/ H100 SPT",
25 nonce: []byte("nonce"),
26 gpuType: deviceinfo.H100,
27 wantPass: true,
28 wantSPT: true,
29 },
30 // Comment out since the mock NVML handler will not return multiple GPU attestations
31 // {
32 // name: "success w/ B200",
33 // nonce: []byte("nonce"),
34 // B200: false,
35 // H100: true,
36 // wantPass: true,
37 // wantMPT: true,
38 // },
39 {
40 name: "failed due to unsupported GPU attestation type",
41 nonce: []byte("nonce"),
42 gpuType: deviceinfo.Others,
43 wantPass: false,
44 },
45 }
46
47 for _, tc := range testCases {
48 t.Run(tc.name, func(t *testing.T) {
49 fn := &getGpuTypeInfo
50 getGpuTypeInfo = func() (deviceinfo.GPUType, error) {
51 return tc.gpuType, nil
52 }
53 // Restore to original func after testing
54 t.Cleanup(func() { getGpuTypeInfo = *fn })
55
56 attester := &NvidiaAttester{}
57 attesation, err := attester.collectAttestationEvidence(handler, tc.nonce)
58 if gotPass := (err == nil); gotPass != tc.wantPass {
59 t.Errorf("CollectAttestationEvidence() = %v, want %v", gotPass, tc.wantPass)
60 }
61 if tc.wantPass {
62 if tc.wantSPT {
63 if _, ok := attesation.CcFeature.(*attestationpb.NvidiaAttestationReport_Spt); !ok {
64 t.Errorf("CollectAttestationEvidence() = %v, want %v", attesation.CcFeature, &attestationpb.NvidiaAttestationReport_Spt{})
65 }
66 }
67 }
68 })
69 }
70 }
71
72 func TestDetermineAttestationType(t *testing.T) {
73 testCases := []struct {
74 name string
75 gpuInfos []*attestationpb.GpuInfo
76 gpuType deviceinfo.GPUType
77 want attestationType
78 }{
79 {
80 name: "UNSUPPORTED GPU type",
81 gpuInfos: []*attestationpb.GpuInfo{
82 {Uuid: "gpu-0"},
83 },
84 gpuType: deviceinfo.Others,
85 want: UNSUPPORTED,
86 },
87 {
88 name: "SPT attestation type (H100)",
89 gpuInfos: []*attestationpb.GpuInfo{
90 {Uuid: "gpu-0"},
91 },
92 gpuType: deviceinfo.H100,
93 want: SPT,
94 },
95 {
96 name: "SPT attestation type (B200 with single GPU)",
97 gpuInfos: []*attestationpb.GpuInfo{
98 {Uuid: "gpu-0"},
99 },
100 gpuType: deviceinfo.B200,
101 want: SPT,
102 },
103 {
104 name: "MPT attestation type (B200 with multiple GPUs)",
105 gpuInfos: []*attestationpb.GpuInfo{
106 {Uuid: "gpu-0"},
107 {Uuid: "gpu-1"},
108 },
109 gpuType: deviceinfo.B200,
110 want: MPT,
111 },
112 }
113
114 for _, tc := range testCases {
115 t.Run(tc.name, func(t *testing.T) {
116 fn := &getGpuTypeInfo
117 getGpuTypeInfo = func() (deviceinfo.GPUType, error) {
118 return tc.gpuType, nil
119 }
120 // Restore to original func after testing
121 t.Cleanup(func() { getGpuTypeInfo = *fn })
122
123 if got := determineAttestationType(tc.gpuInfos); got != tc.want {
124 t.Errorf("determineAttestationType() = %v, want %v", got, tc.want)
125 }
126 })
127 }
128 }
129
130 func TestConvertGPUArchToPB(t *testing.T) {
131 testCases := []struct {
132 arch string
133 wantArch attestationpb.GpuArchitectureType
134 }{
135 {
136 arch: "HOPPER",
137 wantArch: attestationpb.GpuArchitectureType_GPU_ARCHITECTURE_TYPE_HOPPER,
138 },
139 {
140 arch: "BLACKWELL",
141 wantArch: attestationpb.GpuArchitectureType_GPU_ARCHITECTURE_TYPE_BLACKWELL,
142 },
143 {
144 arch: "UNSPECIFIED",
145 wantArch: attestationpb.GpuArchitectureType_GPU_ARCHITECTURE_TYPE_UNSPECIFIED,
146 },
147 }
148
149 for _, tc := range testCases {
150 t.Run(tc.arch, func(t *testing.T) {
151 if got := convertGPUArchToPB(tc.arch); got != tc.wantArch {
152 t.Errorf("convertGPUArchToPB() = %v, want %v", got, tc.wantArch)
153 }
154 })
155 }
156
157 }