From f201b971d80e4020078dcec9a1c85d4d0a2cd02b Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Thu, 20 Dec 2018 23:25:23 +0100 Subject: test: add tests for Unicode paths in Lua and tshark -G folders Check for potential Unicode-related problems on Windows. Change-Id: I147c07749c5073a9ae00f07914dd80347d17c40f Ping-Bug: 15118 Reviewed-on: https://code.wireshark.org/review/31154 Tested-by: Petri Dish Buildbot Petri-Dish: Peter Wu Reviewed-by: Anders Broman --- test/fixtures_ws.py | 20 ++++++++++++++++++- test/lua/unicode.lua | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ test/suite_clopts.py | 7 +++++++ test/suite_wslua.py | 38 ++++++++++++++++++++++++++++++++++++ 4 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 test/lua/unicode.lua diff --git a/test/fixtures_ws.py b/test/fixtures_ws.py index b2e3fc8ff5..cfadaf0ab8 100644 --- a/test/fixtures_ws.py +++ b/test/fixtures_ws.py @@ -74,7 +74,7 @@ def program(program_path): dotexe = '' if sys.platform.startswith('win32'): dotexe = '.exe' - path = os.path.normpath(os.path.join(program_path, name + dotexe)) + path = os.path.abspath(os.path.join(program_path, name + dotexe)) if not os.access(path, os.X_OK): fixtures.skip('Program %s is not available' % (name,)) return path @@ -257,3 +257,21 @@ def test_env(base_env, conf_path, request, dirs): # Inject the test environment as default if it was not overridden. request.instance.injected_test_env = env return env + + +@fixtures.fixture +def unicode_env(home_path, make_env): + '''A Wireshark configuration directory with Unicode in its path.''' + home_env = 'APPDATA' if sys.platform.startswith('win32') else 'HOME' + uni_home = os.path.join(home_path, 'unicode-Ф-€-中-testcases') + env = make_env(home=uni_home) + if sys.platform == 'win32': + pluginsdir = os.path.join(uni_home, 'Wireshark', 'plugins') + else: + pluginsdir = os.path.join(uni_home, '.local/lib/wireshark/plugins') + os.makedirs(pluginsdir) + return types.SimpleNamespace( + path=lambda *args: os.path.join(uni_home, *args), + env=env, + pluginsdir=pluginsdir + ) diff --git a/test/lua/unicode.lua b/test/lua/unicode.lua new file mode 100644 index 0000000000..3510e574a6 --- /dev/null +++ b/test/lua/unicode.lua @@ -0,0 +1,55 @@ +-- +-- Unicode tests +-- + +local errors = 0 + +function assertEqual(what, a, b) + if a == b then + return true + end + print('ERROR:', what) + print('Expected:', tostring(a)) + print(' Actual:', tostring(b)) + errors = errors + 1 +end + +-- script name check +local scriptname = (debug.getinfo(1, 'S').source or ''):gsub("^@.*[/\\]", "") +assertEqual('script name', 'script-Ф-€-中.lua', scriptname) + +-- loadfile +local code, err = loadfile('load-Ф-€-中.lua') +assertEqual('loadfile', nil, err) +assertEqual('loadfile contents', 'Contents of Ф-€-中', code and code()) + +-- dofile +local ok, result = pcall(dofile, 'load-Ф-€-中.lua') +assertEqual('dofile pcall', true, ok) +assertEqual('dofile contents', 'Contents of Ф-€-中', result) + +-- io.open (read) +local fr, err = io.open('load-Ф-€-中.lua') +assertEqual('io.open (read)', nil, err) +assertEqual('io.read', 'return "Contents of Ф-€-中"\n', fr and fr:read('*a')) +if fr then fr:close() end + +-- io.open (write) +local fw, err = io.open('written-by-lua-Ф-€-中.txt', 'w') +assertEqual('io.open (write)', nil, err) +if fw then + local _, err = fw:write('Feedback from Lua: Ф-€-中\n') + assertEqual('io.write', nil, err) +end +if fw then fw:close() end + +-- Check for Unicode in personal plugins directory path. +local pdir_expected = 'unicode-Ф-€-中-testcases' +local pdir = Dir.personal_plugins_path() +pdir = pdir:gsub('.*[/\\]unicode-.*-.*-testcases[/\\].*', pdir_expected) +assertEqual('Unicode in Dir.personal_plugins_path', pdir_expected, pdir) + +if errors ~= 0 then + error('Failed tests: ' .. errors) +end +print("All tests passed!") diff --git a/test/suite_clopts.py b/test/suite_clopts.py index b1e020c560..e598828896 100644 --- a/test/suite_clopts.py +++ b/test/suite_clopts.py @@ -198,6 +198,13 @@ class case_tshark_dump_glossaries(subprocesstest.SubprocessTestCase): del ip_props[key] self.assertEqual(actual_obj, expected_obj) + def test_tshark_unicode_folders(self, cmd_tshark, unicode_env): + '''Folders output with unicode''' + proc = self.assertRun((cmd_tshark, '-G', 'folders'), env=unicode_env.env) + out = proc.stdout_str + pluginsdir = [x.split('\t', 1)[1] for x in out.splitlines() if x.startswith('Personal Lua Plugins:')] + self.assertEqual([unicode_env.pluginsdir], pluginsdir) + @fixtures.mark_usefixtures('test_env') @fixtures.uses_fixtures diff --git a/test/suite_wslua.py b/test/suite_wslua.py index a48f75d596..0119609f7f 100644 --- a/test/suite_wslua.py +++ b/test/suite_wslua.py @@ -11,6 +11,8 @@ import filecmp import os.path +import shutil +import subprocess import subprocesstest import unittest import fixtures @@ -276,3 +278,39 @@ class case_wslua(subprocesstest.SubprocessTestCase): def test_wslua_tvb_no_tree(self, check_lua_script): '''wslua tvb without a tree''' check_lua_script(self, 'tvb.lua', dns_port_pcap, True) + + +@fixtures.uses_fixtures +class case_wslua_unicode(subprocesstest.SubprocessTestCase): + def test_wslua_unicode(self, cmd_tshark, features, dirs, capture_file, unicode_env): + '''Check handling of unicode paths.''' + if not features.have_lua: + self.skipTest('Test requires Lua scripting support.') + + # Prepare test environment, put files in the right places. + uni_script = os.path.join(unicode_env.pluginsdir, 'script-Ф-€-中.lua') + shutil.copy(os.path.join(dirs.lua_dir, 'unicode.lua'), uni_script) + with open(unicode_env.path('load-Ф-€-中.lua'), 'w', encoding='utf8') as f: + f.write('return "Contents of Ф-€-中"\n') + uni_pcap = unicode_env.path('file-Ф-€-中.pcap') + shutil.copy(capture_file('empty.pcap'), uni_pcap) + + # Run process from a Unicode path as working directory. + proc = subprocess.Popen((cmd_tshark, '-r', uni_pcap), env=unicode_env.env, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + cwd=unicode_env.path()) + stdout, stderr = proc.communicate(timeout=60) + stdout_str = stdout.decode('utf8', 'replace') + stderr_str = stderr.decode('utf8', 'replace') + print("-- Begin stdout") + print(stdout_str) + print("-- End stdout") + if stderr_str: + print("-- Begin stderr") + print(stderr_str) + print("-- End stderr") + self.assertIn('All tests passed!', stdout_str) + assert stderr_str == "" + with open(unicode_env.path('written-by-lua-Ф-€-中.txt'), encoding='utf8') as f: + assert f.read() == 'Feedback from Lua: Ф-€-中\n' -- cgit v1.2.3