<s> import unittest <EOL> import tempfile <EOL> import uuid <EOL> import os <EOL> import mittn . headlessscanner . dbtools as dbtools <EOL> import datetime <EOL> import socket <EOL> import json <EOL> import sqlalchemy <EOL> from sqlalchemy import create_engine , Table , Column , MetaData , exc , types <EOL> class dbtools_test_case ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . context = type ( '<STR_LIT>' , ( object , ) , dict ( ) ) <EOL> self . db_file = os . path . join ( tempfile . gettempdir ( ) , <EOL> '<STR_LIT>' + str ( uuid . uuid4 ( ) ) ) <EOL> self . context . dburl = '<STR_LIT>' + self . db_file <EOL> def test_dburl_not_defined ( self ) : <EOL> empty_context = type ( '<STR_LIT>' , ( object , ) , dict ( ) ) <EOL> dbconn = dbtools . open_database ( empty_context ) <EOL> self . assertEqual ( dbconn , <EOL> None , <EOL> "<STR_LIT>" ) <EOL> def test_create_db_connection ( self ) : <EOL> dbconn = dbtools . open_database ( self . context ) <EOL> self . assertEqual ( type ( dbconn ) , <EOL> sqlalchemy . engine . base . Connection , <EOL> "<STR_LIT>" ) <EOL> def test_add_false_positive ( self ) : <EOL> issue = { '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:host>' : '<STR_LIT>' , <EOL> '<STR_LIT:port>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> dbtools . add_false_positive ( self . context , issue ) <EOL> db_engine = sqlalchemy . create_engine ( self . context . dburl ) <EOL> dbconn = db_engine . connect ( ) <EOL> db_metadata = sqlalchemy . MetaData ( ) <EOL> headlessscanner_issues = Table ( '<STR_LIT>' , <EOL> db_metadata , <EOL> Column ( '<STR_LIT>' , types . Boolean ) , <EOL> Column ( '<STR_LIT>' , types . Integer , primary_key = True , nullable = False ) , <EOL> Column ( '<STR_LIT>' , types . DateTime ( timezone = True ) ) , <EOL> Column ( '<STR_LIT>' , types . Text ) , <EOL> Column ( '<STR_LIT>' , types . Text ) , <EOL> Column ( '<STR_LIT:url>' , types . Text ) , <EOL> Column ( '<STR_LIT>' , types . Text ) , <EOL> Column ( '<STR_LIT>' , types . Text ) , <EOL> Column ( '<STR_LIT>' , types . Text ) , <EOL> Column ( '<STR_LIT>' , types . Text ) , <EOL> Column ( '<STR_LIT>' , types . Text ) , <EOL> Column ( '<STR_LIT:host>' , types . Text ) , <EOL> Column ( '<STR_LIT:port>' , types . Text ) , <EOL> Column ( '<STR_LIT>' , types . Text ) , <EOL> Column ( '<STR_LIT>' , types . LargeBinary ) ) <EOL> db_select = sqlalchemy . sql . select ( [ headlessscanner_issues ] ) <EOL> db_result = dbconn . execute ( db_select ) <EOL> result = db_result . fetchone ( ) <EOL> for key , value in issue . iteritems ( ) : <EOL> if key == '<STR_LIT>' : <EOL> self . assertEqual ( result [ key ] , json . dumps ( value ) ) <EOL> else : <EOL> self . assertEqual ( result [ key ] , value , <EOL> '<STR_LIT>' % key ) <EOL> self . assertEqual ( result [ '<STR_LIT>' ] , socket . gethostbyname ( socket . getfqdn ( ) ) , <EOL> '<STR_LIT>' ) <EOL> self . assertLessEqual ( result [ '<STR_LIT>' ] , datetime . datetime . utcnow ( ) , <EOL> '<STR_LIT>' ) <EOL> dbconn . close ( ) <EOL> def test_number_of_new_false_positives ( self ) : <EOL> issue = { '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : datetime . datetime . utcnow ( ) , <EOL> '<STR_LIT>' : '<STR_LIT:localhost>' , <EOL> '<STR_LIT:url>' : '<STR_LIT:url>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:host>' : '<STR_LIT:host>' , <EOL> '<STR_LIT:port>' : '<STR_LIT:port>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> dbtools . add_false_positive ( self . context , issue ) <EOL> self . assertEqual ( dbtools . number_of_new_in_database ( self . context ) , <EOL> <NUM_LIT:1> , "<STR_LIT>" ) <EOL> dbtools . add_false_positive ( self . context , issue ) <EOL> self . assertEqual ( dbtools . number_of_new_in_database ( self . context ) , <EOL> <NUM_LIT:2> , "<STR_LIT>" ) <EOL> def test_false_positive_detection ( self ) : <EOL> issue = { '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : datetime . datetime . utcnow ( ) , <EOL> '<STR_LIT>' : '<STR_LIT:localhost>' , <EOL> '<STR_LIT:url>' : '<STR_LIT:url>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:host>' : '<STR_LIT:host>' , <EOL> '<STR_LIT:port>' : '<STR_LIT:port>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> dbtools . add_false_positive ( self . context , issue ) <EOL> self . assertEqual ( dbtools . known_false_positive ( self . context , <EOL> issue ) , <EOL> True , "<STR_LIT>" ) <EOL> issue [ '<STR_LIT>' ] = '<STR_LIT:2>' <EOL> self . assertEqual ( dbtools . known_false_positive ( self . context , <EOL> issue ) , <EOL> False , "<STR_LIT>" ) <EOL> dbtools . add_false_positive ( self . context , issue ) <EOL> issue [ '<STR_LIT:url>' ] = '<STR_LIT>' <EOL> self . assertEqual ( dbtools . known_false_positive ( self . context , <EOL> issue ) , <EOL> False , "<STR_LIT>" ) <EOL> dbtools . add_false_positive ( self . context , issue ) <EOL> issue [ '<STR_LIT>' ] = '<STR_LIT:foo>' <EOL> self . assertEqual ( dbtools . known_false_positive ( self . context , <EOL> issue ) , <EOL> False , "<STR_LIT>" ) <EOL> dbtools . add_false_positive ( self . context , issue ) <EOL> self . assertEqual ( dbtools . known_false_positive ( self . context , <EOL> issue ) , <EOL> True , "<STR_LIT>" ) <EOL> def tearDown ( self ) : <EOL> try : <EOL> os . unlink ( self . db_file ) <EOL> except : <EOL> pass </s>
