| | 1657 | #ifndef WIN32 // requires fork |
| | 1658 | printf("\n==== Testing that bbackupd responds correctly to " |
| | 1659 | "connection failure\n"); |
| | 1660 | |
| | 1661 | { |
| | 1662 | // Kill the daemons |
| | 1663 | terminate_bbackupd(bbackupd_pid); |
| | 1664 | test_kill_bbstored(); |
| | 1665 | |
| | 1666 | // create a new file to force an upload |
| | 1667 | |
| | 1668 | char* new_file = "testfiles/TestDir1/force-upload-2"; |
| | 1669 | int fd = open(new_file, |
| | 1670 | O_CREAT | O_EXCL | O_WRONLY, 0700); |
| | 1671 | if (fd <= 0) |
| | 1672 | { |
| | 1673 | perror(new_file); |
| | 1674 | } |
| | 1675 | TEST_THAT(fd > 0); |
| | 1676 | |
| | 1677 | char* control_string = "whee!\n"; |
| | 1678 | TEST_THAT(write(fd, control_string, |
| | 1679 | strlen(control_string)) == |
| | 1680 | (int)strlen(control_string)); |
| | 1681 | close(fd); |
| | 1682 | |
| | 1683 | // sleep to make it old enough to upload |
| | 1684 | safe_sleep(4); |
| | 1685 | |
| | 1686 | class MyHook : public BackupStoreContext::TestHook |
| | 1687 | { |
| | 1688 | virtual std::auto_ptr<ProtocolObject> StartCommand( |
| | 1689 | BackupProtocolObject& rCommand) |
| | 1690 | { |
| | 1691 | if (rCommand.GetType() == |
| | 1692 | BackupProtocolServerStoreFile::TypeID) |
| | 1693 | { |
| | 1694 | // terminate badly |
| | 1695 | THROW_EXCEPTION(CommonException, |
| | 1696 | Internal); |
| | 1697 | } |
| | 1698 | return std::auto_ptr<ProtocolObject>(); |
| | 1699 | } |
| | 1700 | }; |
| | 1701 | MyHook hook; |
| | 1702 | |
| | 1703 | bbstored_pid = fork(); |
| | 1704 | |
| | 1705 | if (bbstored_pid < 0) |
| | 1706 | { |
| | 1707 | BOX_LOG_SYS_ERROR("failed to fork()"); |
| | 1708 | return 1; |
| | 1709 | } |
| | 1710 | |
| | 1711 | if (bbstored_pid == 0) |
| | 1712 | { |
| | 1713 | // in fork child |
| | 1714 | TEST_THAT(setsid() != -1); |
| | 1715 | |
| | 1716 | if (!Logging::IsEnabled(Log::TRACE)) |
| | 1717 | { |
| | 1718 | Logging::SetGlobalLevel(Log::NOTHING); |
| | 1719 | } |
| | 1720 | |
| | 1721 | // BackupStoreDaemon must be destroyed before exit(), |
| | 1722 | // to avoid memory leaks being reported. |
| | 1723 | { |
| | 1724 | BackupStoreDaemon bbstored; |
| | 1725 | bbstored.SetTestHook(hook); |
| | 1726 | bbstored.SetRunInForeground(true); |
| | 1727 | bbstored.Main("testfiles/bbstored.conf"); |
| | 1728 | } |
| | 1729 | |
| | 1730 | Timers::Cleanup(); // avoid memory leaks |
| | 1731 | exit(0); |
| | 1732 | } |
| | 1733 | |
| | 1734 | // in fork parent |
| | 1735 | bbstored_pid = WaitForServerStartup("testfiles/bbstored.pid", |
| | 1736 | bbstored_pid); |
| | 1737 | |
| | 1738 | TEST_THAT(::system("rm -f testfiles/notifyran.store-full.*") == 0); |
| | 1739 | |
| | 1740 | // Ignore SIGPIPE so that when the connection is broken, |
| | 1741 | // the daemon doesn't terminate. |
| | 1742 | ::signal(SIGPIPE, SIG_IGN); |
| | 1743 | |
| | 1744 | { |
| | 1745 | Log::Level newLevel = Logging::GetGlobalLevel(); |
| | 1746 | |
| | 1747 | if (!Logging::IsEnabled(Log::TRACE)) |
| | 1748 | { |
| | 1749 | newLevel = Log::NOTHING; |
| | 1750 | } |
| | 1751 | |
| | 1752 | Logging::Guard guard(newLevel); |
| | 1753 | |
| | 1754 | BackupDaemon bbackupd; |
| | 1755 | bbackupd.Configure("testfiles/bbackupd.conf"); |
| | 1756 | bbackupd.InitCrypto(); |
| | 1757 | bbackupd.RunSyncNowWithExceptionHandling(); |
| | 1758 | } |
| | 1759 | |
| | 1760 | ::signal(SIGPIPE, SIG_DFL); |
| | 1761 | |
| | 1762 | TEST_THAT(TestFileExists("testfiles/notifyran.backup-error.1")); |
| | 1763 | TEST_THAT(!TestFileExists("testfiles/notifyran.backup-error.2")); |
| | 1764 | TEST_THAT(!TestFileExists("testfiles/notifyran.store-full.1")); |
| | 1765 | |
| | 1766 | test_kill_bbstored(true); |
| | 1767 | |
| | 1768 | if (failures > 0) |
| | 1769 | { |
| | 1770 | // stop early to make debugging easier |
| | 1771 | return 1; |
| | 1772 | } |
| | 1773 | |
| | 1774 | test_run_bbstored(); |
| | 1775 | |
| | 1776 | cmd = BBACKUPD " " + bbackupd_args + |
| | 1777 | " testfiles/bbackupd.conf"; |
| | 1778 | bbackupd_pid = LaunchServer(cmd, "testfiles/bbackupd.pid"); |
| | 1779 | TEST_THAT(bbackupd_pid != -1 && bbackupd_pid != 0); |
| | 1780 | ::safe_sleep(1); |
| | 1781 | TEST_THAT(ServerIsAlive(bbackupd_pid)); |
| | 1782 | TEST_THAT(ServerIsAlive(bbstored_pid)); |
| | 1783 | if (!ServerIsAlive(bbackupd_pid)) return 1; |
| | 1784 | if (!ServerIsAlive(bbstored_pid)) return 1; |
| | 1785 | } |
| | 1786 | #endif // !WIN32 |
| | 1787 | |