util_windows_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // +build windows
  2. /*
  3. Copyright 2018 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package util
  15. import (
  16. "io/ioutil"
  17. "math/rand"
  18. "net"
  19. "os"
  20. "testing"
  21. "time"
  22. winio "github.com/Microsoft/go-winio"
  23. "github.com/stretchr/testify/assert"
  24. "github.com/stretchr/testify/require"
  25. )
  26. func TestParseEndpoint(t *testing.T) {
  27. tests := []struct {
  28. endpoint string
  29. expectError bool
  30. expectedProtocol string
  31. expectedAddr string
  32. }{
  33. {
  34. endpoint: "unix:///tmp/s1.sock",
  35. expectedProtocol: "unix",
  36. expectError: true,
  37. },
  38. {
  39. endpoint: "tcp://localhost:15880",
  40. expectedProtocol: "tcp",
  41. expectedAddr: "localhost:15880",
  42. },
  43. {
  44. endpoint: "npipe://./pipe/mypipe",
  45. expectedProtocol: "npipe",
  46. expectedAddr: "//./pipe/mypipe",
  47. },
  48. {
  49. endpoint: "npipe:////./pipe/mypipe2",
  50. expectedProtocol: "npipe",
  51. expectedAddr: "//./pipe/mypipe2",
  52. },
  53. {
  54. endpoint: "npipe:/pipe/mypipe3",
  55. expectedProtocol: "npipe",
  56. expectedAddr: "//./pipe/mypipe3",
  57. },
  58. {
  59. endpoint: "npipe:\\\\.\\pipe\\mypipe4",
  60. expectedProtocol: "npipe",
  61. expectedAddr: "//./pipe/mypipe4",
  62. },
  63. {
  64. endpoint: "npipe:\\pipe\\mypipe5",
  65. expectedProtocol: "npipe",
  66. expectedAddr: "//./pipe/mypipe5",
  67. },
  68. {
  69. endpoint: "tcp1://abc",
  70. expectedProtocol: "tcp1",
  71. expectError: true,
  72. },
  73. {
  74. endpoint: "a b c",
  75. expectError: true,
  76. },
  77. }
  78. for _, test := range tests {
  79. protocol, addr, err := parseEndpoint(test.endpoint)
  80. assert.Equal(t, test.expectedProtocol, protocol)
  81. if test.expectError {
  82. assert.NotNil(t, err, "Expect error during parsing %q", test.endpoint)
  83. continue
  84. }
  85. require.Nil(t, err, "Expect no error during parsing %q", test.endpoint)
  86. assert.Equal(t, test.expectedAddr, addr)
  87. }
  88. }
  89. func testPipe(t *testing.T, label string) {
  90. generatePipeName := func(suffixlen int) string {
  91. rand.Seed(time.Now().UnixNano())
  92. letter := []rune("abcdef0123456789")
  93. b := make([]rune, suffixlen)
  94. for i := range b {
  95. b[i] = letter[rand.Intn(len(letter))]
  96. }
  97. return "\\\\.\\pipe\\test-pipe" + string(b)
  98. }
  99. testFile := generatePipeName(4)
  100. pipeln, err := winio.ListenPipe(testFile, &winio.PipeConfig{SecurityDescriptor: "D:P(A;;GA;;;BA)(A;;GA;;;SY)"})
  101. defer pipeln.Close()
  102. require.NoErrorf(t, err, "Failed to listen on named pipe for test purposes: %v while setting up: %s", err, label)
  103. result, err := IsUnixDomainSocket(testFile)
  104. assert.Nil(t, err, "Unexpected error: %v from IsUnixDomainSocket for %s", err, label)
  105. assert.False(t, result, "Unexpected result: true from IsUnixDomainSocket: %v for %s", result, label)
  106. }
  107. func testRegularFile(t *testing.T, label string, exists bool) {
  108. f, err := ioutil.TempFile("", "test-file")
  109. require.NoErrorf(t, err, "Failed to create file for test purposes: %v while setting up: %s", err, label)
  110. testFile := f.Name()
  111. if !exists {
  112. testFile = testFile + ".absent"
  113. }
  114. f.Close()
  115. result, err := IsUnixDomainSocket(testFile)
  116. os.Remove(f.Name())
  117. assert.Nil(t, err, "Unexpected error: %v from IsUnixDomainSocket for %s", err, label)
  118. assert.False(t, result, "Unexpected result: true from IsUnixDomainSocket: %v for %s", result, label)
  119. }
  120. func testUnixDomainSocket(t *testing.T, label string) {
  121. f, err := ioutil.TempFile("", "test-domain-socket")
  122. require.NoErrorf(t, err, "Failed to create file for test purposes: %v while setting up: %s", err, label)
  123. testFile := f.Name()
  124. f.Close()
  125. os.Remove(testFile)
  126. ta, err := net.ResolveUnixAddr("unix", testFile)
  127. require.NoErrorf(t, err, "Failed to ResolveUnixAddr: %v while setting up: %s", err, label)
  128. unixln, err := net.ListenUnix("unix", ta)
  129. require.NoErrorf(t, err, "Failed to ListenUnix: %v while setting up: %s", err, label)
  130. result, err := IsUnixDomainSocket(testFile)
  131. unixln.Close()
  132. assert.Nil(t, err, "Unexpected error: %v from IsUnixDomainSocket for %s", err, label)
  133. assert.True(t, result, "Unexpected result: false from IsUnixDomainSocket: %v for %s", result, label)
  134. }
  135. func TestIsUnixDomainSocket(t *testing.T) {
  136. testPipe(t, "Named Pipe")
  137. testRegularFile(t, "Regular File that Exists", true)
  138. testRegularFile(t, "Regular File that Does Not Exist", false)
  139. testUnixDomainSocket(t, "Unix Domain Socket File")
  140. }
  141. func TestNormalizePath(t *testing.T) {
  142. tests := []struct {
  143. originalpath string
  144. normalizedPath string
  145. }{
  146. {
  147. originalpath: "\\path\\to\\file",
  148. normalizedPath: "c:\\path\\to\\file",
  149. },
  150. {
  151. originalpath: "/path/to/file",
  152. normalizedPath: "c:\\path\\to\\file",
  153. },
  154. {
  155. originalpath: "/path/to/dir/",
  156. normalizedPath: "c:\\path\\to\\dir\\",
  157. },
  158. {
  159. originalpath: "\\path\\to\\dir\\",
  160. normalizedPath: "c:\\path\\to\\dir\\",
  161. },
  162. {
  163. originalpath: "/file",
  164. normalizedPath: "c:\\file",
  165. },
  166. {
  167. originalpath: "\\file",
  168. normalizedPath: "c:\\file",
  169. },
  170. {
  171. originalpath: "fileonly",
  172. normalizedPath: "fileonly",
  173. },
  174. }
  175. for _, test := range tests {
  176. assert.Equal(t, test.normalizedPath, NormalizePath(test.originalpath))
  177. }
  178. }