-
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 524f2b7
Showing
4 changed files
with
129 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,34 @@ | ||
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." | ||
|
||
front = True | ||
|
||
for i in range(len(name)): | ||
c = name[i] | ||
|
||
if ("a" <= c <= "z") or ("A" <= c <= "Z"): | ||
front = False | ||
continue | ||
|
||
if ("0" <= c <= "9") or c == "_": | ||
if not front: | ||
continue | ||
else: | ||
if c == "_": | ||
return f"{prefix}the character '_' cannot be the first character in a package name segment." | ||
else: | ||
return f"{prefix}a digit cannot be the first character in a package name segment." | ||
|
||
if c == ".": | ||
front = True | ||
continue | ||
|
||
return f"{prefix}the character '{c}' is not allowed in Android application package names." | ||
|
||
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,66 @@ | ||
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 character '$' is not allowed in Android application package names." | ||
self.assertEqual(result, expected_error) | ||
|
||
def test_valid_package_name(self): | ||
package_names = ( | ||
"com.zenstudios.zenpinball", | ||
"com.kiloo.subwaysurf", | ||
"jp.mixi.monsterstrike", | ||
"com.outfit7.talkingtom", | ||
"com.supercell.clashofclans", | ||
"com.bandainamcoent.narutoblazingna", | ||
"com.zeptolab.ctr2.f2p.google", | ||
"com.etermax.preguntados.lite", | ||
"com.nianticlabs.pokemongo", | ||
"com.king.candycrushsaga", | ||
"com.ea.game.pvzfree_row", | ||
"com.rovio.angrybirdsspace.premium", | ||
"com.squareenixmontreal.lcgo", | ||
"com.nintendo.zara", | ||
"com.nway.powerrangerslegacywars", | ||
"com.mojang.minecraftpe", | ||
"com.tencent.ig", | ||
"com.ubisoft.hungrysharkworld", | ||
"com.ea.games.simsfreeplay_na", | ||
"com.bethsoft.falloutshelter", | ||
) | ||
|
||
for case in package_names: | ||
result = validate_android_package_name(case) | ||
self.assertIsNone(result) |