Commit graph

26 commits

Author SHA1 Message Date
Christian Boltz
a0e6fbe32a
ProfileStorage: store parent profile
... and extend the tests to get some coverage.
2024-12-17 22:51:11 +01:00
Christian Boltz
cb943e4efc
ProfileStorage: store correct name
Instead of always storing the name of the main profile, store the child
profile/hat name if we are in a child profile or hat.

As a result, we always get the correct "profile xy" header even for
child profiles when dumping the ProfileStorage object.

Also extend the tests to check that the name gets stored correctly.
2024-10-06 14:44:12 +02:00
Georgia Garcia
cec9ae6dff utils: fix coding style to match PEP8
Annotate exceptions with '  # noqa: ERROR'

Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2024-05-20 13:56:37 -03:00
Christian Boltz
eb3550c1b4
ProfileStorage: test invalid type change
... for a type that doesn't have special handling in __setitem__()
2024-05-12 12:37:41 +02:00
Christian Boltz
c5f301f976
ProfileStorage: incldue profile header in __repr__()
ProfileStorage knows a whole profile, therefore it should also include
the profile header in __repr__().

Also add a test for this.
2024-05-12 12:36:09 +02:00
Mark Grassi
c57138f255 Order imports and module-level dunder name assignments. 2022-08-21 11:15:07 -04:00
Mark Grassi
96f7121944 Fix most PEP 8 whitespace, indentation, and major line length violations. 2022-08-21 11:15:07 -04:00
Mark Grassi
7581c9e113 Speed up list creations, and change lists to tuples where appropriate.. 2022-06-26 22:18:56 -04:00
Christian Boltz
e69f46ac3f
ProfileStorage: simplify tests for attachment
attachment is always a str, therefore adjust the test to expect an empty
str ('') instead of None - and later converting that None to ''.
2021-08-16 19:59:05 +02:00
Christian Boltz
337ebcd032
ProfileStorage: Store empty xattrs as empty string
... instead of None.

This avoids the need to allow type changes (None vs. str).

Also adjust the tests accordingly.
2021-08-16 19:59:02 +02:00
Christian Boltz
5d8f66c9e3
get_header() tests: drop write_flags, add xattrs
Drop unused write_flags parameter from AaTest_get_header and
AaTest_get_header_01. This is a cleanup for the previous commit.

While on it, add xattrs parameter to AaTest_get_header, and add two
tests with non-empty xattrs.
2021-05-25 11:31:10 +02:00
Christian Boltz
6e6b94ab57
get_header(): remove always-True write_flags parameter
All the calling code (directly or indirectly) uses write_flags=True,
therefore drop the parameter to simplify the code.

A few tests called get_header() with write_flags=False. Adjust or drop
those tests.

Note: to keep the diff readable, the test changes are as small as
possible. The next commit will cleanup the now-superfluous write_flags
values in the tests.
2021-05-25 11:31:10 +02:00
Christian Boltz
055ca8f857
Add a test to ensure nested childs cause an Exception
... and add some tests for other error conditions that don't imply
nested childs, so that the intended failure gets tested.
(This is probably a leftover of the `hat == profile` -> `hat = None`
(while not in a hat/child profile) change.)
2021-05-16 18:15:03 +02:00
Christian Boltz
42fe65de71
ProfileStorage: merge parse_profile_start() into parse()
... which avoids handing several values around.

Also adjust the tests so that only the end result (of parse() gets
tested.
2021-05-16 18:15:03 +02:00
Christian Boltz
1642fea228
Move parse_profile_start{,_to_storage}() into ProfileStorage
... and make them class functions of ProfileStorage.

parse_profile_start_to_storage() gets renamed to parse().

Also move the tests for parse_profile_start() and
parse_profile_start_to_storage() to test-profile-storage.py.
2021-05-16 18:15:03 +02:00
Steve Beattie
94b7704e56
utils: ProfileStorage - add tests with invalid type
Add tests with invalid type to ensure error handling works as expected.

Merge branch 'cboltz/cboltz-profile-storage-tests'

[Fixed conflict with prior change to utils/test/test-profile-storage.py]
Acked-by: Steve Beattie <steve@nxnw.org>
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/735
2021-04-11 16:54:43 -07:00
Christian Boltz
38c63026a4
ProfileStorage: add tests with invalid type
... to ensure error handling works as expected.
2021-04-11 21:54:36 +02:00
Christian Boltz
6a170ddaa1
Move and rename write_header() to ProfileStorage.get_header()
Also adjust the calling code to use get_header() instead of
write_header().

Finally, move the tests to test-profile-storage.py and do a few
adjustments needed by the change. Part of these adjustments is to hand
over empty params with the correct type instead of just "None".
2021-04-11 15:05:12 +02:00
Christian Boltz
a2d3a382a8
add_or_remove_flag(): allow to add or remove multiple flags
Multiple flags can be given as string (will be split) or as array.

Also add some tests confirming that everything works as expected.
2020-09-24 23:21:54 +02:00
Christian Boltz
e80caa130a
extend add_or_remove_flag() to handle str for old flags
If the old flags are given as str (or None), call split_flags() to
convert them to a list.

This allows to simplify change_profile_flags() which now doesn't need to
call split_flags() on its own.

Also add some tests with a str for the old flags
2018-07-25 20:59:34 +02:00
Christian Boltz
604004c2b6
split off add_or_remove_flag() from change_profile_flags()
Also add some tests for add_or_remove_flag()
2018-07-25 20:44:39 +02:00
Christian Boltz
ce7ea062c5
move splitting flags into profile_storage split_flags() function
... and change change_profile_flags() to use it instead of doing it
itsself

Also add some tests for split_flags()
2018-07-25 20:36:45 +02:00
Christian Boltz
c2a420d32b
let var_transform() sort variable content
This is needed to get a reproducible output.

Also adjust the tests in test-profile-storage.py and add some example
variable to cleanprof.in and cleanprof.out
2018-05-31 22:00:36 +02:00
Christian Boltz
66620f3e19
move several write_* functions to apparmor.profile_storage
ProfileStorage() stores the content of a profile, so it makes sense to
also have the functions to write those rules (including helper functions
used by these functions) in the same file.

Note that I only moved the functions for rule types that are not handled
by *Ruleset classes.

The functions for writing rules stored in a *Ruleset class will
hopefully be superfluous sooner or later (probably later because
serialize_parse_profile_start() depends on them, and rewriting it won't
be easy)

Also move the test for var_transform() to test-profile-storage.py.
2018-05-09 22:23:34 +02:00
Christian Boltz
45922c6d21
make utils tests less verbose
Given the big number of tests, printing a dot for each test (instead of
multiple lines) is enough ;-)
2018-04-08 20:18:30 +02:00
Christian Boltz
325ff61910 [2/3] Make ProfileStorage a class
Move ProfileStorage() from aa.py to the new profile_storage.py and make
it a class. The variable name in __init__() changes (profile -> self.data),
but the content stays the same.

The ProfileStorage class acts like a dict(), but has some additional
checks for unknown keys in place.

Also add some tests to make sure unknown keys really raise an exception.


Acked-by: Seth Arnold <seth.arnold@canonical.com>
2017-07-11 13:32:33 +02:00