def subtest_receptionOfSUBSTwoRequestsOneAvailable(self):
        """
        Asking for two subtitles while the recipent of the request has only one.
        The response should contain only the one available subtitle content,
        plus a bitmask that reflects the contents of the response.
        """

        print >> sys.stderr, "test: test_subtitles_msgs_2_1 -----------------------"
        ol_conn = OLConnection(self.my_keypair, 'localhost', self.hisport)

        bitmask = LanguagesProvider.getLanguagesInstance().langCodesToMask(
            ['nld', 'eng'])
        binmask = utilities.uintToBinaryString(bitmask, length=4)

        request = GET_SUBS + \
                      bencode((
                              self.anotherpermid,
                              self.testInfohash,
                              binmask
                              ))

        subshandler = SubtitlesHandler()
        subshandler.register(ol_conn, self.richMetadata_db, self.session)

        ol_conn.send(request)
        subs_data = ol_conn.recv()
        self.assertEquals(SUBS, subs_data[0])
        data = bdecode(subs_data[1:])
        print >> sys.stderr, "test: subtitles_messages : received SUBS repsonse: ", data

        #check on the format of the response
        self.assertTrue(isinstance(data, list))
        self.assertEquals(4, len(data))  # for fields
        self.assertEquals(self.mdto.channel, data[0])
        self.assertEquals(self.mdto.infohash, data[1])

        #the receiver had only one of the two requested subtitles
        # so I expect a different bitmask
        bitmask = LanguagesProvider.getLanguagesInstance().langCodesToMask(
            ['nld'])
        expectedBinarymask = utilities.uintToBinaryString(bitmask, length=4)

        self.assertEquals(expectedBinarymask, data[2])
        self.assertTrue(isinstance(data[3], list))
        self.assertEquals(1, len(data[3]))
        with codecs.open(self.sub1, "rb", "utf-8") as sub:
            expectedContents = sub.read()
        self.assertEquals(expectedContents, data[3][0])

        ol_conn.close()
        print >> sys.stderr, "test: subtitles_messages: received content is valid."
        print >> sys.stderr, "End of test_subtitles_msgs_2_1 test --------------------"
 def subtest_receptionOfSUBSTwoRequestsOneAvailable(self):
     """
     Asking for two subtitles while the recipent of the request has only one.
     The response should contain only the one available subtitle content,
     plus a bitmask that reflects the contents of the response.
     """
     
     print >> sys.stderr, "test: test_subtitles_msgs_2_1 -----------------------"
     ol_conn = OLConnection(self.my_keypair,'localhost',self.hisport)
     
     bitmask = LanguagesProvider.getLanguagesInstance().langCodesToMask(['nld','eng'])
     binmask = utilities.uintToBinaryString(bitmask, length=4)
     
     request = GET_SUBS + \
                   bencode((
                           self.anotherpermid,
                           self.testInfohash,
                           binmask
                           ))
                   
     subshandler = SubtitlesHandler()
     subshandler.register(ol_conn, self.richMetadata_db, self.session)
     
     ol_conn.send(request)
     subs_data = ol_conn.recv()
     self.assertEquals(SUBS, subs_data[0])
     data = bdecode(subs_data[1:])
     print >> sys.stderr, "test: subtitles_messages : received SUBS repsonse: ", data
     
     #check on the format of the response
     self.assertTrue(isinstance(data,list))
     self.assertEquals(4, len(data)) # for fields
     self.assertEquals(self.mdto.channel,data[0])
     self.assertEquals(self.mdto.infohash, data[1])
     
     #the receiver had only one of the two requested subtitles
     # so I expect a different bitmask
     bitmask = LanguagesProvider.getLanguagesInstance().langCodesToMask(['nld'])
     expectedBinarymask = utilities.uintToBinaryString(bitmask, length=4)
     
     self.assertEquals(expectedBinarymask, data[2])
     self.assertTrue(isinstance(data[3],list))
     self.assertEquals(1, len(data[3]))
     with codecs.open(self.sub1, "rb", "utf-8") as sub:
         expectedContents = sub.read()
     self.assertEquals(expectedContents, data[3][0])
     
     ol_conn.close()
     print >> sys.stderr, "test: subtitles_messages: received content is valid."
     print >> sys.stderr, "End of test_subtitles_msgs_2_1 test --------------------"
    def subtest_invalidRequest3(self):
        """
        Trying to send an invalid message (valid for everythin except that there is one field more)
        The connection should be closed by the receiver
        """
        print >> sys.stderr, "test: test_subtitles_msgs_invalid_request_3 ------------------"
        ol_conn = OLConnection(self.my_keypair, 'localhost', self.hisport)

        bitmask = LanguagesProvider.getLanguagesInstance().langCodesToMask(
            ['nld', 'eng'])
        binmask = utilities.uintToBinaryString(bitmask, length=4)

        request = GET_SUBS + \
                      bencode((
                              self.anotherpermid,
                              self.testInfohash,
                              binmask,
                              42
                              ))

        ol_conn.send(request)
        self.assertEquals(0, len(ol_conn.recv()))
        print >> sys.stderr, "test: test_subtitles_msgs_invalid_request_3: connection closed as expected"

        ol_conn.close()
        print >> sys.stderr, "End of test_subtitles_msgs_invalid_request_3 ------------------"
    def subtest_invalidRequest3(self):
        """
        Trying to send an invalid message (valid for everythin except that there is one field more)
        The connection should be closed by the receiver
        """
        print >> sys.stderr, "test: test_subtitles_msgs_invalid_request_3 ------------------"
        ol_conn = OLConnection(self.my_keypair,'localhost',self.hisport)

        bitmask = LanguagesProvider.getLanguagesInstance().langCodesToMask(['nld','eng'])
        binmask = utilities.uintToBinaryString(bitmask, length=4)
        
        request = GET_SUBS + \
                      bencode((
                              self.anotherpermid,
                              self.testInfohash,
                              binmask,
                              42
                              ))
        
        ol_conn.send(request)
        self.assertEquals(0, len(ol_conn.recv()))
        print >> sys.stderr, "test: test_subtitles_msgs_invalid_request_3: connection closed as expected"
        
        ol_conn.close()
        print >> sys.stderr, "End of test_subtitles_msgs_invalid_request_3 ------------------"
    def subtest_receptionOfSUBS(self):
        '''
        Asking for the single available subtitle. The response should be
        a valid SUBS message containing its contents
        '''

        print >> sys.stderr, "test: test_subtitles_msgs_1_1 -----------------------"
        ol_conn = OLConnection(self.my_keypair, 'localhost', self.hisport)

        bitmask = LanguagesProvider.getLanguagesInstance().langCodesToMask(
            ['nld'])
        binmask = utilities.uintToBinaryString(bitmask, length=4)

        request = GET_SUBS + \
                      bencode((
                              self.anotherpermid,
                              self.testInfohash,
                              binmask
                              ))

        subshandler = SubtitlesHandler()
        subshandler.register(ol_conn, self.richMetadata_db, self.session)

        ol_conn.send(request)
        subs_data = ol_conn.recv()
        print >> sys.stderr, "test: subtitles_messages : received SUBS response: len", len(
            subs_data)
        self.assertEquals(SUBS, subs_data[0])
        data = bdecode(subs_data[1:])
        print >> sys.stderr, "test: subtitles_messages : received SUBS response: ", data

        #check on the format of the response
        self.assertTrue(isinstance(data, list))
        self.assertEquals(4, len(data))  # for fields
        self.assertEquals(self.mdto.channel, data[0])
        self.assertEquals(self.mdto.infohash, data[1])
        self.assertEquals(binmask, data[2])
        self.assertTrue(isinstance(data[3], list))
        self.assertEquals(1, len(data[3]))
        with codecs.open(self.sub1, "rb", "utf-8") as sub:
            expectedContents = sub.read()
        self.assertEquals(expectedContents, data[3][0])

        ol_conn.close()

        print >> sys.stderr, "test: subtitles_messages: received content is valid."
        print >> sys.stderr, "End of test_subtitles_msgs_1_1 test --------------------"
 def subtest_receptionOfSUBS(self):
     '''
     Asking for the single available subtitle. The response should be 
     a valid SUBS message containing its contents
     '''
     
     print >> sys.stderr, "test: test_subtitles_msgs_1_1 -----------------------"
     ol_conn = OLConnection(self.my_keypair,'localhost',self.hisport)
     
     bitmask = LanguagesProvider.getLanguagesInstance().langCodesToMask(['nld'])
     binmask = utilities.uintToBinaryString(bitmask, length=4)
     
     request = GET_SUBS + \
                   bencode((
                           self.anotherpermid,
                           self.testInfohash,
                           binmask
                           ))
                   
     subshandler = SubtitlesHandler()
     subshandler.register(ol_conn, self.richMetadata_db, self.session)
     
     ol_conn.send(request)
     subs_data = ol_conn.recv()
     print >> sys.stderr, "test: subtitles_messages : received SUBS response: len",len(subs_data)
     self.assertEquals(SUBS, subs_data[0])
     data = bdecode(subs_data[1:])
     print >> sys.stderr, "test: subtitles_messages : received SUBS response: ", data
     
     #check on the format of the response
     self.assertTrue(isinstance(data,list))
     self.assertEquals(4, len(data)) # for fields
     self.assertEquals(self.mdto.channel,data[0])
     self.assertEquals(self.mdto.infohash, data[1])
     self.assertEquals(binmask, data[2])
     self.assertTrue(isinstance(data[3],list))
     self.assertEquals(1, len(data[3]))
     with codecs.open(self.sub1, "rb", "utf-8") as sub:
         expectedContents = sub.read()
     self.assertEquals(expectedContents, data[3][0])
     
     ol_conn.close()
     
     print >> sys.stderr, "test: subtitles_messages: received content is valid."
     print >> sys.stderr, "End of test_subtitles_msgs_1_1 test --------------------"