1 /* 2 * Copyright 2013 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.jimfs; 18 19 import static com.google.common.jimfs.UserLookupService.createUserPrincipal; 20 import static com.google.common.truth.Truth.assertThat; 21 import static java.nio.file.attribute.AclEntryFlag.DIRECTORY_INHERIT; 22 import static java.nio.file.attribute.AclEntryPermission.APPEND_DATA; 23 import static java.nio.file.attribute.AclEntryPermission.DELETE; 24 import static java.nio.file.attribute.AclEntryType.ALLOW; 25 import static org.junit.Assert.assertNotNull; 26 27 import com.google.common.collect.ImmutableList; 28 import com.google.common.collect.ImmutableMap; 29 import com.google.common.collect.ImmutableSet; 30 import java.io.IOException; 31 import java.nio.file.attribute.AclEntry; 32 import java.nio.file.attribute.AclFileAttributeView; 33 import java.nio.file.attribute.FileAttributeView; 34 import java.nio.file.attribute.UserPrincipal; 35 import java.util.Map; 36 import java.util.Set; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.junit.runners.JUnit4; 40 41 /** 42 * Tests for {@link AclAttributeProvider}. 43 * 44 * @author Colin Decker 45 */ 46 @RunWith(JUnit4.class) 47 public class AclAttributeProviderTest extends AbstractAttributeProviderTest<AclAttributeProvider> { 48 49 private static final UserPrincipal USER = createUserPrincipal("user"); 50 private static final UserPrincipal FOO = createUserPrincipal("foo"); 51 52 private static final ImmutableList<AclEntry> defaultAcl = 53 new ImmutableList.Builder<AclEntry>() 54 .add( 55 AclEntry.newBuilder() 56 .setType(ALLOW) 57 .setFlags(DIRECTORY_INHERIT) 58 .setPermissions(DELETE, APPEND_DATA) 59 .setPrincipal(USER) 60 .build()) 61 .add( 62 AclEntry.newBuilder() 63 .setType(ALLOW) 64 .setFlags(DIRECTORY_INHERIT) 65 .setPermissions(DELETE, APPEND_DATA) 66 .setPrincipal(FOO) 67 .build()) 68 .build(); 69 70 @Override createProvider()71 protected AclAttributeProvider createProvider() { 72 return new AclAttributeProvider(); 73 } 74 75 @Override createInheritedProviders()76 protected Set<? extends AttributeProvider> createInheritedProviders() { 77 return ImmutableSet.of(new BasicAttributeProvider(), new OwnerAttributeProvider()); 78 } 79 80 @Override createDefaultValues()81 protected Map<String, ?> createDefaultValues() { 82 return ImmutableMap.of("acl:acl", defaultAcl); 83 } 84 85 @Test testInitialAttributes()86 public void testInitialAttributes() { 87 assertThat(provider.get(file, "acl")).isEqualTo(defaultAcl); 88 } 89 90 @Test testSet()91 public void testSet() { 92 assertSetAndGetSucceeds("acl", ImmutableList.of()); 93 assertSetFailsOnCreate("acl", ImmutableList.of()); 94 assertSetFails("acl", ImmutableSet.of()); 95 assertSetFails("acl", ImmutableList.of("hello")); 96 } 97 98 @Test testView()99 public void testView() throws IOException { 100 AclFileAttributeView view = 101 provider.view( 102 fileLookup(), 103 ImmutableMap.<String, FileAttributeView>of( 104 "owner", new OwnerAttributeProvider().view(fileLookup(), NO_INHERITED_VIEWS))); 105 assertNotNull(view); 106 107 assertThat(view.name()).isEqualTo("acl"); 108 109 assertThat(view.getAcl()).isEqualTo(defaultAcl); 110 111 view.setAcl(ImmutableList.<AclEntry>of()); 112 view.setOwner(FOO); 113 114 assertThat(view.getAcl()).isEqualTo(ImmutableList.<AclEntry>of()); 115 assertThat(view.getOwner()).isEqualTo(FOO); 116 117 assertThat(file.getAttribute("acl", "acl")).isEqualTo(ImmutableList.<AclEntry>of()); 118 } 119 } 120