ogfdZdZdZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z dd l mZmZmZmZmZdd lmZdd lmZddl m Z d ZGd deZGdde ZGdde Zy)zSteven Hiscocksz"Copyright (c) 2013 Steven HiscocksGPLN)LogCaptureTestCase)protocol)asyncoreRequestHandlerloop AsyncServerAsyncServerException)Utils)CSocketctd)Ntest unpickle error Exception)argss ?/usr/lib/python3/dist-packages/fail2ban/tests/sockettestcase.py TestMsgErrorr,s&''c eZdZedffdZdZy)TestMsgc||_yNunpickle)selfrs r__init__zTestMsg.__init__/s $-rc|jSrrrs r __reduce__zTestMsg.__reduce__1s rN)__name__ __module__ __qualname__rrr"rrrrr.s*B/rrcleZdZfdZdZedZd dZdZdZ dZ dZ d Z d Z d Zd ZxZS)Socketctj|tt|t ||_t jdd\}}tj|tj|||_ d|_ y)zCall before every test case.z fail2ban.sockz f2b-socketN) rsetUpsuperr'r servertempfilemkstemposcloseremove sock_name serverThread)rsock_fdr1 __class__s rr)z Socket.setUp7sh4 D!$+''F'9((7))I$.$rc|jr*|jj|jt j |y)zCall after every test case.N)r2r+stop_stopServerThreadrtearDownr!s rr8zSocket.tearDownBs5 ;;d#rc|S)z>5"9&;;$l,//%..!5!5x||7O7OPR7STU rc|jtjfdtjj dd|_y)Nchj xsjtjSr)is_alivejoinr DEFAULT_SLEEP_TIME)r2srz*Socket._stopServerThread..Zs/\22442001rr>)r2r rDrFrGrHrr2s @rr7zSocket._stopServerThreadWs:"",..23;<<3K3KB3OQ$rc|j}|jj|j|j |j |jj |j |jj|j tjj|jyr) rJr+r/r7 assertFalserMr6rEr.pathexistsr1rQs rtestStopPerCloseUnexpectedz!Socket.testStopPerCloseUnexpected^s))+,++<((*+++4;;'')*277>>$..12rcZ t|jS#t$r }Yd}~yd}~wwxYwr)rr1r)res r _serverSocketzSocket._serverSocketks) $.. !!  s  **c|j}tj|jd}gd}|j |j |||j |j t ggd|jddd|j |j |||j|j|jjtj|jd}|j |j |dg|jj|j|j|j|j|jj|jt j"j%|j&y) NrAtestr:ERROR: test unpickle errorz!PROTO-error: load message failed:rTallSHUTDOWN)rJr rDrY assertEqualsendr assertLoggedr/r+stop_communicationr6r7rSrMrEr.rTrUr1)rr2client testMessages r testSocketzSocket.testSocketqs]))+, >>$,,a 0&(+6;;{+[96;; }-/KL79NTXY6;;{+[9 ,,.,,.++  " >>$,,a 0&6;;{+j\:++<((*+4;;'')*277>>$..12rcL|j}tj|jddddtj j ggtj} dt_|jtdfd|t_y#|t_wxYw)Nrr\r]r:c"|jSr)r/r!s rrPz0Socket.testSocketConnectBroken..s $**,rzreset by peer|Broken pipecdjtjjdS)Nr>)timeout)rcrFrGrH)rfrgsrrPz0Socket.testSocketConnectBroken..s#FKK X\\-E-Eb-IK Jr) rJr rDrYrCSPROTOENDr found_terminatorassertRaisesRegexr)rr2 org_handlerrfrgs @@rtestSocketConnectBrokenzSocket.testSocketConnectBrokens))+, >>$,,a 0&fi(*:*:*>*>)?@+//+1%>>")%AJL&1>"[>"s #'B B#c|j}tj|jd}gd}|j |j ||t j} dt _|j |j |d|t _|jddd|jj|j|j|jy#|t _wxYw) Nrr[ctSr)rr!s rrPz.Socket.testStopByCommunicate..s,.rr^zUnexpected communication errorrTr_)rJr rDrYrbrcr rordr+r6r7rSrM)rr2rfrgrqs rtestStopByCommunicatezSocket.testStopByCommunicates))+, >>$,,a 0&(+6;;{+[9//+1%@>"FKK ,.JK%0>"46KQUV++<((*+&1>"s '-C== D c\tjddifd} |t_|j}|jt j fdt jjdt_|jddd y#t_wxYw) NcntrrcNdxxdz cc<ddkr td|S)Nrwr2ztest errors in poll) RuntimeError)rerrorg_polls r_produce_errorz-Socket.testLoopErrors.._produce_errors3v;!; &kB , -- D/rcddkDS)Nrwryr)r{srrPz'Socket.testLoopErrors..s#f+*:rr>z1Server connection was closed: test errors in poll0Too many errors - stop logging connection errorsTr_) rpollrJrCr rDrFrGrHrd)rr}r2r{r|s @@rtestLoopErrorszSocket.testLoopErrorss ]](  # !8=**,<??5>>":HLL>$..12r)F)r#r$r%r)r8 staticmethodr;rJr7rVrYrhrrrurr __classcell__)r4s@rr'r'5sP $ 3 3B 1,0A,3rr'ceZdZdZdZy) ClientMisccddifd}fd}t|||jdddd |jd |jd d dd y)NrwrcddkS)Nrw(r)phasesr_activez,ClientMisc.testErrorsInLoop.._actives -" rc@dxxdz cc<tddz)Nrwrz test *%d*r)rrs r_pollz*ClientMisc.testErrorsInLoop.._polls%=A= ;v. //r)use_pollztest *1*z test *10*z test *20*Tr_rz test *21*z test *22*z test *23*)r rdassertNotLogged)rrrrs @rtestErrorsInLoopzClientMisc.testErrorsInLoops_ 1+%0wJ [dCFG{K$Grctj}ttjdt_ t j t j|t_y#|t_wxYw)Nr)sysstdoutrr.devnullrprintFormatted printWiki)r saved_stdouts rtestPrintFormattedAndWikiz$ClientMisc.testPrintFormattedAndWikisK,BJJ$#*  3: 3:s (A)) A6N)r#r$r%rrrrrrrs  H rr) __author__ __copyright__ __license__r.rr,r?timerFutilsrrserver.asyncserverrr r r r server.utilsr client.csocketrrobjectrr'rrrrrsk. 4   %bb $%(fh3 h3V#r