Debian Codegraph @

smbios/decoder_test.go

raw symbols SCIP
1 // Copyright 2017-2018 DigitalOcean.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package smbios_test
16
17 import (
18 "bytes"
19 "testing"
20
21 "github.com/digitalocean/go-smbios/smbios"
22 "github.com/google/go-cmp/cmp"
23 )
24
25 func TestDecoder(t *testing.T) {
26 tests := []struct {
27 name string
28 b []byte
29 ss []*smbios.Structure
30 ok bool
31 }{
32 {
33 name: "short header",
34 b: []byte{0x00},
35 },
36 {
37 name: "length too short",
38 b: []byte{0x00, 0x00, 0x00, 0x00},
39 },
40 {
41 name: "length too long",
42 b: []byte{0x00, 0xff, 0x00, 0x00},
43 },
44 {
45 name: "string not terminated",
46 b: []byte{
47 0x01, 0x04, 0x01, 0x00,
48 'a', 'b', 'c', 'd',
49 },
50 },
51 {
52 name: "no end of table",
53 b: []byte{
54 0x01, 0x04, 0x01, 0x00,
55 0x00,
56 0x00,
57 },
58 },
59 {
60 name: "bad second message",
61 b: []byte{
62 0x01, 0x0c, 0x02, 0x00,
63 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
64 'd', 'e', 'a', 'd', 'b', 'e', 'e', 'f', 0x00,
65 0x00,
66
67 0xff,
68 },
69 },
70 {
71 name: "OK, one, no format, no strings",
72 b: []byte{
73 127, 0x04, 0x01, 0x00,
74 0x00,
75 0x00,
76 },
77 ss: []*smbios.Structure{{
78 Header: smbios.Header{
79 Type: 127,
80 Length: 4,
81 Handle: 1,
82 },
83 }},
84 ok: true,
85 },
86 {
87 name: "OK, one, format, no strings",
88 b: []byte{
89 127, 0x06, 0x01, 0x00,
90 0x01, 0x02,
91 0x00,
92 0x00,
93 },
94 ss: []*smbios.Structure{{
95 Header: smbios.Header{
96 Type: 127,
97 Length: 6,
98 Handle: 1,
99 },
100 Formatted: []byte{0x01, 0x02},
101 }},
102 ok: true,
103 },
104 {
105 name: "OK, one, format, strings",
106 b: []byte{
107 127, 0x06, 0x01, 0x00,
108 0x01, 0x02,
109 'a', 'b', 'c', 'd', 0x00,
110 '1', '2', '3', '4', 0x00,
111 0x00,
112 },
113 ss: []*smbios.Structure{{
114 Header: smbios.Header{
115 Type: 127,
116 Length: 6,
117 Handle: 1,
118 },
119 Formatted: []byte{0x01, 0x02},
120 Strings: []string{"abcd", "1234"},
121 }},
122 ok: true,
123 },
124 {
125 name: "OK, multiple",
126 b: []byte{
127 0x00, 0x05, 0x01, 0x00,
128 0xff,
129 0x00,
130 0x00,
131
132 0x01, 0x0c, 0x02, 0x00,
133 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
134 'd', 'e', 'a', 'd', 'b', 'e', 'e', 'f', 0x00,
135 0x00,
136
137 127, 0x06, 0x03, 0x00,
138 0x01, 0x02,
139 'a', 'b', 'c', 'd', 0x00,
140 '1', '2', '3', '4', 0x00,
141 0x00,
142 },
143 ss: []*smbios.Structure{
144 {
145 Header: smbios.Header{
146 Type: 0,
147 Length: 5,
148 Handle: 1,
149 },
150 Formatted: []byte{0xff},
151 },
152 {
153 Header: smbios.Header{
154 Type: 1,
155 Length: 12,
156 Handle: 2,
157 },
158 Formatted: []byte{0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef},
159 Strings: []string{"deadbeef"},
160 },
161 {
162 Header: smbios.Header{
163 Type: 127,
164 Length: 6,
165 Handle: 3,
166 },
167 Formatted: []byte{0x01, 0x02},
168 Strings: []string{"abcd", "1234"},
169 },
170 },
171 ok: true,
172 },
173 }
174
175 for _, tt := range tests {
176 t.Run(tt.name, func(t *testing.T) {
177 d := smbios.NewDecoder(bytes.NewReader(tt.b))
178 ss, err := d.Decode()
179
180 if tt.ok && err != nil {
181 t.Fatalf("unexpected error: %v", err)
182 }
183 if !tt.ok && err == nil {
184 t.Fatalf("expected an error, but none occurred: %v", err)
185 }
186
187 if diff := cmp.Diff(tt.ss, ss); diff != "" {
188 t.Fatalf("unexpected structures (-want +got):\n%s", diff)
189 }
190 })
191 }
192 }