/*********************************************************************************************************************************** Test Run ***********************************************************************************************************************************/ void testRun(void) { // Create default storage object for testing Storage *storageTest = storagePosixNew( strNew(testPath()), STORAGE_MODE_FILE_DEFAULT, STORAGE_MODE_PATH_DEFAULT, true, NULL); // ***************************************************************************************************************************** if (testBegin("infoNewLoad(), infoFileName(), infoIni()")) { // Initialize test variables //-------------------------------------------------------------------------------------------------------------------------- String *content = NULL; String *fileName = strNewFmt("%s/test.ini", testPath()); String *fileNameCopy = strNewFmt("%s/test.ini.copy", testPath()); Info *info = NULL; content = strNew ( "[backrest]\n" "backrest-checksum=\"1efa53e0611604ad7d833c5547eb60ff716e758c\"\n" "backrest-format=5\n" "backrest-version=\"2.04\"\n" "\n" "[db]\n" "db-id=1\n" "db-system-id=6569239123849665679\n" "db-version=\"9.4\"\n" "\n" "[db:history]\n" "1={\"db-id\":6569239123849665679,\"db-version\":\"9.4\"}\n" ); // Info files missing and at least one is required //-------------------------------------------------------------------------------------------------------------------------- TEST_ERROR_FMT( infoNewLoad(storageLocal(), fileName, cipherTypeNone, NULL, NULL), FileMissingError, "unable to load info file '%s/test.ini' or '%s/test.ini.copy':\n" "FileMissingError: " STORAGE_ERROR_READ_MISSING "\n" "FileMissingError: " STORAGE_ERROR_READ_MISSING, testPath(), testPath(), strPtr(strNewFmt("%s/test.ini", testPath())), strPtr(strNewFmt("%s/test.ini.copy", testPath()))); // Only copy exists and one is required //-------------------------------------------------------------------------------------------------------------------------- TEST_RESULT_VOID( storagePutNP(storageNewWriteNP(storageLocalWrite(), fileNameCopy), BUFSTR(content)), "put info.copy to file"); TEST_ASSIGN(info, infoNewLoad(storageLocal(), fileName, cipherTypeNone, NULL, NULL), "load copy file"); TEST_RESULT_PTR(infoCipherPass(info), NULL, " cipherPass is not set"); // Remove the copy and store only the main info file and encrypt it. One is required. //-------------------------------------------------------------------------------------------------------------------------- StorageWrite *infoWrite = storageNewWriteNP(storageLocalWrite(), fileName); ioWriteFilterGroupSet( storageWriteIo(infoWrite), ioFilterGroupAdd( ioFilterGroupNew(), cipherBlockNew(cipherModeEncrypt, cipherTypeAes256Cbc, BUFSTRDEF("12345678"), NULL))); storageRemoveNP(storageLocalWrite(), fileNameCopy); storagePutNP( infoWrite, BUFSTRDEF( "[backrest]\n" "backrest-checksum=\"9d2f6dce339751e1a056187fad67d2834b3d4ab3\"\n" "backrest-format=5\n" "backrest-version=\"2.04\"\n" "\n" "[cipher]\n" "cipher-pass=\"ABCDEFGH\"\n" "\n" "[db]\n" "db-id=1\n" "db-system-id=6569239123849665679\n" "db-version=\"9.4\"\n" "\n" "[db:history]\n" "1={\"db-id\":6569239123849665679,\"db-version\":\"9.4\"}\n")); // Only main info exists and is required Ini *ini = NULL; TEST_ASSIGN(info, infoNewLoad(storageLocal(), fileName, cipherTypeAes256Cbc, strNew("12345678"), &ini), "load file"); TEST_RESULT_STR(strPtr(iniGet(ini, strNew("cipher"), strNew("cipher-pass"))), "\"ABCDEFGH\"", " check ini"); TEST_RESULT_STR(strPtr(infoCipherPass(info)), "ABCDEFGH", " cipherPass is set"); // Invalid format //-------------------------------------------------------------------------------------------------------------------------- storageRemoveNP(storageLocalWrite(), fileName); content = strNew ( "[backrest]\n" "backrest-checksum=\"14617b089cb5c9b3224e739bb794e865b9bcdf4b\"\n" "backrest-format=4\n" "backrest-version=\"2.04\"\n" "\n" "[db]\n" "db-catalog-version=201409291\n" "db-control-version=942\n" "db-id=1\n" "db-system-id=6569239123849665679\n" "db-version=\"9.4\"\n" "\n" "[db:history]\n" "1={\"db-catalog-version\":201409291,\"db-control-version\":942,\"db-system-id\":6569239123849665679," "\"db-version\":\"9.4\"}\n" ); // Only main file exists but the backrest-format is invalid TEST_RESULT_VOID( storagePutNP(storageNewWriteNP(storageLocalWrite(), fileName), BUFSTR(content)), "put invalid br format to file"); TEST_ERROR_FMT( infoNewLoad(storageLocal(), fileName, cipherTypeNone, NULL, NULL), FormatError, "unable to load info file '%s/test.ini' or '%s/test.ini.copy':\n" "FormatError: invalid format in '%s/test.ini', expected 5 but found 4\n" "FileMissingError: " STORAGE_ERROR_READ_MISSING, testPath(), testPath(), testPath(), strPtr(strNewFmt("%s/test.ini.copy", testPath()))); content = strNew ( "[backrest]\n" "backrest-checksum=\"14617b089cb5c9b3224e739bb794e865b9bcdf4b\"\n" "backrest-format=4\n" "backrest-version=\"2.05\"\n" "\n" "[db]\n" "db-catalog-version=201409291\n" "db-control-version=942\n" "db-id=1\n" "db-system-id=6569239123849665679\n" "db-version=\"9.4\"\n" "\n" "[db:history]\n" "1={\"db-catalog-version\":201409291,\"db-control-version\":942,\"db-system-id\":6569239123849665679," "\"db-version\":\"9.4\"}\n" ); TEST_RESULT_VOID( storagePutNP( storageNewWriteNP(storageLocalWrite(), fileNameCopy), BUFSTR(content)), "put invalid info to copy file"); TEST_ERROR( infoNewLoad(storageLocal(), fileName, cipherTypeNone, NULL, NULL), FileOpenError, strPtr( strNewFmt( "unable to load info file '%s/test.ini' or '%s/test.ini.copy':\n" "FormatError: invalid format in '%s/test.ini', expected 5 but found 4\n" "ChecksumError: invalid checksum in '%s/test.ini.copy', expected 'af92308095d6141bcda6b2df6d574f98d1115163'" " but found '14617b089cb5c9b3224e739bb794e865b9bcdf4b'", testPath(), testPath(), testPath(), testPath()))); // Invalid checksum //-------------------------------------------------------------------------------------------------------------------------- storageRemoveNP(storageLocalWrite(), fileName); storageRemoveNP(storageLocalWrite(), fileNameCopy); // change the checksum content = strNew ( "[backrest]\n" "backrest-checksum=\"4306ec205f71417c301e403c4714090e61c8a999\"\n" "backrest-format=5\n" "backrest-version=\"1.23\"\n" "\n" "[db]\n" "db-id=1\n" "db-system-id=6455618988686438683\n" "db-version=\"9.6\"\n" "\n" "[db:history]\n" "1={\"db-id\":6455618988686438683,\"db-version\":\"9.6\"}\n" "2={\"db-id\":6457457208275135411,\"db-version\":\"9.6\"}\n" ); TEST_RESULT_VOID( storagePutNP(storageNewWriteNP(storageLocalWrite(), fileNameCopy), BUFSTR(content)), "put invalid checksum to copy"); // Empty checksum for main file content = strNew ( "[backrest]\n" "backrest-checksum=\n" "backrest-format=5\n" "backrest-version=\"1.23\"\n" "\n" "[db]\n" "db-id=1\n" "db-system-id=6455618988686438683\n" "db-version=\"9.6\"\n" "\n" "[db:history]\n" "1={\"db-id\":6455618988686438683,\"db-version\":\"9.6\"}\n" "2={\"db-id\":6457457208275135411,\"db-version\":\"9.6\"}\n" ); TEST_RESULT_VOID( storagePutNP(storageNewWriteNP(storageLocalWrite(), fileName), BUFSTR(content)), "put empty checksum to file"); // Copy file error TEST_ERROR( infoNewLoad(storageLocal(), fileName, cipherTypeNone, NULL, NULL), ChecksumError, strPtr( strNewFmt( "unable to load info file '%s/test.ini' or '%s/test.ini.copy':\n" "ChecksumError: invalid checksum in '%s/test.ini', expected '4306ec205f71417c301e403c4714090e61c8a736' but" " no checksum found\n" "ChecksumError: invalid checksum in '%s/test.ini.copy', expected '4306ec205f71417c301e403c4714090e61c8a736'" " but found '4306ec205f71417c301e403c4714090e61c8a999'", testPath(), testPath(), testPath(), testPath()))); // Encryption error //-------------------------------------------------------------------------------------------------------------------------- storageRemoveNP(storageLocalWrite(), fileName); TEST_ERROR_FMT( infoNewLoad(storageLocal(), fileName, cipherTypeAes256Cbc, strNew("12345678"), NULL), CryptoError, "unable to load info file '%s/test.ini' or '%s/test.ini.copy':\n" "FileMissingError: " STORAGE_ERROR_READ_MISSING "\n" "CryptoError: '%s/test.ini.copy' cipher header invalid\n" "HINT: Is or was the repo encrypted?", testPath(), testPath(), strPtr(strNewFmt("%s/test.ini", testPath())), testPath()); storageRemoveNP(storageLocalWrite(), fileNameCopy); // infoFree() //-------------------------------------------------------------------------------------------------------------------------- TEST_RESULT_VOID(infoFree(info), "infoFree() - free info memory context"); } // ***************************************************************************************************************************** if (testBegin("infoSave()")) { const String *fileName = strNew("test.info"); const String *cipherPass = strNew("12345"); Ini *ini = iniNew(); iniSet(ini, strNew("section1"), strNew("key1"), strNew("value1")); TEST_RESULT_VOID(infoSave(infoNew(), ini, storageTest, fileName, cipherTypeNone, NULL), "save info"); ini = NULL; TEST_RESULT_VOID(infoNewLoad(storageTest, fileName, cipherTypeNone, NULL, &ini), " reload info"); TEST_RESULT_STR(strPtr(iniGet(ini, strNew("section1"), strNew("key1"))), "value1", " check ini"); TEST_RESULT_BOOL(storageExistsNP(storageTest, fileName), true, "check main exists"); TEST_RESULT_BOOL(storageExistsNP(storageTest, strNewFmt("%s" INFO_COPY_EXT, strPtr(fileName))), true, "check main exists"); // Add encryption // ------------------------------------------------------------------------------------------------------------------------- ini = iniNew(); iniSet(ini, strNew("section1"), strNew("key1"), strNew("value4")); Info *info = infoNew(); info->cipherPass = strNew("/badpass"); TEST_RESULT_VOID(infoSave(info, ini, storageTest, fileName, cipherTypeAes256Cbc, cipherPass), "save encrypted info"); ini = NULL; TEST_RESULT_VOID(infoNewLoad(storageTest, fileName, cipherTypeAes256Cbc, cipherPass, &ini), " reload info"); TEST_RESULT_STR(strPtr(iniGet(ini, strNew("section1"), strNew("key1"))), "value4", " check ini"); TEST_RESULT_STR(strPtr(iniGet(ini, strNew("cipher"), strNew("cipher-pass"))), "\"/badpass\"", " check cipher-pass"); } }
break; // Now that the async process has been launched, confess any errors that are found confessOnError = true; } while (waitMore(wait)); } // Else perform synchronous get else { // Get the repo storage in case it is remote and encryption settings need to be pulled down storageRepo(); // Get the archive file result = archiveGetFile( storageLocalWrite(), walSegment, walDestination, false, cipherType(cfgOptionStr(cfgOptRepoCipherType)), cfgOptionStr(cfgOptRepoCipherPass)); } // Log whether or not the file was found if (result == 0) LOG_INFO("found %s in the archive", strPtr(walSegment)); else LOG_INFO("unable to find %s in the archive", strPtr(walSegment)); } MEM_CONTEXT_TEMP_END(); FUNCTION_LOG_RETURN(INT, result); } /***********************************************************************************************************************************
/*********************************************************************************************************************************** Test Run ***********************************************************************************************************************************/ void testRun(void) { // ***************************************************************************************************************************** if (testBegin("infoPgNewLoad(), infoPgFree(), infoPgDataCurrent(), infoPgDataToLog(), infoPgAdd(), infoPgIni()")) { String *content = NULL; String *fileName = strNewFmt("%s/test.ini", testPath()); String *fileName2 = strNewFmt("%s/test2.ini", testPath()); // Archive info //-------------------------------------------------------------------------------------------------------------------------- content = strNew ( "[db]\n" "db-id=1\n" "db-system-id=6569239123849665679\n" "db-version=\"9.4\"\n" "\n" "[db:history]\n" "1={\"db-id\":6569239123849665679,\"db-version\":\"9.4\"}\n" ); TEST_RESULT_VOID( storagePutNP(storageNewWriteNP(storageLocalWrite(), fileName), harnessInfoChecksum(content)), "put info to file"); InfoPg *infoPg = NULL; Ini *ini = NULL; TEST_ASSIGN( infoPg, infoPgNewLoad(storageLocal(), fileName, infoPgArchive, cipherTypeNone, NULL, &ini), "load file"); TEST_RESULT_STR(strPtr(iniGet(ini, strNew("db"), strNew("db-id"))), "1", " check ini"); TEST_RESULT_INT(lstSize(infoPg->history), 1, " history record added"); InfoPgData infoPgData = infoPgDataCurrent(infoPg); TEST_RESULT_INT(infoPgData.id, 1, " id set"); TEST_RESULT_INT(infoPgData.version, PG_VERSION_94, " version set"); TEST_RESULT_INT(infoPgData.systemId, 6569239123849665679, " system-id set"); TEST_RESULT_INT(infoPgData.catalogVersion, 0, " catalog-version not set"); TEST_RESULT_INT(infoPgData.controlVersion, 0, " control-version not set"); TEST_RESULT_INT(infoPgDataTotal(infoPg), 1, " check pg data total"); TEST_RESULT_STR(strPtr(infoPgArchiveId(infoPg, 0)), "9.4-1", " check pg archive id"); TEST_RESULT_PTR(infoPgCipherPass(infoPg), NULL, " no cipher passphrase"); // Backup info //-------------------------------------------------------------------------------------------------------------------------- content = strNew ( "[db]\n" "db-catalog-version=201409291\n" "db-control-version=942\n" "db-id=1\n" "db-system-id=6569239123849665679\n" "db-version=\"9.4\"\n" "\n" "[db:history]\n" "1={\"db-catalog-version\":201409291,\"db-control-version\":942,\"db-system-id\":6569239123849665679," "\"db-version\":\"9.4\"}\n" ); TEST_RESULT_VOID( storagePutNP(storageNewWriteNP(storageLocalWrite(), fileName), harnessInfoChecksum(content)), "put info to file"); TEST_ASSIGN( infoPg, infoPgNewLoad(storageLocal(), fileName, infoPgBackup, cipherTypeNone, NULL, NULL), "load file"); TEST_RESULT_INT(lstSize(infoPg->history), 1, " history record added"); infoPgData = infoPgDataCurrent(infoPg); TEST_RESULT_INT(infoPgData.id, 1, " id set"); TEST_RESULT_INT(infoPgData.version, PG_VERSION_94, " version set"); TEST_RESULT_INT(infoPgData.systemId, 6569239123849665679, " system-id set"); TEST_RESULT_INT(infoPgData.catalogVersion, 201409291, " catalog-version set"); TEST_RESULT_INT(infoPgData.controlVersion, 942, " control-version set"); // Manifest info //-------------------------------------------------------------------------------------------------------------------------- content = strNew ( "[db]\n" "db-catalog-version=201510051\n" "db-control-version=942\n" "db-id=2\n" "db-system-id=6365925855999999999\n" "db-version=\"9.5\"\n" "\n" "[db:history]\n" "1={\"db-catalog-version\":201409291,\"db-control-version\":942,\"db-system-id\":6569239123849665679," "\"db-version\":\"9.4\"}\n" "2={\"db-catalog-version\":201510051,\"db-control-version\":942,\"db-system-id\":6365925855999999999," "\"db-version\":\"9.5\"}\n" ); // Put the file and load it TEST_RESULT_VOID( storagePutNP(storageNewWriteNP(storageLocalWrite(), fileName), harnessInfoChecksum(content)), "put info to file"); TEST_ASSIGN( infoPg, infoPgNewLoad(storageLocal(), fileName, infoPgManifest, cipherTypeNone, NULL, NULL), "load file"); // Save the file and verify it ini = iniNew(); TEST_RESULT_VOID(infoPgSave(infoPg, ini, storageLocalWrite(), fileName2, cipherTypeNone, NULL), "save file"); TEST_RESULT_BOOL( bufEq( storageGetNP(storageNewReadNP(storageLocal(), fileName)), storageGetNP(storageNewReadNP(storageLocal(), fileName2))), true, "files are equal"); TEST_RESULT_INT(lstSize(infoPg->history), 2, "history record added"); infoPgData = infoPgDataCurrent(infoPg); TEST_RESULT_INT(infoPgData.id, 2, " id set"); TEST_RESULT_INT(infoPgData.version, PG_VERSION_95, " version set"); TEST_RESULT_INT(infoPgData.systemId, 6365925855999999999, " system-id set"); TEST_RESULT_INT(infoPgData.catalogVersion, 201510051, " catalog-version set"); TEST_RESULT_INT(infoPgData.controlVersion, 942, " control-version set"); // infoPgAdd //-------------------------------------------------------------------------------------------------------------------------- infoPgData.id = 3; infoPgData.version = PG_VERSION_96; infoPgData.systemId = 6399999999999999999; infoPgData.catalogVersion = 201608131; infoPgData.controlVersion = 960; TEST_RESULT_VOID(infoPgAdd(infoPg, &infoPgData), "infoPgAdd"); InfoPgData infoPgDataTest = infoPgDataCurrent(infoPg); TEST_RESULT_INT(infoPgDataTest.id, 3, " id set"); TEST_RESULT_INT(infoPgDataTest.version, PG_VERSION_96, " version set"); TEST_RESULT_INT(infoPgDataTest.systemId, 6399999999999999999, " system-id set"); TEST_RESULT_INT(infoPgDataTest.catalogVersion, 201608131, " catalog-version set"); TEST_RESULT_INT(infoPgDataTest.controlVersion, 960, " control-version set"); // Errors //-------------------------------------------------------------------------------------------------------------------------- TEST_ERROR(infoPgNewLoad(storageLocal(), fileName, 10, cipherTypeNone, NULL, NULL), AssertError, "invalid InfoPg type 10"); TEST_ERROR( infoPgNewLoad(storageLocal(), NULL, infoPgManifest, cipherTypeNone, NULL, NULL), AssertError, "assertion 'fileName != NULL' failed"); TEST_ERROR(infoPgDataCurrent(NULL), AssertError, "assertion 'this != NULL' failed"); TEST_ERROR(infoPgAdd(NULL, &infoPgData), AssertError, "assertion 'this != NULL' failed"); TEST_ERROR(infoPgAdd(infoPg, NULL), AssertError, "assertion 'infoPgData != NULL' failed"); // infoPgFree //-------------------------------------------------------------------------------------------------------------------------- TEST_RESULT_VOID(infoPgFree(infoPg), "infoPgFree() - free infoPg"); // infoPgDataToLog //-------------------------------------------------------------------------------------------------------------------------- // test max values infoPgDataTest.id = (unsigned int)4294967295; infoPgDataTest.version = (unsigned int)4294967295; infoPgDataTest.systemId = 18446744073709551615U; infoPgDataTest.catalogVersion = (uint32_t)4294967295; infoPgDataTest.controlVersion = (uint32_t)4294967295; TEST_RESULT_STR( strPtr(infoPgDataToLog(&infoPgDataTest)), "{id: 4294967295, version: 4294967295, systemId: 18446744073709551615, catalogVersion: 4294967295, controlVersion:" " 4294967295}", " check max format"); } }