Changeset 2535 for box/trunk/test


Ignore:
Timestamp:
28/06/2009 13:04:25 (3 years ago)
Author:
chris
Message:

Update reference counts when files and directories are uploaded.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • box/trunk/test/backupstore/testbackupstore.cpp

    r2481 r2535  
    3535#include "BackupClientCryptoKeys.h" 
    3636#include "ServerControl.h" 
     37#include "BackupStoreAccountDatabase.h" 
     38#include "BackupStoreRefCountDatabase.h" 
    3739 
    3840#include "MemLeakFindOn.h" 
     
    471473} 
    472474 
    473 int64_t create_test_data_subdirs(BackupProtocolClient &protocol, int64_t indir, const char *name, int depth) 
     475void create_file_in_dir(std::string name, std::string source, int64_t parentId, 
     476        BackupProtocolClient &protocol, BackupStoreRefCountDatabase& rRefCount) 
     477{ 
     478        BackupStoreFilenameClear name_encoded("file_One"); 
     479        std::auto_ptr<IOStream> upload(BackupStoreFile::EncodeFile( 
     480                source.c_str(), parentId, name_encoded)); 
     481        std::auto_ptr<BackupProtocolClientSuccess> stored( 
     482                protocol.QueryStoreFile( 
     483                        parentId, 
     484                        0x123456789abcdefLL,            /* modification time */ 
     485                        0x7362383249872dfLL,            /* attr hash */ 
     486                        0,                              /* diff from ID */ 
     487                        name_encoded, 
     488                        *upload)); 
     489        int64_t objectId = stored->GetObjectID(); 
     490        TEST_EQUAL(objectId, rRefCount.GetLastObjectIDUsed()); 
     491        TEST_EQUAL(1, rRefCount.GetRefCount(objectId)) 
     492} 
     493 
     494int64_t create_test_data_subdirs(BackupProtocolClient &protocol, int64_t indir, 
     495        const char *name, int depth, BackupStoreRefCountDatabase& rRefCount) 
    474496{ 
    475497        // Create a directory 
     
    487509         
    488510        printf("Create subdirs, depth = %d, dirid = %llx\n", depth, subdirid); 
     511 
     512        std::auto_ptr<BackupStoreAccountDatabase> apAccounts( 
     513                BackupStoreAccountDatabase::Read("testfiles/accounts.txt")); 
     514        std::auto_ptr<BackupStoreRefCountDatabase> apReferences( 
     515                BackupStoreRefCountDatabase::Load( 
     516                        apAccounts->GetEntry(0x1234567), true)); 
     517        TEST_EQUAL(subdirid, rRefCount.GetLastObjectIDUsed()); 
     518        TEST_EQUAL(1, rRefCount.GetRefCount(subdirid)) 
    489519         
    490520        // Put more directories in it, if we haven't gone down too far 
    491521        if(depth > 0) 
    492522        { 
    493                 create_test_data_subdirs(protocol, subdirid, "dir_One", depth - 1); 
    494                 create_test_data_subdirs(protocol, subdirid, "dir_Two", depth - 1); 
     523                create_test_data_subdirs(protocol, subdirid, "dir_One", 
     524                        depth - 1, rRefCount); 
     525                create_test_data_subdirs(protocol, subdirid, "dir_Two", 
     526                        depth - 1, rRefCount); 
    495527        } 
    496528         
    497529        // Stick some files in it 
    498         { 
    499                 BackupStoreFilenameClear name("file_One"); 
    500                 std::auto_ptr<IOStream> upload(BackupStoreFile::EncodeFile("testfiles/file1", subdirid, name)); 
    501                 std::auto_ptr<BackupProtocolClientSuccess> stored(protocol.QueryStoreFile( 
    502                         subdirid, 
    503                         0x123456789abcdefLL,            /* modification time */ 
    504                         0x7362383249872dfLL,            /* attr hash */ 
    505                         0,                                                      /* diff from ID */ 
    506                         name, 
    507                         *upload)); 
    508         } 
    509         { 
    510                 BackupStoreFilenameClear name("file_Two"); 
    511                 std::auto_ptr<IOStream> upload(BackupStoreFile::EncodeFile("testfiles/file1", subdirid, name)); 
    512                 std::auto_ptr<BackupProtocolClientSuccess> stored(protocol.QueryStoreFile( 
    513                         subdirid, 
    514                         0x123456789abcdefLL,            /* modification time */ 
    515                         0x7362383249872dfLL,            /* attr hash */ 
    516                         0,                                                      /* diff from ID */ 
    517                         name, 
    518                         *upload)); 
    519         } 
    520         { 
    521                 BackupStoreFilenameClear name("file_Three"); 
    522                 std::auto_ptr<IOStream> upload(BackupStoreFile::EncodeFile("testfiles/file1", subdirid, name)); 
    523                 std::auto_ptr<BackupProtocolClientSuccess> stored(protocol.QueryStoreFile( 
    524                         subdirid, 
    525                         0x123456789abcdefLL,            /* modification time */ 
    526                         0x7362383249872dfLL,            /* attr hash */ 
    527                         0,                                                      /* diff from ID */ 
    528                         name, 
    529                         *upload)); 
    530         } 
    531  
     530        create_file_in_dir("file_One", "testfiles/file1", subdirid, protocol, 
     531                rRefCount); 
     532        create_file_in_dir("file_Two", "testfiles/file1", subdirid, protocol, 
     533                rRefCount); 
     534        create_file_in_dir("file_Three", "testfiles/file1", subdirid, protocol, 
     535                rRefCount); 
    532536        return subdirid; 
    533537} 
     
    886890} 
    887891 
    888  
    889 int test_server(const char *hostname) 
    890 { 
    891         // Context 
    892         TLSContext context; 
    893         context.Initialise(false /* client */, 
     892void init_context(TLSContext& rContext) 
     893{ 
     894        rContext.Initialise(false /* client */, 
    894895                        "testfiles/clientCerts.pem", 
    895896                        "testfiles/clientPrivKey.pem", 
    896897                        "testfiles/clientTrustedCAs.pem"); 
     898} 
     899 
     900std::auto_ptr<SocketStreamTLS> open_conn(const char *hostname, 
     901        TLSContext& rContext) 
     902{ 
     903        init_context(rContext); 
     904        std::auto_ptr<SocketStreamTLS> conn(new SocketStreamTLS); 
     905        conn->Open(rContext, Socket::TypeINET, hostname, BOX_PORT_BBSTORED); 
     906        return conn; 
     907} 
     908 
     909std::auto_ptr<BackupProtocolClient> test_server_login(SocketStreamTLS& rConn) 
     910{ 
     911        // Make a protocol 
     912        std::auto_ptr<BackupProtocolClient> protocol(new 
     913                BackupProtocolClient(rConn)); 
     914         
     915        // Check the version 
     916        std::auto_ptr<BackupProtocolClientVersion> serverVersion( 
     917                protocol->QueryVersion(BACKUP_STORE_SERVER_VERSION)); 
     918        TEST_THAT(serverVersion->GetVersion() == BACKUP_STORE_SERVER_VERSION); 
     919 
     920        // Login 
     921        std::auto_ptr<BackupProtocolClientLoginConfirmed> loginConf( 
     922                protocol->QueryLogin(0x01234567, 0)); 
     923 
     924        return protocol; 
     925} 
     926 
     927int test_server(const char *hostname) 
     928{ 
     929        TLSContext context; 
     930        std::auto_ptr<SocketStreamTLS> conn = open_conn(hostname, context); 
     931        std::auto_ptr<BackupProtocolClient> apProtocol( 
     932                test_server_login(*conn)); 
     933        BackupProtocolClient& protocol(*apProtocol); 
    897934 
    898935        // Make some test attributes 
     
    912949        // BLOCK 
    913950        { 
    914                 // Open a connection to the server 
    915                 SocketStreamTLS conn; 
    916                 conn.Open(context, Socket::TypeINET, hostname, BOX_PORT_BBSTORED); 
    917  
    918                 // Make a protocol 
    919                 BackupProtocolClient protocol(conn); 
    920                  
    921951                // Get it logging 
    922952                FILE *protocolLog = ::fopen("testfiles/protocol.log", "w"); 
    923953                TEST_THAT(protocolLog != 0); 
    924954                protocol.SetLogToFile(protocolLog); 
    925  
    926                 // Check the version 
    927                 std::auto_ptr<BackupProtocolClientVersion> serverVersion(protocol.QueryVersion(BACKUP_STORE_SERVER_VERSION)); 
    928                 TEST_THAT(serverVersion->GetVersion() == BACKUP_STORE_SERVER_VERSION); 
    929  
    930                 // Login 
    931                 std::auto_ptr<BackupProtocolClientLoginConfirmed> loginConf(protocol.QueryLogin(0x01234567, 0)); 
    932                  
    933                 // Check marker is 0 
    934                 TEST_THAT(loginConf->GetClientStoreMarker() == 0); 
    935955 
    936956#ifndef WIN32 
     
    14361456                 
    14371457//}     skip: 
     1458 
     1459                std::auto_ptr<BackupStoreAccountDatabase> apAccounts( 
     1460                        BackupStoreAccountDatabase::Read( 
     1461                                "testfiles/accounts.txt")); 
     1462                std::auto_ptr<BackupStoreRefCountDatabase> apRefCount( 
     1463                        BackupStoreRefCountDatabase::Load( 
     1464                                apAccounts->GetEntry(0x1234567), true)); 
    14381465         
    14391466                // Create some nice recursive directories 
    14401467                int64_t dirtodelete = create_test_data_subdirs(protocol, 
    1441                         BackupProtocolClientListDirectory::RootDirectory, "test_delete", 6 /* depth */); 
     1468                        BackupProtocolClientListDirectory::RootDirectory, 
     1469                        "test_delete", 6 /* depth */, *apRefCount); 
    14421470                 
    14431471                // And delete them 
     
    17611789                TEST_THAT(TestGetFileSize("testfiles/accounts.txt") > 8); 
    17621790                // make sure something is written to it 
     1791                 
     1792                std::auto_ptr<BackupStoreAccountDatabase> apAccounts( 
     1793                        BackupStoreAccountDatabase::Read("testfiles/accounts.txt")); 
     1794 
     1795                std::auto_ptr<BackupStoreRefCountDatabase> apReferences( 
     1796                        BackupStoreRefCountDatabase::Load( 
     1797                                apAccounts->GetEntry(0x1234567), true)); 
     1798                TEST_EQUAL(BACKUPSTORE_ROOT_DIRECTORY_ID, 
     1799                        apReferences->GetLastObjectIDUsed()); 
     1800                TEST_EQUAL(1, apReferences->GetRefCount(BACKUPSTORE_ROOT_DIRECTORY_ID)) 
     1801                apReferences.reset(); 
     1802 
     1803                // Delete the refcount database and log in again, 
     1804                // check that it is recreated automatically but with 
     1805                // no objects in it, to ensure seamless upgrade. 
     1806                TEST_EQUAL(0, ::unlink("testfiles/0_0/backup/01234567/refcount.db.rfw")); 
     1807 
     1808                // Context 
     1809                TLSContext context; 
     1810                std::auto_ptr<SocketStreamTLS> conn = open_conn("localhost", 
     1811                        context); 
     1812                test_server_login(*conn)->QueryFinished(); 
     1813 
     1814                apReferences = BackupStoreRefCountDatabase::Load( 
     1815                        apAccounts->GetEntry(0x1234567), true); 
     1816                TEST_EQUAL(0, apReferences->GetLastObjectIDUsed()); 
    17631817 
    17641818                TEST_THAT(ServerIsAlive(pid)); 
Note: See TracChangeset for help on using the changeset viewer.