File: test_httpserver.py

package info (click to toggle)
pocsuite3 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,996 kB
  • sloc: python: 16,816; asm: 911; java: 66; makefile: 30
file content (109 lines) | stat: -rw-r--r-- 3,752 bytes parent folder | download | duplicates (3)
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
#!usr/bin/env python
# -*- coding:utf-8 -*-
"""
@author: longofo
@file: test_httpserver.py
@time: 2019/03/31
"""
import unittest
import warnings

import requests
from urllib3.exceptions import InsecureRequestWarning

from pocsuite3.lib.core.common import get_host_ip, get_host_ipv6
from pocsuite3.lib.core.data import logger
from pocsuite3.modules.httpserver import PHTTPServer, BaseRequestHandler


class TestCase(unittest.TestCase):
    def setUp(self):
        warnings.simplefilter("ignore", ResourceWarning)
        warnings.simplefilter("ignore", InsecureRequestWarning)

    def tearDown(self):
        pass

    # @unittest.skip(reason='test')
    def test_only_start_server_once(self):
        logger.info("Test http server is only start once")
        PHTTPServer._instance = None
        httpd1 = PHTTPServer()
        httpd2 = PHTTPServer()
        httpd1.start()
        httpd2.start()
        httpd1.stop()
        httpd2.stop()

    def test_singleton(self):
        logger.info("Test http server is singleton")
        PHTTPServer._instance = None
        httpd1 = PHTTPServer()
        httpd2 = PHTTPServer()
        self.assertEqual(id(httpd1), id(httpd2))

    def test_ipv4(self):
        try:
            logger.info('Test http server in ipv4')
            PHTTPServer._instance = None
            httpd = PHTTPServer(bind_ip='0.0.0.0', bind_port=6666, requestHandler=BaseRequestHandler)
            httpd.start()
            url = '{}://{}:{}/'.format('http', get_host_ip(), 6666)
            resp = requests.get(url)
            self.assertEqual(resp.status_code, 200)
        except Exception:
            assert False
        finally:
            httpd.stop()

    @unittest.skip(reason='disable ipv6')
    def test_ipv6(self):
        try:
            logger.info('Test http server in ipv6')
            PHTTPServer._instance = None
            httpd = PHTTPServer(bind_ip='::', bind_port=6666, requestHandler=BaseRequestHandler)
            httpd.start()
            url = '{}://{}:{}/'.format('http', '[{}]'.format(get_host_ipv6()), 6666)
            resp = requests.get(url)
            self.assertEqual(resp.status_code, 200)
        except Exception:
            pass
        finally:
            httpd.stop()

    def test_ipv4_https(self):
        try:
            logger.info('Test https server in ipv4')
            PHTTPServer._instance = None
            httpd = PHTTPServer(bind_ip='0.0.0.0', bind_port=6666, use_https=True,
                                requestHandler=BaseRequestHandler)
            httpd.start()
            url = '{}://{}:{}/'.format('https', get_host_ip(), 6666)
            requests.get(url)
        except requests.exceptions.SSLError:
            url = '{}://{}:{}/'.format('https', get_host_ip(), 6666)
            resp = requests.get(url, verify=False)
            self.assertEqual(resp.status_code, 200)
        except Exception:
            assert False
        finally:
            httpd.stop()

    @unittest.skip(reason='disable ipv6')
    def test_ipv6_https(self):
        try:
            logger.info('Test https server in ipv6')
            PHTTPServer._instance = None
            httpd = PHTTPServer(bind_ip='::', bind_port=6666, use_https=True,
                                requestHandler=BaseRequestHandler)
            httpd.start()
            url = '{}://{}:{}/'.format('https', '[{}]'.format(get_host_ipv6()), 6666)
            requests.get(url)
        except requests.exceptions.SSLError:
            url = '{}://{}:{}/'.format('https', '[{}]'.format(get_host_ipv6()), 6666)
            resp = requests.get(url, verify=False)
            self.assertEqual(resp.status_code, 200)
        except Exception:
            pass
        finally:
            httpd.stop()