You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
3.7 KiB

  1. // Copyright 2015 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 ipauth
  15. import (
  16. "errors"
  17. "net/http"
  18. "testing"
  19. "github.com/google/martian"
  20. "github.com/google/martian/auth"
  21. "github.com/google/martian/martiantest"
  22. "github.com/google/martian/proxyutil"
  23. )
  24. func TestModifyRequest(t *testing.T) {
  25. m := NewModifier()
  26. m.SetRequestModifier(nil)
  27. req, err := http.NewRequest("CONNECT", "https://www.example.com", nil)
  28. if err != nil {
  29. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  30. }
  31. ctx, remove, err := martian.TestContext(req, nil, nil)
  32. if err != nil {
  33. t.Fatalf("martian.TestContext(): got %v, want no error", err)
  34. }
  35. defer remove()
  36. if err := m.ModifyRequest(req); err != nil {
  37. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  38. }
  39. actx := auth.FromContext(ctx)
  40. if got, want := actx.ID(), ""; got != want {
  41. t.Errorf("actx.ID(): got %q, want %q", got, want)
  42. }
  43. // IP with port and modifier with error.
  44. tm := martiantest.NewModifier()
  45. reqerr := errors.New("request error")
  46. tm.RequestError(reqerr)
  47. req.RemoteAddr = "1.1.1.1:8111"
  48. m.SetRequestModifier(tm)
  49. if err := m.ModifyRequest(req); err != reqerr {
  50. t.Fatalf("ModifyConnectRequest(): got %v, want %v", err, reqerr)
  51. }
  52. if got, want := actx.ID(), "1.1.1.1"; got != want {
  53. t.Errorf("actx.ID(): got %q, want %q", got, want)
  54. }
  55. // IP without port and modifier with auth error.
  56. req.RemoteAddr = "4.4.4.4"
  57. authErr := errors.New("auth error")
  58. tm.RequestError(nil)
  59. tm.RequestFunc(func(req *http.Request) {
  60. ctx := martian.NewContext(req)
  61. actx := auth.FromContext(ctx)
  62. actx.SetError(authErr)
  63. })
  64. if err := m.ModifyRequest(req); err != nil {
  65. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  66. }
  67. if got, want := actx.ID(), ""; got != want {
  68. t.Errorf("actx.ID(): got %q, want %q", got, want)
  69. }
  70. }
  71. func TestModifyResponse(t *testing.T) {
  72. m := NewModifier()
  73. m.SetResponseModifier(nil)
  74. req, err := http.NewRequest("GET", "http://example.com", nil)
  75. if err != nil {
  76. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  77. }
  78. ctx, remove, err := martian.TestContext(req, nil, nil)
  79. if err != nil {
  80. t.Fatalf("martian.TestContext(): got %v, want no error", err)
  81. }
  82. defer remove()
  83. res := proxyutil.NewResponse(200, nil, req)
  84. if err := m.ModifyResponse(res); err != nil {
  85. t.Fatalf("ModifyResponse(): got %v, want no error", err)
  86. }
  87. // Modifier with error.
  88. tm := martiantest.NewModifier()
  89. reserr := errors.New("response error")
  90. tm.ResponseError(reserr)
  91. m.SetResponseModifier(tm)
  92. if err := m.ModifyResponse(res); err != reserr {
  93. t.Fatalf("ModifyResponse(): got %v, want %v", err, reserr)
  94. }
  95. // Modifier with auth error.
  96. tm.ResponseError(nil)
  97. authErr := errors.New("auth error")
  98. tm.ResponseFunc(func(res *http.Response) {
  99. ctx := martian.NewContext(res.Request)
  100. actx := auth.FromContext(ctx)
  101. actx.SetError(authErr)
  102. })
  103. actx := auth.FromContext(ctx)
  104. actx.SetID("bad-auth")
  105. if err := m.ModifyResponse(res); err != nil {
  106. t.Fatalf("ModifyResponse(): got %v, want no error", err)
  107. }
  108. if got, want := res.StatusCode, 403; got != want {
  109. t.Errorf("res.StatusCode: got %d, want %d", got, want)
  110. }
  111. if got, want := actx.Error(), authErr; got != want {
  112. t.Errorf("actx.Error(): got %v, want %v", got, want)
  113. }
  114. }