blob: 98c30910e7f124db67a81785b21d2543d908bb7b [file] [log] [blame]
Jiri Simsad7616c92015-03-24 23:44:30 -07001// Copyright 2015 The Vanadium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Todd Wang392a9cc2015-04-06 14:35:11 -07005package suid
Robert Kroegera99ad142014-10-30 17:56:39 -07006
7import (
8 "bytes"
9 "io/ioutil"
10 "log"
11 "os"
12 "path"
13 "testing"
14)
15
16func TestChown(t *testing.T) {
17 dir, err := ioutil.TempDir("", "chown_test")
18 if err != nil {
19 t.Fatalf("ioutil.TempDir() failed: %v", err)
20 }
21 defer os.RemoveAll(dir)
22
23 for _, p := range []string{"a/b/c", "c/d"} {
24 fp := path.Join(dir, p)
25 if err := os.MkdirAll(fp, os.FileMode(0700)); err != nil {
26 t.Fatalf("os.MkdirAll(%s) failed: %v", fp, err)
27 }
28 }
29
30 wp := &WorkParameters{
31 uid: 42,
32 gid: 7,
33 logDir: path.Join(dir, "a"),
34 workspace: path.Join(dir, "c"),
35
36 dryrun: true,
37 }
38
39 // Collect the log entries.
40 b := new(bytes.Buffer)
41 log.SetOutput(b)
42 log.SetFlags(0)
43 defer log.SetOutput(os.Stderr)
44 defer log.SetFlags(log.LstdFlags)
45
46 // Mock-chown the tree.
47 if err := wp.Chown(); err != nil {
48 t.Fatalf("wp.Chown() wrongly failed: %v", err)
49 }
50
51 // Edit the log buffer to remove the invocation dependent output.
52 pb := bytes.TrimSpace(bytes.Replace(b.Bytes(), []byte(dir), []byte("$PATH"), -1))
53
54 cmds := bytes.Split(pb, []byte{'\n'})
55 for i, _ := range cmds {
56 cmds[i] = bytes.TrimSpace(cmds[i])
57 }
58
59 expected := []string{
60 "[dryrun] os.Chown($PATH/c, 42, 7)",
61 "[dryrun] os.Chown($PATH/c/d, 42, 7)",
62 "[dryrun] os.Chown($PATH/a, 42, 7)",
63 "[dryrun] os.Chown($PATH/a/b, 42, 7)",
64 "[dryrun] os.Chown($PATH/a/b/c, 42, 7)",
65 }
66 if got, expected := len(cmds), len(expected); got != expected {
67 t.Fatalf("bad length. got: %d, expected %d", got, expected)
68 }
69 for i, _ := range expected {
70 if expected, got := expected[i], string(cmds[i]); expected != got {
71 t.Fatalf("wp.Chown output %d: got %v, expected %v", i, got, expected)
72 }
73 }
74}