Skip to content

Commit

Permalink
sigstore, test: more unit tests, rename mods (#92)
Browse files Browse the repository at this point in the history
Signed-off-by: William Woodruff <william@trailofbits.com>
  • Loading branch information
woodruffw committed May 17, 2022
1 parent ccb00ad commit cdb0cc7
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 3 deletions.
2 changes: 1 addition & 1 deletion sigstore/_internal/fulcio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"""


from ._client import (
from .client import (
FulcioCertificateSigningRequest,
FulcioCertificateSigningResponse,
FulcioClient,
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion sigstore/_internal/rekor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
APIs for interacting with Rekor.
"""

from ._client import RekorClient, RekorEntry, RekorInclusionProof
from .client import RekorClient, RekorEntry, RekorInclusionProof

__all__ = ["RekorClient", "RekorEntry", "RekorInclusionProof"]
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class RekorInclusionProof(BaseModel):
tree_size: int = Field(..., alias="treeSize")
hashes: List[str] = Field(..., alias="hashes")

class Config:
allow_population_by_field_name = True

@validator("log_index")
def log_index_positive(cls, v: int) -> int:
if v < 0:
Expand All @@ -82,7 +85,7 @@ def tree_size_positive(cls, v: int) -> int:
def log_index_within_tree_size(
cls, v: int, values: Dict[str, Any], **kwargs: Any
) -> int:
if v <= values["log_index"]:
if "log_index" in values and v <= values["log_index"]:
raise ValueError(
"Inclusion proof has log index greater than or equal to tree size: "
f"{v} <= {values['log_index']}"
Expand Down
13 changes: 13 additions & 0 deletions test/internal/rekor/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2022 The Sigstore Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
51 changes: 51 additions & 0 deletions test/internal/rekor/test_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2022 The Sigstore Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest
from pydantic import ValidationError

from sigstore._internal.rekor import client


class TestRekorInclusionProof:
def test_valid(self):
proof = client.RekorInclusionProof(
log_index=1, root_hash="abcd", tree_size=2, hashes=[]
)
assert proof is not None

def test_negative_log_index(self):
with pytest.raises(
ValidationError, match="Inclusion proof has invalid log index"
):
client.RekorInclusionProof(
log_index=-1, root_hash="abcd", tree_size=2, hashes=[]
)

def test_negative_tree_size(self):
with pytest.raises(
ValidationError, match="Inclusion proof has invalid tree size"
):
client.RekorInclusionProof(
log_index=1, root_hash="abcd", tree_size=-1, hashes=[]
)

def test_log_index_outside_tree_size(self):
with pytest.raises(
ValidationError,
match="Inclusion proof has log index greater than or equal to tree size",
):
client.RekorInclusionProof(
log_index=2, root_hash="abcd", tree_size=1, hashes=[]
)

0 comments on commit cdb0cc7

Please sign in to comment.