Debian Codegraph @

tpmutil/emulator_read_write_closer_test.go

raw
1 //go:build !windows
2
3 // Copyright (c) 2019, Google LLC All rights reserved.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16
17 package tpmutil
18
19 import (
20 "fmt"
21 "net"
22 "testing"
23 "time"
24 )
25
26 // mockConn records the number of bytes that are read from it and written to it
27 // and tracks whether or not it has been closed.
28 type mockConn struct {
29 network string
30 path string
31 open bool
32 }
33
34 // dialMockConn returns a mockConn that holds the given network and path info.
35 func dialMockConn(network, path string) (net.Conn, error) {
36 return &mockConn{
37 network: network,
38 path: path,
39 open: true,
40 }, nil
41 }
42
43 // Read implements a mock version of Read.
44 func (mc *mockConn) Read(b []byte) (int, error) {
45 // Always read zeros into the bytes for the given length.
46 for i := range b {
47 b[i] = 0
48 }
49 return len(b), nil
50 }
51
52 // Write implements a mock version of Write.
53 func (mc *mockConn) Write(b []byte) (int, error) {
54 return len(b), nil
55 }
56
57 // Close implements a mock version of Close.
58 func (mc *mockConn) Close() error {
59 if !mc.open {
60 return fmt.Errorf("mockConn is already closed")
61 }
62 mc.open = false
63 return nil
64 }
65
66 // LocalAddr returns nil.
67 func (mc *mockConn) LocalAddr() net.Addr {
68 return nil
69 }
70
71 // RemoteAddr returns nil.
72 func (mc *mockConn) RemoteAddr() net.Addr {
73 return nil
74 }
75
76 // SetDeadline returns nil.
77 func (mc *mockConn) SetDeadline(_ time.Time) error {
78 return nil
79 }
80
81 // SetReadDeadline returns nil.
82 func (mc *mockConn) SetReadDeadline(_ time.Time) error {
83 return nil
84 }
85
86 // SetWriteDeadline returns nil.
87 func (mc *mockConn) SetWriteDeadline(_ time.Time) error {
88 return nil
89 }
90
91 func newMockEmulator() *EmulatorReadWriteCloser {
92 path := "/dev/null/fake"
93 rwc := NewEmulatorReadWriteCloser(path)
94 rwc.dialer = dialMockConn
95 return rwc
96 }
97
98 var (
99 input = []byte(`input`)
100 output = make([]byte, 1)
101 )
102
103 func TestEmulatorReadWriteCloserMultipleReads(t *testing.T) {
104 rwc := newMockEmulator()
105 n, err := rwc.Write(input)
106 if err != nil {
107 t.Errorf("failed to write: %v", err)
108 }
109 if n != len(input) {
110 t.Errorf("wrong number of bytes written: got %d, expected %d", n, len(input))
111 }
112
113 n, err = rwc.Read(output)
114 if err != nil {
115 t.Errorf("failed to read: %v", err)
116 }
117 if n != len(output) {
118 t.Errorf("wrong number of bytes read: got %d, expected %d", n, len(output))
119 }
120
121 n, err = rwc.Write(input)
122 if err != nil {
123 t.Errorf("failed to write: %v", err)
124 }
125 if n != len(input) {
126 t.Errorf("wrong number of bytes written: got %d, expected %d", n, len(input))
127 }
128
129 n, err = rwc.Read(output)
130 if err != nil {
131 t.Errorf("failed to read: %v", err)
132 }
133 if n != len(output) {
134 t.Errorf("wrong number of bytes read: got %d, expected %d", n, len(output))
135 }
136 }
137
138 func TestEmulatorReadWriteCloserClose(t *testing.T) {
139 rwc := newMockEmulator()
140 if err := rwc.Close(); err == nil {
141 t.Errorf("incorrectly closed a connection that hadn't been opened")
142 }
143
144 if _, err := rwc.Write(input); err != nil {
145 t.Errorf("failed to write: %v", err)
146 }
147
148 if err := rwc.Close(); err != nil {
149 t.Errorf("failed to close an open connection: %v", err)
150 }
151
152 if err := rwc.Close(); err == nil {
153 t.Errorf("incorrectly closed a connection that had already been closed")
154 }
155 }
156
157 func TestEmulatorReadWriteCloseReadAfterClose(t *testing.T) {
158 rwc := newMockEmulator()
159 if _, err := rwc.Write(input); err != nil {
160 t.Errorf("failed to write: %v", err)
161 }
162
163 if err := rwc.Close(); err != nil {
164 t.Errorf("failed to close the connection: %v", err)
165 }
166
167 if _, err := rwc.Read(output); err == nil {
168 t.Errorf("incorrectly read on a closed connection")
169 }
170 }
171
172 func TestEmulatorReadWriteCloserReadBeforeWrite(t *testing.T) {
173 rwc := newMockEmulator()
174 b := make([]byte, 1)
175 if _, err := rwc.Read(b); err == nil {
176 t.Errorf("incorrectly read on a connection before writing")
177 }
178 }
179
180 func TestEmulatorReadWriteCloserDoubleWrite(t *testing.T) {
181 rwc := newMockEmulator()
182 if _, err := rwc.Write(input); err != nil {
183 t.Errorf("failed to write: %v", err)
184 }
185 if _, err := rwc.Write(input); err == nil {
186 t.Errorf("incorrectly wrote a second time without reading in between")
187 }
188 }
189
190 func TestEmulatorReadWriteCloserDialerError(t *testing.T) {
191 rwc := newMockEmulator()
192 rwc.dialer = func(_, _ string) (net.Conn, error) { return nil, fmt.Errorf("invalid") }
193
194 if _, err := rwc.Write(input); err == nil {
195 t.Errorf("incorrectly wrote when the dialer returned an error")
196 }
197 }