summaryrefslogtreecommitdiff
path: root/tests/integration/bonafide/test_config.py
blob: aeb2c842f8caf9483cf788fe8039418370d151d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# -*- coding: utf-8 -*-
# test_config.py
# Copyright (C) 2015-2017 LEAP
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
import os.path

from BaseHTTPServer import BaseHTTPRequestHandler
from twisted.internet import defer
from twisted.trial import unittest

from leap.bitmask.bonafide.config import Provider
from leap.bitmask.bonafide.errors import NetworkError
from leap.common.testing.basetest import BaseLeapTest
from leap.common.testing.https_server import BaseHTTPSServerTestCase


class ConfigTest(BaseHTTPSServerTestCase, unittest.TestCase, BaseLeapTest):

    def setUp(self):
        self.addr = Addr()
        self.request_handler = request_handler(self.addr)
        BaseHTTPSServerTestCase.setUp(self)
        self.addr.host = 'localhost'
        self.addr.port = self.PORT

    def test_bootstrap_self_sign_cert_fails(self):
        home = os.path.join(self.home, 'self_sign')
        os.mkdir(home)
        provider = Provider(self.addr.domain, autoconf=True, basedir=home)
        d = provider.callWhenMainConfigReady(lambda: "Cert was accepted")
        return self.assertFailure(d, NetworkError)

    def test_bootstrap_pinned_cert(self):
        cacert = os.path.join(os.path.dirname(__file__),
                              "cacert.pem")
        home = os.path.join(self.home, 'pinned')
        os.mkdir(home)
        provider = Provider(self.addr.domain, autoconf=True, basedir=home,
                            cert_path=cacert)

        def check_provider():
            config = provider.config()
            self.assertEqual(config["domain"], self.addr.host)
            self.assertEqual(config["ca_cert_fingerprint"],
                             "SHA256: %s" % fingerprint)

        provider.callWhenMainConfigReady(check_provider)
        return defer.gatherResults([
            provider.first_bootstrap[provider._domain],
            provider.ongoing_bootstrap[provider._domain]])


class Addr(object):
    def __init__(self, host='localhost', port='4443'):
        self.host = host
        self.port = port

    @property
    def domain(self):
        return "%s:%s" % (self.host, self.port)


def request_handler(addr):
    class RequestHandler(BaseHTTPRequestHandler):
        def do_GET(self):
            if self.path == '/provider.json':
                body = provider_json % {
                    'host': addr.host,
                    'port': addr.port,
                    'fingerprint': fingerprint
                }

            elif self.path == '/ca.crt':
                cacert = os.path.join(os.path.dirname(__file__),
                                      "leaptestscert.pem")
                with open(cacert, 'r') as f:
                    castr = f.read()
                body = castr

            elif self.path == '/1/configs.json':
                body = configs_json

            else:
                body = '{"error": "not implemented"}'

            self.send_response(200)
            self.send_header('Content-type', 'applicatino/json')
            self.send_header('Content-Length', str(len(body)))
            self.end_headers()
            self.wfile.write(body)

    return RequestHandler


fingerprint = \
    "cd0131b3352b7a29c307156b24f09fe862b1f5a2e55be7cd888048b91770f220"
provider_json = """
{
  "api_uri": "https://%(host)s:%(port)s",
  "api_version": "1",
  "ca_cert_fingerprint": "SHA256: %(fingerprint)s",
  "ca_cert_uri": "https://%(host)s:%(port)s/ca.crt",
  "default_language": "en",
  "description": {
    "en": "example"
  },
  "domain": "%(host)s",
  "enrollment_policy": "open",
  "languages": [
    "en"
  ],
  "name": {
    "en": "Bitmask"
  },
  "service": {
    "allow_anonymous": false,
    "allow_free": true,
    "allow_limited_bandwidth": false,
    "allow_paid": false,
    "allow_registration": true,
    "allow_unlimited_bandwidth": true,
    "bandwidth_limit": 102401,
    "default_service_level": 1,
    "levels": {
      "1": {
        "description": "hi.",
        "name": "free"
      }
    }
  },
  "services": []
}
"""
configs_json = "{}"