master
go 124 lines 3.99 KB
Raw
1 package ipfs
2
3 import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7 )
8
9 func TestSuffixFromForkPath(t *testing.T) {
10 tests := []struct {
11 name string
12 path string
13 expected string
14 }{
15 {name: "empty", path: "", expected: ""},
16 {name: "upstream", path: "github.com/ipfs/kubo", expected: ""},
17 {name: "github fork", path: "github.com/myorg/kubo", expected: "myorg"},
18 {name: "gitlab fork", path: "gitlab.com/myorg/kubo", expected: "myorg"},
19 {name: "codeberg fork", path: "codeberg.org/myorg/kubo", expected: "myorg"},
20 {name: "bitbucket fork", path: "bitbucket.org/myorg/kubo", expected: "myorg"},
21 {name: "github renamed repo", path: "github.com/myorg/kubo-experimental", expected: "myorg/kubo-experimental"},
22 {name: "unknown host canonical repo", path: "git.example.com/team/kubo", expected: "git.example.com/team"},
23 {name: "unknown host renamed repo", path: "git.example.com/team/kubo-fork", expected: "git.example.com/team/kubo-fork"},
24 {name: "unknown host nested path", path: "git.example.com/group/sub/kubo", expected: "git.example.com/group/sub"},
25 {name: "trailing slash", path: "github.com/myorg/kubo/", expected: "myorg"},
26 {name: "leading slash", path: "/github.com/myorg/kubo", expected: "myorg"},
27 {name: "single segment", path: "kubo", expected: "kubo"},
28 {name: "two segment fork on known host", path: "github.com/kubo", expected: "github.com/kubo"},
29 }
30 for _, tt := range tests {
31 t.Run(tt.name, func(t *testing.T) {
32 assert.Equal(t, tt.expected, suffixFromForkPath(tt.path))
33 })
34 }
35 }
36
37 func TestImplicitAgentSuffix_PrefersBuildOrigin(t *testing.T) {
38 orig := buildOrigin
39 t.Cleanup(func() { buildOrigin = orig })
40
41 buildOrigin = "github.com/myorg/kubo"
42 assert.Equal(t, "myorg", ImplicitAgentSuffix())
43
44 // Falls through to BuildInfo when origin matches upstream or is empty;
45 // BuildInfo.Main.Path is "github.com/ipfs/kubo" during `go test` of this
46 // package, so the implicit suffix is empty.
47 buildOrigin = ""
48 assert.Equal(t, "", ImplicitAgentSuffix())
49
50 buildOrigin = upstreamModulePath
51 assert.Equal(t, "", ImplicitAgentSuffix())
52 }
53
54 // TestGetUserAgentVersion verifies the user agent string used in libp2p
55 // identify and HTTP requests. Tagged release builds (where the commit matches
56 // the tag) skip the commit hash from the agent version, since the version
57 // number already identifies the exact source.
58 func TestGetUserAgentVersion(t *testing.T) {
59 origCommit := CurrentCommit
60 origTagged := taggedRelease
61 origSuffix := userAgentSuffix
62 t.Cleanup(func() {
63 CurrentCommit = origCommit
64 taggedRelease = origTagged
65 userAgentSuffix = origSuffix
66 })
67
68 tests := []struct {
69 name string
70 commit string
71 tagged string
72 suffix string
73 expected string
74 }{
75 // dev builds without ldflags
76 {
77 name: "no commit, no suffix",
78 expected: "kubo/" + CurrentVersionNumber,
79 },
80 // dev builds with commit set via ldflags
81 {
82 name: "with commit",
83 commit: "abc1234",
84 expected: "kubo/" + CurrentVersionNumber + "/abc1234",
85 },
86 {
87 name: "with suffix, no commit",
88 suffix: "test-suffix",
89 expected: "kubo/" + CurrentVersionNumber + "/test-suffix",
90 },
91 {
92 name: "with commit and suffix",
93 commit: "abc1234",
94 suffix: "test-suffix",
95 expected: "kubo/" + CurrentVersionNumber + "/abc1234/test-suffix",
96 },
97 // tagged release builds: commit is redundant because the version
98 // number already maps to an exact git tag, so it is omitted to
99 // save bytes in identify and HTTP user-agent headers.
100 {
101 name: "tagged release ignores commit",
102 commit: "abc1234",
103 tagged: "1",
104 expected: "kubo/" + CurrentVersionNumber,
105 },
106 {
107 name: "tagged release with suffix ignores commit",
108 commit: "abc1234",
109 tagged: "1",
110 suffix: "test-suffix",
111 expected: "kubo/" + CurrentVersionNumber + "/test-suffix",
112 },
113 }
114
115 for _, tt := range tests {
116 t.Run(tt.name, func(t *testing.T) {
117 CurrentCommit = tt.commit
118 taggedRelease = tt.tagged
119 SetUserAgentSuffix(tt.suffix)
120
121 assert.Equal(t, tt.expected, GetUserAgentVersion())
122 })
123 }
124 }