| 1 | // Written by James Turner, started 2017. |
| 2 | // |
| 3 | // Copyright (C) 2017 James Turner |
| 4 | // |
| 5 | // This program is free software; you can redistribute it and/or |
| 6 | // modify it under the terms of the GNU General Public License as |
| 7 | // published by the Free Software Foundation; either version 2 of the |
| 8 | // License, or (at your option) any later version. |
| 9 | // |
| 10 | // This program is distributed in the hope that it will be useful, but |
| 11 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | // General Public License for more details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU General Public License |
| 16 | // along with this program; if not, write to the Free Software |
| 17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | |
| 19 | #include "config.h" |
| 20 | |
| 21 | #include "test_autosaveMigration.hxx" |
| 22 | |
| 23 | #include "test_suite/FGTestApi/testGlobals.hxx" |
| 24 | |
| 25 | #include <simgear/props/props_io.hxx> |
| 26 | #include <simgear/io/iostreams/sgstream.hxx> |
| 27 | #include <simgear/misc/sg_dir.hxx> |
| 28 | |
| 29 | #include "Main/globals.hxx" |
| 30 | #include "Main/options.hxx" |
| 31 | #include "Main/fg_props.hxx" |
| 32 | |
| 33 | using namespace flightgear; |
| 34 | |
| 35 | void writeLegacyAutosave(SGPath userData, int majorVersion, int minorVersion) |
| 36 | { |
| 37 | std::ostringstream os; |
| 38 | os << "autosave_" << majorVersion << "_" << minorVersion << ".xml"; |
| 39 | |
| 40 | sg_ofstream of(userData / os.str()); |
| 41 | { |
| 42 | of << "<?xml version=\"1.0\"?>" \ |
| 43 | "<PropertyList>" \ |
| 44 | "<sim>" \ |
| 45 | "<window-height>42</window-height>" \ |
| 46 | "<presets>" \ |
| 47 | "<foo>12</foo>" \ |
| 48 | "<child><bar>12</bar></child>" \ |
| 49 | "</presets>" \ |
| 50 | "<presets n=\"1\">" \ |
| 51 | "<foo>13</foo>" \ |
| 52 | "</presets>" \ |
| 53 | "<rendering>" \ |
| 54 | "<msaa>10</msaa>" \ |
| 55 | "<texture-size>512</texture-size>" \ |
| 56 | "<texture-pack>" \ |
| 57 | "<foo>abc</foo>" \ |
| 58 | "<wibble>abc</wibble>" \ |
| 59 | "</texture-pack>" \ |
| 60 | "</rendering>" \ |
| 61 | "<gui>" \ |
| 62 | "<dialog n=\"1\">" \ |
| 63 | "<widget>button</widget>" \ |
| 64 | "</dialog>" \ |
| 65 | "<dialog n=\"2\">" \ |
| 66 | "<widget>slider</widget>" \ |
| 67 | "</dialog>"\ |
| 68 | "</gui>" \ |
| 69 | "</sim>" \ |
| 70 | "<some-setting>888</some-setting>" \ |
| 71 | "<views>" \ |
| 72 | "<view>" \ |
| 73 | "<new-prop>somevalue</new-prop>" \ |
| 74 | "<old-prop>somevalue</old-prop>" \ |
| 75 | "</view>" \ |
| 76 | "</views>" \ |
| 77 | "</PropertyList>"; |
| 78 | |
| 79 | } |
| 80 | of.close(); |
| 81 | } |
| 82 | |
| 83 | void writeLegacyAutosave2(SGPath userData, int majorVersion, int minorVersion) |
| 84 | { |
| 85 | std::ostringstream os; |
| 86 | os << "autosave_" << majorVersion << "_" << minorVersion << ".xml"; |
| 87 | |
| 88 | sg_ofstream of(userData / os.str()); |
| 89 | { |
| 90 | of << "<?xml version=\"1.0\"?>" \ |
| 91 | "<PropertyList>" \ |
| 92 | "<sim>" \ |
| 93 | "<bad>1</bad>" \ |
| 94 | "</sim>" \ |
| 95 | "</views>" \ |
| 96 | "</PropertyList>"; |
| 97 | |
| 98 | } |
| 99 | of.close(); |
| 100 | } |
| 101 | |
| 102 | // Set up function for each test. |
| 103 | void AutosaveMigrationTests::setUp() |
| 104 | { |
| 105 | FGTestApi::setUp::initTestGlobals("autosaveMigration"); |
| 106 | |
| 107 | Options::reset(); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | // Clean up after each test. |
| 112 | void AutosaveMigrationTests::tearDown() |
| 113 | { |
| 114 | FGTestApi::tearDown::shutdownTestGlobals(); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | void AutosaveMigrationTests::testMigration() |
| 119 | { |
| 120 | SGPath testUserDataPath = globals->get_fg_home() / "test_autosave_migrate"; |
| 121 | if (!testUserDataPath.exists()) { |
| 122 | SGPath p = testUserDataPath / "foo"; |
| 123 | p.create_dir(0755); |
| 124 | } |
| 125 | |
| 126 | simgear::Dir homeDir(testUserDataPath); |
| 127 | for (auto path : homeDir.children(simgear::Dir::TYPE_FILE, ".xml")) { |
| 128 | path.remove(); |
| 129 | } |
| 130 | |
| 131 | writeLegacyAutosave(testUserDataPath, 2016, 1); |
| 132 | |
| 133 | const string_list versionParts = simgear::strutils::split(VERSION, "."); |
| 134 | CPPUNIT_ASSERT(versionParts.size() == 3); |
| 135 | const int currentMajor = simgear::strutils::to_int(versionParts[0]); |
| 136 | const int currentMinor = simgear::strutils::to_int(versionParts[1]); |
| 137 | |
| 138 | // none of these should not be read |
| 139 | writeLegacyAutosave2(testUserDataPath, 2016, 0); |
| 140 | writeLegacyAutosave2(testUserDataPath, currentMajor, currentMinor + 1); |
| 141 | writeLegacyAutosave2(testUserDataPath, currentMajor+1, currentMinor + 1); |
| 142 | |
| 143 | SGPath p = globals->autosaveFilePath(testUserDataPath); |
| 144 | if (p.exists()) { |
| 145 | CPPUNIT_ASSERT(p.remove()); |
| 146 | } |
| 147 | |
| 148 | // write some blck-list rules to property tree |
| 149 | SGPropertyNode_ptr blacklist = fgGetNode("/sim/autosave-migration/blacklist", true); |
| 150 | blacklist->addChild("path")->setStringValue("/sim[0]/presets[0]/*"); |
| 151 | blacklist->addChild("path")->setStringValue("/sim[0]/rendering[0]/texture-"); |
| 152 | blacklist->addChild("path")->setStringValue("/views[0]/view[*]/old-prop"); |
| 153 | blacklist->addChild("path")->setStringValue("/sim[0]/gui"); |
| 154 | |
| 155 | // execute method under test |
| 156 | globals->loadUserSettings(testUserDataPath); |
| 157 | |
| 158 | CPPUNIT_ASSERT_EQUAL((int)globals->get_props()->getNode("sim")->getChildren("presets").size(), 2); |
| 159 | CPPUNIT_ASSERT_EQUAL((int)globals->get_props()->getNode("sim")->getChildren("gui").size(), 0); |
| 160 | |
| 161 | CPPUNIT_ASSERT_EQUAL(globals->get_props()->getIntValue("sim/window-height"), 42); |
| 162 | CPPUNIT_ASSERT_EQUAL(globals->get_props()->getIntValue("sim/presets/foo"), 0); |
| 163 | CPPUNIT_ASSERT_EQUAL(globals->get_props()->getIntValue("sim/presets[1]/foo"), 13); |
| 164 | |
| 165 | CPPUNIT_ASSERT_EQUAL(globals->get_props()->getIntValue("some-setting"), 888); |
| 166 | |
| 167 | // if this is not zero, one of the bad autosaves was read |
| 168 | CPPUNIT_ASSERT_EQUAL(globals->get_props()->getIntValue("sim/bad"), 0); |
| 169 | } |