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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
|
package mtree
import (
"archive/tar"
"bytes"
"io"
"os"
"path/filepath"
"syscall"
"testing"
"time"
)
func ExampleStreamer() {
fh, err := os.Open("./testdata/test.tar")
if err != nil {
// handle error ...
}
str := NewTarStreamer(fh, nil, nil)
if err := extractTar("/tmp/dir", str); err != nil {
// handle error ...
}
dh, err := str.Hierarchy()
if err != nil {
// handle error ...
}
res, err := Check("/tmp/dir/", dh, nil, nil)
if err != nil {
// handle error ...
}
if len(res) > 0 {
// handle validation issue ...
}
}
func extractTar(root string, tr io.Reader) error {
return nil
}
func TestTar(t *testing.T) {
/*
data, err := makeTarStream()
if err != nil {
t.Fatal(err)
}
buf := bytes.NewBuffer(data)
str := NewTarStreamer(buf, append(DefaultKeywords, "sha1"))
*/
/*
// open empty folder and check size.
fh, err := os.Open("./testdata/empty")
if err != nil {
t.Fatal(err)
}
log.Println(fh.Stat())
fh.Close() */
fh, err := os.Open("./testdata/test.tar")
if err != nil {
t.Fatal(err)
}
str := NewTarStreamer(fh, nil, append(DefaultKeywords, "sha1"))
if _, err := io.Copy(io.Discard, str); err != nil && err != io.EOF {
t.Fatal(err)
}
if err := str.Close(); err != nil {
t.Fatal(err)
}
defer fh.Close()
// get DirectoryHierarcy struct from walking the tar archive
tdh, err := str.Hierarchy()
if err != nil {
t.Fatal(err)
}
if tdh == nil {
t.Fatal("expected a DirectoryHierarchy struct, but got nil")
}
testDir, present := os.LookupEnv("MTREE_TESTDIR")
if present == false {
testDir = "."
}
testPath := filepath.Join(testDir, "test.mtree")
fh, err = os.Create(testPath)
if err != nil {
t.Fatal(err)
}
defer os.Remove(testPath)
// put output of tar walk into test.mtree
_, err = tdh.WriteTo(fh)
if err != nil {
t.Fatal(err)
}
fh.Close()
// now simulate gomtree -T testdata/test.tar -f testdata/test.mtree
fh, err = os.Open(testPath)
if err != nil {
t.Fatal(err)
}
defer fh.Close()
dh, err := ParseSpec(fh)
if err != nil {
t.Fatal(err)
}
res, err := Compare(tdh, dh, append(DefaultKeywords, "sha1"))
if err != nil {
t.Fatal(err)
}
// print any failures, and then call t.Fatal once all failures/extra/missing
// are outputted
if len(res) > 0 {
for _, delta := range res {
t.Error(delta)
}
t.Fatal("unexpected errors")
}
}
// This test checks how gomtree handles archives that were created
// with multiple directories, i.e, archives created with something like:
// `tar -cvf some.tar dir1 dir2 dir3 dir4/dir5 dir6` ... etc.
// The testdata of collection.tar resemble such an archive. the `collection` folder
// is the contents of `collection.tar` extracted
//
//gocyclo:ignore
func TestArchiveCreation(t *testing.T) {
fh, err := os.Open("./testdata/collection.tar")
if err != nil {
t.Fatal(err)
}
str := NewTarStreamer(fh, nil, []Keyword{"sha1"})
if _, err := io.Copy(io.Discard, str); err != nil && err != io.EOF {
t.Fatal(err)
}
if err := str.Close(); err != nil {
t.Fatal(err)
}
defer fh.Close()
// get DirectoryHierarcy struct from walking the tar archive
tdh, err := str.Hierarchy()
if err != nil {
t.Fatal(err)
}
// Test the tar manifest against the actual directory
res, err := Check("./testdata/collection", tdh, []Keyword{"sha1"}, nil)
if err != nil {
t.Fatal(err)
}
if len(res) > 0 {
for _, delta := range res {
t.Error(delta)
}
t.Fatal("unexpected errors")
}
// Test the tar manifest against itself
res, err = Compare(tdh, tdh, []Keyword{"sha1"})
if err != nil {
t.Fatal(err)
}
if len(res) > 0 {
for _, delta := range res {
t.Error(delta)
}
t.Fatal("unexpected errors")
}
// Validate the directory manifest against the archive
dh, err := Walk("./testdata/collection", nil, []Keyword{"sha1"}, nil)
if err != nil {
t.Fatal(err)
}
res, err = Compare(tdh, dh, []Keyword{"sha1"})
if err != nil {
t.Fatal(err)
}
if len(res) > 0 {
for _, delta := range res {
t.Error(delta)
}
t.Fatal("unexpected errors")
}
}
// Now test a tar file that was created with just the path to a file. In this
// test case, the traversal and creation of "placeholder" directories are
// evaluated. Also, The fact that this archive contains a single entry, yet the
// entry is associated with a file that has parent directories, means that the
// "." directory should be the lowest sub-directory under which `file` is contained.
//
//gocyclo:ignore
func TestTreeTraversal(t *testing.T) {
fh, err := os.Open("./testdata/traversal.tar")
if err != nil {
t.Fatal(err)
}
str := NewTarStreamer(fh, nil, DefaultTarKeywords)
if _, err = io.Copy(io.Discard, str); err != nil && err != io.EOF {
t.Fatal(err)
}
if err = str.Close(); err != nil {
t.Fatal(err)
}
fh.Close()
tdh, err := str.Hierarchy()
if err != nil {
t.Fatal(err)
}
res, err := Compare(tdh, tdh, []Keyword{"sha1"})
if err != nil {
t.Fatal(err)
}
if len(res) > 0 {
for _, delta := range res {
t.Error(delta)
}
t.Fatal("unexpected errors")
}
// top-level "." directory will contain contents of traversal.tar
res, err = Check("./testdata/.", tdh, []Keyword{"sha1"}, nil)
if err != nil {
t.Fatal(err)
}
if len(res) > 0 {
var failed bool
for _, delta := range res {
// We only care about missing or modified files.
// The original test was written using the old check code.
if delta.Type() != Extra {
failed = true
t.Error(delta)
}
}
if failed {
t.Fatal("unexpected errors")
}
}
// Now test an archive that requires placeholder directories, i.e, there are
// no headers in the archive that are associated with the actual directory name
fh, err = os.Open("./testdata/singlefile.tar")
if err != nil {
t.Fatal(err)
}
str = NewTarStreamer(fh, nil, DefaultTarKeywords)
if _, err = io.Copy(io.Discard, str); err != nil && err != io.EOF {
t.Fatal(err)
}
if err = str.Close(); err != nil {
t.Fatal(err)
}
tdh, err = str.Hierarchy()
if err != nil {
t.Fatal(err)
}
// Implied top-level "." directory will contain the contents of singlefile.tar
res, err = Check("./testdata/.", tdh, []Keyword{"sha1"}, nil)
if err != nil {
t.Fatal(err)
}
if len(res) > 0 {
var failed bool
for _, delta := range res {
// We only care about missing or modified files.
// The original test was written using the old check code.
if delta.Type() != Extra {
failed = true
t.Error(delta)
}
}
if failed {
t.Fatal("unexpected errors")
}
}
}
func TestHardlinks(t *testing.T) {
fh, err := os.Open("./testdata/hardlinks.tar")
if err != nil {
t.Fatal(err)
}
str := NewTarStreamer(fh, nil, append(DefaultTarKeywords, "nlink"))
if _, err = io.Copy(io.Discard, str); err != nil && err != io.EOF {
t.Fatal(err)
}
if err = str.Close(); err != nil {
t.Fatal(err)
}
fh.Close()
tdh, err := str.Hierarchy()
if err != nil {
t.Fatal(err)
}
foundnlink := false
for _, e := range tdh.Entries {
if e.Type == RelativeType {
for _, kv := range e.Keywords {
if KeyVal(kv).Keyword() == "nlink" {
foundnlink = true
if KeyVal(kv).Value() != "3" {
t.Errorf("expected to have 3 hardlinks for %s", e.Name)
}
}
}
}
}
if !foundnlink {
t.Errorf("nlink expected to be evaluated")
}
}
type fakeFile struct {
Name, Body string
Mode int64
Type byte
Sec, Nsec int64
Xattrs map[string]string
}
func makeTarStream(ff []fakeFile) ([]byte, error) {
buf := new(bytes.Buffer)
// Create a new tar archive.
tw := tar.NewWriter(buf)
// Add some files to the archive.
for _, file := range ff {
hdr := &tar.Header{
Name: file.Name,
Uid: syscall.Getuid(),
Gid: syscall.Getgid(),
Mode: file.Mode,
Typeflag: file.Type,
Size: int64(len(file.Body)),
ModTime: time.Unix(file.Sec, file.Nsec),
AccessTime: time.Unix(file.Sec, file.Nsec),
ChangeTime: time.Unix(file.Sec, file.Nsec),
Xattrs: file.Xattrs,
}
if err := tw.WriteHeader(hdr); err != nil {
return nil, err
}
if len(file.Body) > 0 {
if _, err := tw.Write([]byte(file.Body)); err != nil {
return nil, err
}
}
}
// Make sure to check the error on Close.
if err := tw.Close(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func TestArchiveExcludeNonDirectory(t *testing.T) {
fh, err := os.Open("./testdata/collection.tar")
if err != nil {
t.Fatal(err)
}
str := NewTarStreamer(fh, []ExcludeFunc{ExcludeNonDirectories}, []Keyword{"type"})
if _, err := io.Copy(io.Discard, str); err != nil && err != io.EOF {
t.Fatal(err)
}
if err := str.Close(); err != nil {
t.Fatal(err)
}
fh.Close()
// get DirectoryHierarcy struct from walking the tar archive
tdh, err := str.Hierarchy()
if err != nil {
t.Fatal(err)
}
for i := range tdh.Entries {
for _, keyval := range tdh.Entries[i].AllKeys() {
if tdh.Entries[i].Type == FullType || tdh.Entries[i].Type == RelativeType {
if keyval.Keyword() == "type" && keyval.Value() != "dir" {
t.Errorf("expected only directories, but %q is a %q", tdh.Entries[i].Name, keyval.Value())
}
}
}
}
}
|