| 1 | #!/usr/bin/env python2
|
| 2 | # coding=utf8
|
| 3 | # Copyright 2016 Andy Chu. All rights reserved.
|
| 4 | # Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 | # you may not use this file except in compliance with the License.
|
| 6 | # You may obtain a copy of the License at
|
| 7 | #
|
| 8 | # http://www.apache.org/licenses/LICENSE-2.0
|
| 9 | from __future__ import print_function
|
| 10 | """
|
| 11 | libc_test.py: Tests for libc.py
|
| 12 | """
|
| 13 | import unittest
|
| 14 | import sys
|
| 15 |
|
| 16 | import libc # module under test
|
| 17 |
|
| 18 | # guard some tests that fail on Darwin
|
| 19 | IS_DARWIN = sys.platform == 'darwin'
|
| 20 |
|
| 21 | class LibcTest(unittest.TestCase):
|
| 22 |
|
| 23 | def testConstants(self):
|
| 24 | print('GLOB_PERIOD %d' % libc.GLOB_PERIOD)
|
| 25 | print('HAVE_GLOB_PERIOD %d' % libc.HAVE_GLOB_PERIOD)
|
| 26 | print('HAVE_FNM_EXTMATCH %d' % libc.HAVE_FNM_EXTMATCH)
|
| 27 |
|
| 28 | def testFnmatch(self):
|
| 29 |
|
| 30 | cases = [
|
| 31 | # (pattern, string, result)
|
| 32 |
|
| 33 | ('', '', 1), # no pattern is valid
|
| 34 | ('a', 'a', 1),
|
| 35 | ('?', 'a', 1),
|
| 36 |
|
| 37 | # Test escaping of glob operator chars
|
| 38 | ('\\?', '-', 0),
|
| 39 | ('\\?', '?', 1),
|
| 40 |
|
| 41 | ('\\*', '-', 0),
|
| 42 | ('\\*', '*', 1),
|
| 43 |
|
| 44 | ('\\[', '-', 0),
|
| 45 | ('\\[', '[', 1),
|
| 46 |
|
| 47 | ('\\!', '-', 0),
|
| 48 | ('\\!', '!', 1),
|
| 49 |
|
| 50 | # What if we also escape extended glob chars?
|
| 51 | # Extra escaping is OK, so we should ALWAYS escape them.
|
| 52 | ('\\(', '(', 1),
|
| 53 | ('\\(', 'x', 0),
|
| 54 | ('\\(', '\\', 0),
|
| 55 | ('\\(', '\\(', 0),
|
| 56 |
|
| 57 | ('\\|', '|', 1),
|
| 58 | ('\\|', 'x', 0),
|
| 59 |
|
| 60 | ('\\\\', '\\', 1),
|
| 61 | ('\\\\', 'x', 0),
|
| 62 | ('\\\\', '\\extra', 0),
|
| 63 |
|
| 64 | ('\\f', '\\', 0), # no match
|
| 65 |
|
| 66 | # Hm this is weird, c is not a special character
|
| 67 | ('\\c', 'c', 1),
|
| 68 | ('\\c', '\\c', 0),
|
| 69 | ('\\\\c', '\\c', 1), # the proper way to match
|
| 70 |
|
| 71 | ('c:\\foo', 'c:\\foo', 0),
|
| 72 | ('c:\\foo', 'c:foo', 1),
|
| 73 |
|
| 74 | ('strange]one', 'strange]one', 1),
|
| 75 |
|
| 76 | # What is another error? Invalid escape is OK?
|
| 77 | None if IS_DARWIN else ('\\', '\\', 0), # no pattern is valid
|
| 78 |
|
| 79 | ('[[:alpha:]]', 'a', 1),
|
| 80 | ('[^[:alpha:]]', 'a', 0), # negate
|
| 81 | ('[[:alpha:]]', 'aa', 0), # exact match fails
|
| 82 |
|
| 83 | # Combining char class and a literal character
|
| 84 | ('[[:alpha:]7]', '7', 1),
|
| 85 | ('[[:alpha:]][[:alpha:]]', 'az', 1),
|
| 86 |
|
| 87 | ('[a]', 'a', 1),
|
| 88 | # Hm [] is treated as a constant string, not an empty char class.
|
| 89 | # Should we change LooksLikeGlob?
|
| 90 | ('[]', '', 0),
|
| 91 |
|
| 92 | ('[a-z]', 'a', 1),
|
| 93 | ('[a-z]', '-', 0),
|
| 94 |
|
| 95 | # THIS IS INCONSISTENT WITH REGEX!
|
| 96 | # Somehow in regexes (at least ERE) GNU libc treats [a\-z] as [a-z].
|
| 97 | # See below.
|
| 98 | ('[a\-z]', '-', 1),
|
| 99 | ('[a\-z]', 'b', 0),
|
| 100 |
|
| 101 | # Need double backslash in character class
|
| 102 | ('[\\\\]', '\\', 1),
|
| 103 |
|
| 104 | # Can you escape ] with \? Yes in fnmatch
|
| 105 | ('[\\]]', '\\', 0),
|
| 106 | ('[\\]]', ']', 1),
|
| 107 |
|
| 108 |
|
| 109 | None if IS_DARWIN else ('[]', 'a', 0),
|
| 110 | None if IS_DARWIN else ('[]', '[]', 1),
|
| 111 |
|
| 112 | ('?.c', 'a.c', 1),
|
| 113 | ('?.c', 'aa.c', 0),
|
| 114 | # mu character
|
| 115 | ('?.c', '\xce\xbc.c', 1),
|
| 116 | ]
|
| 117 |
|
| 118 | for pat, s, expected in filter(None, cases):
|
| 119 | actual = libc.fnmatch(pat, s)
|
| 120 | self.assertEqual(
|
| 121 | expected, actual, '%r %r -> got %d' % (pat, s, actual))
|
| 122 |
|
| 123 | def testFnmatchExtglob(self):
|
| 124 | # NOTE: We always use FNM_EXTMATCH when available
|
| 125 |
|
| 126 | # With GNU extension.
|
| 127 | cases = [
|
| 128 | # One of these
|
| 129 | ('--@(help|verbose)', '--verbose', 1),
|
| 130 | ('--@(help|verbose)', '--foo', 0),
|
| 131 |
|
| 132 | ('--*(help|verbose)', '--verbose', 1),
|
| 133 | ('--*(help|verbose)', '--', 1),
|
| 134 | ('--*(help|verbose)', '--helpverbose', 1), # Not what we want
|
| 135 |
|
| 136 | ('--+(help|verbose)', '--verbose', 1),
|
| 137 | ('--+(help|verbose)', '--', 0),
|
| 138 | ('--+(help|verbose)', '--helpverbose', 1), # Not what we want
|
| 139 |
|
| 140 | ('--?(help|verbose)', '--verbose', 1),
|
| 141 | ('--?(help|verbose)', '--helpverbose', 0),
|
| 142 |
|
| 143 | # Neither of these
|
| 144 | ('--!(help|verbose)', '--verbose', 0),
|
| 145 |
|
| 146 | # escaping *
|
| 147 | ('@(ab\*)', 'ab*', 1),
|
| 148 | ('@(ab\*)', 'abc', 0),
|
| 149 | # escaping ?
|
| 150 | ('@(ab\?)', 'ab?', 1),
|
| 151 | ('@(ab\?)', 'abc', 0),
|
| 152 |
|
| 153 | # escaping []
|
| 154 | ('@(ab\[\])', 'ab[]', 1),
|
| 155 | ('@(ab\[\])', 'abcd', 0),
|
| 156 |
|
| 157 | # escaping :
|
| 158 | ('@(ab\:)', 'ab:', 1),
|
| 159 | ('@(ab\:)', 'abc', 0),
|
| 160 |
|
| 161 | # escaping a is no-op
|
| 162 | (r'@(\ab)', 'ab', 1),
|
| 163 | (r'@(\ab)', r'\ab', 0),
|
| 164 |
|
| 165 | #('@(ab\|)', 'ab|', 1), # GNU libc bug? THIS SHOULD WORK
|
| 166 |
|
| 167 | # There's no way to escape | in extended glob??? wtf.
|
| 168 | #('@(ab\|)', 'ab', 1),
|
| 169 | #('@(ab\|)', 'ab\\', 1),
|
| 170 | #('@(ab\|)', 'ab\\|', 1),
|
| 171 | ]
|
| 172 | for pat, s, expected in cases:
|
| 173 | actual = libc.fnmatch(pat, s)
|
| 174 | self.assertEqual(expected, actual,
|
| 175 | "Matching %s against %s: got %s but expected %s" %
|
| 176 | (pat, s, actual, expected))
|
| 177 |
|
| 178 | def testGlob(self):
|
| 179 | print(libc.glob('*.py', 0))
|
| 180 |
|
| 181 | # This will not match anything!
|
| 182 | print(libc.glob('\\', 0))
|
| 183 | # This one will match a file named \
|
| 184 | print(libc.glob('\\\\', 0))
|
| 185 | print(libc.glob('[[:punct:]]', 0))
|
| 186 |
|
| 187 | # core/util_test.py has more tests like this, for util.RegexSearch()
|
| 188 |
|
| 189 | def testRegexSearch(self):
|
| 190 | # Oh it's a PRECEDENCE problem?
|
| 191 | # leftMatch() is not implemented correctly
|
| 192 | #pat = '^([0-9])|([a-z])'
|
| 193 |
|
| 194 | pat = '^([0-9])|^([a-z])'
|
| 195 |
|
| 196 | lines = 'one\n2\nthree\n'
|
| 197 |
|
| 198 | pos = 3
|
| 199 | indices = libc.regex_search(pat, 0, lines, 0, pos)
|
| 200 |
|
| 201 | self.assertEqual(None, indices)
|
| 202 |
|
| 203 | def testRegexSearchError(self):
|
| 204 | try:
|
| 205 | libc.regex_search(r'*', 0, 'abcd', 0)
|
| 206 | except ValueError as e:
|
| 207 | print(e)
|
| 208 | else:
|
| 209 | self.fail('Expected ValueError')
|
| 210 |
|
| 211 | def testRegexSearchUnicode(self):
|
| 212 | self.assertEqual([0, 1], libc.regex_search(r'.', 0, 'a', 0))
|
| 213 | self.assertEqual([0, 1], libc.regex_search(r'.', 0, '\x7f', 0))
|
| 214 |
|
| 215 | # dot matches both bytes of utf-8 encoded MU char - appears independent of LANG=C LC_ALL=C
|
| 216 | self.assertEqual([0, 2], libc.regex_search(r'.', 0, '\xce\xbc', 0))
|
| 217 |
|
| 218 | # Literal
|
| 219 | self.assertEqual([0, 2], libc.regex_search('\xce\xbc', 0, '\xce\xbc', 0))
|
| 220 |
|
| 221 | # literal mu in char class allowed?
|
| 222 | self.assertEqual([0, 2], libc.regex_search('[\xce\xbc]', 0, '\xce\xbc', 0))
|
| 223 | # two bytes here
|
| 224 | self.assertEqual(None, libc.regex_search('[\xce\xbc]', 0, '\xce', 0))
|
| 225 |
|
| 226 | # dot doesn't match high byte? not utf-8
|
| 227 | self.assertEqual(None, libc.regex_search(r'.', 0, '\xce', 0))
|
| 228 |
|
| 229 | # [a] matches a
|
| 230 | self.assertEqual([0, 1], libc.regex_search(r'[a]', 0, 'a', 0))
|
| 231 |
|
| 232 | # \x01 isn't valid syntax
|
| 233 | self.assertEqual(None, libc.regex_search(r'[\x01]', 0, '\x01', 0))
|
| 234 |
|
| 235 | # literal low byte matches
|
| 236 | self.assertEqual([0, 1], libc.regex_search('[\x01]', 0, '\x01', 0))
|
| 237 |
|
| 238 | # literal high byte does NOT match? Why?
|
| 239 | if 0:
|
| 240 | self.assertEqual([0, 1], libc.regex_search('[\xff]', 0, '\xff', 0))
|
| 241 |
|
| 242 | def testRegexFirstGroupMatch(self):
|
| 243 | s='oXooXoooXoX'
|
| 244 | self.assertEqual(
|
| 245 | (1, 3),
|
| 246 | libc.regex_first_group_match('(X.)', s, 0))
|
| 247 |
|
| 248 | # Match from position 3
|
| 249 | self.assertEqual(
|
| 250 | (4, 6),
|
| 251 | libc.regex_first_group_match('(X.)', s, 3))
|
| 252 |
|
| 253 | # Match from position 3
|
| 254 | self.assertEqual(
|
| 255 | (8, 10),
|
| 256 | libc.regex_first_group_match('(X.)', s, 6))
|
| 257 |
|
| 258 | # Syntax Error
|
| 259 | self.assertRaises(
|
| 260 | RuntimeError, libc.regex_first_group_match, r'*', 'abcd', 0)
|
| 261 |
|
| 262 | def testRegexFirstGroupMatchError(self):
|
| 263 | # Helping to debug issue #291
|
| 264 | s = ''
|
| 265 | if 0:
|
| 266 | # Invalid regex syntax
|
| 267 | libc.regex_first_group_match("(['+-'])", s, 6)
|
| 268 |
|
| 269 | def testSpecialCharsInCharClass(self):
|
| 270 | CASES = [
|
| 271 | ("([a-z]+)", '123abc123', (3, 6)),
|
| 272 |
|
| 273 | # Uh what the heck, \- means the same thing as -? It's just ignored. At
|
| 274 | # least in GNU libc.
|
| 275 |
|
| 276 | # https://stackoverflow.com/questions/28495913/how-do-you-escape-a-hyphen-as-character-range-in-a-posix-regex
|
| 277 | # The <hyphen> character shall be treated as itself if it occurs first (after an initial '^', if any) or last in the list, or as an ending range point in a range expression
|
| 278 |
|
| 279 | ("([a\-z]+)", '123abc123', (3, 6)),
|
| 280 |
|
| 281 | # This is an inverted range. TODO: Need to fix the error message.
|
| 282 | #("([a\-.]+)", '123abc123', None),
|
| 283 |
|
| 284 | ("([\\\\]+)", 'a\\b', (1, 2)),
|
| 285 |
|
| 286 | # Can you escape ] with \? Yes in fnmatch, but NO here!!!
|
| 287 | ('([\\]])', '\\', None),
|
| 288 | ('([\\]])', ']', None),
|
| 289 |
|
| 290 | # Weird parsing!!!
|
| 291 | ('([\\]])', '\\]', (0, 2)),
|
| 292 |
|
| 293 | ]
|
| 294 |
|
| 295 | for pat, s, expected in CASES:
|
| 296 | result = libc.regex_first_group_match(pat, s, 0)
|
| 297 | self.assertEqual(expected, result,
|
| 298 | "FAILED: pat %r s %r result %s" % (pat, s, result))
|
| 299 |
|
| 300 | def testRealpathFailOnNonexistentDirectory(self):
|
| 301 | # This behaviour is actually inconsistent with GNU readlink,
|
| 302 | # but matches behaviour of busybox readlink
|
| 303 | # (https://github.com/jgunthorpe/busybox)
|
| 304 | self.assertEqual(None, libc.realpath('_tmp/nonexistent'))
|
| 305 |
|
| 306 | # Consistent with GNU
|
| 307 | self.assertEqual(None, libc.realpath('_tmp/nonexistent/supernonexistent'))
|
| 308 |
|
| 309 | def testPrintTime(self):
|
| 310 | print('', file=sys.stderr)
|
| 311 | libc.print_time(0.1, 0.2, 0.3)
|
| 312 | print('', file=sys.stderr)
|
| 313 |
|
| 314 | def testGethostname(self):
|
| 315 | print(libc.gethostname())
|
| 316 |
|
| 317 | def testGetTerminalWidth(self):
|
| 318 | try:
|
| 319 | width = libc.get_terminal_width()
|
| 320 | except IOError as e:
|
| 321 | print('error getting terminal width: %s' % e)
|
| 322 | else:
|
| 323 | print('width % d' % width)
|
| 324 |
|
| 325 | def testWcsWidth(self):
|
| 326 | if not IS_DARWIN:
|
| 327 | self.assertEqual(1, libc.wcswidth("▶️"))
|
| 328 | self.assertEqual(28, libc.wcswidth("(osh) ~/.../unchanged/oil ▶️ "))
|
| 329 |
|
| 330 | mu = u"\u03bc".encode('utf-8')
|
| 331 | print(repr(mu))
|
| 332 | print(mu)
|
| 333 | print(len(mu))
|
| 334 | self.assertEqual(1, libc.wcswidth(mu))
|
| 335 |
|
| 336 | self.assertEqual(2, libc.wcswidth("→ "))
|
| 337 |
|
| 338 | # mbstowcs fails on invalid utf-8
|
| 339 | try:
|
| 340 | # first byte of mu
|
| 341 | libc.wcswidth("\xce")
|
| 342 | except UnicodeError as e:
|
| 343 | self.assertEqual('mbstowcs() 1', e.message)
|
| 344 | else:
|
| 345 | self.fail('Expected failure')
|
| 346 |
|
| 347 | # wcswidth fails on unprintable character
|
| 348 | try:
|
| 349 | libc.wcswidth("\x01")
|
| 350 | except UnicodeError as e:
|
| 351 | self.assertEqual('wcswidth()', e.message)
|
| 352 | else:
|
| 353 | self.fail('Expected failure')
|
| 354 |
|
| 355 | self.assertRaises(UnicodeError, libc.wcswidth, "\xfe")
|
| 356 |
|
| 357 | def testSleepUntilError(self):
|
| 358 | try:
|
| 359 | bad = libc.sleep_until_error(None)
|
| 360 | except TypeError:
|
| 361 | print('ok')
|
| 362 | else:
|
| 363 | self.fail('Expected TypeError')
|
| 364 |
|
| 365 | result = libc.sleep_until_error(0.001)
|
| 366 | self.assertEqual(0, result)
|
| 367 |
|
| 368 | # Not testing errno case
|
| 369 |
|
| 370 |
|
| 371 | if __name__ == '__main__':
|
| 372 | # To simulate the OVM_MAIN patch in pythonrun.c
|
| 373 | libc.cpython_reset_locale()
|
| 374 | unittest.main()
|
| 375 |
|
| 376 | # vim: ts=2 sw=2
|