def test_basic(self):
        yield self.manager.setup()
        out = yield self.manager.get_all_streams()
        self.assertEqual(len(out), 0)

        stream_hash = random_lbry_hash()
        file_name = 'file_name'
        key = 'key'
        suggested_file_name = 'sug_file_name'
        blob1 = CryptBlobInfo(random_lbry_hash(), 0, 10, 1)
        blob2 = CryptBlobInfo(random_lbry_hash(), 0, 10, 1)
        blobs = [blob1, blob2]

        # save stream
        yield self.manager.save_stream(stream_hash, file_name, key,
                                       suggested_file_name, blobs)

        out = yield self.manager.get_stream_info(stream_hash)
        self.assertEqual(key, out[0])
        self.assertEqual(file_name, out[1])
        self.assertEqual(suggested_file_name, out[2])

        out = yield self.manager.check_if_stream_exists(stream_hash)
        self.assertTrue(out)

        out = yield self.manager.get_blobs_for_stream(stream_hash)
        self.assertEqual(2, len(out))

        out = yield self.manager.get_all_streams()
        self.assertEqual(1, len(out))

        # add a blob to stream
        blob3 = CryptBlobInfo(random_lbry_hash(), 0, 10, 1)
        blobs = [blob3]
        out = yield self.manager.add_blobs_to_stream(stream_hash, blobs)
        out = yield self.manager.get_blobs_for_stream(stream_hash)
        self.assertEqual(3, len(out))

        out = yield self.manager.get_stream_of_blob(blob3.blob_hash)
        self.assertEqual(stream_hash, out)

        # check non existing stream
        with self.assertRaises(NoSuchStreamHash):
            out = yield self.manager.get_stream_info(random_lbry_hash())

        # check save of sd blob hash
        sd_blob_hash = random_lbry_hash()
        yield self.manager.save_sd_blob_hash_to_stream(stream_hash,
                                                       sd_blob_hash)
        out = yield self.manager.get_sd_blob_hashes_for_stream(stream_hash)
        self.assertEqual(1, len(out))
        self.assertEqual(sd_blob_hash, out[0])

        out = yield self.manager.get_stream_hash_for_sd_hash(sd_blob_hash)
        self.assertEqual(stream_hash, out)

        # delete stream
        yield self.manager.delete_stream(stream_hash)
        out = yield self.manager.check_if_stream_exists(stream_hash)
        self.assertFalse(out)
        def _get_blobs_for_stream(transaction):
            crypt_blob_infos = []
            stream_blobs = transaction.execute(
                "select blob_hash, position, iv from stream_blob where stream_hash=?",
                (stream_hash, )).fetchall()
            if only_completed:
                lengths = transaction.execute(
                    "select b.blob_hash, b.blob_length from blob b "
                    "inner join stream_blob s ON b.blob_hash=s.blob_hash and b.status='finished' and s.stream_hash=?",
                    (stream_hash, )).fetchall()
            else:
                lengths = transaction.execute(
                    "select b.blob_hash, b.blob_length from blob b "
                    "inner join stream_blob s ON b.blob_hash=s.blob_hash and s.stream_hash=?",
                    (stream_hash, )).fetchall()

            blob_length_dict = {}
            for blob_hash, length in lengths:
                blob_length_dict[blob_hash] = length

            for blob_hash, position, iv in stream_blobs:
                blob_length = blob_length_dict.get(blob_hash, 0)
                crypt_blob_infos.append(
                    CryptBlobInfo(blob_hash, position, blob_length, iv))
            crypt_blob_infos = sorted(crypt_blob_infos,
                                      key=lambda info: info.blob_num)
            return crypt_blob_infos
Esempio n. 3
0
def save_sd_info(stream_info_manager, sd_info, ignore_duplicate=False):
    log.debug("Saving info for %s", str(sd_info['stream_name']))
    hex_stream_name = sd_info['stream_name']
    key = sd_info['key']
    stream_hash = sd_info['stream_hash']
    raw_blobs = sd_info['blobs']
    suggested_file_name = sd_info['suggested_file_name']
    crypt_blobs = []
    for blob in raw_blobs:
        length = blob['length']
        if length != 0:
            blob_hash = blob['blob_hash']
        else:
            blob_hash = None
        blob_num = blob['blob_num']
        iv = blob['iv']
        crypt_blobs.append(CryptBlobInfo(blob_hash, blob_num, length, iv))
    log.debug("Trying to save stream info for %s", str(hex_stream_name))
    d = stream_info_manager.save_stream(stream_hash, hex_stream_name, key,
                                        suggested_file_name, crypt_blobs)

    def check_if_duplicate(err):
        if ignore_duplicate is True:
            err.trap(DuplicateStreamHashError)

    d.addErrback(check_if_duplicate)

    d.addCallback(lambda _: stream_hash)
    return d
Esempio n. 4
0
 def _get_blobs_for_stream(transaction):
     crypt_blob_infos = []
     stream_blobs = transaction.execute("select blob_hash, position, iv from stream_blob "
                                        "where stream_hash=?", (stream_hash, )).fetchall()
     if stream_blobs:
         for blob_hash, position, iv in stream_blobs:
             if blob_hash is not None:
                 blob_length = transaction.execute("select blob_length from blob "
                                                   "where blob_hash=?",
                                                   (blob_hash,)).fetchone()
                 blob_length = 0 if not blob_length else blob_length[0]
                 crypt_blob_infos.append(CryptBlobInfo(blob_hash, position, blob_length, iv))
             else:
                 crypt_blob_infos.append(CryptBlobInfo(None, position, 0, iv))
         crypt_blob_infos = sorted(crypt_blob_infos, key=lambda info: info.blob_num)
     return crypt_blob_infos
Esempio n. 5
0
 def _format_initial_blobs_for_download_manager(self, blob_infos):
     infos = []
     for blob_hash, blob_num, iv, length in blob_infos:
         if blob_hash is not None:
             infos.append(CryptBlobInfo(blob_hash, blob_num, length, iv))
         else:
             log.debug("Setting _final_blob_num to %s", str(blob_num - 1))
             self._final_blob_num = blob_num - 1
     return infos
 def _format_initial_blobs_for_download_manager(self, blob_infos):
     infos = []
     for i, (blob_hash, blob_num, iv, length) in enumerate(blob_infos):
         if blob_hash is not None and length:
             infos.append(CryptBlobInfo(blob_hash, blob_num, length, iv))
         else:
             if i != len(blob_infos) - 1:
                 raise Exception("Invalid stream terminator")
             log.debug("Setting _final_blob_num to %s", str(blob_num - 1))
             self._final_blob_num = blob_num - 1
     return infos
Esempio n. 7
0
 def __init__(self, blob_hash, blob_num, length, iv, revision, signature):
     CryptBlobInfo.__init__(self, blob_hash, blob_num, length, iv)
     self.revision = revision
     self.signature = signature
Esempio n. 8
0
 def __init__(self, blob_hash, blob_num, length, iv, revision, signature):
     CryptBlobInfo.__init__(self, blob_hash, blob_num, length, iv)
     self.revision = revision
     self.signature = signature