| 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 suites contains reusable acceptance test suites. |
| 18 |
package suites |
| 19 |
|
| 20 |
import ( |
| 21 |
"flag" |
| 22 |
"fmt" |
| 23 |
"net" |
| 24 |
"net/http" |
| 25 |
"os" |
| 26 |
"regexp" |
| 27 |
"runtime" |
| 28 |
"time" |
| 29 |
|
| 30 |
. "gopkg.in/check.v1" |
| 31 |
|
| 32 |
"gitlab.com/ubports/development/core/lomiri-push-service/server/acceptance" |
| 33 |
"gitlab.com/ubports/development/core/lomiri-push-service/server/acceptance/kit" |
| 34 |
helpers "gitlab.com/ubports/development/core/lomiri-push-service/testing" |
| 35 |
) |
| 36 |
|
| 37 |
// ServerHandle holds the information to attach a client to the test server. |
| 38 |
type ServerHandle struct { |
| 39 |
ServerAddr string |
| 40 |
ServerHTTPAddr string |
| 41 |
ServerEvents <-chan string |
| 42 |
// last started session |
| 43 |
LastSession *acceptance.ClientSession |
| 44 |
} |
| 45 |
|
| 46 |
// Start a client. |
| 47 |
func (h *ServerHandle) StartClient(c *C, devId string, levels map[string]int64) (events <-chan string, errorCh <-chan error, stop func()) { |
| 48 |
return h.StartClientAuth(c, devId, levels, "", "") |
| 49 |
} |
| 50 |
|
| 51 |
// Start a client with auth. |
| 52 |
func (h *ServerHandle) StartClientAuth(c *C, devId string, levels map[string]int64, auth string, cookie string) (events <-chan string, errorCh <-chan error, stop func()) { |
| 53 |
cliEvents, errCh, stop := h.StartClientAuthFlex(c, devId, levels, auth, cookie, regexp.QuoteMeta(devId)) |
| 54 |
c.Assert(NextEvent(cliEvents, errCh), Matches, "connected .*") |
| 55 |
return cliEvents, errCh, stop |
| 56 |
} |
| 57 |
|
| 58 |
// Start a client with auth, take a devId regexp, don't check any client event. |
| 59 |
func (h *ServerHandle) StartClientAuthFlex(c *C, devId string, levels map[string]int64, auth, cookie, devIdRegexp string) (events <-chan string, errorCh <-chan error, stop func()) { |
| 60 |
errCh := make(chan error, 1) |
| 61 |
cliEvents := make(chan string, 10) |
| 62 |
sess := testClientSession(h.ServerAddr, devId, "m1", "img1", false) |
| 63 |
sess.Levels = levels |
| 64 |
sess.Auth = auth |
| 65 |
if auth != "" { |
| 66 |
sess.ExchangeTimeout = 5 * time.Second |
| 67 |
} |
| 68 |
if cookie != "" { |
| 69 |
sess.SetCookie(cookie) |
| 70 |
sess.ReportSetParams = true |
| 71 |
} |
| 72 |
err := sess.Dial() |
| 73 |
c.Assert(err, IsNil) |
| 74 |
h.LastSession = sess |
| 75 |
clientShutdown := make(chan bool, 1) // abused as an atomic flag |
| 76 |
intercept := func(ic *interceptingConn, op string, b []byte) (bool, int, error) { |
| 77 |
// read after ack |
| 78 |
if op == "read" && len(clientShutdown) > 0 { |
| 79 |
// exit the sess.Run() goroutine, client will close |
| 80 |
runtime.Goexit() |
| 81 |
} |
| 82 |
return false, 0, nil |
| 83 |
} |
| 84 |
sess.Connection = &interceptingConn{sess.Connection, 0, 0, intercept} |
| 85 |
go func() { |
| 86 |
errCh <- sess.Run(cliEvents) |
| 87 |
}() |
| 88 |
c.Assert(NextEvent(h.ServerEvents, nil), Matches, ".*session.* connected .*") |
| 89 |
c.Assert(NextEvent(h.ServerEvents, nil), Matches, ".*session.* registered "+devIdRegexp) |
| 90 |
return cliEvents, errCh, func() { clientShutdown <- true } |
| 91 |
} |
| 92 |
|
| 93 |
// AcceptanceSuite has the basic functionality of the acceptance suites. |
| 94 |
type AcceptanceSuite struct { |
| 95 |
// hook to start the server(s) |
| 96 |
StartServer func(c *C, s *AcceptanceSuite, handle *ServerHandle) |
| 97 |
// populated by StartServer |
| 98 |
ServerHandle |
| 99 |
kit.APIClient // has ServerAPIURL |
| 100 |
// KillGroup should be populated by StartServer with functions |
| 101 |
// to kill the server process |
| 102 |
KillGroup map[string]func(os.Signal) |
| 103 |
} |
| 104 |
|
| 105 |
// Start a new server for each test. |
| 106 |
func (s *AcceptanceSuite) SetUpTest(c *C) { |
| 107 |
s.KillGroup = make(map[string]func(os.Signal)) |
| 108 |
s.StartServer(c, s, &s.ServerHandle) |
| 109 |
c.Assert(s.ServerHandle.ServerEvents, NotNil) |
| 110 |
c.Assert(s.ServerHandle.ServerAddr, Not(Equals), "") |
| 111 |
c.Assert(s.ServerAPIURL, Not(Equals), "") |
| 112 |
s.SetupClient(nil, false, http.DefaultMaxIdleConnsPerHost) |
| 113 |
} |
| 114 |
|
| 115 |
func (s *AcceptanceSuite) TearDownTest(c *C) { |
| 116 |
for _, f := range s.KillGroup { |
| 117 |
f(os.Kill) |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
func testClientSession(addr string, deviceId, model, imageChannel string, reportPings bool) *acceptance.ClientSession { |
| 122 |
tlsConfig, err := kit.MakeTLSConfig("push-delivery", false, helpers.SourceRelative("../ssl/testing.cert"), "") |
| 123 |
if err != nil { |
| 124 |
panic(fmt.Sprintf("could not read ssl/testing.cert: %v", err)) |
| 125 |
} |
| 126 |
return &acceptance.ClientSession{ |
| 127 |
ExchangeTimeout: 100 * time.Millisecond, |
| 128 |
ServerAddr: addr, |
| 129 |
DeviceId: deviceId, |
| 130 |
Model: model, |
| 131 |
ImageChannel: imageChannel, |
| 132 |
ReportPings: reportPings, |
| 133 |
TLSConfig: tlsConfig, |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
// typically combined with -gocheck.vv or test selection |
| 138 |
var logTraffic = flag.Bool("logTraffic", false, "log traffic") |
| 139 |
|
| 140 |
type connInterceptor func(ic *interceptingConn, op string, b []byte) (bool, int, error) |
| 141 |
|
| 142 |
type interceptingConn struct { |
| 143 |
net.Conn |
| 144 |
totalRead int |
| 145 |
totalWritten int |
| 146 |
intercept connInterceptor |
| 147 |
} |
| 148 |
|
| 149 |
func (ic *interceptingConn) Write(b []byte) (n int, err error) { |
| 150 |
done := false |
| 151 |
before := ic.totalWritten |
| 152 |
if ic.intercept != nil { |
| 153 |
done, n, err = ic.intercept(ic, "write", b) |
| 154 |
} |
| 155 |
if !done { |
| 156 |
n, err = ic.Conn.Write(b) |
| 157 |
} |
| 158 |
ic.totalWritten += n |
| 159 |
if *logTraffic { |
| 160 |
fmt.Printf("W[%v]: %d %#v %v %d\n", ic.Conn.LocalAddr(), before, string(b[:n]), err, ic.totalWritten) |
| 161 |
} |
| 162 |
return |
| 163 |
} |
| 164 |
|
| 165 |
func (ic *interceptingConn) Read(b []byte) (n int, err error) { |
| 166 |
done := false |
| 167 |
before := ic.totalRead |
| 168 |
if ic.intercept != nil { |
| 169 |
done, n, err = ic.intercept(ic, "read", b) |
| 170 |
} |
| 171 |
if !done { |
| 172 |
n, err = ic.Conn.Read(b) |
| 173 |
} |
| 174 |
ic.totalRead += n |
| 175 |
if *logTraffic { |
| 176 |
fmt.Printf("R[%v]: %d %#v %v %d\n", ic.Conn.LocalAddr(), before, string(b[:n]), err, ic.totalRead) |
| 177 |
} |
| 178 |
return |
| 179 |
} |
| 180 |
|
| 181 |
// Long after the end of the tests. |
| 182 |
var future = time.Now().Add(9 * time.Hour).Format(time.RFC3339) |