# coding: utf-8
import sys
import os
import ssl
import mock
myPath = os.path.abspath(os.getcwd())
sys.path.insert(0, myPath)
import hubblestack.extmods.fdg.ssl_certificate


def test_load_certificate_exception():
    host = 'google.com'
    port = 443
    ssl.get_server_certificate = mock.Mock(side_effect=Exception('Test Exception'))
    val = hubblestack.extmods.fdg.ssl_certificate._load_certificate(host, port, 3)
    assert val.get('result') == False

def test_load_certificate():
    host = 'google.com'
    port = 443
    cert_details = {'pem_cert': '---BEGIN CERTIFICATE---- ---END CERTIFICATE----'}
    ssl.get_server_certificate = mock.Mock(return_value=cert_details)
    val = hubblestack.extmods.fdg.ssl_certificate._load_certificate(host, port, 3)
    assert val.get('result') == True

def test_parse_cert_positive():
    pem_file = get_pem_file()
    cert = {'data':pem_file}
    host = 'google.com'
    port = 443
    val = hubblestack.extmods.fdg.ssl_certificate._parse_cert(cert, host, port)
    assert ('error' not in val.keys())

def test_parse_cert_negative():
    cert = {'data':''}
    host = 'google.com'
    port = 443
    val = hubblestack.extmods.fdg.ssl_certificate._parse_cert(cert, host, port)
    assert ('error' in val.keys())

def test_ssl_certificate_positive():
    params = {'params': {'host_ip':'google.com', 'host_port':443}}
    cert_details = {'pem_cert':'---BEGIN CERTIFICATE---- ---END CERTIFICATE----'}
    cert = {'result':True,'data':cert_details}
    hubblestack.extmods.fdg.ssl_certificate._load_certificate = mock.Mock(return_value=cert)
    hubblestack.extmods.fdg.ssl_certificate._parse_cert = mock.Mock(return_value=cert_details)
    val = hubblestack.extmods.fdg.ssl_certificate.get_cert_details(params)
    assert val[0] == True
    assert val[1].get('pem_cert') != None

def test_ssl_certificate_negative():
    params = {'params': {'host_ip': '127.0.0.1', 'host_port': 443}}
    cert = {'result':False,'data':'cert not found'}
    hubblestack.extmods.fdg.ssl_certificate._load_certificate = mock.Mock(return_value=cert)
    val = hubblestack.extmods.fdg.ssl_certificate.get_cert_details(params)
    assert val[0] == True
    assert val[1].get('pem_cert') == None

def test_null_value():
    params = {'params': {'host_ip': '', 'host_port': 443}}
    val = hubblestack.extmods.fdg.ssl_certificate.get_cert_details(params)
    assert val[0] == False
    assert val[1] == ''

def test_garbage_value():
    params = {'params': {'host_ip': 'ag#786asf kjas.{\\}', 'host_port': 443}}
    val = hubblestack.extmods.fdg.ssl_certificate.get_cert_details(params)
    assert val[0] == False
    assert val[1] == ''

def test_invalid_input():
    params = {'params': {'host_p': 'google.com', 'host_po': 443}}
    val = hubblestack.extmods.fdg.ssl_certificate.get_cert_details(params)
    assert val[0] == False
    assert val[1] == ''

def test_chained_positive():
    chained = {'host_ip': 'google.com', 'host_port': 443}
    cert_details = {'pem_cert': '---BEGIN CERTIFICATE---- ---END CERTIFICATE----'}
    cert = {'result': True, 'data': cert_details}
    hubblestack.extmods.fdg.ssl_certificate._load_certificate = mock.Mock(return_value=cert)
    hubblestack.extmods.fdg.ssl_certificate._parse_cert = mock.Mock(return_value=cert_details)
    val = hubblestack.extmods.fdg.ssl_certificate.get_cert_details(chained=chained)
    assert val[0] == True
    assert val[1].get('pem_cert') != None

def test_chained_negative():
    chained = {'host_ip': '127.0.0.1', 'host_port': 0}
    cert = {'result': False, 'data': 'cert not found'}
    hubblestack.extmods.fdg.ssl_certificate._load_certificate = mock.Mock(return_value=cert)
    val = hubblestack.extmods.fdg.ssl_certificate.get_cert_details(chained=chained)
    assert val[0] == True
    assert val[1].get('pem_cert') == None

def test_chained_null_value():
    chained = {'host_ip': '', 'host_port': 443}
    val = hubblestack.extmods.fdg.ssl_certificate.get_cert_details(chained=chained)
    assert val[0] == False
    assert val[1] == ''

def test_chained_invalid_input():
    chained = {'host_i': 'google.com', 'host_po': 443}
    val = hubblestack.extmods.fdg.ssl_certificate.get_cert_details(chained=chained)
    assert val[0] == False
    assert val[1] == ''

def test_chained_garbage_value():
    chained = {'host_ip': 'ag#786asf kjas.{\\}', 'host_port': 443}
    val = hubblestack.extmods.fdg.ssl_certificate.get_cert_details(chained=chained)
    assert val[0] == False
    assert val[1] == ''

def test_check_input_validity_positive():
    host = 'google.com'
    port = 443
    val = hubblestack.extmods.fdg.ssl_certificate._check_input_validity(host, port, 3)
    assert val == True

def test_check_input_validity_negative():
    host = ''
    port = -1
    val = hubblestack.extmods.fdg.ssl_certificate._check_input_validity(host, port, 3)
    assert val == False

def test_check_input_validity_negative_spaces():
    host = 'ag#786asf kjas.{\\}'
    port = 443
    val = hubblestack.extmods.fdg.ssl_certificate._check_input_validity(host, port, 3)
    assert val == False

def get_pem_file():
    return '-----BEGIN CERTIFICATE-----\nMIIKDzCCCPegAwIBAgIQQCqYIy3IbBMIAAAAAB2JsTANBgkqhkiG9w0BAQsFADBC\nMQswCQYDVQQGEwJVUzEeMBwGA1UEChMVR29vZ2xlIFRydXN0IFNlcnZpY2VzMRMw\nEQYDVQQDEwpHVFMgQ0EgMU8xMB4XDTE5MTEwNTA3MzgzMloXDTIwMDEyODA3Mzgz\nMlowZjELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcT\nDU1vdW50YWluIFZpZXcxEzARBgNVBAoTCkdvb2dsZSBMTEMxFTATBgNVBAMMDCou\nZ29vZ2xlLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALzpwqd4\njwhnMoy+qZEKCeSGOsISNramQEYoJv0O3y+DQuItbSvOHBDDc8SNkZU7rAydnxxE\nIBq06hbXUoTeUZL+zeAYvZgnq08utlPlOeMyHbOIFRt8PM14WdNzjFwFpJZDuIbW\nOBuicnaV/TVjmq6VcfQrrA5fBgn8UUTzwTyneV7utk+bHb6mhu44KejwvbqpCp0D\nypL6uCpCEN68nid8JdMxUCC0rtmoi1gzmAJmKFIlY38ImdA4rOIGvS72TP7A847W\nsRcbPchEM3Mu2k3IrPVlkpVxUZip8UFLJ30xyMQfzxEwgdwDST7AuY0r6MH1iBrt\n2dy3zu8PxUUGAZcCAwEAAaOCBtswggbXMA4GA1UdDwEB/wQEAwIFoDATBgNVHSUE\nDDAKBggrBgEFBQcDATAMBgNVHRM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