@@ -3,8 +3,13 @@ package integrationtests
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "io/ioutil"
7
+ "math/rand"
6
8
"os"
9
+ "os/exec"
10
+ "strings"
7
11
"testing"
12
+ "time"
8
13
9
14
"github.com/stretchr/testify/assert"
10
15
"github.com/stretchr/testify/require"
@@ -87,3 +92,113 @@ func TestSmb(t *testing.T) {
87
92
err = writeReadFile (localPath )
88
93
assert .NotNil (t , err )
89
94
}
95
+
96
+ const letterset = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
97
+
98
+ var seededRand = rand .New (rand .NewSource (time .Now ().UnixNano ()))
99
+
100
+ func stringWithCharset (length int , charset string ) string {
101
+ b := make ([]byte , length )
102
+ for i := range b {
103
+ b [i ] = charset [seededRand .Intn (len (charset ))]
104
+ }
105
+ return string (b )
106
+ }
107
+
108
+ // RandomString generates a random string with specified length
109
+ func randomString (length int ) string {
110
+ return stringWithCharset (length , letterset )
111
+ }
112
+
113
+ func setupUser (username , password string ) error {
114
+ cmdLine := fmt .Sprintf (`$PWord = ConvertTo-SecureString $Env:password -AsPlainText -Force` +
115
+ `;New-Localuser -name $Env:username -accountneverexpires -password $PWord` )
116
+ cmd := exec .Command ("powershell" , "/c" , cmdLine )
117
+ cmd .Env = append (os .Environ (),
118
+ fmt .Sprintf ("username=%s" , username ),
119
+ fmt .Sprintf ("password=%s" , password ))
120
+ if output , err := cmd .CombinedOutput (); err != nil {
121
+ return fmt .Errorf ("setupUser failed: %v, output: %q" , err , string (output ))
122
+ }
123
+ return nil
124
+ }
125
+
126
+ func removeUser (t * testing.T , username string ) {
127
+ cmdLine := fmt .Sprintf (`Remove-Localuser -name $Env:username` )
128
+ cmd := exec .Command ("powershell" , "/c" , cmdLine )
129
+ cmd .Env = append (os .Environ (),
130
+ fmt .Sprintf ("username=%s" , username ))
131
+ if output , err := cmd .CombinedOutput (); err != nil {
132
+ t .Fatalf ("setupUser failed: %v, output: %q" , err , string (output ))
133
+ }
134
+ }
135
+
136
+ func setupSmbShare (shareName , localPath , username string ) error {
137
+ if err := os .MkdirAll (localPath , 0755 ); err != nil {
138
+ return fmt .Errorf ("setupSmbShare failed to create local path %q: %v" , localPath , err )
139
+ }
140
+ cmdLine := fmt .Sprintf (`New-SMBShare -Name $Env:sharename -Path $Env:path -fullaccess $Env:username` )
141
+ cmd := exec .Command ("powershell" , "/c" , cmdLine )
142
+ cmd .Env = append (os .Environ (),
143
+ fmt .Sprintf ("sharename=%s" , shareName ),
144
+ fmt .Sprintf ("path=%s" , localPath ),
145
+ fmt .Sprintf ("username=%s" , username ))
146
+ if output , err := cmd .CombinedOutput (); err != nil {
147
+ return fmt .Errorf ("setupSmbShare failed: %v, output: %q" , err , string (output ))
148
+ }
149
+
150
+ return nil
151
+ }
152
+
153
+ func removeSmbShare (t * testing.T , shareName string ) {
154
+ cmdLine := fmt .Sprintf (`Remove-SMBShare -Name $Env:sharename -Force` )
155
+ cmd := exec .Command ("powershell" , "/c" , cmdLine )
156
+ cmd .Env = append (os .Environ (),
157
+ fmt .Sprintf ("sharename=%s" , shareName ))
158
+ if output , err := cmd .CombinedOutput (); err != nil {
159
+ t .Fatalf ("setupSmbShare failed: %v, output: %q" , err , string (output ))
160
+ }
161
+ return
162
+ }
163
+
164
+ func getSmbGlobalMapping (remotePath string ) error {
165
+ // use PowerShell Environment Variables to store user input string to prevent command line injection
166
+ // https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_environment_variables?view=powershell-5.1
167
+ cmdLine := fmt .Sprintf (`(Get-SmbGlobalMapping -RemotePath $Env:smbremotepath).Status` )
168
+
169
+ cmd := exec .Command ("powershell" , "/c" , cmdLine )
170
+ cmd .Env = append (os .Environ (),
171
+ fmt .Sprintf ("smbremotepath=%s" , remotePath ))
172
+ output , err := cmd .CombinedOutput ()
173
+ if err != nil {
174
+ return fmt .Errorf ("Get-SmbGlobalMapping failed: %v, output: %q" , err , string (output ))
175
+ }
176
+ if ! strings .Contains (string (output ), "OK" ) {
177
+ return fmt .Errorf ("Get-SmbGlobalMapping return status %q instead of OK" , string (output ))
178
+ }
179
+ return nil
180
+ }
181
+
182
+ func writeReadFile (path string ) error {
183
+ fileName := path + "\\ hello.txt"
184
+ f , err := os .Create (fileName )
185
+ if err != nil {
186
+ return fmt .Errorf ("create file %q failed: %v" , fileName , err )
187
+ }
188
+ defer f .Close ()
189
+ fileContent := "Hello World"
190
+ if _ , err = f .WriteString (fileContent ); err != nil {
191
+ return fmt .Errorf ("write to file %q failed: %v" , fileName , err )
192
+ }
193
+ if err = f .Sync (); err != nil {
194
+ return fmt .Errorf ("sync file %q failed: %v" , fileName , err )
195
+ }
196
+ dat , err := ioutil .ReadFile (fileName )
197
+ if err != nil {
198
+ return fmt .Errorf ("read file %q failed: %v" , fileName , err )
199
+ }
200
+ if fileContent != string (dat ) {
201
+ return fmt .Errorf ("read content of file %q failed: expected %q, got %q" , fileName , fileContent , string (dat ))
202
+ }
203
+ return nil
204
+ }
0 commit comments