Package org.syncany.tests.util

Examples of org.syncany.tests.util.TestClient


  @Test
  public void testRestoreDeletedFileWithTargetFile() throws Exception {
    // Setup
    File tempDir = TestFileUtil.createTempDirectoryInSystemTemp();
    TransferSettings testConnection = TestConfigUtil.createTestLocalConnection();   
    TestClient clientA = new TestClient("A", testConnection);
    java.sql.Connection databaseConnectionA = DatabaseConnectionFactory.createConnection(clientA.getDatabaseFile());
       
    // A new/up
    clientA.createNewFile("A-original");   
    clientA.upWithForceChecksum();
   
    String originalFileHistoryStr = TestSqlUtil.runSqlSelect("select filehistory_id from fileversion", databaseConnectionA);
    assertNotNull(originalFileHistoryStr);
   
    FileHistoryId originalFileHistoryId = FileHistoryId.parseFileId(originalFileHistoryStr);
       
    // A "delete"
    File deletedFile = new File(tempDir, "A-original-DELETED");
    FileUtils.moveFile(clientA.getLocalFile("A-original"), deletedFile);
           
    clientA.upWithForceChecksum();
   
    // A restore
    RestoreOperationOptions operationOptions = new RestoreOperationOptions();
   
    operationOptions.setFileHistoryId(originalFileHistoryId);
    operationOptions.setFileVersion(1);
    operationOptions.setRelativeTargetPath("restored-file");
   
    clientA.restore(operationOptions);
   
    assertTrue(clientA.getLocalFile("restored-file").exists());
    assertEquals(
        StringUtil.toHex(TestFileUtil.createChecksum(deletedFile)),
        StringUtil.toHex(TestFileUtil.createChecksum(clientA.getLocalFile("restored-file"))));
    assertEquals(deletedFile.lastModified(), clientA.getLocalFile("restored-file").lastModified());
    assertEquals(deletedFile.length(), clientA.getLocalFile("restored-file").length());
   
    // Tear down
    clientA.deleteTestData();
    TestFileUtil.deleteDirectory(tempDir);
  }
View Full Code Here


  @Test
  public void testRestoreDeletedWithSubfolders() throws Exception {
    // Setup
    File tempDir = TestFileUtil.createTempDirectoryInSystemTemp();
    TransferSettings testConnection = TestConfigUtil.createTestLocalConnection();   
    TestClient clientA = new TestClient("A", testConnection);
    java.sql.Connection databaseConnectionA = DatabaseConnectionFactory.createConnection(clientA.getDatabaseFile());
       
    // A new/up
    clientA.createNewFolder("folder/subfolder");
    clientA.createNewFile("folder/subfolder/A-original");   
    clientA.upWithForceChecksum();
   
    String originalFileHistoryStr = TestSqlUtil.runSqlSelect("select filehistory_id from fileversion where path='folder/subfolder/A-original'", databaseConnectionA);
    assertNotNull(originalFileHistoryStr);
   
    FileHistoryId originalFileHistoryId = FileHistoryId.parseFileId(originalFileHistoryStr);
       
    // A "delete"
    FileUtils.deleteDirectory(clientA.getLocalFile("folder"));           
    clientA.upWithForceChecksum();
   
    assertFalse(clientA.getLocalFile("folder").exists());

    // A restore
    RestoreOperationOptions operationOptions = new RestoreOperationOptions();
   
    operationOptions.setFileHistoryId(originalFileHistoryId);
    operationOptions.setFileVersion(1);
   
    clientA.restore(operationOptions);
   
    assertTrue(clientA.getLocalFile("folder/subfolder").exists());
    assertTrue(clientA.getLocalFile("folder/subfolder/A-original (restored version 1)").exists());
   
    // Tear down
    clientA.deleteTestData();
    TestFileUtil.deleteDirectory(tempDir);
  }
View Full Code Here

public class ChangedAttributesScenarioTest {
  @Test
  public void testChangeAttributes() throws Exception {   
    // Setup
    TransferSettings testConnection = TestConfigUtil.createTestLocalConnection();   
    TestClient clientA = new TestClient("A", testConnection);
    TestClient clientB = new TestClient("B", testConnection);

    // Run
    clientA.createNewFile("file1.jpg");
    clientA.upWithForceChecksum();
   
    clientB.down();
   
    File bFile = clientB.getLocalFile("file1.jpg");
    Path bFilePath = Paths.get(bFile.getAbsolutePath());
   
    if (EnvironmentUtil.isWindows()) {
      Files.setAttribute(bFilePath, "dos:readonly", true);
    }
    else if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
      Files.setPosixFilePermissions(bFilePath, PosixFilePermissions.fromString("rwxrwxrwx"));
    }   
   
    StatusOperationResult statusResult = clientB.status();
    assertNotNull(statusResult);
   
    ChangeSet changes = statusResult.getChangeSet();
   
    assertTrue("Status-Operation should return changes.", changes.hasChanges());
    UpOperationResult upResult = clientB.up();
    StatusOperationResult statusResultFromUp = upResult.getStatusResult();
   
    // Test 1: Check result sets for inconsistencies
    assertTrue("Status should return changes.", statusResultFromUp.getChangeSet().hasChanges());
    assertTrue("File should be uploaded.", upResult.getChangeSet().hasChanges());
   
    // Test 2: Check database for inconsistencies
    SqlDatabase database = clientB.loadLocalDatabase();

    assertNotNull("File should be uploaded.", database.getFileVersionByPath("file1.jpg"));   
    assertEquals("There should be a new database version, because file should not have been added.", 2, database.getLocalDatabaseBranch().size());
   
    // B down
    clientA.down();

    // Test 1: file1.jpg permissions
    File aFile = clientA.getLocalFile("file1.jpg");
    Path aFilePath = Paths.get(aFile.getAbsolutePath());
   
    if (EnvironmentUtil.isWindows()) {
      Object readOnlyAttribute = Files.getAttribute(aFilePath, "dos:readonly");
      assertTrue("Read-only should be true.", (Boolean) readOnlyAttribute);
    }
    else if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
      Set<PosixFilePermission> posixFilePermissions = Files.getPosixFilePermissions(aFilePath);
      assertEquals("Should be rwxrwxrwx.", "rwxrwxrwx", PosixFilePermissions.toString(posixFilePermissions));
   
   
    // Test 2: The rest
    assertFileListEquals(clientA.getLocalFilesExcludeLockedAndNoRead(), clientB.getLocalFilesExcludeLockedAndNoRead());
    assertSqlDatabaseEquals(clientA.getDatabaseFile(), clientB.getDatabaseFile());
   
    // Tear down
    clientA.deleteTestData();
    clientB.deleteTestData();
 
View Full Code Here

 
  @Test
  public void testNewFileWithDifferingAttributes() throws Exception {   
    // Setup
    TransferSettings testConnection = TestConfigUtil.createTestLocalConnection();   
    TestClient clientA = new TestClient("A", testConnection);
    TestClient clientB = new TestClient("B", testConnection);

    // Create new file with differing attributes
    clientA.createNewFile("file1.jpg");
    FileUtils.copyFile(clientA.getLocalFile("file1.jpg"), clientB.getLocalFile("file1.jpg"));
   
    File aFile = clientA.getLocalFile("file1.jpg"); // Client B's attributes differ!
    Path aFilePath = Paths.get(aFile.getAbsolutePath());   
   
    Object aReadOnlyAttribute = null;
    Set<PosixFilePermission> aPosixFilePermissions = null;
   
    File bFile = clientB.getLocalFile("file1.jpg"); // Client B's attributes differ!
    Path bFilePath = Paths.get(bFile.getAbsolutePath());
   
    if (EnvironmentUtil.isWindows()) {
      aReadOnlyAttribute = Files.getAttribute(aFilePath, "dos:readonly");
      Files.setAttribute(bFilePath, "dos:readonly", true);
    }
    else if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
      aPosixFilePermissions = Files.getPosixFilePermissions(aFilePath);
      Files.setPosixFilePermissions(bFilePath, PosixFilePermissions.fromString("rwxrwxrwx"));
   
       
    clientA.upWithForceChecksum();   
    DownOperationResult downResult = clientB.down(); // This is the key operation
   
    // Test 1: Check result sets for inconsistencies
    assertTrue("File should be downloaded.", downResult.getChangeSet().hasChanges());
   
    // Test 2: file1.jpg permissions (again!
    if (EnvironmentUtil.isWindows()) {
      Object bReadOnlyAttribute = Files.getAttribute(aFilePath, "dos:readonly");
      assertEquals("Read-only should be true.", aReadOnlyAttribute, bReadOnlyAttribute);
    }
    else if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
      Set<PosixFilePermission> bPosixFilePermissions = Files.getPosixFilePermissions(aFilePath);
      assertEquals("Should be rwxrwxrwx.", PosixFilePermissions.toString(aPosixFilePermissions), PosixFilePermissions.toString(bPosixFilePermissions));
   
   
    // Test 3: The rest
    assertFileListEquals(clientA.getLocalFilesExcludeLockedAndNoRead(), clientB.getLocalFilesExcludeLockedAndNoRead());
    assertSqlDatabaseEquals(clientA.getDatabaseFile(), clientB.getDatabaseFile());
   
    // Tear down
    clientA.deleteTestData();
    clientB.deleteTestData();
 
View Full Code Here

  @Test
  public void testUpWhileWritingFile() throws Exception {
    // Setup
    final TransferSettings testConnection = TestConfigUtil.createTestLocalConnection();

    final TestClient clientA = new TestClient("A", testConnection);
    final TestClient clientB = new TestClient("B", testConnection);

    final File testFile = clientA.getLocalFile("large-test-file");
    final long testFileLength = 100 * 1024 * 1024;

    Thread writeFileThread = new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          logger.log(Level.SEVERE, "Started thread to write file to " + testFile + "  ...");

          FileOutputStream fos = new FileOutputStream(testFile);
          Random randomEngine = new Random();

          byte[] buf = new byte[4096];
          int writtenLen = 0;

          while (writtenLen < testFileLength) {
            randomEngine.nextBytes(buf);
            fos.write(buf, 0, buf.length);

            writtenLen += buf.length;
          }

          fos.close();

          logger.log(Level.SEVERE, "Ended thread to write file to " + testFile + "  ...");
        }
        catch (Exception e) {
          // Nothing.
        }
      }
    });

    Logging.setGlobalLogLevel(Level.SEVERE);

    // Before start: setup up databases (takes a while)
    clientA.status();
    clientB.status();

    // Run!
    writeFileThread.start();

    Thread.sleep(50);

    logger.log(Level.SEVERE, "Started clientA.up()");
    UpOperationResult upResult = clientA.up();
    StatusOperationResult statusResult = upResult.getStatusResult();
    logger.log(Level.SEVERE, "Ended clientA.up()");

    writeFileThread.join();

    // Test 1: Check result sets for inconsistencies
    assertTrue("Status command expected to return changes.", statusResult.getChangeSet().hasChanges());
    assertFalse("File should NOT be uploaded while still writing (no half-file upload).", upResult.getChangeSet().hasChanges());

    // Test 2: Check database for inconsistencies
    SqlDatabase database = clientA.loadLocalDatabase();

    assertNull("File should NOT be uploaded while still writing (no half-file upload).", database.getFileVersionByPath("large-test-file"));
    assertNull("There should NOT be a new database version, because file should not have been added.", database.getLastDatabaseVersionHeader());

    // Test 3: Check file system for inconsistencies
    File repoPath = new File(((LocalTransferSettings) testConnection).getPath() + "/databases");
    String[] repoFileList = repoPath.list(new FilenameFilter() {
      @Override
      public boolean accept(File dir, String name) {
        return name.startsWith("database-");
      }
    });

    assertEquals("Repository should NOT contain any files.", 0, repoFileList.length);

    // Tear down
    clientA.deleteTestData();
    clientB.deleteTestData();
  }
View Full Code Here

   */
  @Test
  public void testAssembler() throws Exception
    LocalTransferSettings testConnection = (LocalTransferSettings) TestConfigUtil.createTestLocalConnection();
   
    TestClient clientA = new TestClient("A", testConnection);
    TestClient clientB = new TestClient("B", testConnection);
   
    clientA.createNewFile("file1.jpg", 20); // small, only one chunk, one multichunk
    clientA.upWithForceChecksum();
   
    // Read chunk from original multichunk
    File repoMultiChunksFolder = new File(testConnection.getPath(), "multichunks");
    File multiChunkFile = repoMultiChunksFolder.listFiles()[0];
   
    MultiChunk multiChunk = clientA.getConfig().getMultiChunker().createMultiChunk(
      clientA.getConfig().getTransformer().createInputStream(new FileInputStream(multiChunkFile)));   
    Chunk chunk = multiChunk.read();
    multiChunk.close();
   
    // Flip byte in chunk and write new "altered" multichunk
    File alteredMultiChunkFile = new File(multiChunkFile + "-altered");
    MultiChunk alteredMultiChunk = clientA.getConfig().getMultiChunker().createMultiChunk(
      multiChunk.getId(), clientA.getConfig().getTransformer().createOutputStream(new FileOutputStream(alteredMultiChunkFile)));
   
    chunk.getContent()[0] ^= 0x01; // Flip one byte!
    alteredMultiChunk.write(chunk);
    alteredMultiChunk.close();
   
    // Now delete old multichunk, and swap by "altered" file
    multiChunkFile.delete();
    FileUtils.moveFile(alteredMultiChunkFile, multiChunkFile);
   
    boolean exceptionThrown = false;
   
    try {
      clientB.down(); // If this does not throw an exception, it's bad!
    }
    catch (Exception e) {
      exceptionThrown = true;
    }
   
    assertTrue(exceptionThrown);
   
    clientA.deleteTestData();
    clientB.deleteTestData();
 
View Full Code Here

