<s> from __future__ import unicode_literals <EOL> import sys <EOL> from winsys . _compat import unittest <EOL> import uuid <EOL> import winerror <EOL> import win32api <EOL> import win32con <EOL> import win32evtlog <EOL> import win32security <EOL> import pywintypes <EOL> from winsys . tests import utils as testutils <EOL> from winsys import event_logs , registry , utils <EOL> LOG_NAME = event_logs . DEFAULT_LOG_NAME <EOL> GUID = "<STR_LIT>" % uuid . uuid1 ( ) <EOL> def yield_logs ( computer = None , log_name = LOG_NAME ) : <EOL> hLog = win32evtlog . OpenEventLog ( computer , log_name ) <EOL> try : <EOL> while True : <EOL> entries = win32evtlog . ReadEventLog ( <EOL> hLog , <EOL> win32evtlog . EVENTLOG_BACKWARDS_READ | win32evtlog . EVENTLOG_SEQUENTIAL_READ , <EOL> <NUM_LIT:0> <EOL> ) <EOL> if entries : <EOL> for entry in entries : <EOL> yield entry <EOL> else : <EOL> break <EOL> finally : <EOL> win32evtlog . CloseEventLog ( hLog ) <EOL> @ unittest . skipUnless ( testutils . i_am_admin ( ) , "<STR_LIT>" ) <EOL> class TestEventLogs ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> event_logs . EventSource . create ( GUID , LOG_NAME ) <EOL> self . registry_root = registry . registry ( r"<STR_LIT>" ) <EOL> def tearDown ( self ) : <EOL> event_logs . event_source ( r"<STR_LIT>" % ( LOG_NAME , GUID ) ) . delete ( ) <EOL> def test_create_source ( self ) : <EOL> log_name = "<STR_LIT>" <EOL> guid = "<STR_LIT>" % uuid . uuid1 ( ) <EOL> try : <EOL> source = event_logs . EventSource . create ( guid , log_name ) <EOL> self . assertTrue ( self . registry_root + log_name + guid ) <EOL> except : <EOL> raise <EOL> else : <EOL> source . delete ( ) <EOL> self . assertFalse ( bool ( self . registry_root + log_name + guid ) ) <EOL> def test_create_source_at_default ( self ) : <EOL> guid = "<STR_LIT>" % uuid . uuid1 ( ) <EOL> try : <EOL> source = event_logs . EventSource . create ( guid ) <EOL> self . assertTrue ( self . registry_root + event_logs . DEFAULT_LOG_NAME + guid ) <EOL> except : <EOL> raise <EOL> else : <EOL> source . delete ( ) <EOL> self . assertFalse ( bool ( self . registry_root + event_logs . DEFAULT_LOG_NAME + guid ) ) <EOL> def test_event_sources ( self ) : <EOL> log_name = "<STR_LIT>" <EOL> self . assertEqual ( <EOL> set ( s . name for s in event_logs . event_sources ( log_name ) ) , <EOL> set ( r . name for r in self . registry_root + log_name ) <EOL> ) <EOL> self . assertTrue ( all ( isinstance ( s , event_logs . EventSource ) for s in event_logs . event_sources ( log_name ) ) ) <EOL> def test_event_source_from_event_source ( self ) : <EOL> for s in event_logs . event_sources ( ) : <EOL> self . assertTrue ( isinstance ( s , event_logs . EventSource ) ) <EOL> self . assertTrue ( event_logs . event_source ( s ) is s ) <EOL> break <EOL> def test_event_source_from_none ( self ) : <EOL> self . assertTrue ( event_logs . event_source ( None ) is None ) <EOL> def test_event_source_from_bad_string ( self ) : <EOL> with self . assertRaises ( event_logs . x_event_logs ) : <EOL> event_logs . event_source ( "<STR_LIT>" ) <EOL> def test_event_source_from_good_string ( self ) : <EOL> self . assertTrue ( <EOL> isinstance ( <EOL> event_logs . event_source ( r"<STR_LIT>" % ( LOG_NAME , GUID ) ) , <EOL> event_logs . EventSource <EOL> ) <EOL> ) <EOL> def test_event_source_from_good_string_default_log ( self ) : <EOL> self . assertTrue ( <EOL> isinstance ( <EOL> event_logs . event_source ( GUID ) , <EOL> event_logs . EventSource <EOL> ) <EOL> ) <EOL> def test_event_source_as_string ( self ) : <EOL> self . assertTrue ( event_logs . event_source ( GUID ) . as_string ( ) ) <EOL> def test_event_source_log_event ( self ) : <EOL> data = str ( GUID ) . encode ( "<STR_LIT:utf8>" ) <EOL> event_logs . event_source ( GUID ) . log_event ( data = data ) <EOL> for event in yield_logs ( ) : <EOL> if event . SourceName == GUID and event . Data == data : <EOL> self . assertTrue ( True ) <EOL> break <EOL> else : <EOL> self . assertTrue ( False ) <EOL> def test_event_logs ( self ) : <EOL> self . assertEqual ( <EOL> set ( s . name for s in event_logs . event_logs ( ) ) , <EOL> set ( r . name for r in self . registry_root . keys ( ) ) <EOL> ) <EOL> self . assertTrue ( all ( isinstance ( s , event_logs . EventLog ) for s in event_logs . event_logs ( ) ) ) <EOL> def test_event_log_from_event_log ( self ) : <EOL> for l in event_logs . event_logs ( ) : <EOL> self . assertTrue ( isinstance ( l , event_logs . EventLog ) ) <EOL> self . assertTrue ( event_logs . event_log ( l ) is l ) <EOL> break <EOL> def test_event_log_from_none ( self ) : <EOL> self . assertTrue ( event_logs . event_log ( None ) is None ) <EOL> def test_event_log_from_bad_string ( self ) : <EOL> with self . assertRaises ( event_logs . x_event_logs ) : <EOL> event_logs . event_log ( "<STR_LIT>" ) <EOL> def test_event_log_from_good_string ( self ) : <EOL> self . assertTrue ( <EOL> isinstance ( <EOL> event_logs . event_log ( LOG_NAME ) , <EOL> event_logs . EventLog <EOL> ) <EOL> ) <EOL> def test_event_log_clear_no_save ( self ) : <EOL> log_name = "<STR_LIT>" <EOL> source_name = "<STR_LIT>" % uuid . uuid1 ( ) <EOL> source = event_logs . EventSource . create ( source_name , log_name ) <EOL> log = event_logs . event_log ( log_name ) <EOL> hLog = win32evtlog . OpenEventLog ( None , log_name ) <EOL> try : <EOL> log . log_event ( source , message = "<STR_LIT:hello>" ) <EOL> self . assertNotEquals ( win32evtlog . GetNumberOfEventLogRecords ( hLog ) , <NUM_LIT:0> ) <EOL> log . clear ( ) <EOL> self . assertEqual ( win32evtlog . GetNumberOfEventLogRecords ( hLog ) , <NUM_LIT:0> ) <EOL> finally : <EOL> win32evtlog . CloseEventLog ( hLog ) <EOL> source . delete ( ) <EOL> def test_event_log_clear_with_save ( self ) : <EOL> log_name = "<STR_LIT>" <EOL> source_name = "<STR_LIT>" % uuid . uuid1 ( ) <EOL> source = event_logs . EventSource . create ( source_name , log_name ) <EOL> log = event_logs . event_log ( log_name ) <EOL> hLog = win32evtlog . OpenEventLog ( None , log_name ) <EOL> try : <EOL> log . log_event ( source , message = "<STR_LIT:hello>" ) <EOL> self . assertNotEquals ( win32evtlog . GetNumberOfEventLogRecords ( hLog ) , <NUM_LIT:0> ) <EOL> log . clear ( ) <EOL> self . assertEqual ( win32evtlog . GetNumberOfEventLogRecords ( hLog ) , <NUM_LIT:0> ) <EOL> finally : <EOL> win32evtlog . CloseEventLog ( hLog ) <EOL> source . delete ( ) <EOL> def test_log_event ( self ) : <EOL> data = str ( GUID ) . encode ( "<STR_LIT:utf8>" ) <EOL> event_logs . log_event ( "<STR_LIT>" % ( LOG_NAME , GUID ) , data = data ) <EOL> for event in yield_logs ( ) : <EOL> if event . SourceName == GUID and event . Data == data : <EOL> self . assertTrue ( True ) <EOL> break <EOL> else : <EOL> self . assertTrue ( False ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) <EOL> if sys . stdout . isatty ( ) : raw_input ( "<STR_LIT>" ) </s>
