def test_encode_eight_nul_bytes_with_zero_timestamp(): key = unhexlify( "73757065727365637265746b6579796f7573686f756c646e6f74636f6d6d6974") branca = Branca(key) branca._nonce = unhexlify( "beefbeefbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef") token = branca.encode(b"\x00\x00\x00\x00\x00\x00\x00\x00", timestamp=0) assert token == "1jIBheHbDdkCDFQmtgw4RUZeQoOJgGwTFJSpwOAk3XYpJJr52DEpILLmmwYl4tjdSbbNqcF1"
def test_encode_non_utf8_payload(): key = unhexlify( "73757065727365637265746b6579796f7573686f756c646e6f74636f6d6d6974") branca = Branca(key) branca._nonce = unhexlify( "beefbeefbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef") token = branca.encode(b"", timestamp=0) assert token == "4sfD0vPFhIif8cy4nB3BQkHeJqkOkDvinI4zIhMjYX4YXZU5WIq9ycCVjGzB5"
def test_encode_hello_world_with_november_27_timestamp(): key = unhexlify( "73757065727365637265746b6579796f7573686f756c646e6f74636f6d6d6974") branca = Branca(key) branca._nonce = unhexlify( "beefbeefbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef") token = branca.encode("Hello world!", timestamp=123206400) assert token == "875GH23U0Dr6nHFA63DhOyd9LkYudBkX8RsCTOMz5xoYAMw9sMd5QwcEqLDRnTDHPenOX7nP2trlT"
def test_encode_hello_world_with_max_timestamp(): key = unhexlify( "73757065727365637265746b6579796f7573686f756c646e6f74636f6d6d6974") branca = Branca(key) branca._nonce = unhexlify( "beefbeefbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef") token = branca.encode("Hello world!", timestamp=4294967295) assert token == "89i7YCwu5tWAJNHUDdmIqhzOi5hVHOd4afjZcGMcVmM4enl4yeLiDyYv41eMkNmTX6IwYEFErCSqr"
def test_encode_hello_world_with_zero_timestamp(): key = unhexlify( "73757065727365637265746b6579796f7573686f756c646e6f74636f6d6d6974") branca = Branca(key) branca._nonce = unhexlify( "beefbeefbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef") token = branca.encode("Hello world!", timestamp=0) assert token == "870S4BYxgHw0KnP3W9fgVUHEhT5g86vJ17etaC5Kh5uIraWHCI1psNQGv298ZmjPwoYbjDQ9chy2z"
def test_should_throw_when_expired(): key = unhexlify( "73757065727365637265746b6579796f7573686f756c646e6f74636f6d6d6974") branca = Branca(key) branca._nonce = unhexlify( "0102030405060708090a0b0c0102030405060708090a0b0c") token = branca.encode(b"Hello world!", timestamp=123206400) with pytest.raises(RuntimeError): branca.decode(token, 3600)
def test_encode_eight_nul_bytes_with_november_27_timestamp(): key = unhexlify( "73757065727365637265746b6579796f7573686f756c646e6f74636f6d6d6974") branca = Branca(key) branca._nonce = unhexlify( "beefbeefbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef") token = branca.encode(b"\x00\x00\x00\x00\x00\x00\x00\x00", timestamp=123206400) assert token == "1jJDJOEjuwVb9Csz1Ypw1KBWSkr0YDpeBeJN6NzJWx1VgPLmcBhu2SbkpQ9JjZ3nfUf7Aytp"
def test_encode_eight_nul_bytes_with_zero_timestamp(): key = unhexlify( "73757065727365637265746b6579796f7573686f756c646e6f74636f6d6d6974") branca = Branca(key) branca._nonce = unhexlify( "beefbeefbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef") token = branca.encode(b"\x00\x00\x00\x00\x00\x00\x00\x00", timestamp=4294967295) assert token == "1jrx6DUu5q06oxykef2e2ZMyTcDRTQot9ZnwgifUtzAphGtjsxfbxXNhQyBEOGtpbkBgvIQx"