Browse Source

Add more precise error codes and messages.

Break down the 'invalid URI' error code into some more precise error
codes depending on why the URI is invalid.
master
Damien Goutte-Gattat 3 months ago
parent
commit
3f4618b71b
  1. 4
      src/gfsec-errors.h
  2. 10
      src/secretcfg.c
  3. 38
      tests/t-parseuri.c

4
src/gfsec-errors.h

@ -30,3 +30,7 @@ GFSEC_ERROR(CONFIG_INVALID_HASH, -11, _("Invalid hash value"))
GFSEC_ERROR(CONFIG_UNKNOWN_SCHEME, -12, _("Unknown URI scheme"))
GFSEC_ERROR(CONFIG_INVALID_THRESHOLD, -13, _("Invalid threshold"))
GFSEC_ERROR(CONFIG_INVALID_SHARENR, -14, _("Invalid share parameter"))
GFSEC_ERROR(CONFIG_INVALID_URI_PATH, -15, _("Invalid path in share URI"))
GFSEC_ERROR(CONFIG_INVALID_URI_PARAM, -16, _("Invalid parameter in share URI"))
GFSEC_ERROR(CONFIG_MISSING_AUTHORITY, -17, _("Missing authority in share URI"))
GFSEC_ERROR(CONFIG_MISSING_SHARENR, -18, _("Missing share number"))

10
src/secretcfg.c

