mirror_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package models
  7. import (
  8. "testing"
  9. . "github.com/smartystreets/goconvey/convey"
  10. )
  11. func Test_parseRemoteUpdateOutput(t *testing.T) {
  12. Convey("Parse mirror remote update output", t, func() {
  13. testCases := []struct {
  14. output string
  15. results []*mirrorSyncResult
  16. }{
  17. {
  18. `
  19. From https://gitlab.com/yoginth/upsteam
  20. * [new branch] develop -> develop
  21. b0bb24f..1d85a4f master -> master
  22. - [deleted] (none) -> bugfix
  23. `,
  24. []*mirrorSyncResult{
  25. {"develop", GitShortEmptySHA, ""},
  26. {"master", "b0bb24f", "1d85a4f"},
  27. {"bugfix", "", GitShortEmptySHA},
  28. },
  29. },
  30. }
  31. for _, tc := range testCases {
  32. results := parseRemoteUpdateOutput(tc.output)
  33. So(len(results), ShouldEqual, len(tc.results))
  34. for i := range tc.results {
  35. So(tc.results[i].refName, ShouldEqual, results[i].refName)
  36. So(tc.results[i].oldCommitID, ShouldEqual, results[i].oldCommitID)
  37. So(tc.results[i].newCommitID, ShouldEqual, results[i].newCommitID)
  38. }
  39. }
  40. })
  41. }
  42. func Test_findPasswordInMirrorAddress(t *testing.T) {
  43. Convey("Find password portion in mirror address", t, func() {
  44. testCases := []struct {
  45. addr string
  46. start, end int
  47. found bool
  48. password string
  49. }{
  50. {"http://localhost:3000/user/repo.git", -1, -1, false, ""},
  51. {"http://user@localhost:3000/user/repo.git", -1, -1, false, ""},
  52. {"http://user:@localhost:3000/user/repo.git", -1, -1, false, ""},
  53. {"http://user:password@localhost:3000/user/repo.git", 12, 20, true, "password"},
  54. {"http://username:my%3Asecure%3Bpassword@localhost:3000/user/repo.git", 16, 38, true, "my%3Asecure%3Bpassword"},
  55. {"http://username:my%40secure%23password@localhost:3000/user/repo.git", 16, 38, true, "my%40secure%23password"},
  56. {"http://username:@@localhost:3000/user/repo.git", 16, 17, true, "@"},
  57. }
  58. for _, tc := range testCases {
  59. start, end, found := findPasswordInMirrorAddress(tc.addr)
  60. So(start, ShouldEqual, tc.start)
  61. So(end, ShouldEqual, tc.end)
  62. So(found, ShouldEqual, tc.found)
  63. if found {
  64. So(tc.addr[start:end], ShouldEqual, tc.password)
  65. }
  66. }
  67. })
  68. }
  69. func Test_unescapeMirrorCredentials(t *testing.T) {
  70. Convey("Escape credentials in mirror address", t, func() {
  71. testCases := []string{
  72. "http://localhost:3000/user/repo.git", "http://localhost:3000/user/repo.git",
  73. "http://user@localhost:3000/user/repo.git", "http://user@localhost:3000/user/repo.git",
  74. "http://user:@localhost:3000/user/repo.git", "http://user:@localhost:3000/user/repo.git",
  75. "http://user:password@localhost:3000/user/repo.git", "http://user:password@localhost:3000/user/repo.git",
  76. "http://user:my%3Asecure%3Bpassword@localhost:3000/user/repo.git", "http://user:my:secure;password@localhost:3000/user/repo.git",
  77. "http://user:my%40secure%23password@localhost:3000/user/repo.git", "http://user:my@secure#password@localhost:3000/user/repo.git",
  78. }
  79. for i := 0; i < len(testCases); i += 2 {
  80. So(unescapeMirrorCredentials(testCases[i]), ShouldEqual, testCases[i+1])
  81. }
  82. })
  83. }
  84. func Test_escapeMirrorCredentials(t *testing.T) {
  85. Convey("Escape credentials in mirror address", t, func() {
  86. testCases := []string{
  87. "http://localhost:3000/user/repo.git", "http://localhost:3000/user/repo.git",
  88. "http://user@localhost:3000/user/repo.git", "http://user@localhost:3000/user/repo.git",
  89. "http://user:@localhost:3000/user/repo.git", "http://user:@localhost:3000/user/repo.git",
  90. "http://user:password@localhost:3000/user/repo.git", "http://user:password@localhost:3000/user/repo.git",
  91. "http://user:my:secure;password@localhost:3000/user/repo.git", "http://user:my%3Asecure%3Bpassword@localhost:3000/user/repo.git",
  92. "http://user:my@secure#password@localhost:3000/user/repo.git", "http://user:my%40secure%23password@localhost:3000/user/repo.git",
  93. }
  94. for i := 0; i < len(testCases); i += 2 {
  95. So(escapeMirrorCredentials(testCases[i]), ShouldEqual, testCases[i+1])
  96. }
  97. })
  98. }