Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce ULID Timestamp Range #64

Closed
ahawker opened this issue Oct 28, 2017 · 0 comments
Closed

Enforce ULID Timestamp Range #64

ahawker opened this issue Oct 28, 2017 · 0 comments
Assignees
Labels

Comments

@ahawker
Copy link
Owner

ahawker commented Oct 28, 2017

A number of the test fixtures that generate data are powered by os.urandom. This works fine until it generates a random sequence of bytes that starts with a leading zero. This will cause tests to fail during duration due to int.bit_length stripping leading zeros in its computation.

Example test failure: https://travis-ci.org/ahawker/ulid/jobs/294263189

All of the above is a side-effect of the fact that there is no validation logic for the timestamp portion of a ULID. It should never contain a zero leading byte since the minimum value is the Unix epoch.

Items to address this issue:

  • Validation rules to enforce minimum and maximum timestamp values upon creation
  • Update test fixtures to specific generates values within valid or invalid ranges

Example:

>>> import ulid
>>> data = b"\x00\xcdh\x95}\xd9\xb2Yp':y0\xe4\xce\xdc"
>>> ulid.from_bytes(data)
<ULID('00SNM9AZESP9CQ09STF4RE9KPW')>
>>> ulid.from_int(int.from_bytes(data, byteorder='big'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/hawker/src/github.com/ahawker/ulid/ulid/api.py", line 76, in from_int
    raise ValueError('Expects integer to be 128 bits; got {} bytes'.format(length))
ValueError: Expects integer to be 128 bits; got 15 bytes
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant