def test_write_sb(self): container = StringBuffer(24) el = StringBuffer(16) el.set('hello world') container.write(el) container.seek(0) self.assertEquals(container.read(), 'hello world')
def test_overflow(self): sb = StringBuffer(24) with self.assertRaises(BufferOverflow): sb.write('this string is definitely longer than 24 characters and should raise an error') self.assertRaises(OverflowError, sb.seek, -1) sb = StringBuffer(8) sb.set('01234567') sb.seek(0) buf = sb.raw_read(8) self.assertEquals(buf, '01234567') self.assertRaises(BufferOverflow, sb.raw_read, 1)
def test_large_hash(self): with open('/dev/urandom', 'r+b') as f: buf = StringBuffer.from_file(f.fileno(), 4096 * 3) buf_sha = buf.hash() from hashlib import sha1 outside = StringBuffer(sha1(buf.raw_read()).digest()) self.assertEquals(outside.raw_read(), buf_sha.raw_read())
def serialize(self): sb = StringBuffer(24) sb.pack('<BBH', self.mfs_type, self.mode, self.flags) sb.write(self.sha) sb.seek(0) return sb
def serialize(self): sb = StringBuffer(16 + self.total_size) sb.pack('<BBHIQ', self.mfs_type, 0, 0, self.entry_no, self.total_size) for symbol in self.symbols: sb.write(symbol.serialize()) sb.seek(0) return sb
def serialize(self): sb = StringBuffer(16 + self.total_size) sb.pack('<BBHHHQ', self.mfs_type, self.ver, self.name_size, self.datatype_size, self.dataspace_size, self.total_size) if self.attr is not None: sb.write(self.attr.serialize()) sb.seek(0) return sb
def serialize(self): sb = StringBuffer(16 + self.total_size) sb.pack('<BBH', self.mfs_type, self.version, self.children) sb.write(self.signature) sb.pack('<Q', self.total_size) for o in self.objects: sb.write(o.serialize()) return sb
def serialize(self): sb = StringBuffer(8 + self.symbol_length) sb.pack('<HHI', self.idx, 0, max(len(self.symbol) + 1, self.symbol_length)) # Account for null-terminator sb.write(self.symbol) sb.seek(0) return sb
def serialize(self): sb = StringBuffer(16 + self.total_size) sb.pack('<BBBBIQ', self.mfs_type, self.ver, self.dims, self.flags, 0, self.total_size) for ds in self.dataspaces: sb.write(ds.serialize()) sb.seek(0) return sb
def test_fwrite(self): sb = StringBuffer(24) sb.set('hello world\n') with TemporaryFile('w+b') as f: fd = f.fileno() sb.fwrite(fd) f.seek(0) buf = f.readline() self.assertEquals(buf, 'hello world\n')
def test_fread(self): sb = StringBuffer(16) with TemporaryFile('w+b') as f: fd = f.fileno() f.write('hello world\n') f.seek(0) sb.fread(fd, 8) sb.seek(0) self.assertEquals(sb.raw_read(8), 'hello wo')
def test_buffer(self): sb = StringBuffer(128) sb.write('hello world') sb.seek(0) self.assertEquals(sb.read(), 'hello world') self.assertEquals(sb.offset(), 0) sb.write('jello') self.assertEquals(sb.offset(), 5) sb.seek(0) self.assertEquals(sb.read(), 'jello world') sb.set('hi') self.assertEquals(sb.offset(), 0) self.assertEquals(sb.read(), 'hi') sb = StringBuffer("Hello World") self.assertEquals(sb.read(), 'Hello World') sb = StringBuffer(sb) self.assertEquals(sb.read(), 'Hello World')
def create_datatype(self, dtype_class): with TemporaryFile('w+b') as f: dtype_h = dtype_class() sb = dtype_h.serialize() sb.fwrite(f.fileno()) f.seek(0) sb = StringBuffer(16) sb.fread(f.fileno()) sb.seek(0) dtype_h = MFSObjectHeader.deserialize(sb) self.assertIsInstance(dtype_h, DatatypeHeader)
def test_read_uint(self): sb = StringBuffer(16) sb.pack('<H', 16) sb.seek(0) i = sb.read_uint(2) self.assertEquals(i, 16) self.assertEquals(sb.offset(), 2) sb.pack('<Q', 1024) sb.seek(2) i = sb.read_uint(8) self.assertEquals(1024, i)
def header_check(self, f, dtype_h, datatype, flags, size, total_size): sb = dtype_h.serialize() sb.fwrite(f.fileno()) f.seek(0) sb = StringBuffer(16) sb.fread(f.fileno()) sb.seek(0) dtype_h = MFSObjectHeader.deserialize(sb) self.assertIsInstance(dtype_h, DatatypeHeader) self.assertEquals(dtype_h.datatype, datatype) self.assertEquals(dtype_h.flags, flags) self.assertEquals(dtype_h.size, size) self.assertEquals(dtype_h.total_size, total_size)
def test_hash(self): buf = StringBuffer("Hello World") buf_sha = buf.hash() from hashlib import sha1 outside = StringBuffer(sha1(buf.raw_read()).digest()) self.assertEquals(outside.raw_read(), buf_sha.raw_read())
def test_word_align(self): sb = StringBuffer(12) self.assertEquals(sb.buffer_size(), 16)
def serialize(self): sb = StringBuffer(16) sb.pack('<BBHIQ', self.mfs_type, self.datatype, self.flags, self.size, self.total_size) sb.seek(0) return sb
def serialize(self): sb = StringBuffer(8) sb.pack('<Q', self.dim_size) sb.seek(0) return sb