Skip to content


Merge pull request #1 from SebDieBln/SanitizeEntryNames
Browse files Browse the repository at this point in the history
add a test for makeKeyTokens_()
  • Loading branch information
olivierdalang committed Nov 19, 2015
2 parents 313fe6a + 174fbb9 commit 0132a3e
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ ADD_PYTHON_TEST(PyQgsApplication
Expand Down
113 changes: 113 additions & 0 deletions tests/src/python/
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsProject.
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
__author__ = 'Sebastian Dietrich'
__date__ = '19/11/2015'
__copyright__ = 'Copyright 2015, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import qgis

from qgis.core import QgsProject, QgsMessageLog

from utilities import getQgisTestApp, TestCase, unittest


class TestQgsProject(TestCase):

def __init__(self, methodName):
"""Run once on class initialisation."""
unittest.TestCase.__init__(self, methodName)
self.messageCaught = False

def test_makeKeyTokens_(self):
# see for a list of valid characters

invalidTokens = []
validTokens = []

# all test tokens will be generated by prepending or inserting characters to this token
validBase = u"valid";

# some invalid characters, not allowed anywhere in a token
# note that '/' must not be added here because it is taken as a separator by makeKeyTokens_()
invalidChars = u"+*,;<>|!$%()=?#\x01";

# generate the characters that are allowed at the start of a token (and at every other position)
validStartChars = u":_";
charRanges = [
(ord(u'a'), ord(u'z')),
(ord(u'A'), ord(u'Z')),
(0x00F8, 0x02FF),
(0x0370, 0x037D),
(0x037F, 0x1FFF),
(0x200C, 0x200D),
(0x2070, 0x218F),
(0x2C00, 0x2FEF),
(0x3001, 0xD7FF),
(0xF900, 0xFDCF),
(0xFDF0, 0xFFFD),
#(0x10000, 0xEFFFF), while actually valid, these are not yet accepted by makeKeyTokens_()
for r in charRanges:
for c in range(r[0], r[1]):
validStartChars += unichr(c)

# generate the characters that are only allowed inside a token, not at the start
validInlineChars = u"-.\xB7";
charRanges = [
(ord(u'0'), ord(u'9')),
(0x0300, 0x036F),
(0x203F, 0x2040),
for r in charRanges:
for c in range(r[0], r[1]):
validInlineChars += unichr(c)

# test forbidden start characters
for c in invalidChars + validInlineChars:
invalidTokens.append(c + validBase)

# test forbidden inline characters
for c in invalidChars:
invalidTokens.append(validBase[:4] + c + validBase[4:])

# test each allowed start character
for c in validStartChars:
validTokens.append(c + validBase)

# test each allowed inline character
for c in validInlineChars:
validTokens.append(validBase[:4] + c + validBase[4:]);

logger = QgsMessageLog.instance()
prj = QgsProject.instance()

for token in validTokens:
self.messageCaught = False
prj.readEntry("test", token)
myMessage = "valid token '%s' not accepted" % (token)
assert not self.messageCaught, myMessage

for token in invalidTokens:
self.messageCaught = False
prj.readEntry("test", token)
myMessage = "invalid token '%s' accepted" % (token)
assert self.messageCaught, myMessage


def catchMessage(self):
self.messageCaught = True

if __name__ == '__main__':

0 comments on commit 0132a3e

Please sign in to comment.