Без опису

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // Copyright 2017 Google Inc. All Rights Reserved.
  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.
  14. package driver
  15. import (
  16. "fmt"
  17. "io/ioutil"
  18. "net"
  19. "net/http"
  20. "net/http/httptest"
  21. "net/url"
  22. "os/exec"
  23. "regexp"
  24. "runtime"
  25. "testing"
  26. "github.com/google/pprof/internal/plugin"
  27. "github.com/google/pprof/profile"
  28. )
  29. func TestWebInterface(t *testing.T) {
  30. prof := makeFakeProfile()
  31. ui := makeWebInterface(prof, &plugin.Options{Obj: fakeObjTool{}})
  32. // Start test server.
  33. server := httptest.NewServer(http.HandlerFunc(
  34. func(w http.ResponseWriter, r *http.Request) {
  35. switch r.URL.Path {
  36. case "/":
  37. ui.dot(w, r)
  38. case "/top":
  39. ui.top(w, r)
  40. case "/disasm":
  41. ui.disasm(w, r)
  42. case "/peek":
  43. ui.peek(w, r)
  44. case "/source":
  45. ui.source(w, r)
  46. }
  47. }))
  48. defer server.Close()
  49. haveDot := false
  50. if _, err := exec.LookPath("dot"); err == nil {
  51. haveDot = true
  52. }
  53. type testCase struct {
  54. path string
  55. want []string
  56. needDot bool
  57. }
  58. testcases := []testCase{
  59. {"/", []string{"F1", "F2", "F3", "testbin", "cpu"}, true},
  60. {"/top", []string{"Flat", "200ms.*100%.*F2"}, false},
  61. {"/source?f=" + url.QueryEscape("F[12]"),
  62. []string{"F1", "F2", "300ms line1"}, false},
  63. {"/peek?f=" + url.QueryEscape("F[12]"),
  64. []string{"300ms.*F1", "200ms.*300ms.*F2"}, false},
  65. {"/disasm?f=" + url.QueryEscape("F[12]"),
  66. []string{"f1:asm", "f2:asm"}, false},
  67. }
  68. for _, c := range testcases {
  69. if c.needDot && !haveDot {
  70. t.Log("skipping", c.path, "since dot (graphviz) does not seem to be installed")
  71. continue
  72. }
  73. res, err := http.Get(server.URL + c.path)
  74. if err != nil {
  75. t.Error("could not fetch", c.path, err)
  76. continue
  77. }
  78. data, err := ioutil.ReadAll(res.Body)
  79. if err != nil {
  80. t.Error("could not read response", c.path, err)
  81. continue
  82. }
  83. result := string(data)
  84. for _, w := range c.want {
  85. if match, _ := regexp.MatchString(w, result); !match {
  86. t.Errorf("response for %s does not match "+
  87. "expected pattern '%s'; "+
  88. "actual result:\n%s", c.path, w, result)
  89. }
  90. }
  91. }
  92. }
  93. // Implement fake object file support.
  94. const addrBase = 0x1000
  95. const fakeSource = "testdata/file1000.src"
  96. type fakeObj struct{}
  97. func (f fakeObj) Close() error { return nil }
  98. func (f fakeObj) Name() string { return "testbin" }
  99. func (f fakeObj) Base() uint64 { return 0 }
  100. func (f fakeObj) BuildID() string { return "" }
  101. func (f fakeObj) SourceLine(addr uint64) ([]plugin.Frame, error) {
  102. return nil, fmt.Errorf("SourceLine unimplemented")
  103. }
  104. func (f fakeObj) Symbols(r *regexp.Regexp, addr uint64) ([]*plugin.Sym, error) {
  105. return []*plugin.Sym{
  106. {[]string{"F1"}, fakeSource, addrBase, addrBase + 10},
  107. {[]string{"F2"}, fakeSource, addrBase + 10, addrBase + 20},
  108. {[]string{"F3"}, fakeSource, addrBase + 20, addrBase + 30},
  109. }, nil
  110. }
  111. type fakeObjTool struct{}
  112. func (obj fakeObjTool) Open(file string, start, limit, offset uint64) (plugin.ObjFile, error) {
  113. return fakeObj{}, nil
  114. }
  115. func (obj fakeObjTool) Disasm(file string, start, end uint64) ([]plugin.Inst, error) {
  116. return []plugin.Inst{
  117. {Addr: addrBase + 0, Text: "f1:asm", Function: "F1"},
  118. {Addr: addrBase + 10, Text: "f2:asm", Function: "F2"},
  119. {Addr: addrBase + 20, Text: "d3:asm", Function: "F3"},
  120. }, nil
  121. }
  122. func makeFakeProfile() *profile.Profile {
  123. // Three functions: F1, F2, F3 with three lines, 11, 22, 33.
  124. funcs := []*profile.Function{
  125. {ID: 1, Name: "F1", Filename: fakeSource, StartLine: 3},
  126. {ID: 2, Name: "F2", Filename: fakeSource, StartLine: 5},
  127. {ID: 3, Name: "F3", Filename: fakeSource, StartLine: 7},
  128. }
  129. lines := []profile.Line{
  130. {Function: funcs[0], Line: 11},
  131. {Function: funcs[1], Line: 22},
  132. {Function: funcs[2], Line: 33},
  133. }
  134. mapping := []*profile.Mapping{
  135. {
  136. ID: 1,
  137. Start: addrBase,
  138. Limit: addrBase + 10,
  139. Offset: 0,
  140. File: "testbin",
  141. HasFunctions: true,
  142. HasFilenames: true,
  143. HasLineNumbers: true,
  144. },
  145. }
  146. // Three interesting addresses: base+{10,20,30}
  147. locs := []*profile.Location{
  148. {ID: 1, Address: addrBase + 10, Line: lines[0:1], Mapping: mapping[0]},
  149. {ID: 2, Address: addrBase + 20, Line: lines[1:2], Mapping: mapping[0]},
  150. {ID: 3, Address: addrBase + 30, Line: lines[2:3], Mapping: mapping[0]},
  151. }
  152. // Two stack traces.
  153. return &profile.Profile{
  154. PeriodType: &profile.ValueType{Type: "cpu", Unit: "milliseconds"},
  155. Period: 1,
  156. DurationNanos: 10e9,
  157. SampleType: []*profile.ValueType{
  158. {Type: "cpu", Unit: "milliseconds"},
  159. },
  160. Sample: []*profile.Sample{
  161. {
  162. Location: []*profile.Location{locs[2], locs[1], locs[0]},
  163. Value: []int64{100},
  164. },
  165. {
  166. Location: []*profile.Location{locs[1], locs[0]},
  167. Value: []int64{200},
  168. },
  169. },
  170. Location: locs,
  171. Function: funcs,
  172. Mapping: mapping,
  173. }
  174. }
  175. func TestNewListenerAndURL(t *testing.T) {
  176. if runtime.GOOS == "nacl" {
  177. t.Skip("test assumes tcp available")
  178. }
  179. tests := []struct {
  180. hostport string
  181. wantErr bool
  182. wantURLRe *regexp.Regexp
  183. wantLocal bool
  184. }{
  185. {
  186. hostport: ":",
  187. wantURLRe: regexp.MustCompile(`http://localhost:\d+`),
  188. wantLocal: true,
  189. },
  190. {
  191. hostport: "localhost:",
  192. wantURLRe: regexp.MustCompile(`http://localhost:\d+`),
  193. wantLocal: true,
  194. },
  195. {
  196. hostport: "http://localhost:12345",
  197. wantErr: true,
  198. },
  199. }
  200. for _, tt := range tests {
  201. t.Run(tt.hostport, func(t *testing.T) {
  202. _, url, isLocal, err := newListenerAndURL(tt.hostport)
  203. if tt.wantErr {
  204. if err == nil {
  205. t.Fatalf("newListenerAndURL(%v) want error; got none", tt.hostport)
  206. }
  207. return
  208. }
  209. if err != nil {
  210. t.Fatalf("newListenerAndURL(%v) = %v", tt.hostport, err)
  211. }
  212. if !tt.wantURLRe.MatchString(url) {
  213. t.Errorf("newListenerAndURL(%v) URL = %v; want URL matching %v", tt.hostport, url, tt.wantURLRe)
  214. }
  215. if got, want := isLocal, tt.wantLocal; got != want {
  216. t.Errorf("newListenerAndURL(%v) isLocal = %v; want %v", tt.hostport, got, want)
  217. }
  218. })
  219. }
  220. }
  221. func TestIsLocalHost(t *testing.T) {
  222. for _, s := range []string{"localhost:10000", "[::1]:10000", "127.0.0.1:10000"} {
  223. host, _, err := net.SplitHostPort(s)
  224. if err != nil {
  225. t.Error("unexpected error when splitting", s)
  226. continue
  227. }
  228. if !isLocalhost(host) {
  229. t.Errorf("host %s from %s not considered local", host, s)
  230. }
  231. }
  232. }