[PATCH] erofs-utils: lib: add more tests for s3erofs_prepare_url()
Yifan Zhao
zhaoyifan28 at huawei.com
Fri Oct 31 20:30:37 AEDT 2025
Add more tests varying `path` and `key` to cover real use cases.
Signed-off-by: Yifan Zhao <zhaoyifan28 at huawei.com>
---
lib/remotes/s3.c | 100 +++++++++++++++++++++++++++++++++++++----------
1 file changed, 80 insertions(+), 20 deletions(-)
diff --git a/lib/remotes/s3.c b/lib/remotes/s3.c
index 74cae8b..0f7e1a9 100644
--- a/lib/remotes/s3.c
+++ b/lib/remotes/s3.c
@@ -768,10 +768,10 @@ err_global:
}
#ifdef TEST
-struct s3erofs_prepare_utl_testcase {
+struct s3erofs_prepare_url_testcase {
const char *name;
const char *endpoint;
- const char *bucket;
+ const char *path;
const char *key;
enum s3erofs_url_style url_style;
const char *expected_url;
@@ -779,7 +779,7 @@ struct s3erofs_prepare_utl_testcase {
int expected_ret;
};
-static bool run_s3erofs_prepare_url_test(const struct s3erofs_prepare_utl_testcase *tc)
+static bool run_s3erofs_prepare_url_test(const struct s3erofs_prepare_url_testcase *tc)
{
struct s3erofs_curl_request req = { .method = "GET" };
struct s3erofs_query_params params = { .num = 0 };
@@ -787,7 +787,7 @@ static bool run_s3erofs_prepare_url_test(const struct s3erofs_prepare_utl_testca
printf("Running test: %s\n", tc->name);
- ret = s3erofs_prepare_url(&req, tc->endpoint, tc->bucket, tc->key, ¶ms,
+ ret = s3erofs_prepare_url(&req, tc->endpoint, tc->path, tc->key, ¶ms,
tc->url_style);
if (ret != tc->expected_ret) {
@@ -823,11 +823,11 @@ static bool run_s3erofs_prepare_url_test(const struct s3erofs_prepare_utl_testca
static bool test_s3erofs_prepare_url(void)
{
- struct s3erofs_prepare_utl_testcase tests[] = {
+ struct s3erofs_prepare_url_testcase tests[] = {
{
.name = "Virtual-hosted style with https",
.endpoint = "s3.amazonaws.com",
- .bucket = "my-bucket",
+ .path = "my-bucket",
.key = "path/to/object.txt",
.url_style = S3EROFS_URL_STYLE_VIRTUAL_HOST,
.expected_url =
@@ -838,7 +838,7 @@ static bool test_s3erofs_prepare_url(void)
{
.name = "Path style with https",
.endpoint = "s3.amazonaws.com",
- .bucket = "my-bucket",
+ .path = "my-bucket",
.key = "path/to/object.txt",
.url_style = S3EROFS_URL_STYLE_PATH,
.expected_url =
@@ -849,7 +849,7 @@ static bool test_s3erofs_prepare_url(void)
{
.name = "Virtual-hosted with explicit https://",
.endpoint = "https://s3.us-west-2.amazonaws.com",
- .bucket = "test-bucket",
+ .path = "test-bucket",
.key = "file.bin",
.url_style = S3EROFS_URL_STYLE_VIRTUAL_HOST,
.expected_url =
@@ -860,7 +860,7 @@ static bool test_s3erofs_prepare_url(void)
{
.name = "Path style with explicit http://",
.endpoint = "http://localhost:9000",
- .bucket = "local-bucket",
+ .path = "local-bucket",
.key = "data/file.dat",
.url_style = S3EROFS_URL_STYLE_PATH,
.expected_url =
@@ -871,7 +871,7 @@ static bool test_s3erofs_prepare_url(void)
{
.name = "Virtual-hosted style with key ends with slash",
.endpoint = "http://localhost:9000",
- .bucket = "local-bucket",
+ .path = "local-bucket",
.key = "data/file.dat/",
.url_style = S3EROFS_URL_STYLE_VIRTUAL_HOST,
.expected_url =
@@ -882,7 +882,7 @@ static bool test_s3erofs_prepare_url(void)
{
.name = "Path style with key ends with slash",
.endpoint = "http://localhost:9000",
- .bucket = "local-bucket",
+ .path = "local-bucket",
.key = "data/file.dat/",
.url_style = S3EROFS_URL_STYLE_PATH,
.expected_url =
@@ -893,7 +893,7 @@ static bool test_s3erofs_prepare_url(void)
{
.name = "Virtual-hosted without key",
.endpoint = "s3.amazonaws.com",
- .bucket = "my-bucket",
+ .path = "my-bucket",
.key = NULL,
.url_style = S3EROFS_URL_STYLE_VIRTUAL_HOST,
.expected_url = "https://my-bucket.s3.amazonaws.com/",
@@ -903,17 +903,77 @@ static bool test_s3erofs_prepare_url(void)
{
.name = "Path style without key",
.endpoint = "s3.amazonaws.com",
- .bucket = "my-bucket",
+ .path = "my-bucket",
.key = NULL,
.url_style = S3EROFS_URL_STYLE_PATH,
.expected_url = "https://s3.amazonaws.com/my-bucket",
.expected_canonical = "/my-bucket",
.expected_ret = 0,
},
+ {
+ .name = "Path style bucket ending with slash without key",
+ .endpoint = "s3.amazonaws.com",
+ .path = "bucket/",
+ .key = NULL,
+ .url_style = S3EROFS_URL_STYLE_PATH,
+ .expected_url = "https://s3.amazonaws.com/bucket/",
+ .expected_canonical = "/bucket/",
+ .expected_ret = 0,
+ },
+ {
+ .name = "Virtual-hosted bucket ending with slash without key",
+ .endpoint = "s3.amazonaws.com",
+ .path = "bucket/",
+ .key = NULL,
+ .url_style = S3EROFS_URL_STYLE_VIRTUAL_HOST,
+ .expected_url = "https://bucket.s3.amazonaws.com/",
+ .expected_canonical = "/bucket/",
+ .expected_ret = 0,
+ },
+ {
+ .name = "Path style bucket ending with slash",
+ .endpoint = "s3.amazonaws.com",
+ .path = "bucket/",
+ .key = "object.txt",
+ .url_style = S3EROFS_URL_STYLE_PATH,
+ .expected_url = "https://s3.amazonaws.com/bucket/object.txt",
+ .expected_canonical = "/bucket/object.txt",
+ .expected_ret = 0,
+ },
+ {
+ .name = "Virtual-hosted bucket ending with slash",
+ .endpoint = "s3.amazonaws.com",
+ .path = "bucket/",
+ .key = "object.txt",
+ .url_style = S3EROFS_URL_STYLE_VIRTUAL_HOST,
+ .expected_url = "https://bucket.s3.amazonaws.com/object.txt",
+ .expected_canonical = "/bucket/object.txt",
+ .expected_ret = 0,
+ },
+ {
+ .name = "Path style bucket ending with slash key with slash",
+ .endpoint = "s3.amazonaws.com",
+ .path = "bucket/",
+ .key = "a/b/c/object.txt",
+ .url_style = S3EROFS_URL_STYLE_PATH,
+ .expected_url = "https://s3.amazonaws.com/bucket/a/b/c/object.txt",
+ .expected_canonical = "/bucket/a/b/c/object.txt",
+ .expected_ret = 0,
+ },
+ {
+ .name = "Virtual-hosted bucket ending with slash key with slash",
+ .endpoint = "s3.amazonaws.com",
+ .path = "bucket/",
+ .key = "a/b/c/object.txt",
+ .url_style = S3EROFS_URL_STYLE_VIRTUAL_HOST,
+ .expected_url = "https://bucket.s3.amazonaws.com/a/b/c/object.txt",
+ .expected_canonical = "/bucket/a/b/c/object.txt",
+ .expected_ret = 0,
+ },
{
.name = "Error: NULL endpoint",
.endpoint = NULL,
- .bucket = "my-bucket",
+ .path = "my-bucket",
.key = "file.txt",
.url_style = S3EROFS_URL_STYLE_PATH,
.expected_url = NULL,
@@ -923,7 +983,7 @@ static bool test_s3erofs_prepare_url(void)
{
.name = "Error: NULL bucket",
.endpoint = "s3.amazonaws.com",
- .bucket = NULL,
+ .path = NULL,
.key = "file.txt",
.url_style = S3EROFS_URL_STYLE_PATH,
.expected_url = NULL,
@@ -933,7 +993,7 @@ static bool test_s3erofs_prepare_url(void)
{
.name = "Key with special characters",
.endpoint = "s3.amazonaws.com",
- .bucket = "bucket",
+ .path = "bucket",
.key = "path/to/file-name_v2.0.txt",
.url_style = S3EROFS_URL_STYLE_VIRTUAL_HOST,
.expected_url =
@@ -942,16 +1002,16 @@ static bool test_s3erofs_prepare_url(void)
.expected_ret = 0,
}
};
- bool succ = true;
int i;
+ int pass = 0;
for (i = 0; i < ARRAY_SIZE(tests); ++i) {
- succ &= run_s3erofs_prepare_url_test(&tests[i]);
+ pass += run_s3erofs_prepare_url_test(&tests[i]);
putc('\n', stdout);
}
- printf("Run all %d tests\n", i);
- return succ;
+ printf("Run all %d tests with %d PASSED\n", i, pass);
+ return ARRAY_SIZE(tests) == pass;
}
int main(int argc, char *argv[])
--
2.33.0
More information about the Linux-erofs
mailing list