| 
									
										
										
										
											2020-11-11 15:54:01 +00:00
										 |  |  | import pytest | 
					
						
							| 
									
										
										
										
											2013-05-03 19:33:13 -04:00
										 |  |  | from sure import expect | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | from moto.s3.utils import ( | 
					
						
							|  |  |  |     bucket_name_from_url, | 
					
						
							|  |  |  |     _VersionedKeyStore, | 
					
						
							|  |  |  |     parse_region_from_url, | 
					
						
							|  |  |  |     clean_key_name, | 
					
						
							|  |  |  |     undo_clean_key_name, | 
					
						
							| 
									
										
										
										
											2022-12-06 22:03:28 -01:00
										 |  |  |     compute_checksum, | 
					
						
							| 
									
										
										
										
											2023-03-03 21:40:55 -01:00
										 |  |  |     cors_matches_origin, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | ) | 
					
						
							| 
									
										
										
										
											2021-08-04 17:24:26 +01:00
										 |  |  | from unittest.mock import patch | 
					
						
							| 
									
										
										
										
											2013-05-03 19:33:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_base_url(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     expect(bucket_name_from_url("https://s3.amazonaws.com/")).should.equal(None) | 
					
						
							| 
									
										
										
										
											2013-05-03 19:33:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_localhost_bucket(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     expect(bucket_name_from_url("https://wfoobar.localhost:5000/abc")).should.equal( | 
					
						
							|  |  |  |         "wfoobar" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2013-05-03 19:33:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_localhost_without_bucket(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     expect(bucket_name_from_url("https://www.localhost:5000/def")).should.equal(None) | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-21 16:33:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-11 20:28:45 -01:00
										 |  |  | def test_force_ignore_subdomain_for_bucketnames(): | 
					
						
							| 
									
										
										
										
											2021-03-26 17:51:19 +01:00
										 |  |  |     with patch("moto.s3.utils.S3_IGNORE_SUBDOMAIN_BUCKETNAME", True): | 
					
						
							|  |  |  |         expect( | 
					
						
							|  |  |  |             bucket_name_from_url("https://subdomain.localhost:5000/abc/resource") | 
					
						
							|  |  |  |         ).should.equal(None) | 
					
						
							| 
									
										
										
										
											2014-06-27 15:37:51 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-10 13:52:53 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-27 15:37:51 -06:00
										 |  |  | def test_versioned_key_store(): | 
					
						
							|  |  |  |     d = _VersionedKeyStore() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     d.should.have.length_of(0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     d["key"] = [1] | 
					
						
							| 
									
										
										
										
											2014-06-27 15:37:51 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     d.should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     d["key"] = 2 | 
					
						
							| 
									
										
										
										
											2014-06-27 15:37:51 -06:00
										 |  |  |     d.should.have.length_of(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     d.should.have.key("key").being.equal(2) | 
					
						
							| 
									
										
										
										
											2014-06-27 15:37:51 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     d.get.when.called_with("key").should.return_value(2) | 
					
						
							|  |  |  |     d.get.when.called_with("badkey").should.return_value(None) | 
					
						
							|  |  |  |     d.get.when.called_with("badkey", "HELLO").should.return_value("HELLO") | 
					
						
							| 
									
										
										
										
											2014-06-27 15:37:51 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Tests key[ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     d.shouldnt.have.key("badkey") | 
					
						
							|  |  |  |     d.__getitem__.when.called_with("badkey").should.throw(KeyError) | 
					
						
							| 
									
										
										
										
											2014-06-27 15:37:51 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     d.getlist("key").should.have.length_of(2) | 
					
						
							|  |  |  |     d.getlist("key").should.be.equal([[1], 2]) | 
					
						
							|  |  |  |     d.getlist("badkey").should.be.none | 
					
						
							| 
									
										
										
										
											2014-06-27 15:37:51 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     d.setlist("key", 1) | 
					
						
							|  |  |  |     d.getlist("key").should.be.equal([1]) | 
					
						
							| 
									
										
										
										
											2014-06-27 15:37:51 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     d.setlist("key", (1, 2)) | 
					
						
							|  |  |  |     d.getlist("key").shouldnt.be.equal((1, 2)) | 
					
						
							|  |  |  |     d.getlist("key").should.be.equal([1, 2]) | 
					
						
							| 
									
										
										
										
											2014-06-27 15:37:51 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     d.setlist("key", [[1], [2]]) | 
					
						
							|  |  |  |     d["key"].should.have.length_of(1) | 
					
						
							|  |  |  |     d.getlist("key").should.be.equal([[1], [2]]) | 
					
						
							| 
									
										
										
										
											2018-01-30 16:10:43 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_parse_region_from_url(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07: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", | 
					
						
							|  |  |  |     ]: | 
					
						
							| 
									
										
										
										
											2018-01-30 16:10:43 -08:00
										 |  |  |         parse_region_from_url(url).should.equal(expected) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07: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", | 
					
						
							|  |  |  |     ]: | 
					
						
							| 
									
										
										
										
											2018-01-30 16:10:43 -08:00
										 |  |  |         parse_region_from_url(url).should.equal(expected) | 
					
						
							| 
									
										
										
										
											2019-09-24 15:22:25 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-11 15:55:37 +00:00
										 |  |  | @pytest.mark.parametrize( | 
					
						
							|  |  |  |     "key,expected", | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     [ | 
					
						
							|  |  |  |         ("foo/bar/baz", "foo/bar/baz"), | 
					
						
							|  |  |  |         ("foo", "foo"), | 
					
						
							|  |  |  |         ( | 
					
						
							|  |  |  |             "foo/run_dt%3D2019-01-01%252012%253A30%253A00", | 
					
						
							|  |  |  |             "foo/run_dt=2019-01-01%2012%3A30%3A00", | 
					
						
							|  |  |  |         ), | 
					
						
							| 
									
										
										
										
											2020-11-11 15:55:37 +00:00
										 |  |  |     ], | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | ) | 
					
						
							| 
									
										
										
										
											2019-09-24 15:22:25 -05:00
										 |  |  | def test_clean_key_name(key, expected): | 
					
						
							| 
									
										
										
										
											2019-09-24 17:07:58 -05:00
										 |  |  |     clean_key_name(key).should.equal(expected) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-11 15:55:37 +00:00
										 |  |  | @pytest.mark.parametrize( | 
					
						
							|  |  |  |     "key,expected", | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     [ | 
					
						
							|  |  |  |         ("foo/bar/baz", "foo/bar/baz"), | 
					
						
							|  |  |  |         ("foo", "foo"), | 
					
						
							|  |  |  |         ( | 
					
						
							|  |  |  |             "foo/run_dt%3D2019-01-01%252012%253A30%253A00", | 
					
						
							|  |  |  |             "foo/run_dt%253D2019-01-01%25252012%25253A30%25253A00", | 
					
						
							|  |  |  |         ), | 
					
						
							| 
									
										
										
										
											2020-11-11 15:55:37 +00:00
										 |  |  |     ], | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | ) | 
					
						
							| 
									
										
										
										
											2019-09-24 17:07:58 -05:00
										 |  |  | def test_undo_clean_key_name(key, expected): | 
					
						
							|  |  |  |     undo_clean_key_name(key).should.equal(expected) | 
					
						
							| 
									
										
										
										
											2022-12-06 22:03:28 -01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_checksum_sha256(): | 
					
						
							|  |  |  |     checksum = b"ODdkMTQ5Y2I0MjRjMDM4NzY1NmYyMTFkMjU4OWZiNWIxZTE2MjI5OTIxMzA5ZTk4NTg4NDE5Y2NjYThhNzM2Mg==" | 
					
						
							|  |  |  |     compute_checksum(b"somedata", "SHA256").should.equal(checksum) | 
					
						
							|  |  |  |     # Unknown algorithms fallback to SHA256 for now | 
					
						
							|  |  |  |     compute_checksum(b"somedata", algorithm="unknown").should.equal(checksum) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_checksum_sha1(): | 
					
						
							|  |  |  |     compute_checksum(b"somedata", "SHA1").should.equal( | 
					
						
							|  |  |  |         b"ZWZhYTMxMWFlNDQ4YTczNzRjMTIyMDYxYmZlZDk1MmQ5NDBlOWUzNw==" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_checksum_crc32(): | 
					
						
							|  |  |  |     compute_checksum(b"somedata", "CRC32").should.equal(b"MTM5MzM0Mzk1Mg==") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_checksum_crc32c(): | 
					
						
							|  |  |  |     compute_checksum(b"somedata", "CRC32C").should.equal(b"MTM5MzM0Mzk1Mg==") | 
					
						
							| 
									
										
										
										
											2023-03-03 21:40:55 -01: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://*"]) |