@ -118,7 +118,7 @@ parse_path(const char **uri, char **path)
qm = strchrnul(*uri, '?');
if ( qm == *uri || *(qm - 1) == '/' )
return GFSEC_ERR_CONFIG_INVALID_URI;
return GFSEC_ERR_CONFIG_INVALID_URI_PATH;
*path = xstrndup(*uri, qm - *uri);
*uri = qm;
@ -216,10 +216,10 @@ parse_parameter(const char **uri, gfsec_share_t *share)
amp = strchrnul(*uri, '&');
if ( ! (eq = strchr(*uri, '=')) || amp < eq )
return GFSEC_ERR_CONFIG_INVALID_URI; /* Missing value. */
return GFSEC_ERR_CONFIG_INVALID_URI_PARAM; /* Missing value. */
if ( *(eq + 1) == '\0' || *(eq + 1) == '&' )
return GFSEC_ERR_CONFIG_INVALID_URI; /* Empty value. */
return GFSEC_ERR_CONFIG_INVALID_URI_PARAM; /* Empty value. */
if ( strncmp(*uri, "share", eq - *uri) == 0 ) {
eq += 1;
@ -319,7 +319,7 @@ gfsec_parse_uri(const char *uri, gfsec_secret_t *secret, int assign)
rc = parse_authority(&p, &(share->authority));
if ( ! share->authority && share->scheme != GFSEC_SCHEME_FILE )
rc = GFSEC_ERR_CONFIG_INVALID_URI;
rc = GFSEC_ERR_CONFIG_MISSING_AUTHORITY;
if ( rc == 0 )
rc = parse_path(&p, &(share->path));
@ -329,7 +329,7 @@ gfsec_parse_uri(const char *uri, gfsec_secret_t *secret, int assign)
if ( rc == 0 && share->number == GFSEC_SHARE_NUMBER_AUTOASSIGN && ! assign ) {
if ( (share->number = get_share_number_from_path(share->path)) == 0 )
rc = GFSEC_ERR_CONFIG_INVALID_URI;
rc = GFSEC_ERR_CONFIG_MISSING_SHARENR;
}
if ( rc == 0 )

38
tests/t-parseuri.c

@ -67,19 +67,19 @@ struct test tests[] = {
{ "scheme:///path/to/share.123", GFSEC_ERR_CONFIG_UNKNOWN_SCHEME },
/* Empty authority. */
{ "uuid:///", GFSEC_ERR_CONFIG_INVALID_URI },
{ "mtp:///", GFSEC_ERR_CONFIG_INVALID_URI },
{ "label:///", GFSEC_ERR_CONFIG_INVALID_URI },
{ "http:///", GFSEC_ERR_CONFIG_INVALID_URI },
{ "https:///", GFSEC_ERR_CONFIG_INVALID_URI },
{ "uuid:///", GFSEC_ERR_CONFIG_MISSING_AUTHORITY },
{ "mtp:///", GFSEC_ERR_CONFIG_MISSING_AUTHORITY },
{ "label:///", GFSEC_ERR_CONFIG_MISSING_AUTHORITY },
{ "http:///", GFSEC_ERR_CONFIG_MISSING_AUTHORITY },
{ "https:///", GFSEC_ERR_CONFIG_MISSING_AUTHORITY },
/* Empty path. */
{ "file:///", GFSEC_ERR_CONFIG_INVALID_URI },
{ "file:///?param=value", GFSEC_ERR_CONFIG_INVALID_URI },
{ "file:///", GFSEC_ERR_CONFIG_INVALID_URI_PATH },
{ "file:///?param=value", GFSEC_ERR_CONFIG_INVALID_URI_PATH },
/* Path ending with a directory separator. */
{ "file:///path/", GFSEC_ERR_CONFIG_INVALID_URI },
{ "file:///path/?param=value", GFSEC_ERR_CONFIG_INVALID_URI },
{ "file:///path/", GFSEC_ERR_CONFIG_INVALID_URI_PATH },
{ "file:///path/?param=value", GFSEC_ERR_CONFIG_INVALID_URI_PATH },
/* Several parameters. */
{ "file:///path/to/share.123?param1=val1&param2=val2&param3=val3",
@ -94,15 +94,15 @@ struct test tests[] = {
0, GFSEC_SCHEME_FILE, NULL, "/path/to/share.123", 124, 0 },
/* Parameter with no value. */
{ "file:///path/to/share.123?param", GFSEC_ERR_CONFIG_INVALID_URI },
{ "file:///path/to/share.123?param", GFSEC_ERR_CONFIG_INVALID_URI_PARAM },
{ "file:///path/to/share.123?param1=val1&param2",
GFSEC_ERR_CONFIG_INVALID_URI },
GFSEC_ERR_CONFIG_INVALID_URI_PARAM },
{ "file:///path/to/share.123?param1=val1&param2&param3=val",
GFSEC_ERR_CONFIG_INVALID_URI },
GFSEC_ERR_CONFIG_INVALID_URI_PARAM },
/* Parameter with empty value. */
{ "file:///path/to/share.123?param=", GFSEC_ERR_CONFIG_INVALID_URI },
{ "file:///path/to/share.123?param1=&param2=val", GFSEC_ERR_CONFIG_INVALID_URI },
{ "file:///path/to/share.123?param=", GFSEC_ERR_CONFIG_INVALID_URI_PARAM },
{ "file:///path/to/share.123?param1=&param2=val", GFSEC_ERR_CONFIG_INVALID_URI_PARAM },
/* Handling the share parameter. */
{ "file:///path/to/share?share=no",
@ -135,11 +135,11 @@ struct test tests[] = {
GFSEC_ERR_CONFIG_INVALID_HASH },
/* Invalid or missing share number in path. */
{ "file:///path/to/share", GFSEC_ERR_CONFIG_INVALID_URI },
{ "file:///path/to/share.2", GFSEC_ERR_CONFIG_INVALID_URI },
{ "file:///path/to/share.000", GFSEC_ERR_CONFIG_INVALID_URI },
{ "file:///path/to/share.256", GFSEC_ERR_CONFIG_INVALID_URI },
{ "file:///path/to/share.abc", GFSEC_ERR_CONFIG_INVALID_URI },
{ "file:///path/to/share", GFSEC_ERR_CONFIG_MISSING_SHARENR },
{ "file:///path/to/share.2", GFSEC_ERR_CONFIG_MISSING_SHARENR },
{ "file:///path/to/share.000", GFSEC_ERR_CONFIG_MISSING_SHARENR },
{ "file:///path/to/share.256", GFSEC_ERR_CONFIG_MISSING_SHARENR },
{ "file:///path/to/share.abc", GFSEC_ERR_CONFIG_MISSING_SHARENR },
{ NULL }
};

Loading…
Cancel
Save