Example #1
0
    def test_collection_unclosed(self):
        fh1 = io.BytesIO()
        fw1 = datafile.DataFileWriter(fh1)
        fw1.collection_start(id_=0, type_=1)
        fw1.collection_end()
        fw1.finalize()

        fh2 = io.BytesIO()
        fw2 = datafile.DataFileWriter(fh2)
        fw2.collection_start(id_=0, type_=1)
        fw2.finalize()

        self.assertEqual(fh1.getbuffer(), fh2.getbuffer())
Example #2
0
 def make_encrypted(self, compress=False):
     data = np.arange(2**16, dtype=np.uint16).tobytes()
     fh = io.BytesIO(F1)
     fw = datafile.DataFileWriter(fh)
     signing_key_prv = bytes(range(32))
     encryption_key = bytes(range(1, 33))
     nonce = bytes(range(16, 16 + 24 * 2, 2))
     associated_data = bytes(range(2, 50, 2))
     fw.append_encrypted(b'RAW',
                         data,
                         signing_key_prv,
                         encryption_key,
                         nonce,
                         associated_data,
                         compress=compress)
     fw.finalize()
     fh.seek(0)
     fr = datafile.DataFileReader(fh)
     return fr, {
         'data':
         data,
         'signing_key_prv':
         signing_key_prv,
         'signing_key_pub':
         monocypher.compute_signing_public_key(signing_key_prv),
         'encryption_key':
         encryption_key,
         'nonce':
         nonce,
         'associated_data':
         associated_data,
         'fh':
         fh,
     }
Example #3
0
 def test_write_known_value(self):
     fh = io.BytesIO()
     f = datafile.DataFileWriter(fh)
     f.append(b'TAG', bytes(range(10)))
     f.finalize()
     value = fh.getvalue()
     self.assertEqual(F1, value)
Example #4
0
    def test_compressed_write_read(self):
        data = np.arange(2**16, dtype=np.uint16).tobytes()
        fh = io.BytesIO(F1)
        fw = datafile.DataFileWriter(fh)
        fw.append(b'RAW', data, compress=True)
        fw.finalize()
        fh.seek(0)

        fr = datafile.DataFileReader(fh)
        tag, value = next(fr)
        self.assertEqual(b'RAW', tag)
        self.assertEqual(data, value)
        with self.assertRaises(StopIteration):
            next(fr)
Example #5
0
 def _construct_collection(self, collection_data=None):
     data = [
         bytes(range(00, 10)),
         bytes(range(10, 20)),
         bytes(range(20, 30))
     ]
     fh = io.BytesIO()
     fw = datafile.DataFileWriter(fh)
     c = fw.collection_start(id_=0, type_=1, data=collection_data)
     for d in data:
         fw.append(b'TAG', d)
     fw.collection_end(c)
     fw.finalize()
     fh.seek(0)
     return data, fh
Example #6
0
    def test_write_read(self):
        tags = [[42, bytes(range(1, 17))], (43, b'1234')]
        fh = io.BytesIO(F1)
        fw = datafile.DataFileWriter(fh)
        for tag, value in tags:
            fw.append(tag, value)
        fw.finalize()
        fh.seek(0)

        fr = datafile.DataFileReader(fh)
        for tag, value in tags:
            tag_rd, value_rd = yield fr
            tag_rd, = struct.unpack('<I', tag_rd)
            self.assertEqual(tag, tag_rd)
            self.assertEqual(value, value_rd)
        with self.assertRaises(StopIteration):
            next(fr)
Example #7
0
    def test_valid_signature(self):
        key = b';\xfa\xe7\xa7(\xa5\xc8M\xcb\xb8\xe1H\x84\x95rB\x99\xafW\x91T\x10\nE\x80\xb2]AT\xfd\xf3\xcb'
        fh = io.BytesIO()
        fw = datafile.DataFileWriter(fh)
        fw.signature_start(key)
        fw.append(datafile.TAG_DATA_BINARY, bytes(range(256)))
        fw.signature_end()
        fw.finalize()

        fh.seek(0)
        fr = datafile.DataFileReader(fh)
        tag, value = next(fr)
        self.assertEqual(datafile.TAG_SIGNATURE_START, tag)
        next(fr)
        tag, value = next(fr)
        self.assertEqual(datafile.TAG_SIGNATURE_END, tag)
        self.assertTrue(value)
Example #8
0
    def test_invalid_signature(self):
        key = b';\xfa\xe7\xa7(\xa5\xc8M\xcb\xb8\xe1H\x84\x95rB\x99\xafW\x91T\x10\nE\x80\xb2]AT\xfd\xf3\xcb'
        fh = io.BytesIO()
        fw = datafile.DataFileWriter(fh)
        fw.signature_start(key)
        fw.append(datafile.TAG_DATA_BINARY, bytes(range(256)))

        # add data to signature computation, but mess with underlying file
        pos = fh.tell()
        fw.append(datafile.TAG_DATA_BINARY, bytes(range(256)))
        fh.seek(pos)

        fw.signature_end()
        fw.finalize()
        fh.seek(0)
        fr = datafile.DataFileReader(fh)
        tag, value = next(fr)
        self.assertEqual(datafile.TAG_SIGNATURE_START, tag)
        next(fr)
        with self.assertRaises(ValueError):
            next(fr)
Example #9
0
    def save(self, private_key=None):
        """Save calibration to bytes.

        :param private_key: The private key used to sign the calibration.
            None (default) does not sign the calibration.
            The Joulescope software will display warnings if the calibration
            is not signed using a valid key.
        :return: The calibration as bytes.
        """
        if isinstance(self.time, str):
            self.time = dateutil.parser.parse(self.time)
        fh = io.BytesIO()
        dfw = datafile.DataFileWriter(fh)
        if private_key is not None:
            dfw.signature_start(private_key)
        dfw.append_header(
            timestamp=seconds_to_timestamp(self.time.timestamp()),
            version=_version_str_to_u32(self.version),
            product_id=self.product_id,
            vendor_id=self.vendor_id,
            subtype_id=self.subtype_id,
            hardware_compatibility=0,
            serial_number=binascii.unhexlify(self.serial_number),
        )
        self.current_offset = np.concatenate(
            (self.current_offset[:7], [0.0, np.nan]))
        self.current_gain = np.concatenate(
            (self.current_gain[:7], [0.0, np.nan]))
        if len(self.current_offset) != 9 or len(self.current_gain) != 9:
            raise ValueError('Invalid length for current')
        if len(self.voltage_offset) != 2 or len(self.voltage_gain) != 2:
            raise ValueError('Invalid length for voltage')
        cal = self.json()
        dfw.append(datafile.TAG_DATA_JSON,
                   json.dumps(cal, allow_nan=True).encode('utf-8'))
        if private_key is not None:
            dfw.signature_end()
        dfw.finalize()
        return bytes(fh.getbuffer())