Changeset 946c6da for .tests/test1.py


Ignore:
Timestamp:
2021-03-15T13:45:09Z (4 years ago)
Author:
/ <>
Children:
e98404d
Parents:
484e460
Message:

just for fun

File:
1 edited

Legend:

Unmodified
Added
Removed
  • .tests/test1.py

    r484e460 r946c6da  
    1 import socket, sys, time, select
     1import socket
     2import sys
     3import time
     4import select
    25
    3 class ircClient:
    4     def __init__(self, nick, pw):
     6MESSAGETEST = True
     7BLOCKTEST = False
     8OFFLINETEST = False
     9RENAMETEST = True
     10
     11FAILED = False
     12
     13class IrcClient:
     14    def __init__(self, nick, pwd):
    515        self.nick = nick
    6         self.pw = pw
     16        self.pwd = pwd
    717        self.log = ''
    818        self.sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    919
    10     def sendRaw(self, msg, loud = True):
     20    def send_raw(self, msg, loud = True):
    1121        self.receive()
    1222        if loud:
     
    1525        self.sck.send((msg+'\r\n').encode())
    1626
    17     def sendPrivMsg(self, recip, msg, loud = True):
    18         self.sendRaw('PRIVMSG '+recip+' :'+msg, loud)
     27    def send_priv_msg(self, recip, msg, loud = True):
     28        self.send_raw('PRIVMSG '+recip+' :'+msg, loud)
    1929
    2030    def connect(self):
     
    2737        print("IRC connection established for " + self.nick)
    2838
    29         self.sendRaw('USER ' + (self.nick + " ")*3)
    30         self.sendRaw('NICK ' + self.nick)
    31         self.sendRaw('JOIN &bitlbee')
     39        self.send_raw('USER ' + (self.nick + " ")*3)
     40        self.send_raw('NICK ' + self.nick)
     41        self.send_raw('JOIN &bitlbee')
    3242
    33     def jabberLogin(self):
    34         self.sendPrivMsg("&bitlbee", "account add jabber "+self.nick+"@localhost "+self.pw)
     43    def jabber_login(self):
     44        self.send_priv_msg("&bitlbee", "account add jabber "+self.nick+"@localhost "+self.pwd)
    3545        time.sleep(0.3)
    36         self.sendPrivMsg("&bitlbee", "account on")
     46        self.send_priv_msg("&bitlbee", "account on")
    3747        time.sleep(1)
    3848        self.receive()
     
    5161                for line in text.split('\n'):
    5262                    if line.find('PING') != -1:
    53                         self.sendRaw('PONG ' + line.split()[1])
     63                        self.send_raw('PONG ' + line.split()[1])
    5464            else:
    5565                break
     
    5767        return text
    5868
    59     def addJabberBuddy(self, nick):
    60         self.sendPrivMsg("&bitlbee", "add 0 " + nick+"@localhost")
     69    def add_jabber_buddy(self, nick):
     70        self.send_priv_msg("&bitlbee", "add 0 " + nick+"@localhost")
    6171   
    62     def blockJabberBuddy(self, nick):
    63         self.sendPrivMsg("&bitlbee", "block " + nick)
     72    def block_jabber_buddy(self, nick):
     73        self.send_priv_msg("&bitlbee", "block " + nick)
    6474
    65     def unblockJabberBuddy(self, nick):
    66         self.sendPrivMsg("&bitlbee", "allow " + nick)
     75    def unblock_jabber_buddy(self, nick):
     76        self.send_priv_msg("&bitlbee", "allow " + nick)
    6777
    68     def renameJabberBuddy(self, oldnick, newnick):
    69         self.sendPrivMsg("&bitlbee", "rename " + oldnick + " " + newnick)
     78    def rename_jabber_buddy(self, oldnick, newnick):
     79        self.send_priv_msg("&bitlbee", "rename " + oldnick + " " + newnick)
    7080       
    71 def testSendMessage(sender, receiver, message, shouldreceive = True):
    72     sender.sendPrivMsg(receiver.nick, message)
     81def test_send_message(sender, receiver, message):
     82    sender.send_priv_msg(receiver.nick, message)
    7383    received = receiver.receive().find(message) != -1
    74     if shouldreceive ^ received:
    75         print('Test failed: Message from ' + sender.nick + ' to ' + receiver.nick)
    76         print('Sender Log:\n' + sender.log)
    77         print('Receiver Log:\n' + receiver.log)
    78         sys.exit(1)
     84    return received;
    7985
    80 def runTests():
     86def run_tests():
     87    global FAILED
    8188    clis = []
    82     clis += [ircClient('test1', 'asd')]
    83     clis += [ircClient('test2', 'asd')]
     89    clis += [IrcClient('test1', 'asd')]
     90    clis += [IrcClient('test2', 'asd')]
    8491    for cli in clis:
    8592        cli.connect()
    86         cli.jabberLogin()
     93        cli.jabber_login()
    8794
    88     clis[0].addJabberBuddy(clis[1].nick)
     95    clis[0].add_jabber_buddy(clis[1].nick)
    8996
    90     print("Test: Send message")
    91     testSendMessage(clis[0], clis[1], 'ohai <3')
    92     testSendMessage(clis[1], clis[0], 'uwu *pounces*')
    93     print("Test passed")
     97    if MESSAGETEST:
     98        print("Test: Send message")
     99        ret = test_send_message(clis[0], clis[1], 'ohai <3')
     100        ret = ret & test_send_message(clis[1], clis[0], 'uwu *pounces*')
     101        if ret:
     102            print("Test passed")
     103        else:
     104            print("Test failed")
     105            FAILED = True;
     106           
    94107
    95     print("Test: Block/Unblock")
    96     clis[0].blockJabberBuddy(clis[1].nick)
    97     testSendMessage(clis[1], clis[0], 'm-meow?', shouldreceive = False)
    98     clis[0].unblockJabberBuddy(clis[1].nick)
    99     testSendMessage(clis[1], clis[0], '*purrs*')
    100     print("Test passed")
     108    if BLOCKTEST:
     109        print("Test: Block/Unblock")
     110        clis[0].block_jabber_buddy(clis[1].nick)
     111        ret = not test_send_message(clis[1], clis[0], 'm-meow?')
     112        clis[0].unblock_jabber_buddy(clis[1].nick)
     113        ret = ret & test_send_message(clis[1], clis[0], '*purrs*')
     114        if ret:
     115            print("Test passed")
     116        else:
     117            print("Test failed")
     118            FAILED = True;
     119
     120    if RENAMETEST:
     121        print("Test: Rename buddy")
     122        newname = "xXx_pup_LINKENPARK4EVA<3"
     123        message = "rawr meanmz i luv u in dinosaur"
     124
     125        clis[0].rename_jabber_buddy(clis[1].nick, newname)
     126        clis[0].send_priv_msg(newname, message)
     127        ret = clis[1].receive().find(message) != -1
     128
     129        clis[0].rename_jabber_buddy(newname, clis[1].nick)
     130        ret = ret & test_send_message(clis[0], clis[1], "rawr")
     131        if ret:
     132            print("Test passed")
     133        else:
     134            print("Test failed")
     135            FAILED = True;
    101136   
    102137if __name__ == "__main__":
    103     runTests()
     138    run_tests()
     139    if FAILED:
     140        sys.exit(1)
Note: See TracChangeset for help on using the changeset viewer.