2023-08-07 16:48:48 +00:00
|
|
|
from unittest.mock import patch
|
2023-11-30 15:55:51 +00:00
|
|
|
|
2020-11-11 15:54:01 +00:00
|
|
|
import pytest
|
2023-11-30 15:55:51 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
from moto.s3.utils import (
|
|
|
|
_VersionedKeyStore,
|
2023-11-30 15:55:51 +00:00
|
|
|
bucket_name_from_url,
|
2022-12-06 23:03:28 +00:00
|
|
|
compute_checksum,
|
2023-03-03 22:40:55 +00:00
|
|
|
cors_matches_origin,
|
2023-11-30 15:55:51 +00:00
|
|
|
parse_region_from_url,
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
2013-05-03 23:33:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_base_url():
|
2023-08-07 16:48:48 +00:00
|
|
|
assert bucket_name_from_url("https://s3.amazonaws.com/") is None
|
2013-05-03 23:33:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_localhost_bucket():
|
2023-08-07 16:48:48 +00:00
|
|
|
assert bucket_name_from_url("https://wfoobar.localhost:5000/abc") == "wfoobar"
|
2013-05-03 23:33:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_localhost_without_bucket():
|
2023-08-07 16:48:48 +00:00
|
|
|
assert bucket_name_from_url("https://www.localhost:5000/def") is None
|
2017-02-24 02:37:43 +00:00
|
|
|
|
2018-03-21 16:33:09 +00:00
|
|
|
|
2022-03-11 21:28:45 +00:00
|
|
|
def test_force_ignore_subdomain_for_bucketnames():
|
2021-03-26 16:51:19 +00:00
|
|
|
with patch("moto.s3.utils.S3_IGNORE_SUBDOMAIN_BUCKETNAME", True):
|
2023-08-07 16:48:48 +00:00
|
|
|
assert (
|
2021-03-26 16:51:19 +00:00
|
|
|
bucket_name_from_url("https://subdomain.localhost:5000/abc/resource")
|
2023-08-07 16:48:48 +00:00
|
|
|
is None
|
|
|
|
)
|
2014-06-27 21:37:51 +00:00
|
|
|
|
2018-07-10 17:52:53 +00:00
|
|
|
|
2014-06-27 21:37:51 +00:00
|
|
|
def test_versioned_key_store():
|
2023-08-07 16:48:48 +00:00
|
|
|
key_store = _VersionedKeyStore()
|
2014-06-27 21:37:51 +00:00
|
|
|
|
2023-08-07 16:48:48 +00:00
|
|
|
assert not key_store
|
2014-06-27 21:37:51 +00:00
|
|
|
|
2023-08-07 16:48:48 +00:00
|
|
|
key_store["key"] = [1]
|
|
|
|
assert len(key_store) == 1
|
2014-06-27 21:37:51 +00:00
|
|
|
|
2023-08-07 16:48:48 +00:00
|
|
|
key_store["key"] = 2
|
|
|
|
assert len(key_store) == 1
|
2014-06-27 21:37:51 +00:00
|
|
|
|
2023-08-07 16:48:48 +00:00
|
|
|
assert key_store["key"] == 2
|
|
|
|
assert key_store.get("key") == 2
|
|
|
|
assert key_store.get("badkey") is None
|
|
|
|
assert key_store.get("badkey", "HELLO") == "HELLO"
|
2014-06-27 21:37:51 +00:00
|
|
|
|
|
|
|
# Tests key[
|
2023-08-07 16:48:48 +00:00
|
|
|
assert "badkey" not in key_store
|
|
|
|
with pytest.raises(KeyError):
|
|
|
|
_ = key_store["badkey"]
|
2014-06-27 21:37:51 +00:00
|
|
|
|
2023-08-07 16:48:48 +00:00
|
|
|
assert len(key_store.getlist("key")) == 2
|
|
|
|
assert key_store.getlist("key") == [[1], 2]
|
|
|
|
assert key_store.getlist("badkey") is None
|
2014-06-27 21:37:51 +00:00
|
|
|
|
2023-08-07 16:48:48 +00:00
|
|
|
key_store.setlist("key", 1)
|
|
|
|
assert key_store.getlist("key") == [1]
|
2014-06-27 21:37:51 +00:00
|
|
|
|
2023-08-07 16:48:48 +00:00
|
|
|
key_store.setlist("key", (1, 2))
|
|
|
|
assert key_store.getlist("key") != (1, 2)
|
|
|
|
assert key_store.getlist("key") == [1, 2]
|
2014-06-27 21:37:51 +00:00
|
|
|
|
2023-08-07 16:48:48 +00:00
|
|
|
key_store.setlist("key", [[1], [2]])
|
|
|
|
assert len(key_store["key"]) == 1
|
|
|
|
assert key_store.getlist("key") == [[1], [2]]
|
2018-01-31 00:10:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_parse_region_from_url():
|
2019-10-31 15:44:26 +00:00
|
|
|
expected = "us-west-2"
|
|
|
|
for url in [
|
|
|
|
"http://s3-us-west-2.amazonaws.com/bucket",
|
|
|
|
"http://s3.us-west-2.amazonaws.com/bucket",
|
|
|
|
"http://bucket.s3-us-west-2.amazonaws.com",
|
|
|
|
"https://s3-us-west-2.amazonaws.com/bucket",
|
|
|
|
"https://s3.us-west-2.amazonaws.com/bucket",
|
|
|
|
"https://bucket.s3-us-west-2.amazonaws.com",
|
|
|
|
]:
|
2023-08-07 16:48:48 +00:00
|
|
|
assert parse_region_from_url(url) == expected
|
2018-01-31 00:10:43 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
expected = "us-east-1"
|
|
|
|
for url in [
|
|
|
|
"http://s3.amazonaws.com/bucket",
|
|
|
|
"http://bucket.s3.amazonaws.com",
|
|
|
|
"https://s3.amazonaws.com/bucket",
|
|
|
|
"https://bucket.s3.amazonaws.com",
|
|
|
|
]:
|
2023-08-07 16:48:48 +00:00
|
|
|
assert parse_region_from_url(url) == expected
|
2019-09-24 20:22:25 +00:00
|
|
|
|
|
|
|
|
2022-12-06 23:03:28 +00:00
|
|
|
def test_checksum_sha256():
|
2023-05-12 12:00:00 +00:00
|
|
|
checksum = b"h9FJy0JMA4dlbyEdJYn7Wx4WIpkhMJ6YWIQZzMqKc2I="
|
2023-08-07 16:48:48 +00:00
|
|
|
assert compute_checksum(b"somedata", "SHA256") == checksum
|
2022-12-06 23:03:28 +00:00
|
|
|
# Unknown algorithms fallback to SHA256 for now
|
2023-08-07 16:48:48 +00:00
|
|
|
assert compute_checksum(b"somedata", algorithm="unknown") == checksum
|
2022-12-06 23:03:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_checksum_sha1():
|
2023-08-07 16:48:48 +00:00
|
|
|
assert compute_checksum(b"somedata", "SHA1") == b"76oxGuRIpzdMEiBhv+2VLZQOnjc="
|
2022-12-06 23:03:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_checksum_crc32():
|
2023-08-07 16:48:48 +00:00
|
|
|
assert compute_checksum(b"somedata", "CRC32") == b"Uwy90A=="
|
2022-12-06 23:03:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_checksum_crc32c():
|
2023-07-19 09:36:38 +00:00
|
|
|
try:
|
|
|
|
import crc32c # noqa # pylint: disable=unused-import
|
|
|
|
|
2023-08-07 16:48:48 +00:00
|
|
|
assert compute_checksum(b"somedata", "CRC32C") == b"dB9qBQ=="
|
2023-07-19 09:36:38 +00:00
|
|
|
except: # noqa: E722 Do not use bare except
|
|
|
|
# Optional library Can't be found - just revert to CRC32
|
2023-08-07 16:48:48 +00:00
|
|
|
assert compute_checksum(b"somedata", "CRC32C") == b"Uwy90A=="
|
2023-03-03 22:40:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_cors_utils():
|
|
|
|
"Fancy string matching"
|
|
|
|
assert cors_matches_origin("a", ["a"])
|
|
|
|
assert cors_matches_origin("b", ["a", "b"])
|
|
|
|
assert not cors_matches_origin("c", [])
|
|
|
|
assert not cors_matches_origin("c", ["a", "b"])
|
|
|
|
|
|
|
|
assert cors_matches_origin("http://www.google.com", ["http://*.google.com"])
|
|
|
|
assert cors_matches_origin("http://www.google.com", ["http://www.*.com"])
|
|
|
|
assert cors_matches_origin("http://www.google.com", ["http://*"])
|
|
|
|
assert cors_matches_origin("http://www.google.com", ["*"])
|
|
|
|
|
|
|
|
assert not cors_matches_origin("http://www.google.com", ["http://www.*.org"])
|
|
|
|
assert not cors_matches_origin("http://www.google.com", ["https://*"])
|