Debian Codegraph @

server/acceptance/suites/unicast.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 suites
18
19 import (
20 "encoding/json"
21 "fmt"
22 "strings"
23
24 . "gopkg.in/check.v1"
25
26 "gitlab.com/ubports/development/core/lomiri-push-service/server/acceptance/kit"
27 "gitlab.com/ubports/development/core/lomiri-push-service/server/api"
28 )
29
30 // UnicastAcceptanceSuite has tests about unicast.
31 type UnicastAcceptanceSuite struct {
32 AcceptanceSuite
33 AssociatedAuth func(string) (string, string)
34 }
35
36 func (s *UnicastAcceptanceSuite) associatedAuth(deviceId string) (userId string, auth string) {
37 if s.AssociatedAuth != nil {
38 return s.AssociatedAuth(deviceId)
39 }
40 return deviceId, ""
41 }
42
43 func (s *UnicastAcceptanceSuite) TestUnicastToConnected(c *C) {
44 _, auth := s.associatedAuth("DEV1")
45 res, err := s.PostRequest("/register", &api.Registration{
46 DeviceId: "DEV1",
47 AppId: "app1",
48 })
49 c.Assert(err, IsNil, Commentf("%v", res))
50 events, errCh, stop := s.StartClientAuth(c, "DEV1", nil, auth, "")
51 got, err := s.PostRequest("/notify", &api.Unicast{
52 Token: res["token"].(string),
53 AppId: "app1",
54 ExpireOn: future,
55 Data: json.RawMessage(`{"a": 42}`),
56 })
57 c.Assert(err, IsNil, Commentf("%v", got))
58 c.Check(NextEvent(events, errCh), Equals, `unicast app:app1 payload:{"a":42};`)
59 stop()
60 c.Assert(NextEvent(s.ServerEvents, nil), Matches, `.* ended with:.*EOF`)
61 c.Check(len(errCh), Equals, 0)
62 }
63
64 func (s *UnicastAcceptanceSuite) TestUnicastCorrectDistribution(c *C) {
65 userId1, auth1 := s.associatedAuth("DEV1")
66 userId2, auth2 := s.associatedAuth("DEV2")
67 // start 1st client
68 events1, errCh1, stop1 := s.StartClientAuth(c, "DEV1", nil, auth1, "")
69 // start 2nd client
70 events2, errCh2, stop2 := s.StartClientAuth(c, "DEV2", nil, auth2, "")
71 // unicast to one and the other
72 got, err := s.PostRequest("/notify", &api.Unicast{
73 UserId: userId1,
74 DeviceId: "DEV1",
75 AppId: "app1",
76 ExpireOn: future,
77 Data: json.RawMessage(`{"to": 1}`),
78 })
79 c.Assert(err, IsNil, Commentf("%v", got))
80 got, err = s.PostRequest("/notify", &api.Unicast{
81 UserId: userId2,
82 DeviceId: "DEV2",
83 AppId: "app1",
84 ExpireOn: future,
85 Data: json.RawMessage(`{"to": 2}`),
86 })
87 c.Assert(err, IsNil, Commentf("%v", got))
88 c.Check(NextEvent(events1, errCh1), Equals, `unicast app:app1 payload:{"to":1};`)
89 c.Check(NextEvent(events2, errCh2), Equals, `unicast app:app1 payload:{"to":2};`)
90 stop1()
91 stop2()
92 c.Assert(NextEvent(s.ServerEvents, nil), Matches, `.* ended with:.*EOF`)
93 c.Assert(NextEvent(s.ServerEvents, nil), Matches, `.* ended with:.*EOF`)
94 c.Check(len(errCh1), Equals, 0)
95 c.Check(len(errCh2), Equals, 0)
96 }
97
98 func (s *UnicastAcceptanceSuite) TestUnicastPending(c *C) {
99 // send unicast that will be pending
100 userId, auth := s.associatedAuth("DEV1")
101 got, err := s.PostRequest("/notify", &api.Unicast{
102 UserId: userId,
103 DeviceId: "DEV1",
104 AppId: "app1",
105 ExpireOn: future,
106 Data: json.RawMessage(`{"a": 42}`),
107 })
108 c.Assert(err, IsNil, Commentf("%v", got))
109
110 // get pending on connect
111 events, errCh, stop := s.StartClientAuth(c, "DEV1", nil, auth, "")
112 c.Check(NextEvent(events, errCh), Equals, `unicast app:app1 payload:{"a":42};`)
113 stop()
114 c.Assert(NextEvent(s.ServerEvents, nil), Matches, `.* ended with:.*EOF`)
115 c.Check(len(errCh), Equals, 0)
116 }
117
118 func (s *UnicastAcceptanceSuite) TestUnicastLargeNeedsSplitting(c *C) {
119 userId, auth := s.associatedAuth("DEV2")
120 // send bunch of unicasts that will be pending
121 payloadFmt := fmt.Sprintf(`{"serial":%%d,"bloat":"%s"}`, strings.Repeat("x", 2024))
122 for i := 0; i < 32; i++ {
123 got, err := s.PostRequest("/notify", &api.Unicast{
124 UserId: userId,
125 DeviceId: "DEV2",
126 AppId: "app1",
127 ExpireOn: future,
128 Data: json.RawMessage(fmt.Sprintf(payloadFmt, i)),
129 })
130 c.Assert(err, IsNil, Commentf("%v", got))
131 }
132
133 events, errCh, stop := s.StartClientAuth(c, "DEV2", nil, auth, "")
134 // getting pending on connect
135 n := 0
136 for {
137 evt := NextEvent(events, errCh)
138 c.Check(evt, Matches, "unicast app:app1 .*")
139 n += 1
140 if strings.Contains(evt, `"serial":31`) {
141 break
142 }
143 }
144 // was split
145 c.Check(n > 1, Equals, true)
146 stop()
147 c.Assert(NextEvent(s.ServerEvents, nil), Matches, `.* ended with:.*EOF`)
148 c.Check(len(errCh), Equals, 0)
149 }
150
151 func (s *UnicastAcceptanceSuite) TestUnicastTooManyClearPending(c *C) {
152 userId, auth := s.associatedAuth("DEV2")
153 // send too many unicasts that will be pending
154 payloadFmt := `{"serial":%d}`
155 for i := 0; i < MaxNotificationsPerApplication; i++ {
156 got, err := s.PostRequest("/notify", &api.Unicast{
157 UserId: userId,
158 DeviceId: "DEV2",
159 AppId: "app1",
160 ExpireOn: future,
161 Data: json.RawMessage(fmt.Sprintf(payloadFmt, i)),
162 })
163 c.Assert(err, IsNil, Commentf("%v", got))
164 }
165
166 got, err := s.PostRequest("/notify", &api.Unicast{
167 UserId: userId,
168 DeviceId: "DEV2",
169 AppId: "app1",
170 ExpireOn: future,
171 Data: json.RawMessage(fmt.Sprintf(payloadFmt, MaxNotificationsPerApplication)),
172 })
173 c.Assert(err, Equals, kit.ErrNOk, Commentf("%v", got))
174 errorStr, _ := got["error"].(string)
175 c.Assert(errorStr, Equals, "too-many-pending")
176
177 // clear all pending
178 got, err = s.PostRequest("/notify", &api.Unicast{
179 UserId: userId,
180 DeviceId: "DEV2",
181 AppId: "app1",
182 ExpireOn: future,
183 Data: json.RawMessage(fmt.Sprintf(payloadFmt, 1000)),
184 ClearPending: true,
185 })
186 c.Assert(err, IsNil, Commentf("%v", got))
187
188 events, errCh, stop := s.StartClientAuth(c, "DEV2", nil, auth, "")
189 // getting the 1 pending on connect
190 c.Check(NextEvent(events, errCh), Equals, `unicast app:app1 payload:{"serial":1000};`)
191 stop()
192 c.Assert(NextEvent(s.ServerEvents, nil), Matches, `.* ended with:.*EOF`)
193 c.Check(len(errCh), Equals, 0)
194 }
195
196 func (s *UnicastAcceptanceSuite) TestUnicastReplaceTag(c *C) {
197 userId, auth := s.associatedAuth("DEV2")
198
199 // send with replace_tag
200 got, err := s.PostRequest("/notify", &api.Unicast{
201 UserId: userId,
202 DeviceId: "DEV2",
203 AppId: "app1",
204 ExpireOn: future,
205 Data: json.RawMessage(`{"m": 1}`),
206 ReplaceTag: "tagFoo",
207 })
208 c.Assert(err, IsNil, Commentf("%v", got))
209
210 // replace
211 got, err = s.PostRequest("/notify", &api.Unicast{
212 UserId: userId,
213 DeviceId: "DEV2",
214 AppId: "app1",
215 ExpireOn: future,
216 Data: json.RawMessage(`{"m": 2}`),
217 ReplaceTag: "tagFoo",
218 })
219 c.Assert(err, IsNil, Commentf("%v", got))
220
221 events, errCh, stop := s.StartClientAuth(c, "DEV2", nil, auth, "")
222 // getting the 1 pending on connect
223 c.Check(NextEvent(events, errCh), Equals, `unicast app:app1 payload:{"m":2};`)
224 stop()
225 c.Assert(NextEvent(s.ServerEvents, nil), Matches, `.* ended with:.*EOF`)
226 c.Check(len(errCh), Equals, 0)
227 }