-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
init: provide a better error message for missing symbols
When the there is a difference in versions between build time and run time libraries for the bindings there might be missing symbols. So provide a slightly better error message when that happens, as the usual message is not to clear. Signed-off-by: Erik Larsson <who+github@cnackers.org>
- Loading branch information
Showing
2 changed files
with
64 additions
and
0 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
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,47 @@ | ||
#!/usr/bin/python3 -u | ||
# SPDX-License-Identifier: BSD-2 | ||
|
||
import sys | ||
import unittest | ||
|
||
|
||
class mocklib: | ||
def __init__(self, msg): | ||
self.msg = msg | ||
|
||
@property | ||
def lib(self): | ||
raise ImportError(self.msg) | ||
|
||
|
||
class ImportTest(unittest.TestCase): | ||
def test_missing_symbol(self): | ||
if "tpm2_pytss" in sys.modules: | ||
del sys.modules["tpm2_pytss"] | ||
sys.modules["tpm2_pytss._libtpm2_pytss"] = mocklib( | ||
"/tmp/tpm2_pytss/_libtpm2_pytss.abi3.so: undefined symbol: test_symbol" | ||
) | ||
with self.assertRaises(ImportError) as e: | ||
import tpm2_pytss | ||
self.assertEqual( | ||
e.exception.msg, | ||
"failed to load tpm2-tss bindigs in /tmp/tpm2_pytss/_libtpm2_pytss.abi3.so" | ||
+ " due to missing symbol test_symbol, ensure that you are using the same" | ||
+ " libraries the python module was built against.", | ||
) | ||
|
||
def test_other_message(self): | ||
if "tpm2_pytss" in sys.modules: | ||
del sys.modules["tpm2_pytss"] | ||
sys.modules["tpm2_pytss._libtpm2_pytss"] = mocklib("I am a teapot") | ||
with self.assertRaises(ImportError) as e: | ||
import tpm2_pytss | ||
self.assertEqual(e.exception.msg, "I am a teapot") | ||
|
||
def test_not_missing_symbol(self): | ||
if "tpm2_pytss" in sys.modules: | ||
del sys.modules["tpm2_pytss"] | ||
sys.modules["tpm2_pytss._libtpm2_pytss"] = mocklib("/bin/ls: not a: teapot") | ||
with self.assertRaises(ImportError) as e: | ||
import tpm2_pytss | ||
self.assertEqual(e.exception.msg, "/bin/ls: not a: teapot") |