public class DirtyDatabaseVersionsScenarioTest {
  @Test
  public void testThreeClientsOneLoser() throws Exception {   
    // Setup
    TransferSettings testConnection = TestConfigUtil.createTestLocalConnection();   
    TestClient clientA = new TestClient("A", testConnection);
    TestClient clientB = new TestClient("B", testConnection);
    TestClient clientC = new TestClient("C", testConnection);

    UpOperationOptions cUpOptionsWithForce = new UpOperationOptions();
    cUpOptionsWithForce.setForceUploadEnabled(true);
   
    // Run
    clientA.createNewFile("file1.jpg");
    clientA.upWithForceChecksum();
   
    clientB.down();   
    clientB.createNewFile("file2.jpg");
    clientB.upWithForceChecksum();
   
    clientC.createNewFile("file3.jpg"); // Client C: No down!
    clientC.up(cUpOptionsWithForce);   
   
    // A tries to upload, this fails due to C's unknown database
    clientA.createNewFile("file4.jpg");
    UpOperationResult aUpResult = clientA.upWithForceChecksum(); //
    assertEquals("Expected to fail, because db-C-1 has not been looked at", UpResultCode.NOK_UNKNOWN_DATABASES, aUpResult.getResultCode());
    assertFalse(clientA.getLocalFile("file2.jpg").exists());
    assertFalse(clientA.getLocalFile("file3.jpg").exists());
   
    // A downloads C's changes, no file changes are expected
    DownOperationResult aDownResult = clientA.down();
    assertEquals("Expected to succeed with remote changes (a new database file, but no file changes!).", DownResultCode.OK_WITH_REMOTE_CHANGES, aDownResult.getResultCode());
    assertTrue(clientA.getLocalFile("file2.jpg").exists());
    assertFalse(clientA.getLocalFile("file3.jpg").exists());
   
    // TODO [low] Add assert: "no file changes are expected"
   
    // A uploads again, this time it should succeed, because C's file is in knowndbs.list
    aUpResult = clientA.upWithForceChecksum();
    assertEquals("Expected to succeed, because db-C-1 has already been looked at", UpResultCode.OK_CHANGES_UPLOADED, aUpResult.getResultCode());
   
    // C calls down and up, to sync its changes
    clientC.down(); // Adds dirty database
    assertSqlResultEquals(clientC.getDatabaseFile(), "select count(*) from databaseversion where status='DIRTY'", "1");
   
    clientC.upWithForceChecksum();
    assertSqlResultEquals(clientC.getDatabaseFile(), "select count(*) from databaseversion where status='DIRTY'", "0");
   
    clientA.down();
    assertFileListEquals(clientA.getLocalFilesExcludeLockedAndNoRead(), clientC.getLocalFilesExcludeLockedAndNoRead());   
   
    // Tear down
    clientA.deleteTestData();
    clientB.deleteTestData();
    clientC.deleteTestData();
 
View Full Code Here

  private Map<Integer, Response> responses = new HashMap<Integer, Response>();
 
  @Test
  public void testNoCliRequestWhileSyncing() throws Exception {
    final TransferSettings testConnection = TestConfigUtil.createTestLocalConnection();   
    final TestClient clientA = new TestClient("ClientA", testConnection);
    int port = 58444;
   
    // Load config template
    DaemonConfigTO daemonConfig = TestDaemonUtil.loadDaemonConfig("daemonOneFolderNoWebServer.xml");
   
    // Set port to prevent conflicts with default daemons
    daemonConfig.getWebServer().setBindPort(port);
   
    // Dynamically insert paths
    daemonConfig.getFolders().get(0).setPath(clientA.getConfig().getLocalDir().getAbsolutePath());
   
    // Create access token (not needed in this test, but prevents errors in daemon)
    daemonConfig.setPortTO(TestDaemonUtil.createPortTO(port));
   
    // Register to event bus     
    LocalEventBus localEventBus = LocalEventBus.getInstance();
    localEventBus.register(this);
       
    // Prepare CLI request
    StatusFolderRequest cliStatusRequest = new StatusFolderRequest();
    cliStatusRequest.setId(2586);
    cliStatusRequest.setRoot(clientA.getConfig().getLocalDir().getAbsolutePath());
   
    // Create watchServer
    WatchServer watchServer = new WatchServer()
    watchServer.start(daemonConfig);   
    Thread.sleep(1000); // Settlement for watch server
   
    // Create large file, then wait 3sec for the settlement timer and
    // send the CLI request at the same time   
    clientA.createNewFile("largefile", 10*1024*1024);
    Thread.sleep(3000); // Settlement in Watcher!
   
    localEventBus.post(cliStatusRequest);
   
    // Then, let's hope the response is "no, no, no!"
    Response response = waitForResponse(2586);
   
    assertTrue(response instanceof StatusFolderResponse);
    StatusFolderResponse cliResponse = (StatusFolderResponse) response;
   
    //assertEquals("Cannot run CLI commands while sync is running or requested.\n", cliResponse.getOutput());
   
    watchServer.stop();
    clientA.deleteTestData();
  }
View Full Code Here

     * multichunks of DIRTY database versions of other clients.
     */
   
    // Setup
    LocalTransferSettings testConnection = (LocalTransferSettings) TestConfigUtil.createTestLocalConnection();   
    TestClient clientA = new TestClient("A", testConnection);
    TestClient clientB = new TestClient("B", testConnection);

    UpOperationOptions upOptionsWithForce = new UpOperationOptions();
    upOptionsWithForce.setForceUploadEnabled(true);
   
    // Run
    clientA.createNewFile("file1.jpg");
    clientA.up();
   
    clientB.down();   
    clientB.createNewFile("file2.jpg");
    clientB.up();
   
    clientA.createNewFile("file3.jpg");
    clientA.up(upOptionsWithForce);    // No 'down' (creates dirty version) 
   
    FileUtils.copyDirectory(testConnection.getPath(), new File(testConnection.getPath()+"_1_before_down"));
    FileUtils.copyDirectory(clientB.getConfig().getLocalDir(), new File(clientB.getConfig().getLocalDir()+"_1_before_down"));

    assertSqlResultEquals(clientB.getDatabaseFile(), "select count(*) from multichunk_muddy", "0");

    clientB.down(); // Download A's version, realize it's muddy (= dirty by other client)
   
    assertSqlResultEquals(clientB.getDatabaseFile(), "select count(*) from multichunk_muddy", "1");
   
    FileUtils.copyDirectory(testConnection.getPath(), new File(testConnection.getPath()+"_2_after_down_before_cleanup"));
    FileUtils.copyDirectory(clientB.getConfig().getLocalDir(), new File(clientB.getConfig().getLocalDir()+"_2_after_down_before_cleanup"));
   
    assertEquals(3, new File(testConnection.getPath(), "multichunks").listFiles().length);
    clientB.cleanup(); // <<< This should NOT delete any lost multichunks of DIRTY database versions!
    assertEquals(3, new File(testConnection.getPath(), "multichunks").listFiles().length);
   
    FileUtils.copyDirectory(testConnection.getPath(), new File(testConnection.getPath()+"_3_after_cleanup"));
    FileUtils.copyDirectory(clientB.getConfig().getLocalDir(), new File(clientB.getConfig().getLocalDir()+"_3_after_cleanup"));
   
    clientA.down(); // Adds dirty database
    assertSqlResultEquals(clientA.getDatabaseFile(), "select count(*) from databaseversion where status='DIRTY'", "1");
   
    clientA.up();
    assertSqlResultEquals(clientA.getDatabaseFile(), "select count(*) from databaseversion where status='DIRTY'", "0");

    clientB.down();
    assertFileListEquals(clientA.getLocalFilesExcludeLockedAndNoRead(), clientB.getLocalFilesExcludeLockedAndNoRead());   
    assertSqlResultEquals(clientB.getDatabaseFile(), "select count(*) from multichunk_muddy", "0");
   
    // Tear down
    clientA.deleteTestData();
    clientB.deleteTestData();
 
View Full Code Here

     * multichunks of DIRTY database versions of other clients.
     */
   
    // Setup
    LocalTransferSettings testConnection = (LocalTransferSettings) TestConfigUtil.createTestLocalConnection();   
    TestClient clientA = new TestClient("A", testConnection);
    TestClient clientB = new TestClient("B", testConnection);

    UpOperationOptions upOptionsWithForce = new UpOperationOptions();
    upOptionsWithForce.setForceUploadEnabled(true);
   
    // Run
   
    // Round 1
    clientA.createNewFile("file1.jpg");
    clientA.up();
   
    clientB.down();   
    clientB.createNewFile("file2.jpg");
    clientB.up();
   
    clientA.createNewFile("file3.jpg");
    clientA.up(upOptionsWithForce);    // No 'down' (creates dirty version) 
   
    clientB.down(); // Download A's version, realize it's muddy (= dirty by other client)
    assertSqlResultEquals(clientB.getDatabaseFile(), "select count(*) from multichunk_muddy", "1");

    assertEquals(3, new File(testConnection.getPath(), "multichunks").listFiles().length);
    clientB.cleanup(); // <<< This should NOT delete any lost multichunks of DIRTY database versions!
    assertEquals(3, new File(testConnection.getPath(), "multichunks").listFiles().length);
   
    // Round 2
    clientA.createNewFile("file4.jpg");
    clientA.up(upOptionsWithForce);    // This creates ANOTHER dirty version remotely! 
   
    clientB.down(); // Download A's version, realize it's muddy (= dirty by other client)
    assertSqlResultEquals(clientB.getDatabaseFile(), "select count(*) from multichunk_muddy", "2");
   
    assertEquals(4, new File(testConnection.getPath(), "multichunks").listFiles().length);
    clientB.cleanup(); // <<< This should NOT delete any lost multichunks of DIRTY database versions!
    assertEquals(4, new File(testConnection.getPath(), "multichunks").listFiles().length);

    clientA.down(); // Adds dirty databases
    assertSqlResultEquals(clientA.getDatabaseFile(), "select count(*) from databaseversion where status='DIRTY'", "2");
   
    clientA.up();
    assertSqlResultEquals(clientA.getDatabaseFile(), "select count(*) from databaseversion where status='DIRTY'", "0");

    clientB.down();
    assertFileListEquals(clientA.getLocalFilesExcludeLockedAndNoRead(), clientB.getLocalFilesExcludeLockedAndNoRead());   
    assertSqlResultEquals(clientB.getDatabaseFile(), "select count(*) from multichunk_muddy", "0");
   
    // Tear down
    clientA.deleteTestData();
    clientB.deleteTestData();
 
View Full Code Here

TOP

Related Classes of org.syncany.tests.util.TestClient

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.