-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check if the interface name is too long (LP: #1988749) (#313)
* tests: fix a keyfile test logic for bridges For bridges, the emitter will use the interface name as ID and not the NetworkManager UUID. * parser: warning if the interface name is too long Interfaces names are limited to 15 characters (see IF_NAMESIZE in net/if.h). Currently Netplan will allow the user to use invalid names without a warning. In this case the operation will be ignored by the backends. With this change, Netplan will show a warning but still complete successfully. As there might be users with invalid interface names in their configuration without knowing it, changing the behavior to fail would probably cause problems. * validation: check if netdef is NULL Also, fix apply.py code style to make the linter happy. * ctests: add tests for the new validation function Also add a utility to load the yaml content from strings. * validation: add a comment about IF_NAMSIZE warning * nm: replace undocumented constant with IF_NAMESIZE * ctest:utils: adopt to netplan_error_clear rename Co-authored-by: Lukas Märdian <[email protected]>
- Loading branch information
1 parent
22de574
commit d53d96c
Showing
7 changed files
with
221 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
#include <stdio.h> | ||
#include <stdarg.h> | ||
#include <stddef.h> | ||
#include <setjmp.h> | ||
|
||
#include <cmocka.h> | ||
|
||
#include "netplan.h" | ||
#include "parse.h" | ||
|
||
#undef __USE_MISC | ||
#include "error.c" | ||
#include "names.c" | ||
#include "validation.c" | ||
#include "types.c" | ||
#include "util.c" | ||
#include "parse.c" | ||
|
||
#include "test_utils.h" | ||
|
||
void | ||
test_validate_interface_name_length(void** state) | ||
{ | ||
const char* yaml = | ||
"network:\n" | ||
" version: 2\n" | ||
" bridges:\n" | ||
" ashortname:\n" | ||
" dhcp4: no\n"; | ||
|
||
NetplanState* np_state = load_string_to_netplan_state(yaml); | ||
NetplanStateIterator iter; | ||
NetplanNetDefinition* netdef = NULL; | ||
netplan_state_iterator_init(np_state, &iter); | ||
|
||
netdef = netplan_state_iterator_next(&iter); | ||
|
||
assert_true(validate_interface_name_length(netdef)); | ||
|
||
netplan_state_clear(&np_state); | ||
} | ||
|
||
void | ||
test_validate_interface_name_length_set_name(void** state) | ||
{ | ||
const char* yaml = | ||
"network:\n" | ||
" version: 2\n" | ||
" ethernets:\n" | ||
" eth0:\n" | ||
" match:\n" | ||
" macaddress: aa:bb:cc:dd:ee:ff\n" | ||
" set-name: ashortname\n"; | ||
|
||
NetplanState* np_state = load_string_to_netplan_state(yaml); | ||
NetplanStateIterator iter; | ||
NetplanNetDefinition* netdef = NULL; | ||
netplan_state_iterator_init(np_state, &iter); | ||
|
||
netdef = netplan_state_iterator_next(&iter); | ||
|
||
assert_true(validate_interface_name_length(netdef)); | ||
|
||
netplan_state_clear(&np_state); | ||
} | ||
|
||
void | ||
test_validate_interface_name_length_too_long(void** state) | ||
{ | ||
const char* yaml = | ||
"network:\n" | ||
" version: 2\n" | ||
" bridges:\n" | ||
" averylongnameforaninterface:\n" | ||
" dhcp4: no\n"; | ||
|
||
NetplanState* np_state = load_string_to_netplan_state(yaml); | ||
NetplanStateIterator iter; | ||
NetplanNetDefinition* netdef = NULL; | ||
netplan_state_iterator_init(np_state, &iter); | ||
|
||
netdef = netplan_state_iterator_next(&iter); | ||
|
||
assert_false(validate_interface_name_length(netdef)); | ||
|
||
netplan_state_clear(&np_state); | ||
} | ||
|
||
void | ||
test_validate_interface_name_length_set_name_too_long(void** state) | ||
{ | ||
const char* yaml = | ||
"network:\n" | ||
" version: 2\n" | ||
" ethernets:\n" | ||
" eth0:\n" | ||
" match:\n" | ||
" macaddress: aa:bb:cc:dd:ee:ff\n" | ||
" set-name: averylongnameforaninterface\n"; | ||
|
||
NetplanState* np_state = load_string_to_netplan_state(yaml); | ||
NetplanStateIterator iter; | ||
NetplanNetDefinition* netdef = NULL; | ||
netplan_state_iterator_init(np_state, &iter); | ||
|
||
netdef = netplan_state_iterator_next(&iter); | ||
|
||
assert_false(validate_interface_name_length(netdef)); | ||
|
||
netplan_state_clear(&np_state); | ||
} | ||
|
||
int | ||
setup(void** state) | ||
{ | ||
return 0; | ||
} | ||
|
||
int | ||
tear_down(void** state) | ||
{ | ||
return 0; | ||
} | ||
|
||
int | ||
main() | ||
{ | ||
|
||
const struct CMUnitTest tests[] = { | ||
cmocka_unit_test(test_validate_interface_name_length), | ||
cmocka_unit_test(test_validate_interface_name_length_too_long), | ||
cmocka_unit_test(test_validate_interface_name_length_set_name), | ||
cmocka_unit_test(test_validate_interface_name_length_set_name_too_long), | ||
}; | ||
|
||
return cmocka_run_group_tests(tests, setup, tear_down); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters