Debian Codegraph @

smbios/stream_windows_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
16
17 import (
18 "bytes"
19 "encoding/hex"
20 "io/ioutil"
21 "testing"
22 )
23
24 // makeRawSMBIOSData creates a buffer with a valid RawSMBIOSData struct with the
25 // given version and stream information.
26 func makeRawSMBIOSData(major, minor, revision byte, stream []byte) []byte {
27 buffer := make([]byte, rawSMBIOSDataHeaderSize+len(stream))
28 buffer[0] = 0
29 buffer[1] = major
30 buffer[2] = minor
31 buffer[3] = revision
32 nativeEndian().PutUint32(buffer[4:8], uint32(len(stream)))
33 copy(buffer[8:], stream)
34 return buffer
35 }
36
37 func Test_windowsStream(t *testing.T) {
38 const major = byte(2)
39 const minor = byte(4)
40 const revision = byte(1)
41
42 // Note: buffer will be automatically created from the stream if it is not
43 // explicitly set to a non-nil value. This prevents having to duplicate the
44 // stream data in the struct definitions below for large test cases.
45 //
46 // Unlike in Test_memoryStream, we're not worrying about the actual decoding
47 // of the structures here. All we care about is whether or not windowsStream
48 // gives us back the stream data we expect. Whether or not that is valid can
49 // be tested separately.
50 tests := []struct {
51 name string
52 buffer []byte
53 stream []byte
54 ok bool
55 }{
56 {
57 name: "empty buffer",
58 buffer: []byte{}, // purposefully not nil
59 },
60 {
61 name: "short buffer",
62 buffer: []byte{0, 1, 2, 3, 4, 5, 6}, // only 7 bytes
63 },
64 {
65 name: "valid header, empty table",
66 stream: nil,
67 ok: true,
68 },
69 {
70 name: "length too large",
71 buffer: func() []byte {
72 buf := []byte{
73 0, 2, 4, 1, // version
74 0, 0, 0, 0, // length placeholder
75 1, 2, 3, 4, // stream
76 }
77 nativeEndian().PutUint32(buf[4:8], 5)
78 return buf
79 }(),
80 },
81 {
82 name: "valid header and stream",
83 stream: []byte{
84 0x00, 0x05, 0x01, 0x00,
85 0xff,
86 0x00,
87 0x00,
88
89 0x01, 0x0c, 0x02, 0x00,
90 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
91 'd', 'e', 'a', 'd', 'b', 'e', 'e', 'f', 0x00,
92 0x00,
93
94 127, 0x06, 0x03, 0x00,
95 0x01, 0x02,
96 'a', 'b', 'c', 'd', 0x00,
97 '1', '2', '3', '4', 0x00,
98 0x00,
99 },
100 ok: true,
101 },
102 {
103 name: "buffer larger than needed",
104 buffer: func() []byte {
105 buf := makeRawSMBIOSData(major, minor, revision, []byte{1, 2, 3, 4})
106 buf = append(buf, 5, 6, 7, 8)
107 return buf
108 }(),
109 stream: []byte{1, 2, 3, 4},
110 ok: true,
111 },
112 }
113
114 for _, tt := range tests {
115 // Make buffer if not set explicitly
116 if tt.buffer == nil {
117 tt.buffer = makeRawSMBIOSData(major, minor, revision, tt.stream)
118 }
119
120 t.Run(tt.name, func(t *testing.T) {
121 rc, ep, err := windowsStream(tt.buffer)
122
123 if tt.ok && err != nil {
124 t.Fatalf("unexpected error: %v", err)
125 }
126 if !tt.ok && err == nil {
127 t.Fatalf("expected an error, but none occurred: %v", err)
128 }
129
130 if !tt.ok {
131 // Don't bother doing comparison if entry point is invalid
132 t.Logf("OK error: %v", err)
133 return
134 }
135 defer rc.Close()
136
137 _, streamSize := ep.Table()
138 if streamSize != len(tt.stream) {
139 t.Fatalf("bad stream size: got %d, wanted %d", streamSize, len(tt.stream))
140 }
141 maj, min, rev := ep.Version()
142 if maj != int(major) {
143 t.Fatalf("bad major version: got %d, wanted %d", maj, major)
144 }
145 if min != int(minor) {
146 t.Fatalf("bad minor version: got %d, wanted %d", min, minor)
147 }
148 if rev != int(revision) {
149 t.Fatalf("bad revision: got %d, wanted %d", rev, revision)
150 }
151
152 streamData, err := ioutil.ReadAll(rc)
153 if err != nil {
154 t.Fatalf("failed to read stream: %v", err)
155 }
156 if len(streamData) != len(tt.stream) {
157 t.Fatalf("bad stream data: got %d bytes, wanted %d", len(streamData), len(tt.stream))
158 }
159 if bytes.Compare(tt.stream, streamData) != 0 {
160 t.Fatalf(
161 "stream data different:\nwant: %s\ngot : %s",
162 hex.EncodeToString(tt.stream),
163 hex.EncodeToString(streamData),
164 )
165 }
166 })
167 }
168 }