1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 //    names, trademarks, service marks, or product names of the Licensor
11 //    and its affiliates, except as required to comply with Section 4(c) of
12 //    the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 //     http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 
25 #include "pxr/pxr.h"
26 #include "pxr/base/tf/regTest.h"
27 #include "pxr/base/tf/debugCodes.h"
28 #include "pxr/base/tf/debug.h"
29 #include "pxr/base/tf/stringUtils.h"
30 #include "pxr/base/tf/dl.h"
31 #include "pxr/base/arch/fileSystem.h"
32 #include "pxr/base/arch/library.h"
33 #include "pxr/base/arch/symbols.h"
34 
35 using std::string;
36 PXR_NAMESPACE_USING_DIRECTIVE
37 
38 static bool
Test_TfDl()39 Test_TfDl()
40 {
41     // We should not be in the process of opening/closing a DL right now
42     TF_AXIOM(!Tf_DlOpenIsActive());
43     TF_AXIOM(!Tf_DlCloseIsActive());
44 
45     // Turn on TfDlopen debugging so we get coverage on the debug output too
46     TfDebug::Enable(TF_DLOPEN);
47     TfDebug::Enable(TF_DLCLOSE);
48 
49     // Check that opening a non-existing shared library fails
50     TF_AXIOM(!TfDlopen("nonexisting" ARCH_LIBRARY_SUFFIX, ARCH_LIBRARY_NOW));
51 
52     // Check that TfDlopen fills in our error string with something
53     string dlerror;
54     TfDlopen("nonexisting" ARCH_LIBRARY_SUFFIX, ARCH_LIBRARY_NOW, &dlerror);
55     TF_AXIOM(!dlerror.empty());
56 
57     // Compute path to test library.
58     string dlname;
59     TF_AXIOM(ArchGetAddressInfo((void*)Test_TfDl, &dlname, NULL, NULL, NULL));
60     dlname = TfGetPathName(dlname) +
61         "lib" ARCH_PATH_SEP
62 #if !defined(ARCH_OS_WINDOWS)
63         "lib"
64 #endif
65         "TestTfDl" ARCH_LIBRARY_SUFFIX;
66 
67     // Make sure that this .so does indeed exist first
68     printf("Checking test shared lib: %s\n", dlname.c_str());
69     TF_AXIOM(!ArchFileAccess(dlname.c_str(), R_OK));
70 
71     // Check that we can open the existing library.
72     void *handle =
73         TfDlopen(dlname, ARCH_LIBRARY_LAZY|ARCH_LIBRARY_LOCAL, &dlerror);
74     TF_AXIOM(handle);
75     TF_AXIOM(dlerror.empty());
76     TF_AXIOM(!TfDlclose(handle));
77 
78     // we should not be in the process of opening/closing a DL now either
79     TF_AXIOM(!Tf_DlOpenIsActive());
80     TF_AXIOM(!Tf_DlCloseIsActive());
81 
82     return true;
83 }
84 
85 TF_ADD_REGTEST(TfDl);
86