Commit 03b7118a authored by Siebers, Michael's avatar Siebers, Michael
Browse files

Merge branch '64-endpoint-irrelevant-file-accepts-malformatted-request' into 'master'

Resolve "Endpoint `irrelevant/file` accepts malformatted request"

Closes #65 and #64

See merge request cogsys/dare2del/demonstrator!30
parents 57098570 4c7d5268
...@@ -5,7 +5,10 @@ ...@@ -5,7 +5,10 @@
:- use_module(src/theory_bg). :- use_module(src/theory_bg).
% Error handling % Error handling
:- consult(error_handling). :- ensure_loaded(error_handling).
% Item definition
:- ensure_loaded(type_defs).
/** /**
...@@ -65,24 +68,3 @@ all_items_external(Items) :- ...@@ -65,24 +68,3 @@ all_items_external(Items) :-
all_items_external_(_Key-Value,Item) :- all_items_external_(_Key-Value,Item) :-
is_dict(Value, Type), is_dict(Value, Type),
parse_type(Value, dict([type:atom=Type]), Item, [extra_keys(add)]). parse_type(Value, dict([type:atom=Type]), Item, [extra_keys(add)]).
%! item_type(-Type) is det.
%
% Generates the type definition for an item (that is a file or a directory). Type is a valid type as defined by types:check_type_def/1.
item_type(anyof([DirectoryType,FileType])) :-
CommonKeys=[
abs_path: atom('^(/|.*[^/])$'),
creation_time: integer
],
FileType = dict([
type: atom('^file$'),
file_size: positive_integer,
media_type: atom,
filename_extension: atom,
modification_time: integer,
access_time: integer,
change_time: integer
| CommonKeys
]),
DirectoryType = dict([type:atom('^directory$')|CommonKeys]).
:- ensure_loaded(requests). :- ensure_loaded(requests).
:- ensure_loaded(error_handling). :- ensure_loaded(error_handling).
:- ensure_loaded(type_defs).
:- use_module(src/explanations). :- use_module(src/explanations).
:- multifile user:irrelevant/1. :- multifile user:irrelevant/1.
...@@ -9,7 +10,8 @@ ...@@ -9,7 +10,8 @@
% True if the item associated with the absolute path given in Parameters % True if the item associated with the absolute path given in Parameters
% is irrelevant. The absolute path is accessed using the dict key `abs_path`. % is irrelevant. The absolute path is accessed using the dict key `abs_path`.
% An error is thrown if the dict does not contain this key, if its value % An error is thrown if the dict does not contain this key, if its value
% is not an atom or if the dict contains any other keys. % is not an atom representing an absolute file path, or if the dict contains
% any other keys.
% %
% @throws parse_error(Reason, Location) if Parameters cannot be parsed % @throws parse_error(Reason, Location) if Parameters cannot be parsed
% @error api_error(_, parameter_error) if the format of Parameters is erroneous, % @error api_error(_, parameter_error) if the format of Parameters is erroneous,
...@@ -17,9 +19,10 @@ ...@@ -17,9 +19,10 @@
% value read is unbound. % value read is unbound.
% @error api_error(_, implementation_error) if checking the irrelevance itself % @error api_error(_, implementation_error) if checking the irrelevance itself
% throws an exception. % throws an exception.
do_handle_irrelevant_file(Parameters,AbsPath) :- do_handle_irrelevant_file(Parameters, AbsPath) :-
abs_path_type(AbsPathType),
parse_parameters(Parameters, parse_parameters(Parameters,
dict([abs_path:atom]), dict([abs_path:AbsPathType]),
Parsed), Parsed),
AbsPath=Parsed.abs_path, AbsPath=Parsed.abs_path,
catch(user:irrelevant(AbsPath), catch(user:irrelevant(AbsPath),
...@@ -66,8 +69,9 @@ do_handle_irrelevant_file(Parameters,AbsPath) :- ...@@ -66,8 +69,9 @@ do_handle_irrelevant_file(Parameters,AbsPath) :-
% any value read from Parameters is % any value read from Parameters is
% unbound, or Result or More are bound % unbound, or Result or More are bound
do_handle_explain(Parameters, Result, More) :- do_handle_explain(Parameters, Result, More) :-
abs_path_type(AbsPathType),
parse_parameters(Parameters, parse_parameters(Parameters,
dict([abs_path:atom, limit:positive_integer=5]), dict([abs_path:AbsPathType, limit:positive_integer=5]),
Parsed), Parsed),
( var(Result) ( var(Result)
-> true -> true
......
%! abs_path_type(-Type) is det.
%
% Return the type definition for an absolute path used to identify items. Type
% is a valid type as defined by types:check_type_def/1.
abs_path_type(atom('^(/|.*[^/])$')).
%! item_type(-Type) is det.
%
% Generates the type definition for an item (that is a file or a directory).
% Type is a valid type as defined by types:check_type_def/1.
item_type(anyof([DirectoryType,FileType])) :-
directory_type(DirectoryType),
file_type(FileType).
%! file_type(-Type) is det.
%
% Generates the type definition for a file. Type is a valid type as defined by
% types:check_type_def/1.
file_type(dict([
abs_path: AbsPathType,
type: atom('^file$'),
file_size: positive_integer,
media_type: atom,
filename_extension: atom,
creation_time: integer,
modification_time: integer,
access_time: integer,
change_time: integer
])) :- abs_path_type(AbsPathType).
%! directory_type(-Type) is det.
%
% Generates the type definition for a directory. Type is a valid type as defined
% by types:check_type_def/1.
directory_type(dict([
abs_path: AbsPathType,
creation_time: integer,
type:atom('^directory$')
])) :- abs_path_type(AbsPathType).
...@@ -67,9 +67,12 @@ test(parameter_no_dict, ...@@ -67,9 +67,12 @@ test(parameter_no_dict,
[ [
forall(type_not(dict, OtherTypeValue)), forall(type_not(dict, OtherTypeValue)),
throws(parse_error( throws(parse_error(
type_mismatch(dict([abs_path:atom]), OtherTypeValue), type_mismatch(dict([abs_path:AbsPathType]), OtherTypeValue),
root)), root)),
setup(setup_irrelevance_throws(some_error)), setup((
web_api:abs_path_type(AbsPathType),
setup_irrelevance_throws(some_error)
)),
cleanup(cleanup_irrelevance_throws) cleanup(cleanup_irrelevance_throws)
]) :- ]) :-
web_api:do_handle_irrelevant_file(OtherTypeValue,_). web_api:do_handle_irrelevant_file(OtherTypeValue,_).
...@@ -112,11 +115,13 @@ test(abs_path_wrong_type, ...@@ -112,11 +115,13 @@ test(abs_path_wrong_type,
type_is(dict, RandomDict), type_is(dict, RandomDict),
type_not_atom(OtherTypeValue) type_not_atom(OtherTypeValue)
)), )),
setup((setup_irrelevance_throws(some_error), setup((
put_dict(t{abs_path:OtherTypeValue}, RandomDict, Dict) web_api:abs_path_type(AbsPathType),
setup_irrelevance_throws(some_error),
put_dict(t{abs_path:OtherTypeValue}, RandomDict, Dict)
)), )),
cleanup(cleanup_irrelevance_throws), cleanup(cleanup_irrelevance_throws),
throws(parse_error(type_mismatch(atom, OtherTypeValue), key(abs_path))) throws(parse_error(type_mismatch(AbsPathType, OtherTypeValue), key(abs_path)))
]) :- ]) :-
web_api:do_handle_irrelevant_file(Dict,_). web_api:do_handle_irrelevant_file(Dict,_).
...@@ -354,8 +359,12 @@ test(abs_path_value_wrong_type, ...@@ -354,8 +359,12 @@ test(abs_path_value_wrong_type,
another_key:some_atom}) another_key:some_atom})
) )
), ),
throws(parse_error(type_mismatch(atom, OtherTypeValue), key(abs_path))), throws(parse_error(type_mismatch(AbsPathType, OtherTypeValue),
setup(setup_explain_throws(some_error)), key(abs_path))),
setup((
web_api:abs_path_type(AbsPathType),
setup_explain_throws(some_error)
)),
cleanup(cleanup_explain_throws) cleanup(cleanup_explain_throws)
]) :- ]) :-
web_api:do_handle_explain(Parameters,_Result,_More). web_api:do_handle_explain(Parameters,_Result,_More).
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment