mirror_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2017 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "testing"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. func Test_findPasswordInMirrorAddress(t *testing.T) {
  10. Convey("Find password portion in mirror address", t, func() {
  11. testCases := []struct {
  12. addr string
  13. start, end int
  14. found bool
  15. password string
  16. }{
  17. {"http://localhost:3000/user/repo.git", -1, -1, false, ""},
  18. {"http://user@localhost:3000/user/repo.git", -1, -1, false, ""},
  19. {"http://user:@localhost:3000/user/repo.git", -1, -1, false, ""},
  20. {"http://user:password@localhost:3000/user/repo.git", 12, 20, true, "password"},
  21. {"http://username:my%3Asecure%3Bpassword@localhost:3000/user/repo.git", 16, 38, true, "my%3Asecure%3Bpassword"},
  22. {"http://username:my%40secure%23password@localhost:3000/user/repo.git", 16, 38, true, "my%40secure%23password"},
  23. {"http://username:@@localhost:3000/user/repo.git", 16, 17, true, "@"},
  24. }
  25. for _, tc := range testCases {
  26. start, end, found := findPasswordInMirrorAddress(tc.addr)
  27. So(start, ShouldEqual, tc.start)
  28. So(end, ShouldEqual, tc.end)
  29. So(found, ShouldEqual, tc.found)
  30. if found {
  31. So(tc.addr[start:end], ShouldEqual, tc.password)
  32. }
  33. }
  34. })
  35. }
  36. func Test_unescapeMirrorCredentials(t *testing.T) {
  37. Convey("Escape credentials in mirror address", t, func() {
  38. testCases := []string{
  39. "http://localhost:3000/user/repo.git", "http://localhost:3000/user/repo.git",
  40. "http://user@localhost:3000/user/repo.git", "http://user@localhost:3000/user/repo.git",
  41. "http://user:@localhost:3000/user/repo.git", "http://user:@localhost:3000/user/repo.git",
  42. "http://user:password@localhost:3000/user/repo.git", "http://user:password@localhost:3000/user/repo.git",
  43. "http://user:my%3Asecure%3Bpassword@localhost:3000/user/repo.git", "http://user:my:secure;password@localhost:3000/user/repo.git",
  44. "http://user:my%40secure%23password@localhost:3000/user/repo.git", "http://user:my@secure#password@localhost:3000/user/repo.git",
  45. }
  46. for i := 0; i < len(testCases); i += 2 {
  47. So(unescapeMirrorCredentials(testCases[i]), ShouldEqual, testCases[i+1])
  48. }
  49. })
  50. }
  51. func Test_escapeMirrorCredentials(t *testing.T) {
  52. Convey("Escape credentials in mirror address", t, func() {
  53. testCases := []string{
  54. "http://localhost:3000/user/repo.git", "http://localhost:3000/user/repo.git",
  55. "http://user@localhost:3000/user/repo.git", "http://user@localhost:3000/user/repo.git",
  56. "http://user:@localhost:3000/user/repo.git", "http://user:@localhost:3000/user/repo.git",
  57. "http://user:password@localhost:3000/user/repo.git", "http://user:password@localhost:3000/user/repo.git",
  58. "http://user:my:secure;password@localhost:3000/user/repo.git", "http://user:my%3Asecure%3Bpassword@localhost:3000/user/repo.git",
  59. "http://user:my@secure#password@localhost:3000/user/repo.git", "http://user:my%40secure%23password@localhost:3000/user/repo.git",
  60. }
  61. for i := 0; i < len(testCases); i += 2 {
  62. So(escapeMirrorCredentials(testCases[i]), ShouldEqual, testCases[i+1])
  63. }
  64. })
  65. }