def test_is_utf8() -> None: blob: Blob = Blob( ref=Ref.from_ref_str( "sha224-d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"), readall=lambda: "test".encode("utf-8"), ) assert blob.is_utf8() blob = Blob( ref=Ref.from_ref_str( "sha224-d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"), readall=lambda: b"\xdc", ) assert not blob.is_utf8()
def get_test_env() -> Iterator[JSONSignTestEnv]: """Returns utils for performing a jsonsign test""" # Create a blobserver bs: Storage = MemoryBlobServer() # Add a public key with open( os.path.join(os.path.dirname(__file__), "testdata", "key01.pub"), encoding="utf-8", ) as public_key_file: public_key_blob: Blob = Blob.from_contents_str(public_key_file.read()) bs.receive_blob(public_key_blob) # Load the corresponding private key with open( os.path.join(os.path.dirname(__file__), "testdata", "key01.priv"), encoding="utf-8", ) as key_file: key_1_priv: str = key_file.read() yield JSONSignTestEnv( bs=bs, gpg_key_inspector=PGPYGPGKeyInspector(), gpg_signer=PGPYGPGSigner(armored_private_keys=[key_1_priv]), gpg_signature_verifier=PGPYGPGSignatureVerifier(), public_key_ref=public_key_blob.get_ref(), )
def test_signature_from_go_implementation() -> None: public_key_blob_str: str = """-----BEGIN PGP PUBLIC KEY BLOCK----- xsBNBGAHnVgBCADerM1lth1pgduiuOOhu6vFxnt5PoZbH+PRB/iJflgmBrswryPr 5oyalJrv6k5mnbgHumz46OaZFW6oaOpO5xLHVvcNuPjJceyPV2IeSz5PLm1SAlge gSgCWGpAdcNsj+FIdftd7d1/mL42S9DQ4xByrkTkY06mAnci8oy5mplNHGa6nnib sm5iWPcGX6Rvz3YCkm/kdKyYhBzoBJFUh4r13LxCiF6aefHN1NXvJQsleAPjUsTl aouhfumRpKnEol/tJJuys/LnCC+tkU1D6qiAI+ALnCzWKhbCgfbt2InimxEC3YQZ MfoyWlkBlLxT9rDBf8rkXuF7qXNKiPA4w571ABEBAAE= =4ilV -----END PGP PUBLIC KEY BLOCK----- """ public_key_blob = Blob.from_contents_str(public_key_blob_str) assert (public_key_blob.get_ref().to_str() == "sha224-755426de872509a10461cb908a2ab8012df9f63706aaa6994f0ad895") signed_json_object: bytes = b"""{"camliVersion": 1, "camliSigner": "sha224-755426de872509a10461cb908a2ab8012df9f63706aaa6994f0ad895", "camliType": "permanode", "claimDate": "2021-08-08T21:49:57.225132035Z", "random": "wNqQLPEH/aq/lGYN2D43EV1UEu8=" ,"camliSig":"wsBcBAABCAAQBQJhEFGFCRAwgMgbA5XuOQAAMDcIADiyzvzCAhjcwbmLuSHicMihrwHRC+4S/GxERNiqf+5nW/lCbwUa9quvFadukc0+OK18IiqYPXnPe9OAxgH29Yds60WtzVATOrSqarmWuy48gZekQ8m+r3qRMs4fbu0PgUSnz3bPYeNwx+4NncoO1lwM9o9brA9HRHkDmuJ1jYWTuuDDsC5NbuHxwsLD1ATF9JH0S/NWNFpl0El+9RfsbRg7FdC3O37Dqu7nO9giM5XQDViNiLT/gKStck28COdhyHJvB+l0egqir5oQJ1wODErcOdVpS7k7bAfS9+I1WBuLs1++bVk8beVBp4GsJGKRgor1o+7FFFqDUMEDIPyE43o==c+i6"}""" with get_test_env() as test_env: # Load the public key test_env.bs.receive_blob(public_key_blob) # Verify the signature assert (jsonsign.verify_json_signature( signed_json_object=signed_json_object, fetcher=test_env.bs, gpg_signature_verifier=test_env.gpg_signature_verifier, ) is True)
def test_from_contents_str() -> None: blob: Blob = Blob.from_contents_str("test") assert blob.get_ref().get_digest_algorithm().get_digest_name() == "sha224" assert (blob.get_ref().to_str() == "sha224-90a3ed9e32b2aaf4c61c410eb925426119e1a9dc53d4286ade99a809") assert (blob.get_ref().get_hexdigest() == "90a3ed9e32b2aaf4c61c410eb925426119e1a9dc53d4286ade99a809") assert blob.get_bytes().decode("utf-8") == "test" assert blob.is_valid()
def test_is_valid() -> None: blob_content: bytes = "test".encode("utf-8") hexdigest: str = hashlib.sha224(blob_content).hexdigest() ref: Ref = Ref.from_ref_str(f"sha224-{hexdigest}") blob: Blob = Blob( ref=ref, readall=lambda: blob_content, ) assert blob.is_valid()
def fetch_blob(self, ref: Ref) -> Blob: resp: S3GetObjectResponse = self.client.get_object( Bucket=self.bucket, Key=self.dirprefix + ref.to_str(), ) blob: Blob = Blob( ref=ref, readall=lambda: resp["Body"].read(), ) return blob
def test_schema_from_blob_permanode() -> None: blob: Blob = Blob.from_contents_str( """ {"camliVersion": 1, "camliType": "permanode", "random": "615e05c68c8411df81a2001b639d041f", "camliSigner": "hashalg-xxxxxxxxxxx" } """ ) schema: Schema = Schema.from_blob(blob) assert schema.get_type() == CamliType.PERMANODE
def from_blob(cls, blob: Blob) -> "Schema": if len(blob.get_bytes()) > cls.SCHEMA_MAX_BYTES: raise Exception( f"Schema blobs must be smaller than {cls.SCHEMA_MAX_BYTES} bytes, got {len(blob.get_bytes())}" ) if not blob.is_utf8(): raise Exception("Schema blobs must be encoded using utf-8") blob_str: str = blob.get_bytes().decode("utf-8") blob_json: SchemaSuperset = json.loads(blob_str) validation_errors = JsonSchemaValidator.validate(dict(blob_json)) if validation_errors: raise Exception( f"The blob's schema is not valid: {str(validation_errors)}") return cls( blob=blob, ss=blob_json, )
def receive_blob(self, blob: Blob) -> None: # Stop immediately if we have already indexed this blob. have_key: str = self._key_value_builder.get_have_key(blob.get_ref()) have_value_str: Optional[str] = self._sorted_kv.get(have_key) if have_value_str is not None: have_value: HaveValue = self._key_value_builder.parse_have_value( have_value_str ) if have_value.indexed: return raise NotImplementedError()
def test_schema_from_blob_raises() -> None: blob: Blob = Blob.from_contents_str( """ {"camliVersion": 1, "parts": [ {"blobRef": "digalg-blobref", "size": 1024}, {"bytesRef": "digalg-blobref", "size": 5000000, "offset": 492 }, {"blobRef": "digalg-blobref", "size": 10} ] } """ ) with pytest.raises(Exception): schema: Schema = Schema.from_blob(blob)
def test_schema_from_blob_bytes() -> None: schema: Schema = Schema.from_blob( Blob.from_contents_str( """ {"camliVersion": 1, "camliType": "bytes", "parts": [ {"blobRef": "digalg-blobref", "size": 1024}, {"bytesRef": "blobref", "size": 5000000, "offset": 492 }, {"blobRef": "digalg-blobref", "size": 10} ] } """ ) ) assert schema.get_type() == CamliType.BYTES
def test_schema_from_blob_file() -> None: blob: Blob = Blob.from_contents_str( """ { "camliType": "file", "camliVersion": 1, "fileName": "pattes_orford.png", "parts": [{ "blobRef": "sha224-0ec3f537a82cb2d95b9929bef457ac21ed1ea22d22ff6b5bc401de31", "size": 262144 }, { "blobRef": "sha224-2835a980eb488ae1dfbd5e471cd868fccd9ca7e47b403e8a281666a5", "size": 88599 }, { "blobRef": "sha224-ed1d80769d615aec8181d04d9f25492843fbb6eea49fe058d8e34ff8", "size": 73889 }, { "blobRef": "sha224-f64dd508cb359703862e31989f74d478faa54c62e50266cab86ddb08", "size": 69568 }, { "bytesRef": "sha224-0b2bfe9ce5ac54990512c226e172a144a9a38e16b8b0c88aed6099b1", "size": 291060 }, { "blobRef": "sha224-0e4615f54e6575c5e260d18cb760340e44cc150c817e8dbb5562d221", "size": 70617 }, { "blobRef": "sha224-3d16526dd3bbc81dba64f5562a90c348e58f5929421c8e786a65e2e6", "size": 74981 }, { "blobRef": "sha224-f25954248c1d91cc4465fc304b35cd0ec3075431c8d4c06e9fcb42ec", "size": 16558 }], "unixMtime": "2020-10-21T04:51:52Z" } """ ) schema: Schema = Schema.from_blob(blob) assert schema.get_type() == CamliType.FILE
def receive_blob(self, blob: Blob) -> None: self.blobs[blob.get_ref().to_str()] = blob