@Before public void createTreeNode() { // create a tree KeyPair keys = EncryptionUtil.generateRSAKeyPair(H2HConstants.KEYLENGTH_META_FILE); KeyPair protectionKeys = EncryptionUtil.generateRSAKeyPair(H2HConstants.KEYLENGTH_META_FILE); root = new FolderIndex(null, keys, null); root.addUserPermissions(new UserPermission(userId, PermissionType.WRITE)); root.setProtectionKeys(protectionKeys); // naming convention: // [number][type][index] where number is the level and type is either 'f' for file or 'd' for // directory. The index is to distinct two files/folders on the same level // setup is like // root: // - child1 // - child2 // - 1d: // - - child3 // - - 2d (empty folder) child1 = new FileIndex(root, keys, "1f1", null); child2 = new FileIndex(root, keys, "1f2", null); dir1 = new FolderIndex(root, keys, "1d"); child3 = new FileIndex(dir1, keys, "2f", null); dir2 = new FolderIndex(dir1, keys, "2d"); }
@Test public void testShare() { // set 1d to be shared dir1.share(EncryptionUtil.generateRSAKeyPair(H2HConstants.KEYLENGTH_META_FILE)); // 1d, 2f and 2d should return to be shared, others not Assert.assertTrue(dir1.isShared()); Assert.assertTrue(dir2.isShared()); Assert.assertTrue(child3.isShared()); Assert.assertFalse(root.isShared()); Assert.assertFalse(child1.isShared()); Assert.assertFalse(child2.isShared()); // set 1d to be not shared dir1.unshare(); // root, 1f1, 1f2, 1d, 2f and 2d should return to be not shared Assert.assertFalse(root.isShared()); Assert.assertFalse(dir1.isShared()); Assert.assertFalse(dir2.isShared()); Assert.assertFalse(child1.isShared()); Assert.assertFalse(child2.isShared()); Assert.assertFalse(child3.isShared()); }
@Test public void testHasShared() { // set 2d to be shared dir2.share(EncryptionUtil.generateRSAKeyPair(H2HConstants.KEYLENGTH_META_FILE)); // root, 1d and 2d should show that they contain a shared folder Assert.assertTrue(root.isSharedOrHasSharedChildren()); Assert.assertTrue(dir1.isSharedOrHasSharedChildren()); Assert.assertTrue(dir2.isSharedOrHasSharedChildren()); Assert.assertFalse(child1.isSharedOrHasSharedChildren()); Assert.assertFalse(child2.isSharedOrHasSharedChildren()); Assert.assertFalse(child3.isSharedOrHasSharedChildren()); // set 2d to be not shared dir2.unshare(); // root, 1f1, 1f2, 1d, 2f and 2d should not contain a shared folder Assert.assertFalse(root.isSharedOrHasSharedChildren()); Assert.assertFalse(dir1.isSharedOrHasSharedChildren()); Assert.assertFalse(dir2.isSharedOrHasSharedChildren()); Assert.assertFalse(child1.isSharedOrHasSharedChildren()); Assert.assertFalse(child2.isSharedOrHasSharedChildren()); Assert.assertFalse(child3.isSharedOrHasSharedChildren()); }
@Test public void testPermissions() { Assert.assertTrue(root.getCalculatedUserList().contains(userId)); Assert.assertEquals(1, root.getCalculatedUserList().size()); // add permission to sub-folder dir1.share( EncryptionUtil.generateRSAKeyPair(H2HConstants.KEYLENGTH_META_FILE), new UserPermission("UserB", PermissionType.READ)); // check the sub-folder and the sub-files permission Assert.assertEquals(2, dir1.getCalculatedUserList().size()); Assert.assertEquals(2, child3.getCalculatedUserList().size()); Assert.assertEquals(2, dir2.getCalculatedUserList().size()); Assert.assertFalse(dir1.canWrite("UserB")); Assert.assertFalse(dir2.canWrite("UserB")); // validate that the root still has only one user Assert.assertTrue(root.getCalculatedUserList().contains(userId)); Assert.assertEquals(1, root.getCalculatedUserList().size()); // add a third permission to the dir1 dir1.addUserPermissions(new UserPermission("UserC", PermissionType.WRITE)); // check again Assert.assertEquals(3, dir1.getCalculatedUserList().size()); Assert.assertEquals(3, child3.getCalculatedUserList().size()); Assert.assertEquals(3, dir2.getCalculatedUserList().size()); Assert.assertTrue(dir1.canWrite("UserC")); Assert.assertTrue(dir2.canWrite("UserC")); }
@Test(expected = IllegalStateException.class) public void testShareRoot() { root.share(EncryptionUtil.generateRSAKeyPair(H2HConstants.KEYLENGTH_META_FILE)); }