| 1 |
/* |
| 2 |
Copyright 2013-2015 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 connectivity |
| 18 |
|
| 19 |
import ( |
| 20 |
. "gopkg.in/check.v1" |
| 21 |
helpers "gitlab.com/ubports/development/core/lomiri-push-service/testing" |
| 22 |
"gitlab.com/ubports/development/core/lomiri-push-service/util" |
| 23 |
"net/http" |
| 24 |
"net/http/httptest" |
| 25 |
"time" |
| 26 |
) |
| 27 |
|
| 28 |
type WebcheckerSuite struct { |
| 29 |
timeouts []time.Duration |
| 30 |
log *helpers.TestLogger |
| 31 |
} |
| 32 |
|
| 33 |
var _ = Suite(&WebcheckerSuite{}) |
| 34 |
|
| 35 |
const ( |
| 36 |
staticText = "something ipsum dolor something" |
| 37 |
staticMatch = "ipsum dolor" |
| 38 |
staticHash = "6155f83b471583f47c99998a472a178f" |
| 39 |
bigText = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. |
| 40 |
Vivamus tincidunt vitae sapien tempus fermentum. Cras commodo augue luctu, |
| 41 |
tempus libero sit amet, laoreet lectus. Vestibulum ali justo et malesuada |
| 42 |
placerat. Pellentesque viverra luctus velit, adipiscing fermentum tortori |
| 43 |
vehicula nec. Integer tincidunt purus et pretium vestibulum. Donec portas |
| 44 |
suscipit pulvinar. Suspendisse potenti. Donec sit amet pharetra nisl, sit |
| 45 |
amet posuere orci. In feugiat elitist nec augue fringilla, a rutrum risus |
| 46 |
posuere. Aliquam erat volutpat. Morbi aliquam arcu et eleifend placeraten. |
| 47 |
Pellentesque egestas varius aliquam. In egestas nisi sed ipsum tristiquer |
| 48 |
lacinia. Sed vitae nisi non eros consectetur vestibulum vehicularum vitae. |
| 49 |
Curabitur cursus consectetur eros, in vestibulum turpis cursus at i lorem. |
| 50 |
Pellentesque ultrices arcu ut massa faucibus, e consequat sapien placerat. |
| 51 |
Maecenas quis ultricies mi. Phasellus turpis nisl, porttitor ac mi cursus, |
| 52 |
euismod imperdiet lorem. Donec facilisis est id dignissim imperdiet.` |
| 53 |
bigHash = "9bf86bce26e8f2d9c9d9bd4a98f9e668" |
| 54 |
) |
| 55 |
|
| 56 |
// mkHandler makes an http.HandlerFunc that returns the provided text |
| 57 |
// for whatever request it's given. |
| 58 |
func mkHandler(text string) http.HandlerFunc { |
| 59 |
return func(w http.ResponseWriter, r *http.Request) { |
| 60 |
w.(http.Flusher).Flush() |
| 61 |
w.Write([]byte(text)) |
| 62 |
w.(http.Flusher).Flush() |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func (s *WebcheckerSuite) SetUpSuite(c *C) { |
| 67 |
s.timeouts = util.SwapTimeouts([]time.Duration{0}) |
| 68 |
} |
| 69 |
|
| 70 |
func (s *WebcheckerSuite) TearDownSuite(c *C) { |
| 71 |
util.SwapTimeouts(s.timeouts) |
| 72 |
s.timeouts = nil |
| 73 |
} |
| 74 |
|
| 75 |
func (s *WebcheckerSuite) SetUpTest(c *C) { |
| 76 |
s.log = helpers.NewTestLogger(c, "debug") |
| 77 |
} |
| 78 |
|
| 79 |
// Webchecker sends true when everything works |
| 80 |
func (s *WebcheckerSuite) TestWorks(c *C) { |
| 81 |
ts := httptest.NewServer(mkHandler(staticText)) |
| 82 |
defer ts.Close() |
| 83 |
|
| 84 |
ck := NewWebchecker(ts.URL, staticHash, "", 5*time.Second, s.log) |
| 85 |
defer ck.Close() |
| 86 |
ch := make(chan bool, 1) |
| 87 |
ck.Webcheck(ch) |
| 88 |
c.Check(<-ch, Equals, true) |
| 89 |
} |
| 90 |
|
| 91 |
// Webchecker sends true when everything works |
| 92 |
func (s *WebcheckerSuite) TestStringWorks(c *C) { |
| 93 |
ts := httptest.NewServer(mkHandler(staticText)) |
| 94 |
defer ts.Close() |
| 95 |
|
| 96 |
ck := NewWebchecker(ts.URL, "", staticMatch, 5*time.Second, s.log) |
| 97 |
defer ck.Close() |
| 98 |
ch := make(chan bool, 1) |
| 99 |
ck.Webcheck(ch) |
| 100 |
c.Check(<-ch, Equals, true) |
| 101 |
} |
| 102 |
|
| 103 |
// Webchecker sends false if the download fails. |
| 104 |
func (s *WebcheckerSuite) TestActualFails(c *C) { |
| 105 |
ck := NewWebchecker("garbage://", "", "", 5*time.Second, s.log) |
| 106 |
defer ck.Close() |
| 107 |
ch := make(chan bool, 1) |
| 108 |
ck.Webcheck(ch) |
| 109 |
c.Check(<-ch, Equals, false) |
| 110 |
} |
| 111 |
|
| 112 |
// Webchecker sends false if the string doesn't match |
| 113 |
func (s *WebcheckerSuite) TestStringFails(c *C) { |
| 114 |
ts := httptest.NewServer(mkHandler("")) |
| 115 |
defer ts.Close() |
| 116 |
|
| 117 |
ck := NewWebchecker(ts.URL, "", staticMatch, 5*time.Second, s.log) |
| 118 |
defer ck.Close() |
| 119 |
ch := make(chan bool, 1) |
| 120 |
ck.Webcheck(ch) |
| 121 |
c.Check(<-ch, Equals, false) |
| 122 |
c.Check(s.log.Captured(), Matches, "(?ism).*string not found.*") |
| 123 |
} |
| 124 |
|
| 125 |
// Webchecker sends false if the hash doesn't match |
| 126 |
func (s *WebcheckerSuite) TestHashFails(c *C) { |
| 127 |
ts := httptest.NewServer(mkHandler("")) |
| 128 |
defer ts.Close() |
| 129 |
|
| 130 |
ck := NewWebchecker(ts.URL, staticHash, "", 5*time.Second, s.log) |
| 131 |
defer ck.Close() |
| 132 |
ch := make(chan bool, 1) |
| 133 |
ck.Webcheck(ch) |
| 134 |
c.Check(<-ch, Equals, false) |
| 135 |
c.Check(s.log.Captured(), Matches, "(?ism).*content mismatch.*") |
| 136 |
} |
| 137 |
|
| 138 |
// Webchecker sends false if the download is too big |
| 139 |
func (s *WebcheckerSuite) TestTooBigFails(c *C) { |
| 140 |
ts := httptest.NewServer(mkHandler(bigText)) |
| 141 |
defer ts.Close() |
| 142 |
|
| 143 |
ck := NewWebchecker(ts.URL, bigHash, "", 5*time.Second, s.log) |
| 144 |
defer ck.Close() |
| 145 |
ch := make(chan bool, 1) |
| 146 |
ck.Webcheck(ch) |
| 147 |
c.Check(<-ch, Equals, false) |
| 148 |
c.Check(s.log.Captured(), Matches, "(?ism).*larger than 1k.*") |
| 149 |
} |
| 150 |
|
| 151 |
// Webchecker sends false if the request timeouts |
| 152 |
func (s *WebcheckerSuite) TestTooSlowFails(c *C) { |
| 153 |
finish := make(chan bool) |
| 154 |
handler := http.HandlerFunc(func(http.ResponseWriter, *http.Request) { |
| 155 |
<-finish // get stuck |
| 156 |
}) |
| 157 |
ts := httptest.NewServer(handler) |
| 158 |
defer ts.Close() |
| 159 |
defer func() { |
| 160 |
finish <- true |
| 161 |
}() |
| 162 |
|
| 163 |
ck := NewWebchecker(ts.URL, bigHash, "", time.Second, s.log) |
| 164 |
defer ck.Close() |
| 165 |
ch := make(chan bool, 1) |
| 166 |
ck.Webcheck(ch) |
| 167 |
c.Check(<-ch, Equals, false) |
| 168 |
} |