1// Copyright 2015 CoreOS, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package dlopen
15
16import (
17	"fmt"
18	"testing"
19)
20
21func checkFailure(shouldSucceed bool, err error) (rErr error) {
22	switch {
23	case err != nil && shouldSucceed:
24		rErr = fmt.Errorf("expected test to succeed, failed unexpectedly: %v", err)
25	case err == nil && !shouldSucceed:
26		rErr = fmt.Errorf("expected test to fail, succeeded unexpectedly")
27	}
28
29	return
30}
31
32func TestDlopen(t *testing.T) {
33	tests := []struct {
34		libs          []string
35		shouldSucceed bool
36	}{
37		{
38			libs: []string{
39				"libc.so.6",
40				"libc.so",
41			},
42			shouldSucceed: true,
43		},
44		{
45			libs: []string{
46				"libstrange.so",
47			},
48			shouldSucceed: false,
49		},
50	}
51
52	for i, tt := range tests {
53		expLen := 4
54		len, err := strlen(tt.libs, "test")
55		if checkFailure(tt.shouldSucceed, err) != nil {
56			t.Errorf("case %d: %v", i, err)
57		}
58
59		if tt.shouldSucceed && len != expLen {
60			t.Errorf("case %d: expected length %d, got %d", i, expLen, len)
61		}
62	}
63}
64