forked from mvo5/libsmbclient-go
/
libsmbclient_test.go
235 lines (206 loc) · 4.45 KB
/
libsmbclient_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package libsmbclient
import (
"fmt"
"log"
"testing"
"os"
"os/exec"
"path/filepath"
"io"
"io/ioutil"
_ "time"
"text/template"
"math/rand"
)
var SMB_CONF_TEMPLATE = `[global]
workgroup = TESTGROUP
interfaces = lo 127.0.0.0/8
smb ports = 1445
log level = 2
map to guest = Bad User
passdb backend = smbpasswd
smb passwd file = {{.Tempdir}}/smbpasswd
lock directory = {{.Tempdir}}/intern
state directory = {{.Tempdir}}/intern
cache directory = {{.Tempdir}}/intern
pid directory = {{.Tempdir}}/intern
private dir = {{.Tempdir}}/intern
ncalrpc dir = {{.Tempdir}}/intern
[public]
path = {{.Tempdir}}/public
guest ok = yes
[private]
path = {{.Tempdir}}/private
read only = no
`
// global teardown funcitons
var teardown []func()
func generateSmbdConf() string {
tempdir, _ := filepath.Abs("./tmp/samba")
teardown = append(teardown, func() {
os.RemoveAll(tempdir)
})
paths := [...]string{
tempdir,
filepath.Join(tempdir, "samaba", "private"),
filepath.Join(tempdir, "samba", "public"),
}
for _, d := range(paths) {
err := os.MkdirAll(d, 0755)
if err != nil {
log.Fatal(err)
}
}
os.Mkdir(filepath.Join(tempdir, "private"), 0755)
os.Mkdir(filepath.Join(tempdir, "public"), 0755)
f, err := os.Create(filepath.Join(tempdir, "smbd.conf"))
if err != nil {
log.Fatal(err)
}
defer f.Close()
templateText := SMB_CONF_TEMPLATE
type Dir struct {
Tempdir string
}
t, err := template.New("smb-conf").Parse(templateText)
if err != nil {
log.Fatal(err)
}
t.Execute(f, Dir{tempdir})
return f.Name()
}
func startSmbd() io.Reader {
// thanks pitti :)
os.Setenv("LIBSMB_PROG", "nc localhost 1445")
smb_conf := generateSmbdConf()
cmd := exec.Command("smbd", "-FS", "-s", smb_conf)
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
cmd.Start()
teardown = append(teardown, func() {
cmd.Process.Kill()
cmd.Process.Wait()
})
return stdout
}
func setUp() {
stdout := startSmbd()
//time.Sleep(100 * time.Millisecond)
_ = stdout
//fmt.Println(stdout)
//go io.Copy(os.Stdout, stdout)
}
func tearDown() {
// cleanup
for _,f := range teardown {
f()
}
// cleanup atexit
teardown = []func(){}
}
func TestLibsmbclientBindings(t *testing.T) {
fmt.Println("libsmbclient opendir test")
setUp()
// open client
client := New()
d, err := client.Opendir("smb://localhost")
if err != nil {
t.Error("failed to open localhost ", err)
}
// collect dirs
foundSmbDirs := map[string]bool{}
for {
dirent, err := d.Readdir()
if err != nil {
break
}
foundSmbDirs[dirent.Name] = true
}
// check for expected data
if !foundSmbDirs["private"] || !foundSmbDirs["public"] {
t.Error(fmt.Sprintf("missing excpected folder in (%v)", foundSmbDirs))
}
tearDown()
}
func getRandomFileName() string {
return fmt.Sprintf("%d", rand.Int())
}
func openFile(client *Client, path string, c chan int) {
f, err := client.Open(path, 0, 0)
if err != nil {
log.Fatal(fmt.Sprintf("%s: %s", path, err))
}
defer func() {
f.Close()
c <- 1
}()
// FIXME: move this into the lib as ioutil.ReadFile()
buf := make([]byte, 64*1024)
for {
_, err := f.Read(buf)
if err == io.EOF {
break
}
if err != nil {
log.Print(err)
return
}
}
}
func readAllFilesInDir(client *Client, baseDir string, c chan int) {
d, err := client.Opendir(baseDir)
if err != nil {
log.Fatal(fmt.Sprintf("%s: %s", baseDir, err))
}
defer d.Closedir()
for {
dirent, err := d.Readdir()
if err != nil {
break
}
if dirent.Name == "." || dirent.Name == ".." {
continue
}
if dirent.Type == SMBC_DIR {
go readAllFilesInDir(client, baseDir+dirent.Name+"/", c)
}
if dirent.Type == SMBC_FILE {
go openFile(client, baseDir+dirent.Name, c)
}
}
}
func TestLibsmbclientThreaded(t *testing.T) {
fmt.Println("libsmbclient threaded test")
setUp()
CLIENTS := 4
DIRS := 4
THREADS := 8
FILE_SIZE := 4*1024
for i := 0; i < DIRS; i++ {
dirname := fmt.Sprintf("./tmp/samba/public/%d/", i)
os.MkdirAll(dirname, 0755)
// create a bunch of test files
buf := make([]byte, FILE_SIZE)
for j := 0; j < THREADS; j++ {
tmpf := dirname + getRandomFileName()
ioutil.WriteFile(tmpf, buf, 0644)
}
}
// make N clients
c := make(chan int)
for i := 0; i < CLIENTS; i++ {
baseDir := "smb://localhost/public/"
client := New()
// FIXME: close eventually
//defer client.Close()
go readAllFilesInDir(client, baseDir, c)
}
count := 0
for count < THREADS*DIRS*CLIENTS {
count += <- c
}
fmt.Println(fmt.Sprintf("done: %d", count))
tearDown()
}