mirror_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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_parseRemoteUpdateOutput(t *testing.T) {
  10. Convey("Parse mirror remote update output", t, func() {
  11. testCases := []struct {
  12. output string
  13. results []*mirrorSyncResult
  14. }{
  15. {
  16. `
  17. From https://try.gogs.io/unknwon/upsteam
  18. * [new branch] develop -> develop
  19. b0bb24f..1d85a4f master -> master
  20. - [deleted] (none) -> bugfix
  21. `,
  22. []*mirrorSyncResult{
  23. {"develop", GIT_SHORT_EMPTY_SHA, ""},
  24. {"master", "b0bb24f", "1d85a4f"},
  25. {"bugfix", "", GIT_SHORT_EMPTY_SHA},
  26. },
  27. },
  28. }
  29. for _, tc := range testCases {
  30. results := parseRemoteUpdateOutput(tc.output)
  31. So(len(results), ShouldEqual, len(tc.results))
  32. for i := range tc.results {
  33. So(tc.results[i].refName, ShouldEqual, results[i].refName)
  34. So(tc.results[i].oldCommitID, ShouldEqual, results[i].oldCommitID)
  35. So(tc.results[i].newCommitID, ShouldEqual, results[i].newCommitID)
  36. }
  37. }
  38. })
  39. }
  40. func Test_findPasswordInMirrorAddress(t *testing.T) {
  41. Convey("Find password portion in mirror address", t, func() {
  42. testCases := []struct {
  43. addr string
  44. start, end int
  45. found bool
  46. password string
  47. }{
  48. {"http://localhost:3000/user/repo.git", -1, -1, false, ""},
  49. {"http://user@localhost:3000/user/repo.git", -1, -1, false, ""},
  50. {"http://user:@localhost:3000/user/repo.git", -1, -1, false, ""},
  51. {"http://user:password@localhost:3000/user/repo.git", 12, 20, true, "password"},
  52. {"http://username:my%3Asecure%3Bpassword@localhost:3000/user/repo.git", 16, 38, true, "my%3Asecure%3Bpassword"},
  53. {"http://username:my%40secure%23password@localhost:3000/user/repo.git", 16, 38, true, "my%40secure%23password"},
  54. {"http://username:@@localhost:3000/user/repo.git", 16, 17, true, "@"},
  55. }
  56. for _, tc := range testCases {
  57. start, end, found := findPasswordInMirrorAddress(tc.addr)
  58. So(start, ShouldEqual, tc.start)
  59. So(end, ShouldEqual, tc.end)
  60. So(found, ShouldEqual, tc.found)
  61. if found {
  62. So(tc.addr[start:end], ShouldEqual, tc.password)
  63. }
  64. }
  65. })
  66. }
  67. func Test_unescapeMirrorCredentials(t *testing.T) {
  68. Convey("Escape credentials in mirror address", t, func() {
  69. testCases := []string{
  70. "http://localhost:3000/user/repo.git", "http://localhost:3000/user/repo.git",
  71. "http://user@localhost:3000/user/repo.git", "http://user@localhost:3000/user/repo.git",
  72. "http://user:@localhost:3000/user/repo.git", "http://user:@localhost:3000/user/repo.git",
  73. "http://user:password@localhost:3000/user/repo.git", "http://user:password@localhost:3000/user/repo.git",
  74. "http://user:my%3Asecure%3Bpassword@localhost:3000/user/repo.git", "http://user:my:secure;password@localhost:3000/user/repo.git",
  75. "http://user:my%40secure%23password@localhost:3000/user/repo.git", "http://user:my@secure#password@localhost:3000/user/repo.git",
  76. }
  77. for i := 0; i < len(testCases); i += 2 {
  78. So(unescapeMirrorCredentials(testCases[i]), ShouldEqual, testCases[i+1])
  79. }
  80. })
  81. }
  82. func Test_escapeMirrorCredentials(t *testing.T) {
  83. Convey("Escape credentials in mirror address", t, func() {
  84. testCases := []string{
  85. "http://localhost:3000/user/repo.git", "http://localhost:3000/user/repo.git",
  86. "http://user@localhost:3000/user/repo.git", "http://user@localhost:3000/user/repo.git",
  87. "http://user:@localhost:3000/user/repo.git", "http://user:@localhost:3000/user/repo.git",
  88. "http://user:password@localhost:3000/user/repo.git", "http://user:password@localhost:3000/user/repo.git",
  89. "http://user:my:secure;password@localhost:3000/user/repo.git", "http://user:my%3Asecure%3Bpassword@localhost:3000/user/repo.git",
  90. "http://user:my@secure#password@localhost:3000/user/repo.git", "http://user:my%40secure%23password@localhost:3000/user/repo.git",
  91. }
  92. for i := 0; i < len(testCases); i += 2 {
  93. So(escapeMirrorCredentials(testCases[i]), ShouldEqual, testCases[i+1])
  94. }
  95. })
  96. }