cache_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // cache_test.go - Dynamic linker cache tests.
  2. // Copyright 2016 Yawning Angel
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a
  5. // copy of this software and associated documentation files (the "Software"),
  6. // to deal in the Software without restriction, including without limitation
  7. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. // and/or sell copies of the Software, and to permit persons to whom the
  9. // Software is furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included
  12. // in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. // DEALINGS IN THE SOFTWARE.
  21. package dynlib
  22. import (
  23. "os"
  24. "runtime"
  25. "testing"
  26. "github.com/stretchr/testify/require"
  27. )
  28. type ctorFn func() (*Cache, error)
  29. func TestCache(t *testing.T) {
  30. // Obtain the path to the test executable.
  31. require := require.New(t)
  32. require.Equal("linux", runtime.GOOS, "os is linux")
  33. fn, err := os.Readlink("/proc/self/exe")
  34. require.NoError(err, "readlink(\"/proc/self/exe\")")
  35. t.Logf("Test binary: %+v", fn)
  36. impls := []struct {
  37. name string
  38. ctor ctorFn
  39. }{
  40. {"glibc", loadCacheGlibc},
  41. {"fallback", loadCacheFallback},
  42. }
  43. for _, impl := range impls {
  44. t.Run(impl.name, func(t *testing.T) {
  45. testCacheImpl(t, impl.ctor, fn)
  46. })
  47. }
  48. }
  49. func testCacheImpl(t *testing.T, ctor ctorFn, fn string) {
  50. require := require.New(t)
  51. c, err := ctor()
  52. require.NoError(err, "Failed to enumerate system libraries")
  53. libs, err := c.ResolveLibraries([]string{fn}, nil, "", "", nil)
  54. require.NoError(err, "Failed to resolve libraries")
  55. t.Logf("Libraries: %+v", libs)
  56. }