t6.pl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #!/usr/bin/perl
  2. #
  3. # Test script for the ownCloud module of csync.
  4. # This script requires a running ownCloud instance accessible via HTTP.
  5. # It does quite some fancy tests and asserts the results.
  6. #
  7. # Copyright (C) by Klaas Freitag <freitag@owncloud.com>
  8. #
  9. # This library is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU Lesser General Public
  11. # License as published by the Free Software Foundation; either
  12. # version 2.1 of the License, or (at your option) any later version.
  13. #
  14. # This library is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. # Lesser General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU Lesser General Public
  20. # License along with this library; if not, write to the Free Software
  21. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. #
  23. use lib ".";
  24. use File::Copy;
  25. use ownCloud::Test;
  26. use strict;
  27. print "Hello, this is t6, a tester for csync with ownCloud.\n";
  28. # Checking CURL is installed to avoid misleading errors later...
  29. system(("curl", "--help", ">", "/dev/null"));
  30. if ($? != 0) {
  31. print "CURL is needed for this script, aborting with error\n";
  32. exit 1;
  33. }
  34. initTesting();
  35. sub createPostUpdateScript($)
  36. {
  37. my ($name) = @_;
  38. my $srcFile = localDir().'BIG1.file';
  39. my $cred = configValue("user") . ":" . configValue("passwd");
  40. my $cmd = "curl -T $srcFile -u $cred --insecure " . testDirUrl().$name;
  41. my $script = "/tmp/post_update_script.sh";
  42. open SC, ">$script" || die("Can not create script file");
  43. print SC "#!/bin/bash\n";
  44. print SC "$cmd\n";
  45. close SC;
  46. chmod 0755, $script;
  47. return $script;
  48. }
  49. sub getETagFromJournal($$)
  50. {
  51. my ($name,$num) = @_;
  52. my $sql = "sqlite3 " . localDir() . "._sync_*.db \"SELECT md5 FROM metadata WHERE path='$name';\"";
  53. open(my $fh, '-|', $sql) or die $!;
  54. my $etag = <$fh>;
  55. close $fh;
  56. print "$num etag: $etag";
  57. return $etag;
  58. }
  59. sub chunkFileTest( $$ )
  60. {
  61. my ($name, $size) = @_;
  62. # Big file chunking
  63. createLocalFile( localDir().$name, $size );
  64. assert( -e localDir().$name );
  65. my $bigMd5 = md5OfFile( localDir().$name );
  66. csync();
  67. my $newMd5 = md5OfFile( localDir().$name );
  68. assert( $newMd5 eq $bigMd5, "Different MD5 sums!" );
  69. # download
  70. my $ctrlFile = "/tmp/file.download";
  71. getToFileCurl( $name, $ctrlFile );
  72. assert( -e $ctrlFile, "File does not exist!" );
  73. # assert files
  74. my $dlMd5 = md5OfFile( $ctrlFile );
  75. assert( $dlMd5 eq $newMd5, "Different MD5 sums 2" );
  76. unlink( $ctrlFile );
  77. }
  78. printInfo("Big file that needs chunking with default chunk size");
  79. chunkFileTest( "BIG1.file", 23251233 );
  80. printInfo("Update the existing file and trigger reupload");
  81. # change the existing file again -> update
  82. chunkFileTest( "BIG2.file", 21762122 );
  83. printInfo("Cause a precondition failed error");
  84. # Now overwrite the existing file to change it
  85. createLocalFile( localDir()."BIG3.file", 21832 );
  86. sleep(2);
  87. csync();
  88. createLocalFile( localDir().'BIG3.file', 34323 );
  89. sleep(2);
  90. # and create a post update script
  91. my $script = createPostUpdateScript('BIG3.file');
  92. $ENV{'OWNCLOUD_POST_UPDATE_SCRIPT'} = $script;
  93. # Save the etag before the sync
  94. my $firstETag = getETagFromJournal('BIG3.file', 'First');
  95. sleep(2);
  96. csync(); # Sync, which ends in a precondition failed error
  97. # get the etag again. It has to be unchanged because of the error.
  98. my $secondETag = getETagFromJournal('BIG3.file', 'Second');
  99. # Now the result is that there is a conflict file because since 1.7
  100. # the sync is stopped on preconditoin failed and done again.
  101. my $seen = 0;
  102. opendir(my $dh, localDir() );
  103. while(readdir $dh) {
  104. $seen = 1 if ( /BIG3_conflict.*\.file/ );
  105. }
  106. closedir $dh;
  107. assert( $seen == 1, "No conflict file created on precondition failed!" );
  108. unlink($script);
  109. $ENV{'OWNCLOUD_POST_UPDATE_SCRIPT'} = "";
  110. assertLocalAndRemoteDir( '', 1);
  111. # Set a custom chunk size in environment.
  112. my $ChunkSize = 1*1024*1024;
  113. $ENV{'OWNCLOUD_CHUNK_SIZE'} = $ChunkSize;
  114. printInfo("Big file exactly as big as one chunk size");
  115. chunkFileTest( "oneChunkSize.bin", $ChunkSize);
  116. printInfo("Big file exactly as big as one chunk size minus 1 byte");
  117. chunkFileTest( "oneChunkSizeminusone.bin", $ChunkSize-1);
  118. printInfo("Big file exactly as big as one chunk size plus 1 byte");
  119. chunkFileTest( "oneChunkSizeplusone.bin", $ChunkSize+1);
  120. printInfo("Big file exactly as big as 2*chunk size");
  121. chunkFileTest( "twoChunkSize.bin", 2*$ChunkSize);
  122. printInfo("Big file exactly as big as 2*chunk size minus 1 byte");
  123. chunkFileTest( "twoChunkSizeminusone.bin", 2*$ChunkSize-1);
  124. printInfo("Big file exactly as big as 2*chunk size plus 1 byte");
  125. chunkFileTest( "twoChunkSizeplusone.bin", 2*$ChunkSize+1);
  126. printInfo("Big file with many chunks");
  127. chunkFileTest( "bigFileManyChunk.bin", 10*$ChunkSize);
  128. printInfo("Big file with many chunks reuploaded twice (1)");
  129. createLocalFile( "BIG4.file", 21762122 );
  130. csync();
  131. assertLocalAndRemoteDir( '', 1);
  132. printInfo("Big file with many chunks reuploaded twice (2)");
  133. createLocalFile( "BIG4.file", 21783424 );
  134. csync();
  135. assertLocalAndRemoteDir( '', 1);
  136. # ==================================================================
  137. cleanup();
  138. # --