@Override protected Void doExecute() throws InvalidProcessStateException, ProcessExecutionException { String userId = context.consumeUserId(); logger.trace("Creating new user profile. user id ='{}'", userId); // generate keys KeyPair encryptionKeys = encryption.generateRSAKeyPair(H2HConstants.KEYLENGTH_USER_KEYS); KeyPair protectionKeys = encryption.generateRSAKeyPair(H2HConstants.KEYLENGTH_PROTECTION); context.provideUserProfile(new UserProfile(userId, encryptionKeys, protectionKeys)); return null; }
@Override public void modifyUserProfile(UserProfile userProfile) throws AbortModifyException { FolderIndex parentNode = (FolderIndex) userProfile.getFileById(parentKey); if (parentNode == null) { logger.error("Could not process the task because the parent node has not been found."); return; } // validate if the other sharer has the right to share if (parentNode.canWrite(sender)) { logger.debug("Rights of user '{}' checked. User is allowed to modify.", sender); } else { logger.error("Permission of user '{}' not found. Deny to apply this user's changes.", sender); return; } logger.debug("Newly shared file '{}' received.", addedFileIndex.getName()); // file is new, link parent and new child parentNode.addChild(addedFileIndex); addedFileIndex.setParent(parentNode); }
/** * Returns a list of files that have been deleted by another client during the absence of this client. * * @return a list of files that has been deleted remotely */ public List<File> getDeletedRemotely() { List<File> deletedRemotely = new ArrayList<File>(); for (String p : now.keySet()) { File file = new File(root, p); if (before.containsKey(p) && userProfile.getFileByPath(file, root) == null) { // is on disk but deleted in the user profile if (HashUtil.compare(before.get(p), now.get(p))) { // only delete the file, if it was not modified locally deletedRemotely.add(file); } } } logger.debug("Found {} files/folders that have been deleted remotely during absence.", deletedRemotely.size()); return deletedRemotely; }
private void checkFileIndex(File fileA, File fileB, byte[] hash) throws GetFailedException, NoSessionException { UserProfile userProfileA = nodeA.getSession().getProfileManager().readUserProfile(); FileIndex indexA = (FileIndex) userProfileA.getFileByPath(fileA, nodeA.getSession().getRootFile()); FileIndex indexB = (FileIndex) userProfileB.getFileByPath(fileB, nodeB.getSession().getRootFile()); Assert.assertFalse(indexA.getProtectionKeys().getPrivate().equals(userProfileA.getProtectionKeys().getPrivate())); Assert.assertFalse(indexA.getProtectionKeys().getPublic().equals(userProfileA.getProtectionKeys().getPublic()));
@Test public void testDeleteFolderInFolder() throws IOException, IllegalArgumentException, GetFailedException, InterruptedException, NoSessionException, NoPeerConnectionException { // add a folder to the network File folder = new File(root, randomString()); folder.mkdir(); UseCaseTestUtil.uploadNewFile(client, folder); // add a 2nd folder to the network File innerFolder = new File(folder, "inner-folder"); innerFolder.mkdir(); UseCaseTestUtil.uploadNewFile(client, innerFolder); // store some things to be able to test later UserProfile userProfileBeforeDeletion = UseCaseTestUtil.getUserProfile(client, userCredentials); KeyPair metaKeyPairFolder = userProfileBeforeDeletion.getFileByPath(folder, root).getFileKeys(); KeyPair metaKeyPairInnerFolder = userProfileBeforeDeletion.getFileByPath(innerFolder, root).getFileKeys(); // delete the inner folder UseCaseTestUtil.deleteFile(client, innerFolder); // check if the inner folder is still in the DHT UserProfile userProfile = UseCaseTestUtil.getUserProfile(client, userCredentials); Assert.assertNull(userProfile.getFileById(metaKeyPairInnerFolder.getPublic())); // check if the outer folder is still in the DHT Assert.assertNotNull(userProfile.getFileById(metaKeyPairFolder.getPublic())); }
private void checkMovedIndex(File movedFile, File root, UserProfile userProfile) throws GetFailedException, NoSessionException { Index index = userProfile.getFileByPath(movedFile, root); Assert.assertTrue(index.getProtectionKeys().getPrivate().equals(userProfile.getProtectionKeys().getPrivate())); Assert.assertTrue(index.getProtectionKeys().getPublic().equals(userProfile.getProtectionKeys().getPublic())); Assert.assertTrue(users.contains(userProfile.getUserId())); Set<UserPermission> permissions = ((FolderIndex) index).getCalculatedUserPermissions(); Assert.assertEquals(1, permissions.size()); Assert.assertTrue(permissions.contains(new UserPermission(userProfile.getUserId(), PermissionType.WRITE))); } else { Assert.assertTrue(index.isFile());
@Before public void setup() throws NoPeerConnectionException, IOException, GeneralSecurityException { userCredentials = generateRandomCredentials(); client = NetworkTestUtil.getRandomNode(network); // create an user profile UserProfile userProfile = new UserProfile(userCredentials.getUserId(), generateRSAKeyPair(H2HConstants.KEYLENGTH_USER_KEYS), generateRSAKeyPair(H2HConstants.KEYLENGTH_PROTECTION)); // encrypt it (fake encryption) EncryptedNetworkContent encrypted = client.getDataManager().getEncryption().encryptAES(userProfile, null); encrypted.setVersionKey(userProfile.getVersionKey()); encrypted.generateVersionKey(); // upload user profile, avoids register step IParameters parameters = new Parameters().setLocationKey(userCredentials.getProfileLocationKey()) .setContentKey(H2HConstants.USER_PROFILE).setVersionKey(encrypted.getVersionKey()) .setNetworkContent(encrypted).setProtectionKeys(userProfile.getProtectionKeys()) .setTTL(userProfile.getTimeToLive()); client.getDataManager().put(parameters); }
context.consumeUserId(), userProfile.getProtectionKeys()); params.setLocationsManager(locationsManager); PublicKeyManager keyManager = new PublicKeyManager(userProfile.getUserId(), userProfile.getEncryptionKeys(), userProfile.getProtectionKeys(), networkManager.getDataManager());
@Test public void testDirectoryToRoot() throws IOException, IllegalArgumentException, GetFailedException, InterruptedException, NoSessionException, NoPeerConnectionException { // add the source folder File folder = new File(root, "from-source-folder-to-root"); folder.mkdir(); UseCaseTestUtil.uploadNewFile(client, folder); // add a file to the folder File file = new File(folder, "test-file-from-folder-to-root"); FileUtils.write(file, randomString()); UseCaseTestUtil.uploadNewFile(client, file); File destination = new File(root, file.getName()); // move the file UseCaseTestUtil.moveFile(client, file, destination); // check that the user profile has a correct entry UserProfile userProfile = UseCaseTestUtil.getUserProfile(client, userCredentials); Index fileNode = userProfile.getFileByPath(destination, root); Assert.assertNotNull(fileNode); Assert.assertEquals(userProfile.getRoot(), fileNode.getParent()); }
@Test public void testGetFileById() { FolderIndex root = userProfile.getRoot(); KeyPair child1Key = generateRSAKeyPair(RSA_KEYLENGTH.BIT_512); FolderIndex child1Folder = new FolderIndex(root, child1Key, randomString()); KeyPair child2Key = generateRSAKeyPair(RSA_KEYLENGTH.BIT_1024); new FileIndex(root, child2Key, randomString(), "bla".getBytes()); KeyPair child3Key = generateRSAKeyPair(RSA_KEYLENGTH.BIT_2048); new FileIndex(child1Folder, child3Key, randomString(), "blubb".getBytes()); Assert.assertNotNull(userProfile.getFileById(child1Key.getPublic())); Assert.assertNotNull(userProfile.getFileById(child2Key.getPublic())); Assert.assertNotNull(userProfile.getFileById(child3Key.getPublic())); }
/** * @param rootDirectory the root Hive2Hive directory * @param userProfile the current user profile * @param before represents the file state at the last logout, before H2H was shutdown. The key of the map * is the path, the byte[] is the hash of the file content. * {@link FileSynchronizer#visitFiles(File)} can be used to generate this map. * @param now represents the current file state. The key of the map is the path, the byte[] is the hash of * the file content. {@link FileSynchronizer#visitFiles(File)} can be used to generate this * map. */ public FileSynchronizer(File rootDirectory, UserProfile userProfile, Map<String, byte[]> before, Map<String, byte[]> now) { this.root = rootDirectory; this.userProfile = userProfile; this.before = before; this.now = now; this.profileRootNode = userProfile.getRoot(); }
userProfile = new UserProfile("test-user", keys, keys); root = userProfile.getRoot(); node1f1 = new FileIndex(root, keys, "1f1", HashUtil.hash(file1f1)); node1f2 = new FileIndex(root, keys, "1f2", HashUtil.hash(file1f2));
public KeyPair consumeUserProfileProtectionKeys() { return profile.getProtectionKeys(); }
@Test public void testRegisterProcessSuccess() throws InvalidProcessStateException, ClassNotFoundException, IOException, GetFailedException, NoPeerConnectionException { NetworkManager client = network.get(0); NetworkManager otherClient = network.get(1); UserCredentials credentials = generateRandomCredentials(); UseCaseTestUtil.register(credentials, client); // verify put user profile UserProfile getUserProfile = UseCaseTestUtil.getUserProfile(otherClient, credentials); assertNotNull(getUserProfile); assertEquals(credentials.getUserId(), getUserProfile.getUserId()); // verify put locations Locations locations = (Locations) otherClient.getDataManager().get( new Parameters().setLocationKey(credentials.getUserId()).setContentKey(H2HConstants.USER_LOCATIONS)); assertNotNull(locations); assertEquals(credentials.getUserId(), locations.getUserId()); assertTrue(locations.getPeerAddresses().isEmpty()); // verify put user public key UserPublicKey publicKey = (UserPublicKey) otherClient.getDataManager().get( new Parameters().setLocationKey(credentials.getUserId()).setContentKey(H2HConstants.USER_PUBLIC_KEY)); assertNotNull(publicKey); }
public Index getFileById(PublicKey fileId) { return findById(root, fileId); }
@Override protected Void doExecute() throws InvalidProcessStateException, ProcessExecutionException { UserPublicKey publicKey = new UserPublicKey(context.consumeUserProfile().getEncryptionKeys().getPublic()); try { put(context.consumeUserId(), H2HConstants.USER_PUBLIC_KEY, publicKey, context.consumeUserPublicKeyProtectionKeys()); } catch (PutFailedException ex) { throw new ProcessExecutionException(this, ex); } return null; }
private void checkIndexAfterMoving(File oldFileA, File oldFileB, File newFileA, File newFileB) throws GetFailedException, NoSessionException { UserProfile userProfileA = nodeA.getSession().getProfileManager().readUserProfile(); Index oldIndexAtA = userProfileA.getFileByPath(oldFileA, nodeA.getSession().getRootFile()); Index newIndexAtA = userProfileA.getFileByPath(newFileA, nodeA.getSession().getRootFile()); Index oldIndexAtB = userProfileB.getFileByPath(oldFileB, nodeB.getSession().getRootFile()); Index newIndexAtB = userProfileB.getFileByPath(newFileB, nodeB.getSession().getRootFile()); .equals(userProfileA.getProtectionKeys().getPrivate())); Assert.assertFalse(newIndexAtA.getProtectionKeys().getPublic().equals(userProfileA.getProtectionKeys().getPublic()));
@Test public void testDeleteFolder() throws FileNotFoundException, IllegalArgumentException, GetFailedException, InterruptedException, NoSessionException, NoPeerConnectionException { // add a folder to the network File folder = new File(root, randomString()); folder.mkdir(); UseCaseTestUtil.uploadNewFile(client, folder); // store some keys before deletion UserProfile userProfileBeforeDeletion = UseCaseTestUtil.getUserProfile(client, userCredentials); KeyPair metaKeyPair = userProfileBeforeDeletion.getFileByPath(folder, root).getFileKeys(); // delete the folder UseCaseTestUtil.deleteFile(client, folder); // check if the folder is still in the DHT UserProfile userProfile = UseCaseTestUtil.getUserProfile(client, userCredentials); Assert.assertNull(userProfile.getFileById(metaKeyPair.getPublic())); }
private void checkIndexesAfterMoving(File oldFileA, File oldFileB, File newFileA) throws GetFailedException, NoSessionException { UserProfile userProfileA = nodeA.getSession().getProfileManager().readUserProfile(); Index indexOldA = userProfileA.getFileByPath(oldFileA, nodeA.getSession().getRootFile()); Index indexOldB = userProfileB.getFileByPath(oldFileB, nodeB.getSession().getRootFile()); Index indexNew = userProfileA.getFileByPath(newFileA, nodeA.getSession().getRootFile()); Assert.assertTrue(indexNew.getProtectionKeys().getPrivate().equals(userProfileA.getProtectionKeys().getPrivate())); Assert.assertTrue(indexNew.getProtectionKeys().getPublic().equals(userProfileA.getProtectionKeys().getPublic())); Assert.assertTrue(users.contains(userProfileA.getUserId())); Assert.assertTrue(permissions.contains(new UserPermission(userProfileA.getUserId(), PermissionType.WRITE)));
@Test public void getFileByPath() throws IOException { FolderIndex root = userProfile.getRoot(); // tree in the UP FolderIndex folderIndex1 = new FolderIndex(root, generateRSAKeyPair(RSA_KEYLENGTH.BIT_512), randomString()); FileIndex fileIndex1 = new FileIndex(folderIndex1, generateRSAKeyPair(RSA_KEYLENGTH.BIT_512), randomString(), "bla".getBytes()); FileIndex fileIndex2 = new FileIndex(folderIndex1, generateRSAKeyPair(RSA_KEYLENGTH.BIT_512), randomString(), "blubb".getBytes()); FolderIndex folderIndex2 = new FolderIndex(folderIndex1, generateRSAKeyPair(RSA_KEYLENGTH.BIT_512), randomString()); FileIndex fileIndex3 = new FileIndex(folderIndex2, generateRSAKeyPair(RSA_KEYLENGTH.BIT_512), randomString(), "bla".getBytes()); // tree on the FS File rootFile = tempFolder.newFolder(); File folder1 = new File(rootFile, folderIndex1.getName()); File file1 = new File(folder1, fileIndex1.getName()); File file2 = new File(folder1, fileIndex2.getName()); File folder2 = new File(folder1, folderIndex2.getName()); File file3 = new File(folder2, fileIndex3.getName()); Assert.assertEquals(root, userProfile.getFileByPath(rootFile, rootFile)); Assert.assertEquals(folderIndex1, userProfile.getFileByPath(folder1, rootFile)); Assert.assertEquals(fileIndex1, userProfile.getFileByPath(file1, rootFile)); Assert.assertEquals(fileIndex2, userProfile.getFileByPath(file2, rootFile)); Assert.assertEquals(folderIndex2, userProfile.getFileByPath(folder2, rootFile)); Assert.assertEquals(fileIndex3, userProfile.getFileByPath(file3, rootFile)); } }