| 1 |
/* |
| 2 |
Copyright 2013-2014 Canonical Ltd. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify it |
| 5 |
under the terms of the GNU General Public License version 3, as published |
| 6 |
by the Free Software Foundation. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, but |
| 9 |
WITHOUT ANY WARRANTY; without even the implied warranties of |
| 10 |
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 11 |
PURPOSE. See the GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License along |
| 14 |
with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
package listener |
| 18 |
|
| 19 |
import ( |
| 20 |
"crypto/tls" |
| 21 |
"io" |
| 22 |
"net" |
| 23 |
"os/exec" |
| 24 |
"regexp" |
| 25 |
"syscall" |
| 26 |
"testing" |
| 27 |
"time" |
| 28 |
"unicode" |
| 29 |
|
| 30 |
. "gopkg.in/check.v1" |
| 31 |
|
| 32 |
helpers "gitlab.com/ubports/development/core/lomiri-push-service/testing" |
| 33 |
) |
| 34 |
|
| 35 |
func TestListener(t *testing.T) { TestingT(t) } |
| 36 |
|
| 37 |
type listenerSuite struct { |
| 38 |
testlog *helpers.TestLogger |
| 39 |
} |
| 40 |
|
| 41 |
var _ = Suite(&listenerSuite{}) |
| 42 |
|
| 43 |
const NofileMax = 20 |
| 44 |
|
| 45 |
func (s *listenerSuite) SetUpSuite(*C) { |
| 46 |
// make it easier to get a too many open files error |
| 47 |
var nofileLimit syscall.Rlimit |
| 48 |
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &nofileLimit) |
| 49 |
if err != nil { |
| 50 |
panic(err) |
| 51 |
} |
| 52 |
nofileLimit.Cur = NofileMax |
| 53 |
err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &nofileLimit) |
| 54 |
if err != nil { |
| 55 |
panic(err) |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
func (s *listenerSuite) SetUpTest(c *C) { |
| 60 |
s.testlog = helpers.NewTestLogger(c, "error") |
| 61 |
} |
| 62 |
|
| 63 |
type testDevListenerCfg struct { |
| 64 |
addr string |
| 65 |
} |
| 66 |
|
| 67 |
func (cfg *testDevListenerCfg) Addr() string { |
| 68 |
return cfg.addr |
| 69 |
} |
| 70 |
|
| 71 |
func (cfg *testDevListenerCfg) TLSServerConfig() *tls.Config { |
| 72 |
return helpers.TestTLSServerConfig |
| 73 |
} |
| 74 |
|
| 75 |
func (s *listenerSuite) TestDeviceListen(c *C) { |
| 76 |
lst, err := DeviceListen(nil, &testDevListenerCfg{"127.0.0.1:0"}) |
| 77 |
c.Check(err, IsNil) |
| 78 |
defer lst.Close() |
| 79 |
c.Check(lst.Addr().String(), Matches, `127.0.0.1:\d{5}`) |
| 80 |
} |
| 81 |
|
| 82 |
func (s *listenerSuite) TestDeviceListenError(c *C) { |
| 83 |
// assume tests are not running as root |
| 84 |
_, err := DeviceListen(nil, &testDevListenerCfg{"127.0.0.1:99"}) |
| 85 |
c.Check(err, ErrorMatches, ".*permission denied.*") |
| 86 |
} |
| 87 |
|
| 88 |
type testNetError struct { |
| 89 |
temp bool |
| 90 |
} |
| 91 |
|
| 92 |
func (tne *testNetError) Error() string { |
| 93 |
return "test net error" |
| 94 |
} |
| 95 |
|
| 96 |
func (tne *testNetError) Temporary() bool { |
| 97 |
return tne.temp |
| 98 |
} |
| 99 |
|
| 100 |
func (tne *testNetError) Timeout() bool { |
| 101 |
return false |
| 102 |
} |
| 103 |
|
| 104 |
var _ net.Error = &testNetError{} // sanity check |
| 105 |
|
| 106 |
func (s *listenerSuite) TestHandleTemporary(c *C) { |
| 107 |
c.Check(handleTemporary(&testNetError{true}), Equals, true) |
| 108 |
c.Check(handleTemporary(&testNetError{false}), Equals, false) |
| 109 |
} |
| 110 |
|
| 111 |
func testSession(conn net.Conn) error { |
| 112 |
defer conn.Close() |
| 113 |
conn.SetDeadline(time.Now().Add(10 * time.Second)) |
| 114 |
var buf [1]byte |
| 115 |
for { |
| 116 |
_, err := io.ReadFull(conn, buf[:]) |
| 117 |
if err != nil { |
| 118 |
return err |
| 119 |
} |
| 120 |
// 1|2... send digit back |
| 121 |
if unicode.IsDigit(rune(buf[0])) { |
| 122 |
break |
| 123 |
} |
| 124 |
} |
| 125 |
_, err := conn.Write(buf[:]) |
| 126 |
return err |
| 127 |
} |
| 128 |
|
| 129 |
func testTlsDial(addr string) (net.Conn, error) { |
| 130 |
return tls.Dial("tcp", addr, helpers.TestTLSClientConfig) |
| 131 |
} |
| 132 |
|
| 133 |
func testWriteByte(c *C, conn net.Conn, toWrite uint32) { |
| 134 |
conn.SetDeadline(time.Now().Add(2 * time.Second)) |
| 135 |
_, err := conn.Write([]byte{byte(toWrite)}) |
| 136 |
c.Assert(err, IsNil) |
| 137 |
} |
| 138 |
|
| 139 |
func testReadByte(c *C, conn net.Conn, expected uint32) { |
| 140 |
var buf [1]byte |
| 141 |
_, err := io.ReadFull(conn, buf[:]) |
| 142 |
c.Check(err, IsNil) |
| 143 |
c.Check(buf[0], Equals, byte(expected)) |
| 144 |
} |
| 145 |
|
| 146 |
type testSessionResourceManager struct { |
| 147 |
event chan string |
| 148 |
} |
| 149 |
|
| 150 |
func (r *testSessionResourceManager) ConsumeConn() { |
| 151 |
r.event <- "consume" |
| 152 |
} |
| 153 |
|
| 154 |
// takeNext takes a string from given channel with a 5s timeout |
| 155 |
func takeNext(ch <-chan string) string { |
| 156 |
select { |
| 157 |
case <-time.After(5 * time.Second): |
| 158 |
panic("test protocol exchange stuck: too long waiting") |
| 159 |
case v := <-ch: |
| 160 |
return v |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
func (s *listenerSuite) TestDeviceAcceptLoop(c *C) { |
| 165 |
lst, err := DeviceListen(nil, &testDevListenerCfg{"127.0.0.1:0"}) |
| 166 |
c.Check(err, IsNil) |
| 167 |
defer lst.Close() |
| 168 |
errCh := make(chan error) |
| 169 |
rEvent := make(chan string) |
| 170 |
resource := &testSessionResourceManager{rEvent} |
| 171 |
go func() { |
| 172 |
errCh <- lst.AcceptLoop(testSession, resource, s.testlog) |
| 173 |
}() |
| 174 |
listenerAddr := lst.Addr().String() |
| 175 |
c.Check(takeNext(rEvent), Equals, "consume") |
| 176 |
conn1, err := testTlsDial(listenerAddr) |
| 177 |
c.Assert(err, IsNil) |
| 178 |
c.Check(takeNext(rEvent), Equals, "consume") |
| 179 |
defer conn1.Close() |
| 180 |
testWriteByte(c, conn1, '1') |
| 181 |
conn2, err := testTlsDial(listenerAddr) |
| 182 |
c.Assert(err, IsNil) |
| 183 |
c.Check(takeNext(rEvent), Equals, "consume") |
| 184 |
defer conn2.Close() |
| 185 |
testWriteByte(c, conn2, '2') |
| 186 |
testReadByte(c, conn1, '1') |
| 187 |
testReadByte(c, conn2, '2') |
| 188 |
lst.Close() |
| 189 |
c.Check(<-errCh, ErrorMatches, ".*use of closed.*") |
| 190 |
c.Check(s.testlog.Captured(), Equals, "") |
| 191 |
} |
| 192 |
|
| 193 |
// waitForLogs waits for the logs captured in s.testlog to match reStr. |
| 194 |
func (s *listenerSuite) waitForLogs(c *C, reStr string) { |
| 195 |
rx := regexp.MustCompile("^" + reStr + "$") |
| 196 |
for i := 0; i < 100; i++ { |
| 197 |
if rx.MatchString(s.testlog.Captured()) { |
| 198 |
break |
| 199 |
} |
| 200 |
time.Sleep(20 * time.Millisecond) |
| 201 |
} |
| 202 |
c.Check(s.testlog.Captured(), Matches, reStr) |
| 203 |
} |
| 204 |
|
| 205 |
func (s *listenerSuite) TestDeviceAcceptLoopTemporaryError(c *C) { |
| 206 |
// ENFILE is not the temp network error we want to handle this way |
| 207 |
// but is relatively easy to generate in a controlled way |
| 208 |
var err error |
| 209 |
lst, err := DeviceListen(nil, &testDevListenerCfg{"127.0.0.1:0"}) |
| 210 |
c.Check(err, IsNil) |
| 211 |
defer lst.Close() |
| 212 |
errCh := make(chan error) |
| 213 |
resource := &NopSessionResourceManager{} |
| 214 |
go func() { |
| 215 |
errCh <- lst.AcceptLoop(testSession, resource, s.testlog) |
| 216 |
}() |
| 217 |
listenerAddr := lst.Addr().String() |
| 218 |
connectMany := helpers.ScriptAbsPath("connect-many.py") |
| 219 |
cmd := exec.Command(connectMany, listenerAddr) |
| 220 |
res, err := cmd.Output() |
| 221 |
c.Assert(err, IsNil) |
| 222 |
c.Assert(string(res), Matches, "(?s).*timed out.*") |
| 223 |
conn2, err := testTlsDial(listenerAddr) |
| 224 |
c.Assert(err, IsNil) |
| 225 |
defer conn2.Close() |
| 226 |
testWriteByte(c, conn2, '2') |
| 227 |
testReadByte(c, conn2, '2') |
| 228 |
lst.Close() |
| 229 |
c.Check(<-errCh, ErrorMatches, ".*use of closed.*") |
| 230 |
s.waitForLogs(c, "(?ms).*device listener:.*accept.*too many open.*-- retrying") |
| 231 |
} |
| 232 |
|
| 233 |
func (s *listenerSuite) TestDeviceAcceptLoopPanic(c *C) { |
| 234 |
lst, err := DeviceListen(nil, &testDevListenerCfg{"127.0.0.1:0"}) |
| 235 |
c.Check(err, IsNil) |
| 236 |
defer lst.Close() |
| 237 |
errCh := make(chan error) |
| 238 |
resource := &NopSessionResourceManager{} |
| 239 |
go func() { |
| 240 |
errCh <- lst.AcceptLoop(func(conn net.Conn) error { |
| 241 |
defer conn.Close() |
| 242 |
panic("session crash") |
| 243 |
}, resource, s.testlog) |
| 244 |
}() |
| 245 |
listenerAddr := lst.Addr().String() |
| 246 |
_, err = testTlsDial(listenerAddr) |
| 247 |
c.Assert(err, Not(IsNil)) |
| 248 |
lst.Close() |
| 249 |
c.Check(<-errCh, ErrorMatches, ".*use of closed.*") |
| 250 |
s.waitForLogs(c, "(?s)ERROR\\(PANIC\\) terminating device connection on: session crash:.*AcceptLoop.*") |
| 251 |
} |
| 252 |
|
| 253 |
func (s *listenerSuite) TestForeignListener(c *C) { |
| 254 |
foreignLst, err := net.Listen("tcp", "127.0.0.1:0") |
| 255 |
c.Check(err, IsNil) |
| 256 |
lst, err := DeviceListen(foreignLst, &testDevListenerCfg{"127.0.0.1:0"}) |
| 257 |
c.Check(err, IsNil) |
| 258 |
defer lst.Close() |
| 259 |
errCh := make(chan error) |
| 260 |
resource := &NopSessionResourceManager{} |
| 261 |
go func() { |
| 262 |
errCh <- lst.AcceptLoop(testSession, resource, s.testlog) |
| 263 |
}() |
| 264 |
listenerAddr := lst.Addr().String() |
| 265 |
c.Check(listenerAddr, Equals, foreignLst.Addr().String()) |
| 266 |
conn1, err := testTlsDial(listenerAddr) |
| 267 |
c.Assert(err, IsNil) |
| 268 |
defer conn1.Close() |
| 269 |
testWriteByte(c, conn1, '1') |
| 270 |
testReadByte(c, conn1, '1') |
| 271 |
lst.Close() |
| 272 |
c.Check(<-errCh, ErrorMatches, ".*use of closed.*") |
| 273 |
c.Check(s.testlog.Captured(), Equals, "") |
| 274 |
} |