Пример #1
0
func TestSet(t *testing.T) {
	/*
		Access ACL
	*/
	f := testutil.MustTempFile(t, "", "acl").Name()
	defer os.Remove(f)
	acl, err := Get(f)
	testutil.Must(t, err)
	for i := range acl {
		acl[i].Perms = (^acl[i].Perms) & 0x7 // Flip the rwx bits
	}
	err = Set(f, acl)
	testutil.Must(t, err)
	acl2, err := Get(f)
	testutil.Must(t, err)
	if !reflect.DeepEqual(acl, acl2) {
		t.Errorf("unexpected ACL: want %v; got %v", acl, acl2)
	}

	/*
		Default ACL
	*/
	d := testutil.MustTempDir(t, "", "acl")
	defer os.Remove(d)
	// reuse the acl from above since we know it's valid
	err = SetDefault(d, acl)
	testutil.Must(t, err)
	acl2, err = GetDefault(d)
	testutil.Must(t, err)
	if !reflect.DeepEqual(acl, acl2) {
		t.Errorf("unexpected default ACL: want %v; got %v", acl, acl2)
	}
}
Пример #2
0
func TestGet(t *testing.T) {
	f := testutil.MustTempFile(t, "", "acl").Name()
	defer os.Remove(f)
	_, err := Get(f)
	testutil.Must(t, err)

	d := testutil.MustTempDir(t, "", "acl")
	defer os.Remove(d)
	_, err = GetDefault(d)
	testutil.Must(t, err)
}
Пример #3
0
func TestIsValid(t *testing.T) {
	f := testutil.MustTempFile(t, "", "acl").Name()
	defer os.Remove(f)
	acl, err := Get(f)
	testutil.Must(t, err)
	if !acl.IsValid() {
		t.Errorf("ACL reported invalid: %v", acl)
	}
	for _, v := range validACLs {
		if !v.IsValid() {
			t.Errorf("ACL reported invalid: %v", v)
		}
	}
	for _, i := range invalidACLs {
		if i.IsValid() {
			t.Errorf("ACL reported valid: %v", i)
		}
	}
}
Пример #4
0
func TestAdd(t *testing.T) {
	f := testutil.MustTempFile(t, "", "acl").Name()
	defer os.Remove(f)

	base := ACL{
		{TagUserObj, "", 7},
		{TagGroupObj, "", 0},
		{TagOther, "", 0},
	}

	testCases := []struct {
		Before ACL
		Add    []Entry
		Afer   ACL
	}{
		// Make sure mask is generated
		{
			base,
			[]Entry{{TagUser, "0", 4}, {TagGroup, "0", 2}},
			append(ACL{{TagUser, "0", 4}, {TagGroup, "0", 2}, {TagMask, "", 6}}, base...),
		},
		// Make sure mask is not generated if a mask is supplied
		{
			base,
			[]Entry{{TagUser, "0", 4}, {TagGroup, "0", 2}, {TagMask, "", 1}},
			append(ACL{{TagUser, "0", 4}, {TagGroup, "0", 2}, {TagMask, "", 1}}, base...),
		},
		// Make sure the original mask is overridden
		{
			append(ACL{{TagMask, "", 7}}, base...),
			[]Entry{{TagUser, "0", 4}, {TagGroup, "0", 2}},
			append(ACL{{TagUser, "0", 4}, {TagGroup, "0", 2}, {TagMask, "", 6}}, base...),
		},
		// Make sure TagUser, TagGroup, or TagGroupObj in original is used
		// in calculating new mask
		{
			append(ACL{{TagUser, "0", 4}, {TagMask, "", 0}}, base...),
			[]Entry{{TagGroup, "0", 2}, {TagGroupObj, "", 1}},
			ACL{{TagUser, "0", 4}, {TagGroup, "0", 2}, {TagMask, "", 7},
				{TagUserObj, "", 7},
				{TagGroupObj, "", 1},
				{TagOther, "", 0}},
		},
		// Make sure TagUser or TagGroup in original is NOT used
		// in calculating new mask if it's overwritten
		{
			append(ACL{{TagUser, "0", 7}, {TagMask, "", 0}}, base...),
			[]Entry{{TagUser, "0", 4}, {TagGroup, "0", 2}},
			append(ACL{{TagUser, "0", 4}, {TagGroup, "0", 2}, {TagMask, "", 6}}, base...),
		},
	}

	for i, c := range testCases {
		err := Set(f, c.Before)
		testutil.Must(t, err)
		err = Add(f, c.Add...)
		testutil.Must(t, err)
		acl, err := Get(f)
		testutil.Must(t, err)

		m1 := make(map[Entry]bool)
		m2 := make(map[Entry]bool)
		for _, e := range acl {
			m1[e] = true
		}
		for _, e := range c.Afer {
			m2[e] = true
		}

		if !reflect.DeepEqual(m1, m2) {
			t.Errorf("case %v: unexpected ACL: want %v; got %v", i, c.Afer, acl)
		}
	}
}