test: add tests for Unicode paths in Lua and tshark -G folders
authorPeter Wu <peter@lekensteyn.nl>
Thu, 20 Dec 2018 22:25:23 +0000 (23:25 +0100)
committerAnders Broman <a.broman58@gmail.com>
Sat, 29 Dec 2018 08:27:58 +0000 (08:27 +0000)
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 <peter@lekensteyn.nl>
Reviewed-by: Anders Broman <a.broman58@gmail.com>
test/fixtures_ws.py
test/lua/unicode.lua [new file with mode: 0644]
test/suite_clopts.py
test/suite_wslua.py

index b2e3fc8ff51f5baff5d7a505252ce0416e22b31a..cfadaf0ab8bdbe886deff7a3eb80ade094aa9c49 100644 (file)
@@ -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 (file)
index 0000000..3510e57
--- /dev/null
@@ -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!")
index b1e020c560f2e73e79a974bf8a0410a7846420ce..e598828896bf4fe58cdef68e42a3957d1fd0a0fc 100644 (file)
@@ -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
index a48f75d5961ca5508677700c3a18503d5bf0a748..0119609f7f637bacf06b3fd93b7d1740b66fff71 100644 (file)
@@ -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'