Welcome to mirror list, hosted at ThFree Co, Russian Federation.

local_country_file_tests.cpp « platform_tests « platform - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 478bfd35dd1e50703e3a8708ba794af90fee0ea5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include "testing/testing.hpp"

#include "platform/country_file.hpp"
#include "platform/local_country_file.hpp"
#include "platform/local_country_file_utils.hpp"
#include "platform/platform.hpp"
#include "platform/platform_tests_support/scoped_dir.hpp"
#include "platform/platform_tests_support/scoped_file.hpp"

#include "coding/file_name_utils.hpp"
#include "coding/file_writer.hpp"
#include "coding/internal/file_data.hpp"

#include "base/logging.hpp"
#include "base/scope_guard.hpp"

#include "defines.hpp"

#include "std/algorithm.hpp"
#include "std/bind.hpp"
#include "std/set.hpp"

using namespace platform::tests_support;

namespace platform
{
namespace
{
template <typename T>
bool Contains(vector<T> const & v, T const & t)
{
  return find(v.begin(), v.end(), t) != v.end();
}
}  // namespace

// Checks that all unsigned numbers less than 10 ^ 18 can be parsed as
// a timestamp.
UNIT_TEST(LocalCountryFile_ParseVersion)
{
  int64_t version = 0;
  TEST(ParseVersion("1", version), ());
  TEST_EQUAL(version, 1, ());

  TEST(ParseVersion("141010", version), ());
  TEST_EQUAL(version, 141010, ());

  TEST(ParseVersion("150309", version), ());
  TEST_EQUAL(version, 150309, ());

  TEST(ParseVersion("999999999999999999", version), ());
  TEST_EQUAL(version, 999999999999999999, ());

  TEST(!ParseVersion("1000000000000000000", version), ());
  TEST(!ParseVersion("00000000000000000000000000000000123", version), ());

  TEST(!ParseVersion("", version), ());
  TEST(!ParseVersion("150309 ", version), ());
  TEST(!ParseVersion(" 150309", version), ());
  TEST(!ParseVersion("-150309", version), ());
  TEST(!ParseVersion("just string", version), ());
}

// Checks basic functionality of LocalCountryFile.
UNIT_TEST(LocalCountryFile_Smoke)
{
  CountryFile countryFile("TestCountry");
  countryFile.SetRemoteSizes(1 /* mapSize */, 2 /* routingSize */);

  LocalCountryFile localFile("/test-dir", countryFile, 150309);

  TEST_EQUAL("/test-dir/TestCountry" DATA_FILE_EXTENSION, localFile.GetPath(MapOptions::Map), ());
  TEST_EQUAL("/test-dir/TestCountry" DATA_FILE_EXTENSION ROUTING_FILE_EXTENSION,
             localFile.GetPath(MapOptions::CarRouting), ());

  // Not synced with disk yet.
  TEST_EQUAL(MapOptions::Nothing, localFile.GetFiles(), ());

  // Any statement is true about elements of an empty set.
  TEST(localFile.OnDisk(MapOptions::Nothing), ());

  TEST(!localFile.OnDisk(MapOptions::Map), ());
  TEST(!localFile.OnDisk(MapOptions::CarRouting), ());
  TEST(!localFile.OnDisk(MapOptions::MapWithCarRouting), ());

  TEST_EQUAL("/test-dir", localFile.GetDirectory(), ());

  TEST_EQUAL(0, localFile.GetSize(MapOptions::Nothing), ());
  TEST_EQUAL(0, localFile.GetSize(MapOptions::Map), ());
  TEST_EQUAL(0, localFile.GetSize(MapOptions::CarRouting), ());
  TEST_EQUAL(0, localFile.GetSize(MapOptions::MapWithCarRouting), ());

  TEST_EQUAL(150309, localFile.GetVersion(), ());
}

// Creates test country map file and routing file and checks
// sync-with-disk functionality.
UNIT_TEST(LocalCountryFile_DiskFiles)
{
  Platform & platform = GetPlatform();

  CountryFile countryFile("TestCountry");
  countryFile.SetRemoteSizes(1 /* mapSize */, 2 /* routingSize */);

  LocalCountryFile localFile(platform.WritableDir(), countryFile, 0 /* version */);
  TEST(!localFile.OnDisk(MapOptions::Map), ());
  TEST(!localFile.OnDisk(MapOptions::CarRouting), ());
  TEST(!localFile.OnDisk(MapOptions::MapWithCarRouting), ());

  ScopedFile testMapFile(countryFile.GetNameWithExt(MapOptions::Map), "map");

  localFile.SyncWithDisk();
  TEST(localFile.OnDisk(MapOptions::Map), ());
  TEST(!localFile.OnDisk(MapOptions::CarRouting), ());
  TEST(!localFile.OnDisk(MapOptions::MapWithCarRouting), ());
  TEST_EQUAL(3, localFile.GetSize(MapOptions::Map), ());

  ScopedFile testRoutingFile(countryFile.GetNameWithExt(MapOptions::CarRouting), "routing");

  localFile.SyncWithDisk();
  TEST(localFile.OnDisk(MapOptions::Map), ());
  TEST(localFile.OnDisk(MapOptions::CarRouting), ());
  TEST(localFile.OnDisk(MapOptions::MapWithCarRouting), ());
  TEST_EQUAL(3, localFile.GetSize(MapOptions::Map), ());
  TEST_EQUAL(7, localFile.GetSize(MapOptions::CarRouting), ());
  TEST_EQUAL(10, localFile.GetSize(MapOptions::MapWithCarRouting), ());

  localFile.DeleteFromDisk(MapOptions::MapWithCarRouting);
  TEST(!testMapFile.Exists(), (testMapFile, "wasn't deleted by LocalCountryFile."));
  testMapFile.Reset();

  TEST(!testRoutingFile.Exists(), (testRoutingFile, "wasn't deleted by LocalCountryFile."));
  testRoutingFile.Reset();
}

UNIT_TEST(LocalCountryFile_CleanupMapFiles)
{
  Platform & platform = GetPlatform();
  string const mapsDir = platform.WritableDir();

  CountryFile japanFile("Japan");
  CountryFile brazilFile("Brazil");
  CountryFile irelandFile("Ireland");

  LocalCountryFile japanLocalFile(mapsDir, japanFile, 0 /* version */);
  ScopedFile japanMapFile("Japan.mwm", "Japan");

  LocalCountryFile brazilLocalFile(mapsDir, brazilFile, 0 /* version */);
  ScopedFile brazilMapFile("Brazil.mwm", "Brazil");

  LocalCountryFile irelandLocalFile(mapsDir, irelandFile, 0 /* version */);
  ScopedFile irelandMapFile("Ireland.mwm", "Ireland");

  ScopedDir emptyDir("3");

  // Check that FindAllLocalMaps()
  vector<LocalCountryFile> localFiles;
  FindAllLocalMaps(localFiles);
  TEST(Contains(localFiles, japanLocalFile), (japanLocalFile, localFiles));
  TEST(Contains(localFiles, brazilLocalFile), (brazilLocalFile, localFiles));
  TEST(Contains(localFiles, irelandLocalFile), (irelandLocalFile, localFiles));

  CleanupMapsDirectory();

  japanLocalFile.SyncWithDisk();
  TEST_EQUAL(MapOptions::Nothing, japanLocalFile.GetFiles(), ());
  TEST(!japanMapFile.Exists(), (japanMapFile));
  japanMapFile.Reset();

  brazilLocalFile.SyncWithDisk();
  TEST_EQUAL(MapOptions::Nothing, brazilLocalFile.GetFiles(), ());
  TEST(!brazilMapFile.Exists(), (brazilMapFile));
  brazilMapFile.Reset();

  irelandLocalFile.SyncWithDisk();
  TEST_EQUAL(MapOptions::Map, irelandLocalFile.GetFiles(), ());
  irelandLocalFile.DeleteFromDisk(MapOptions::Map);
  TEST(!irelandMapFile.Exists(), (irelandMapFile));
  irelandMapFile.Reset();

  TEST(!emptyDir.Exists(), ("Empty directory", emptyDir, "wasn't removed."));
  emptyDir.Reset();
}

UNIT_TEST(LocalCountryFile_CleanupPartiallyDownloadedFiles)
{
  ScopedFile toBeDeleted[] = {{"Ireland.mwm.ready", "Ireland"},
                              {"Netherlands.mwm.routing.downloading2", "Netherlands"},
                              {"Germany.mwm.ready3", "Germany"},
                              {"UK_England.mwm.resume4", "UK"}};
  ScopedFile toBeKept[] = {
      {"Italy.mwm", "Italy"}, {"Spain.mwm", "Spain map"}, {"Spain.mwm.routing", "Spain routing"}};

  CleanupMapsDirectory();

  for (ScopedFile & file : toBeDeleted)
  {
    TEST(!file.Exists(), (file));
    file.Reset();
  }

  for (ScopedFile & file : toBeKept)
    TEST(file.Exists(), (file));
}

// Creates test-dir and following files:
// * test-dir/Ireland.mwm
// * test-dir/Netherlands.mwm
// * test-dir/Netherlands.mwm.routing
// After that, checks that FindAllLocalMapsInDirectory() correctly finds all created files.
UNIT_TEST(LocalCountryFile_DirectoryLookup)
{
  // This tests creates a map file for Ireland and map + routing files
  // for Netherlands in a test directory.
  CountryFile const irelandFile("Ireland");
  CountryFile const netherlandsFile("Netherlands");

  ScopedDir testDir("test-dir");

  ScopedFile testIrelandMapFile(testDir, irelandFile, MapOptions::Map, "Ireland-map");
  ScopedFile testNetherlandsMapFile(testDir, netherlandsFile, MapOptions::Map, "Netherlands-map");
  ScopedFile testNetherlandsRoutingFile(testDir, netherlandsFile, MapOptions::CarRouting,
                                        "Netherlands-routing");

  vector<LocalCountryFile> localFiles;
  FindAllLocalMapsInDirectory(testDir.GetFullPath(), 150309, localFiles);
  sort(localFiles.begin(), localFiles.end());
  for (LocalCountryFile & localFile : localFiles)
    localFile.SyncWithDisk();

  LocalCountryFile expectedIrelandFile(testDir.GetFullPath(), irelandFile, 150309);
  expectedIrelandFile.m_files = MapOptions::Map;

  LocalCountryFile expectedNetherlandsFile(testDir.GetFullPath(), netherlandsFile, 150309);
  expectedNetherlandsFile.m_files = MapOptions::MapWithCarRouting;

  vector<LocalCountryFile> expectedLocalFiles = {expectedIrelandFile, expectedNetherlandsFile};
  sort(expectedLocalFiles.begin(), expectedLocalFiles.end());
  TEST_EQUAL(expectedLocalFiles, localFiles, ());
}

// Creates directory 010101 and 010101/Italy.mwm file.  After that,
// checks that this file will be recognized as a map file for Italy
// with version 010101. Also, checks that World.mwm and
// WorldCoasts.mwm exist in writable dir.
UNIT_TEST(LocalCountryFile_AllLocalFilesLookup)
{
  CountryFile const italyFile("Italy");

  ScopedDir testDir("010101");
  ScopedFile testItalyMapFile(testDir, italyFile, MapOptions::Map, "Italy-map");

  vector<LocalCountryFile> localFiles;
  FindAllLocalMaps(localFiles);
  multiset<LocalCountryFile> localFilesSet(localFiles.begin(), localFiles.end());

  bool worldFound = false;
  bool worldCoastsFound = false;
  for (auto const & file : localFiles)
  {
    // With the new concepts, World mwm files have valid version.
    if (file.GetCountryName() == WORLD_FILE_NAME)
    {
      worldFound = true;
      TEST_NOT_EQUAL(0, file.GetVersion(), ());
    }
    if (file.GetCountryName() == WORLD_COASTS_FILE_NAME)
    {
      worldCoastsFound = true;
      TEST_NOT_EQUAL(0, file.GetVersion(), ());
    }
  }
  TEST(worldFound, ());
  TEST(worldCoastsFound, ());

  LocalCountryFile expectedItalyFile(testDir.GetFullPath(), italyFile, 10101);
  TEST_EQUAL(1, localFilesSet.count(expectedItalyFile), (localFiles));
}

UNIT_TEST(LocalCountryFile_PreparePlaceForCountryFiles)
{
  Platform & platform = GetPlatform();

  CountryFile italyFile("Italy");
  LocalCountryFile expectedItalyFile(platform.WritableDir(), italyFile, 0 /* version */);
  shared_ptr<LocalCountryFile> italyLocalFile =
      PreparePlaceForCountryFiles(italyFile, 0 /* version */);
  TEST(italyLocalFile.get(), ());
  TEST_EQUAL(expectedItalyFile, *italyLocalFile, ());

  ScopedDir directoryForV1("1");

  CountryFile germanyFile("Germany");
  LocalCountryFile expectedGermanyFile(directoryForV1.GetFullPath(), germanyFile, 1 /* version */);
  shared_ptr<LocalCountryFile> germanyLocalFile =
      PreparePlaceForCountryFiles(germanyFile, 1 /* version */);
  TEST(germanyLocalFile.get(), ());
  TEST_EQUAL(expectedGermanyFile, *germanyLocalFile, ());

  CountryFile franceFile("France");
  LocalCountryFile expectedFranceFile(directoryForV1.GetFullPath(), franceFile, 1 /* version */);
  shared_ptr<LocalCountryFile> franceLocalFile =
      PreparePlaceForCountryFiles(franceFile, 1 /* version */);
  TEST(franceLocalFile.get(), ());
  TEST_EQUAL(expectedFranceFile, *franceLocalFile, ());
}

UNIT_TEST(LocalCountryFile_CountryIndexes)
{
  ScopedDir testDir("101010");

  CountryFile germanyFile("Germany");
  LocalCountryFile germanyLocalFile(testDir.GetFullPath(), germanyFile, 101010 /* version */);
  TEST_EQUAL(
      my::JoinFoldersToPath(germanyLocalFile.GetDirectory(), germanyFile.GetNameWithoutExt()),
      CountryIndexes::IndexesDir(germanyLocalFile), ());
  CountryIndexes::PreparePlaceOnDisk(germanyLocalFile);

  string const bitsPath = CountryIndexes::GetPath(germanyLocalFile, CountryIndexes::Index::Bits);
  TEST(!Platform::IsFileExistsByFullPath(bitsPath), (bitsPath));
  {
    FileWriter writer(bitsPath);
    string const contents = "bits index";
    writer.Write(contents.data(), contents.size());
  }
  TEST(Platform::IsFileExistsByFullPath(bitsPath), (bitsPath));

  TEST(CountryIndexes::DeleteFromDisk(germanyLocalFile),
       ("Can't delete indexes for:", germanyLocalFile));

  TEST(!Platform::IsFileExistsByFullPath(bitsPath), (bitsPath));
}

UNIT_TEST(LocalCountryFile_DoNotDeleteUserFiles)
{
  my::LogLevel oldLogLevel = my::g_LogLevel;
  my::g_LogLevel = LCRITICAL;
  MY_SCOPE_GUARD(restoreLogLevel, [&oldLogLevel]()
  {
    my::g_LogLevel = oldLogLevel;
  });

  ScopedDir testDir("101010");

  CountryFile germanyFile("Germany");
  LocalCountryFile germanyLocalFile(testDir.GetFullPath(), germanyFile, 101010 /* version */);
  CountryIndexes::PreparePlaceOnDisk(germanyLocalFile);

  string const userFilePath =
      my::JoinFoldersToPath(CountryIndexes::IndexesDir(germanyLocalFile), "user-data.txt");
  {
    FileWriter writer(userFilePath);
    string const data = "user data";
    writer.Write(data.data(), data.size());
  }
  TEST(!CountryIndexes::DeleteFromDisk(germanyLocalFile),
       ("Indexes dir should not be deleted for:", germanyLocalFile));

  TEST(my::DeleteFileX(userFilePath), ("Can't delete test file:", userFilePath));
  TEST(CountryIndexes::DeleteFromDisk(germanyLocalFile),
       ("Can't delete indexes for:", germanyLocalFile));
}

UNIT_TEST(LocalCountryFile_MakeTemporary)
{
  string const path = GetPlatform().WritablePathForFile("minsk-pass" DATA_FILE_EXTENSION);
  LocalCountryFile file = LocalCountryFile::MakeTemporary(path);
  TEST_EQUAL(file.GetPath(MapOptions::Map), path, ());
}

}  // namespace platform