-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved validating android package names
- Loading branch information
1 parent
af0764c
commit a6e2874
Showing
4 changed files
with
122 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import re | ||
|
||
|
||
def validate_android_package_name(name: str) -> str | None: | ||
prefix = "Invalid Android package name - " | ||
|
||
if not name.strip(): | ||
return f"{prefix}package name is missing." | ||
|
||
if "." not in name: | ||
return f"{prefix}the package name must have at least one '.' separator." | ||
|
||
if name[-1] == ".": | ||
return f"{prefix}the package name cannot end in a '.' separator." | ||
|
||
segments = name.split(".") | ||
if any(segment == "" for segment in segments): | ||
return f"{prefix}package segments must be of non-zero length." | ||
|
||
if any(segment.startswith("_") for segment in segments): | ||
return f"{prefix}the character '_' cannot be the first character in a package name segment." | ||
|
||
if any(segment[0].isdigit() for segment in segments): | ||
return f"{prefix}a digit cannot be the first character in a package name segment." | ||
|
||
pattern = re.compile(r"[^a-zA-Z0-9._]") | ||
for segment in segments: | ||
if pattern.search(segment): | ||
return f"{prefix}the package name contains not allowed characters." | ||
|
||
return None |
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,63 @@ | ||
from pyxform.validators.pyxform.android_package_name import validate_android_package_name | ||
from tests.pyxform_test_case import PyxformTestCase | ||
|
||
|
||
class TestAndroidPackageNameValidator(PyxformTestCase): | ||
def test_empty_package_name(self): | ||
result = validate_android_package_name("") | ||
self.assertEqual( | ||
result, "Invalid Android package name - package name is missing." | ||
) | ||
|
||
def test_blank_package_name(self): | ||
result = validate_android_package_name(" ") | ||
self.assertEqual( | ||
result, "Invalid Android package name - package name is missing." | ||
) | ||
|
||
def test_missing_separator(self): | ||
result = validate_android_package_name("comexampleapp") | ||
self.assertEqual( | ||
result, | ||
"Invalid Android package name - the package name must have at least one '.' separator.", | ||
) | ||
|
||
def test_invalid_start_with_underscore(self): | ||
result = validate_android_package_name("_com.example.app") | ||
expected_error = "Invalid Android package name - the character '_' cannot be the first character in a package name segment." | ||
self.assertEqual(result, expected_error) | ||
|
||
def test_invalid_start_with_digit(self): | ||
result = validate_android_package_name("1com.example.app") | ||
expected_error = "Invalid Android package name - a digit cannot be the first character in a package name segment." | ||
self.assertEqual(result, expected_error) | ||
|
||
def test_invalid_character(self): | ||
result = validate_android_package_name("com.example.app$") | ||
expected_error = "Invalid Android package name - the package name contains not allowed characters." | ||
self.assertEqual(result, expected_error) | ||
|
||
def test_package_name_segment_with_zero_length(self): | ||
result = validate_android_package_name("com..app") | ||
expected_error = ( | ||
"Invalid Android package name - package segments must be of non-zero length." | ||
) | ||
self.assertEqual(result, expected_error) | ||
|
||
def test_separator_as_last_char_in_package_name(self): | ||
result = validate_android_package_name("com.example.app.") | ||
expected_error = "Invalid Android package name - the package name cannot end in a '.' separator." | ||
self.assertEqual(result, expected_error) | ||
|
||
def test_valid_package_name(self): | ||
package_names = ( | ||
"com.zenstudios.zenpinball", | ||
"com.outfit7.talkingtom", | ||
"com.zeptolab.ctr2.f2p.google", | ||
"com.ea.game.pvzfree_row", | ||
"com.rovio.angrybirdsspace.premium", | ||
) | ||
|
||
for case in package_names: | ||
result = validate_android_package_name(case) | ||
self.assertIsNone(result) |