Debian Codegraph @

server/store/store_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 store
18
19 import (
20 // "fmt"
21 "testing"
22
23 . "gopkg.in/check.v1"
24
25 "gitlab.com/ubports/development/core/lomiri-push-service/protocol"
26 )
27
28 func TestStore(t *testing.T) { TestingT(t) }
29
30 type storeSuite struct{}
31
32 var _ = Suite(&storeSuite{})
33
34 func (s *storeSuite) TestInternalChannelIdToHex(c *C) {
35 c.Check(InternalChannelIdToHex(SystemInternalChannelId), Equals, protocol.SystemChannelId)
36 c.Check(InternalChannelIdToHex(InternalChannelId("Bf1c9bf7096084cb2a154979ce00c7f50")), Equals, "f1c9bf7096084cb2a154979ce00c7f50")
37 c.Check(func() { InternalChannelIdToHex(InternalChannelId("U")) }, PanicMatches, "InternalChannelIdToHex is for broadcast channels")
38 }
39
40 func (s *storeSuite) TestHexToInternalChannelId(c *C) {
41 i0, err := HexToInternalChannelId("0")
42 c.Check(err, IsNil)
43 c.Check(i0, Equals, SystemInternalChannelId)
44 i1, err := HexToInternalChannelId("00000000000000000000000000000000")
45 c.Check(err, IsNil)
46 c.Check(i1, Equals, SystemInternalChannelId)
47 c.Check(i1.BroadcastChannel(), Equals, true)
48 i2, err := HexToInternalChannelId("f1c9bf7096084cb2a154979ce00c7f50")
49 c.Check(err, IsNil)
50 c.Check(i2.BroadcastChannel(), Equals, true)
51 c.Check(i2, Equals, InternalChannelId("Bf1c9bf7096084cb2a154979ce00c7f50"))
52 _, err = HexToInternalChannelId("01")
53 c.Check(err, Equals, ErrExpected128BitsHexRepr)
54 _, err = HexToInternalChannelId("abceddddddddddddddddzeeeeeeeeeee")
55 c.Check(err, Equals, ErrExpected128BitsHexRepr)
56 _, err = HexToInternalChannelId("f1c9bf7096084cb2a154979ce00c7f50ff")
57 c.Check(err, Equals, ErrExpected128BitsHexRepr)
58 }
59
60 func (s *storeSuite) TestUnicastInternalChannelId(c *C) {
61 chanId := UnicastInternalChannelId("user1", "dev2")
62 c.Check(chanId.BroadcastChannel(), Equals, false)
63 c.Check(chanId.UnicastChannel(), Equals, true)
64 u, d := chanId.UnicastUserAndDevice()
65 c.Check(u, Equals, "user1")
66 c.Check(d, Equals, "dev2")
67 c.Check(func() { SystemInternalChannelId.UnicastUserAndDevice() }, PanicMatches, "UnicastUserAndDevice is for unicast channels")
68 }
69
70 func (s *storeSuite) TestFilterOutByMsgId(c *C) {
71 orig := []protocol.Notification{
72 protocol.Notification{MsgId: "a"},
73 protocol.Notification{MsgId: "b"},
74 protocol.Notification{MsgId: "c"},
75 protocol.Notification{MsgId: "d"},
76 }
77 // removing the continuous head
78 res := FilterOutByMsgId(orig, orig[:3])
79 c.Check(res, DeepEquals, orig[3:])
80
81 // random removal
82 res = FilterOutByMsgId(orig, orig[1:2])
83 c.Check(res, DeepEquals, []protocol.Notification{
84 protocol.Notification{MsgId: "a"},
85 protocol.Notification{MsgId: "c"},
86 protocol.Notification{MsgId: "d"},
87 })
88
89 // looks like removing the continuous head, but it isn't
90 res = FilterOutByMsgId(orig, []protocol.Notification{
91 protocol.Notification{MsgId: "a"},
92 protocol.Notification{MsgId: "c"},
93 protocol.Notification{MsgId: "d"},
94 })
95 c.Check(res, DeepEquals, []protocol.Notification{
96 protocol.Notification{MsgId: "b"},
97 })
98
99 }