summaryrefslogtreecommitdiff
path: root/tests/unit/vpn/test_management.py
blob: e44f53318a095872cd0cd143d63fe5ee6a19b457 (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
# -*- coding: utf-8 -*-
# test_management.py
# Copyright (C) 2017 LEAP Encryption Access Project
#
# 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/>.

"""
Tests for the VPN Management Interface
"""

import os
import StringIO

from twisted.trial import unittest
from leap.bitmask.vpn.management import ManagementProtocol


def get_data(name):
    return os.path.join(
        os.path.abspath(os.path.split(__file__)[0]), '%s.data' % name)


session1 = open(get_data('session1')).readlines()
session2 = open(get_data('session2')).readlines()


def feed_the_protocol(protocol, data):
    for line in data:
        protocol.lineReceived(line)


class StateListener(object):

    def __init__(self):
        self.states = []

    def changeState(self, state):
        self.states.append(state)

    def getStateHistory(self):
        return self.states


class ManagementTestCase(unittest.TestCase):

    def test_final_state_is_connected(self):
        proto = ManagementProtocol()
        feed_the_protocol(proto, session1)
        assert proto.state.state == 'CONNECTED'
        assert proto.state.simple == 'ON'
        assert proto.remote == '46.165.242.169'

    def test_final_state_stopping(self):
        proto = ManagementProtocol()
        feed_the_protocol(proto, session2)
        assert proto.state.state == 'EXITING'
        assert proto.state.simple == 'STOPPING'

    def test_get_state_history(self):
        proto = ManagementProtocol()
        listener = StateListener()
        proto.addStateListener(listener)
        feed_the_protocol(proto, session1)
        log = listener.getStateHistory()
        states = [st.state for st in log]
        assert len(states) == 4
        assert states == ['AUTH', 'GET_CONFIG', 'ASSIGN_IP', 'CONNECTED']

    def test_state_listener(self):
        proto = ManagementProtocol()
        listener = StateListener()
        proto.addStateListener(listener)
        feed_the_protocol(proto, session1)
        states = [st.state for st in listener.states]
        assert states == ['AUTH', 'GET_CONFIG', 'ASSIGN_IP', 'CONNECTED']

    def test_bytecount(self):
        proto = ManagementProtocol()
        feed_the_protocol(proto, session1)
        assert proto.traffic.down == 17168
        assert proto.traffic.up == 7657

    def test_bytecount_rate(self):
        proto = ManagementProtocol()
        proto.traffic.update(1024, 512, 1)
        proto.traffic.update(2048, 1024, 2)
        print proto.traffic._buf
        assert proto.traffic.down == 2048
        assert proto.traffic.up == 1024
        assert proto.traffic.get_rate() == ['1.0 K', '512.0 B']

    def test_get_pid(self):
        proto = ManagementProtocol()
        proto.transport = StringIO.StringIO()
        assert proto.pid is None
        proto.getPid()
        pid_lines = ['SUCCESS: pid=99999']
        feed_the_protocol(proto, pid_lines)
        assert proto.pid == 99999

    def test_parse_version_string(self):
        proto = ManagementProtocol()
        proto.transport = StringIO.StringIO()
        assert proto.openvpn_version == ''
        feed_the_protocol(proto, session1[2:4])
        proto.getVersion()
        feed_the_protocol(proto, ['END'])
        assert proto.openvpn_version.startswith('OpenVPN 2.4.0')

    def test_get_info(self):
        proto = ManagementProtocol()
        proto.transport = StringIO.StringIO()
        feed_the_protocol(proto, session1)

        feed_the_protocol(proto, session1[2:4])
        proto.getVersion()
        feed_the_protocol(proto, ['END'])
        proto.getPid()
        pid_lines = ['SUCCESS: pid=23783']
        feed_the_protocol(proto, pid_lines)

        info = proto.getInfo()
        assert info['remote'] == '46.165.242.169'
        assert info['rport'] == '443'
        assert info['openvpn_version'].startswith('OpenVPN 2.4.0')
        assert info['pid'] == 23783