sshd_config_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package fsdriver
  2. import (
  3. "reflect"
  4. "testing"
  5. )
  6. func TestGenSshdConfig(t *testing.T) {
  7. cases := []struct {
  8. config []string
  9. loginAccount string
  10. loginPassword string
  11. sshPort int
  12. expected []string
  13. }{
  14. {
  15. config: []string{
  16. "PermitRootLogin no",
  17. "PasswordAuthentication no",
  18. "# Port 22",
  19. },
  20. loginAccount: "root",
  21. loginPassword: "123456",
  22. sshPort: 22,
  23. expected: []string{
  24. "PermitRootLogin yes",
  25. "PasswordAuthentication yes",
  26. "# Port 22",
  27. },
  28. },
  29. {
  30. config: []string{
  31. "PermitRootLogin no",
  32. "PasswordAuthentication no",
  33. "#Port 22",
  34. },
  35. loginAccount: "yunion",
  36. loginPassword: "123456",
  37. sshPort: 22,
  38. expected: []string{
  39. "PermitRootLogin no",
  40. "PasswordAuthentication yes",
  41. "#Port 22",
  42. },
  43. },
  44. {
  45. config: []string{
  46. "PermitRootLogin no",
  47. "PasswordAuthentication no",
  48. "Port 22",
  49. },
  50. loginAccount: "yunion",
  51. loginPassword: "123456",
  52. sshPort: 9000,
  53. expected: []string{
  54. "PermitRootLogin no",
  55. "PasswordAuthentication yes",
  56. "Port 9000",
  57. },
  58. },
  59. {
  60. config: []string{
  61. "# PermitRootLogin no",
  62. "PasswordAuthentication no",
  63. "Port 22",
  64. },
  65. loginAccount: "root",
  66. loginPassword: "123456",
  67. sshPort: 9000,
  68. expected: []string{
  69. "# PermitRootLogin no",
  70. "PasswordAuthentication yes",
  71. "Port 9000",
  72. "PermitRootLogin yes",
  73. },
  74. },
  75. {
  76. config: []string{
  77. "# PermitRootLogin no",
  78. "PasswordAuthentication no",
  79. "Port 22",
  80. },
  81. loginAccount: "root",
  82. loginPassword: "123456",
  83. sshPort: 22,
  84. expected: []string{
  85. "# PermitRootLogin no",
  86. "PasswordAuthentication yes",
  87. "Port 22",
  88. "PermitRootLogin yes",
  89. },
  90. },
  91. {
  92. config: []string{
  93. "# PermitRootLogin no",
  94. "PasswordAuthentication no",
  95. "Port 22",
  96. },
  97. loginAccount: "root",
  98. loginPassword: "",
  99. sshPort: 22,
  100. expected: []string{
  101. "# PermitRootLogin no",
  102. "PasswordAuthentication no",
  103. "Port 22",
  104. "PermitRootLogin yes",
  105. },
  106. },
  107. }
  108. for _, c := range cases {
  109. actual := genSshdConfig(c.config, c.loginAccount, c.loginPassword, c.sshPort)
  110. if !reflect.DeepEqual(actual, c.expected) {
  111. t.Errorf("expected %v, got %v", c.expected, actual)
  112. }
  113. }
  114. }