Debian Codegraph @

server/runner_test.go

raw
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 server
18
19 import (
20 "crypto/tls"
21 "fmt"
22 "io/ioutil"
23 "net"
24 "net/http"
25 "time"
26
27 . "gopkg.in/check.v1"
28
29 "gitlab.com/ubports/development/core/lomiri-push-service/config"
30 "gitlab.com/ubports/development/core/lomiri-push-service/server/listener"
31 helpers "gitlab.com/ubports/development/core/lomiri-push-service/testing"
32 )
33
34 type runnerSuite struct {
35 prevBootLogListener func(string, net.Listener)
36 prevBootLogFatalf func(string, ...interface{})
37 lst net.Listener
38 kind string
39 }
40
41 var _ = Suite(&runnerSuite{})
42
43 func (s *runnerSuite) SetUpSuite(c *C) {
44 s.prevBootLogFatalf = BootLogFatalf
45 s.prevBootLogListener = BootLogListener
46 BootLogFatalf = func(format string, v ...interface{}) {
47 panic(fmt.Sprintf(format, v...))
48 }
49 BootLogListener = func(kind string, lst net.Listener) {
50 s.kind = kind
51 s.lst = lst
52 }
53 }
54
55 func (s *runnerSuite) TearDownSuite(c *C) {
56 BootLogListener = s.prevBootLogListener
57 BootLogFatalf = s.prevBootLogFatalf
58 }
59
60 var testHTTPServeParsedConfig = HTTPServeParsedConfig{
61 "127.0.0.1:0",
62 config.ConfigTimeDuration{5 * time.Second},
63 config.ConfigTimeDuration{5 * time.Second},
64 }
65
66 func testHandle(w http.ResponseWriter, r *http.Request) {
67 fmt.Fprintf(w, "yay!\n")
68 }
69
70 func (s *runnerSuite) TestHTTPServeRunner(c *C) {
71 errCh := make(chan interface{}, 1)
72 h := http.HandlerFunc(testHandle)
73 runner := HTTPServeRunner(nil, h, &testHTTPServeParsedConfig, nil)
74 c.Assert(s.lst, Not(IsNil))
75 defer s.lst.Close()
76 c.Check(s.kind, Equals, "http")
77 go func() {
78 defer func() {
79 errCh <- recover()
80 }()
81 runner()
82 }()
83 resp, err := http.Get(fmt.Sprintf("http://%s/", s.lst.Addr()))
84 c.Assert(err, IsNil)
85 defer resp.Body.Close()
86 c.Assert(resp.StatusCode, Equals, 200)
87 body, err := ioutil.ReadAll(resp.Body)
88 c.Assert(err, IsNil)
89 c.Check(string(body), Equals, "yay!\n")
90 s.lst.Close()
91 c.Check(<-errCh, Matches, "accepting http connections:.*closed.*")
92 }
93
94 func cert() tls.Certificate {
95 cert, err := tls.X509KeyPair(helpers.TestCertPEMBlock, helpers.TestKeyPEMBlock)
96 if err != nil {
97 panic(err)
98 }
99 return cert
100 }
101
102 var testDevicesParsedConfig = DevicesParsedConfig{
103 ParsedPingInterval: config.ConfigTimeDuration{60 * time.Second},
104 ParsedExchangeTimeout: config.ConfigTimeDuration{10 * time.Second},
105 ParsedBrokerQueueSize: config.ConfigQueueSize(1000),
106 ParsedSessionQueueSize: config.ConfigQueueSize(10),
107 ParsedAddr: "127.0.0.1:0",
108 TLSParsedConfig: TLSParsedConfig{
109 ParsedKeyPEMFile: "",
110 ParsedCertPEMFile: "",
111 cert: cert(),
112 },
113 }
114
115 var resource = &listener.NopSessionResourceManager{}
116
117 func (s *runnerSuite) TestDevicesRunner(c *C) {
118 prevBootLogger := BootLogger
119 testlog := helpers.NewTestLogger(c, "debug")
120 BootLogger = testlog
121 defer func() {
122 BootLogger = prevBootLogger
123 }()
124 runner := DevicesRunner(nil, func(conn net.Conn) error { return nil }, BootLogger, resource, &testDevicesParsedConfig)
125 c.Assert(s.lst, Not(IsNil))
126 s.lst.Close()
127 c.Check(s.kind, Equals, "devices")
128 c.Check(runner, PanicMatches, "accepting device connections:.*closed.*")
129 }
130
131 func (s *runnerSuite) TestDevicesRunnerAdoptListener(c *C) {
132 prevBootLogger := BootLogger
133 testlog := helpers.NewTestLogger(c, "debug")
134 BootLogger = testlog
135 defer func() {
136 BootLogger = prevBootLogger
137 }()
138 lst0, err := net.Listen("tcp", "127.0.0.1:0")
139 c.Assert(err, IsNil)
140 defer lst0.Close()
141 DevicesRunner(lst0, func(conn net.Conn) error { return nil }, BootLogger, resource, &testDevicesParsedConfig)
142 c.Assert(s.lst, Not(IsNil))
143 c.Check(s.lst.Addr().String(), Equals, lst0.Addr().String())
144 s.lst.Close()
145 }
146
147 func (s *runnerSuite) TestHTTPServeRunnerAdoptListener(c *C) {
148 lst0, err := net.Listen("tcp", "127.0.0.1:0")
149 c.Assert(err, IsNil)
150 defer lst0.Close()
151 HTTPServeRunner(lst0, nil, &testHTTPServeParsedConfig, nil)
152 c.Assert(s.lst, Equals, lst0)
153 c.Check(s.kind, Equals, "http")
154 }
155
156 func (s *runnerSuite) TestHTTPServeRunnerTLS(c *C) {
157 errCh := make(chan interface{}, 1)
158 h := http.HandlerFunc(testHandle)
159 runner := HTTPServeRunner(nil, h, &testHTTPServeParsedConfig, helpers.TestTLSServerConfig)
160 c.Assert(s.lst, Not(IsNil))
161 defer s.lst.Close()
162 c.Check(s.kind, Equals, "http")
163 go func() {
164 defer func() {
165 errCh <- recover()
166 }()
167 runner()
168 }()
169 cli := http.Client{
170 Transport: &http.Transport{
171 TLSClientConfig: helpers.TestTLSClientConfig,
172 },
173 }
174 resp, err := cli.Get(fmt.Sprintf("https://%s/", s.lst.Addr()))
175 c.Assert(err, IsNil)
176 defer resp.Body.Close()
177 c.Assert(resp.StatusCode, Equals, 200)
178 body, err := ioutil.ReadAll(resp.Body)
179 c.Assert(err, IsNil)
180 c.Check(string(body), Equals, "yay!\n")
181 s.lst.Close()
182 c.Check(<-errCh, Matches, "accepting http connections:.*closed.*")
183 }