Exemplo n.º 1
0
  /**
   * Test if files inherit the group from its parent if the setgid bit is set, and if subdirectories
   * inherit the group and the setgid bit.
   */
  @Test
  public void testSetgid() throws Exception {
    VOLUME_NAME = "testSetgid";
    client.createVolume(mrcAddress, auth, userCredentials, VOLUME_NAME);
    Volume volume = client.openVolume(VOLUME_NAME, null, options);

    volume.createDirectory(userCredentials, "/DIR1", 0777);

    Stat stat;
    stat = volume.getAttr(userCredentials, "/DIR1");

    int mode = stat.getMode() | 02000;
    stat = stat.toBuilder().setGroupId("foobar").setMode(mode).build();

    UserCredentials rootCreds =
        userCredentials.toBuilder().setUsername("root").setGroups(0, "root").build();
    volume.setAttr(
        rootCreds,
        "/DIR1",
        stat,
        Setattrs.SETATTR_MODE.getNumber() | Setattrs.SETATTR_GID.getNumber());

    // Test if the setgid bit and the group is set
    stat = volume.getAttr(userCredentials, "/DIR1");
    assertEquals(02000, stat.getMode() & 02000);
    assertEquals("foobar", stat.getGroupId());

    // Test if new subdirectories inherit the setgid bit and the group
    volume.createDirectory(userCredentials, "/DIR1/DIR2", 0777);
    stat = volume.getAttr(userCredentials, "/DIR1/DIR2");
    assertEquals(02000, stat.getMode() & 02000);
    assertEquals("foobar", stat.getGroupId());

    // Test if new files inherit the group
    FileHandle fh =
        volume.openFile(
            userCredentials, "/DIR1/FILE1", SYSTEM_V_FCNTL.SYSTEM_V_FCNTL_H_O_CREAT.getNumber());
    fh.close();
    stat = volume.getAttr(userCredentials, "/DIR1/FILE1");
    assertEquals("foobar", stat.getGroupId());

    volume.close();
  }
Exemplo n.º 2
0
  @Test
  public void testGetSetAttr() throws Exception {
    VOLUME_NAME = "testGetSetAttr";

    final String TESTFILE = "testfile";
    final String TESTDIR = "testdir";

    final int TESTMODE = 0731;

    client.createVolume(mrcAddress, auth, userCredentials, VOLUME_NAME);
    Volume volume = client.openVolume(VOLUME_NAME, null, options);

    volume.createDirectory(userCredentials, TESTDIR, TESTMODE);
    FileHandle fh =
        volume.openFile(
            userCredentials,
            TESTDIR + "/" + TESTFILE,
            SYSTEM_V_FCNTL.SYSTEM_V_FCNTL_H_O_CREAT.getNumber(),
            TESTMODE);
    fh.close();

    Stat stat = volume.getAttr(userCredentials, TESTDIR);
    assertEquals(userCredentials.getUsername(), stat.getUserId());
    assertEquals(userCredentials.getGroups(0), stat.getGroupId());
    assertEquals(0, stat.getSize());
    assertTrue(stat.getAtimeNs() > 0);
    assertTrue(stat.getCtimeNs() > 0);
    assertTrue(stat.getMtimeNs() > 0);
    assertTrue((stat.getMode() & TESTMODE) > 0);
    assertEquals(1, stat.getNlink());

    stat = volume.getAttr(userCredentials, TESTDIR + "/" + TESTFILE);
    assertEquals(userCredentials.getUsername(), stat.getUserId());
    assertEquals(userCredentials.getGroups(0), stat.getGroupId());
    assertEquals(0, stat.getSize());
    assertTrue(stat.getAtimeNs() > 0);
    assertTrue(stat.getCtimeNs() > 0);
    assertTrue(stat.getMtimeNs() > 0);
    assertTrue((stat.getMode() & TESTMODE) == TESTMODE);
    assertEquals(1, stat.getNlink());

    stat = stat.toBuilder().setGroupId("foobar").setUserId("FredFoobar").build();
    try {
      volume.setAttr(
          userCredentials,
          TESTDIR,
          stat,
          Setattrs.SETATTR_UID.getNumber() | Setattrs.SETATTR_GID.getNumber());
      fail("changing username and groups should be restricted to superuser");
    } catch (PosixErrorException e) {
    }

    UserCredentials rootCreds =
        userCredentials.toBuilder().setUsername("root").setGroups(0, "root").build();

    volume.setAttr(
        rootCreds,
        TESTDIR,
        stat,
        Setattrs.SETATTR_UID.getNumber() | Setattrs.SETATTR_GID.getNumber());

    stat = volume.getAttr(userCredentials, TESTDIR);
    assertEquals("FredFoobar", stat.getUserId());
    assertEquals("foobar", stat.getGroupId());
    assertTrue((stat.getMode() & TESTMODE) > 0);

    stat = stat.toBuilder().setMode(0777).build();
    volume.setAttr(
        userCredentials, TESTDIR + "/" + TESTFILE, stat, Setattrs.SETATTR_MODE.getNumber());
    stat = volume.getAttr(userCredentials, TESTDIR + "/" + TESTFILE);
    assertTrue((stat.getMode() & 0777) == 0777);
  }