Debian Codegraph @

server/config_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 "bytes"
21 "io/ioutil"
22 "os"
23 "path/filepath"
24 "time"
25
26 . "gopkg.in/check.v1"
27
28 "gitlab.com/ubports/development/core/lomiri-push-service/config"
29 helpers "gitlab.com/ubports/development/core/lomiri-push-service/testing"
30 )
31
32 type configSuite struct{}
33
34 var _ = Suite(&configSuite{})
35
36 func (s *configSuite) TestDevicesParsedConfig(c *C) {
37 buf := bytes.NewBufferString(`{
38 "ping_interval": "5m",
39 "exchange_timeout": "10s",
40 "session_queue_size": 10,
41 "broker_queue_size": 100,
42 "addr": "127.0.0.1:9999",
43 "key_pem_file": "key.key",
44 "cert_pem_file": "cert.cert"
45 }`)
46 cfg := &DevicesParsedConfig{}
47 err := config.ReadConfig(buf, cfg)
48 c.Assert(err, IsNil)
49 c.Check(cfg.PingInterval(), Equals, 5*time.Minute)
50 c.Check(cfg.ExchangeTimeout(), Equals, 10*time.Second)
51 c.Check(cfg.BrokerQueueSize(), Equals, uint(100))
52 c.Check(cfg.SessionQueueSize(), Equals, uint(10))
53 c.Check(cfg.Addr(), Equals, "127.0.0.1:9999")
54 }
55
56 func (s *configSuite) TestTLSParsedConfigLoadPEMs(c *C) {
57 tmpDir := c.MkDir()
58 cfg := &TLSParsedConfig{
59 ParsedKeyPEMFile: "key.key",
60 ParsedCertPEMFile: "cert.cert",
61 }
62 err := cfg.LoadPEMs(tmpDir)
63 c.Check(err, ErrorMatches, "reading key_pem_file:.*no such file.*")
64 err = ioutil.WriteFile(filepath.Join(tmpDir, "key.key"), helpers.TestKeyPEMBlock, os.ModePerm)
65 c.Assert(err, IsNil)
66 err = cfg.LoadPEMs(tmpDir)
67 c.Check(err, ErrorMatches, "reading cert_pem_file:.*no such file.*")
68 err = ioutil.WriteFile(filepath.Join(tmpDir, "cert.cert"), helpers.TestCertPEMBlock, os.ModePerm)
69 c.Assert(err, IsNil)
70 err = cfg.LoadPEMs(tmpDir)
71 c.Assert(err, IsNil)
72 tlsCfg := cfg.TLSServerConfig()
73 c.Check(tlsCfg.Certificates, HasLen, 1)
74 }