Skip to content

Commit

Permalink
Merge pull request Yubico#2 from solokeys/black-with-prehooks
Browse files Browse the repository at this point in the history
Black with prehooks
  • Loading branch information
nickray committed Aug 9, 2019
2 parents 30e2dc6 + 7c75def commit 937bcd8
Show file tree
Hide file tree
Showing 18 changed files with 503 additions and 440 deletions.
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
repos:
- repo: https://github.com/psf/black
rev: stable
hooks:
- id: black
language_version: python3.7
42 changes: 35 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,46 @@
.PHONY: standard-tests vendor-tests

PY_VERSION=$(shell python -c "import sys; print('%d.%d'% sys.version_info[0:2])")
VALID=$(shell python -c "print($(PY_VERSION) >= 3.6)")

ifeq ($(OS),Windows_NT)
BIN=venv/Scripts
else
BIN=venv/bin
endif

ifeq (True,$(VALID))
PYTHON=python
else
PYTHON=python3
endif

standard-tests: venv
venv/bin/pytest tests/standard
$(BIN)/pytest tests/standard

vendor-tests: venv
venv/bin/pytest tests/vendor
$(BIN)/pytest tests/vendor

# setup development environment
venv:
python3 -m venv venv
venv/bin/pip install -U pip
venv/bin/pip install -U -r requirements.txt
$(PYTHON) -m venv venv
$(BIN)/python -m pip install -U pip
$(BIN)/pip install -U -r requirements.txt
$(BIN)/pip install -U -r dev-requirements.txt
$(BIN)/pre-commit install

# re-run if dependencies change
update:
venv/bin/pip install -U pip
venv/bin/pip install -U -r requirements.txt
$(BIN)/python -m pip install -U pip
$(BIN)/pip install -U -r requirements.txt
$(BIN)/pip install -U -r dev-requirements.txt

# ensure this passes before commiting
check:
$(BIN)/black --check tests/

# automatic code fixes
fix: black

black:
$(BIN)/black tests/
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,15 @@ To run tests against a "simulation" build of the Solo authenticator, supply the
pytest --sim tests/standard
```

# Contributing

We use `black` to prevent code formatting discussions.

The `make venv` setup method installs git pre-commit hooks that check conformance automatically.

You can also `make check` and `make fix` manually, or use an editor plugin.

# License

Apache-2.0 OR MIT

2 changes: 2 additions & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
black==18.9b0 # 19.3b0 has an issue on Windows
pre-commit
64 changes: 30 additions & 34 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,58 +32,55 @@ def is_simulation(pytestconfig):
def is_nfc(pytestconfig):
return pytestconfig.getoption("nfc")


@pytest.fixture(scope="module")
def info(device):
info = device.ctap2.get_info()
#print("data:", bytes(info))
#print("decoded:", cbor.decode_from(bytes(info)))
# print("data:", bytes(info))
# print("decoded:", cbor.decode_from(bytes(info)))
return info


@pytest.fixture(scope="module")
def MCRes(resetDevice,):
req = FidoRequest()
res = resetDevice.sendMC(
*req.toMC(),
)
setattr(res,'request',req)
res = resetDevice.sendMC(*req.toMC())
setattr(res, "request", req)
return res

@pytest.fixture(scope='class')
def GARes(device,MCRes):
req = FidoRequest(allow_list = [{
"id": MCRes.auth_data.credential_data.credential_id,
"type": "public-key",
}])
res = device.sendGA(
*req.toGA(),

@pytest.fixture(scope="class")
def GARes(device, MCRes):
req = FidoRequest(
allow_list=[
{"id": MCRes.auth_data.credential_data.credential_id, "type": "public-key"}
]
)
setattr(res,'request',req)
res = device.sendGA(*req.toGA())
setattr(res, "request", req)
return res


@pytest.fixture(scope="module")
def RegRes(resetDevice,):
req = FidoRequest()
res = resetDevice.register(
req.challenge, req.appid
)
setattr(res,'request',req)
res = resetDevice.register(req.challenge, req.appid)
setattr(res, "request", req)
return res

@pytest.fixture(scope='class')

@pytest.fixture(scope="class")
def AuthRes(device, RegRes):
req = FidoRequest()
res = device.authenticate(
req.challenge, req.appid, RegRes.key_handle
)
setattr(res,'request',req)
res = device.authenticate(req.challenge, req.appid, RegRes.key_handle)
setattr(res, "request", req)
return res



@pytest.fixture(scope='module')
@pytest.fixture(scope="module")
def allowListItem(MCRes):
return
return


@pytest.fixture(scope="session")
def device(pytestconfig):
Expand All @@ -98,16 +95,19 @@ def device(pytestconfig):

return dev

@pytest.fixture(scope='class')

@pytest.fixture(scope="class")
def rebootedDevice(device):
device.reboot()
return device

@pytest.fixture(scope='module')

@pytest.fixture(scope="module")
def resetDevice(device):
device.reset()
return device


class Packet(object):
def __init__(self, data):
self.data = data
Expand All @@ -120,7 +120,6 @@ def FromWireFormat(pkt_size, data):
return Packet(data)



class TestDevice:
def __init__(self, tester=None):
self.origin = "https://examplo.org"
Expand Down Expand Up @@ -270,9 +269,7 @@ def reset(self,):
self.ctap2.reset()

def sendMC(self, *args, **kwargs):
attestation_object = self.ctap2.make_credential(
*args, **kwargs
)
attestation_object = self.ctap2.make_credential(*args, **kwargs)
if attestation_object:
verifier = Attestation.for_type(attestation_object.fmt)
client_data = args[0]
Expand All @@ -292,6 +289,5 @@ def sendCP(self, *args, **kwargs):
def sendPP(self, *args, **kwargs):
return self.client.pin_protocol.get_pin_token(*args, **kwargs)


def delay(secs):
time.sleep(secs)
11 changes: 4 additions & 7 deletions tests/standard/fido2/pin/test_lockout.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
from tests.utils import *


def test_lockout(device,resetDevice):
pin = 'TestPin'
def test_lockout(device, resetDevice):
pin = "TestPin"
device.client.pin_protocol.set_pin(pin)

pin_token = device.client.pin_protocol.get_pin_token(pin)
req = FidoRequest(pin_token = pin_token)
req = FidoRequest(pin_token=pin_token)

req.pin_auth = hmac_sha256(pin_token, req.cdh)[:16]

Expand All @@ -23,9 +23,7 @@ def test_lockout(device,resetDevice):
err = [CtapError.ERR.PIN_BLOCKED, CtapError.ERR.PIN_INVALID]

with pytest.raises(CtapError) as e:
device.sendPP(
"WrongPin",
)
device.sendPP("WrongPin")
assert e.value.code == err or e.value.code in err

attempts = 8 - i
Expand All @@ -46,4 +44,3 @@ def test_lockout(device,resetDevice):
with pytest.raises(CtapError) as e:
device.sendPP(pin)
assert e.value.code == CtapError.ERR.PIN_BLOCKED

0 comments on commit 937bcd8

Please sign in to comment.