Skip to content
Navigation Menu
{{ message }}
forked from Unity-Technologies/UnityDataTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnityDataToolTests.cs
More file actions
415 lines (336 loc) · 15.7 KB
/
Copy pathUnityDataToolTests.cs
File metadata and controls
415 lines (336 loc) · 15.7 KB
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
using System;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using NUnit.Framework;
using UnityDataTools.FileSystem;
using UnityDataTools.TestCommon;
namespace UnityDataTools.UnityDataTool.Tests;
#pragma warning disable NUnit2005, NUnit2006
public class UnityDataToolTests : AssetBundleTestFixture
{
private string m_TestOutputFolder;
public UnityDataToolTests(Context context) : base(context)
{
}
protected override void OnLoadExpectedData(Context context)
{
// Uncomment to regenerate expected data.
//ExpectedDataGenerator.Generate(context);
}
[OneTimeSetUp]
public void OneTimeSetup()
{
m_TestOutputFolder = Path.Combine(TestContext.CurrentContext.TestDirectory, "test_folder");
Directory.CreateDirectory(m_TestOutputFolder);
Directory.SetCurrentDirectory(m_TestOutputFolder);
}
[TearDown]
public void Teardown()
{
var testDir = new DirectoryInfo(m_TestOutputFolder);
testDir.EnumerateFiles()
.ToList().ForEach(f => f.Delete());
testDir.EnumerateDirectories()
.ToList().ForEach(d => d.Delete(true));
}
[Test]
public async Task InvalidFile(
[Values(
new string[] {"archive", "extract"},
new string[] {"archive", "list"},
new string[] {"dump"}
)] string[] args)
{
var path = Path.Combine(Context.TestDataFolder, "invalidfile");
var command = args.Append(path);
Assert.AreNotEqual(0, await Program.Main(command.ToArray()));
}
public void IsWebBundle_True()
{
var webBundlePath = Path.Combine(Context.TestDataFolder, "WebBundles", "HelloWorld.data");
Assert.IsTrue(Archive.IsWebBundle(new FileInfo(webBundlePath)));
}
[Test]
public void IsWebBundle_False()
{
var nonWebBundlePath = Path.Combine(Context.TestDataFolder, "WebBundles", "NotAWebBundle.txt");
Assert.IsFalse(Archive.IsWebBundle(new FileInfo(nonWebBundlePath)));
}
[Test]
public async Task ArchiveExtract_AssetBundle_FilesExtractedSuccessfully(
[Values("", "-o archive", "--output-path archive")] string options)
{
var path = Path.Combine(Context.UnityDataFolder, "assetbundle");
Assert.AreEqual(0, await Program.Main(new string[] { "archive", "extract", path }.Concat(options.Split(" ", StringSplitOptions.RemoveEmptyEntries)).ToArray()));
Assert.IsTrue(File.Exists(Path.Combine(m_TestOutputFolder, "archive", "CAB-5d40f7cad7c871cf2ad2af19ac542994")));
Assert.IsTrue(File.Exists(Path.Combine(m_TestOutputFolder, "archive", "CAB-5d40f7cad7c871cf2ad2af19ac542994.resS")));
Assert.IsTrue(File.Exists(Path.Combine(m_TestOutputFolder, "archive", "CAB-5d40f7cad7c871cf2ad2af19ac542994.resource")));
}
[Test]
public async Task ArchiveExtract_WebBundle_FileExtractedSuccessfully(
[Values("", "-o archive", "--output-path archive")] string options,
[Values("HelloWorld.data", "HelloWorld.data.gz", "HelloWorld.data.br")] string bundlePath)
{
var path = Path.Combine(Context.TestDataFolder, "WebBundles", bundlePath);
string[] expectedFiles = {
"boot.config",
"data.unity3d",
"RuntimeInitializeOnLoads.json",
"ScriptingAssemblies.json",
Path.Combine("Il2CppData", "Metadata", "global-metadata.dat"),
Path.Combine("Resources", "unity_default_resources"),
};
Assert.AreEqual(0, await Program.Main(new string[] { "archive", "extract", path }.Concat(options.Split(" ", StringSplitOptions.RemoveEmptyEntries)).ToArray()));
foreach (var file in expectedFiles)
{
Assert.IsTrue(File.Exists(Path.Combine(m_TestOutputFolder, "archive", file)));
}
}
[Test]
public async Task ArchiveList_AssetBundle_ListFilesCorrectly()
{
var path = Path.Combine(Context.UnityDataFolder, "assetbundle");
using var sw = new StringWriter();
var currentOut = Console.Out;
try
{
Console.SetOut(sw);
Assert.AreEqual(0, await Program.Main(new string[] { "archive", "list", path }));
var lines = sw.ToString().Split(sw.NewLine);
Assert.AreEqual("CAB-5d40f7cad7c871cf2ad2af19ac542994", lines[0]);
Assert.AreEqual($" Size: {Context.ExpectedData.Get("CAB-5d40f7cad7c871cf2ad2af19ac542994-Size")}", lines[1]);
Assert.AreEqual($" Flags: {(ArchiveNodeFlags)(long)Context.ExpectedData.Get("CAB-5d40f7cad7c871cf2ad2af19ac542994-Flags")}", lines[2]);
Assert.AreEqual("CAB-5d40f7cad7c871cf2ad2af19ac542994.resS", lines[4]);
Assert.AreEqual($" Size: {Context.ExpectedData.Get("CAB-5d40f7cad7c871cf2ad2af19ac542994.resS-Size")}", lines[5]);
Assert.AreEqual($" Flags: {(ArchiveNodeFlags)(long)Context.ExpectedData.Get("CAB-5d40f7cad7c871cf2ad2af19ac542994.resS-Flags")}", lines[6]);
Assert.AreEqual("CAB-5d40f7cad7c871cf2ad2af19ac542994.resource", lines[8]);
Assert.AreEqual($" Size: {Context.ExpectedData.Get("CAB-5d40f7cad7c871cf2ad2af19ac542994.resource-Size")}", lines[9]);
Assert.AreEqual($" Flags: {(ArchiveNodeFlags)(long)Context.ExpectedData.Get("CAB-5d40f7cad7c871cf2ad2af19ac542994.resource-Flags")}", lines[10]);
}
finally
{
Console.SetOut(currentOut);
}
}
[Test]
public async Task ArchiveList_WebBundle_ListFilesCorrectly(
[Values(
"HelloWorld.data",
"HelloWorld.data.gz",
"HelloWorld.data.br"
)] string bundlePath)
{
var path = Path.Combine(Context.TestDataFolder, "WebBundles", bundlePath);
using var sw = new StringWriter();
var currentOut = Console.Out;
try
{
Console.SetOut(sw);
Assert.AreEqual(0, await Program.Main(new string[] { "archive", "list", path }));
var actualOutput = sw.ToString();
var expectedOutput = (
@"data.unity3d
Size: 253044
RuntimeInitializeOnLoads.json
Size: 700
ScriptingAssemblies.json
Size: 3060
boot.config
Size: 93
Il2CppData/Metadata/global-metadata.dat
Size: 1641180
Resources/unity_default_resources
Size: 607376
"
);
Assert.AreEqual(expectedOutput, actualOutput);
}
finally
{
Console.SetOut(currentOut);
}
}
[Test]
public async Task DumpText_DefaultArgs_TextFileCreatedCorrectly(
[Values("", "-f text", "--output-format text")] string options)
{
var path = Path.Combine(Context.UnityDataFolder, "assetbundle");
var outputFile = Path.Combine(m_TestOutputFolder, "CAB-5d40f7cad7c871cf2ad2af19ac542994.txt");
Assert.AreEqual(0, await Program.Main(new string[] { "dump", path }.Concat(options.Split(" ", StringSplitOptions.RemoveEmptyEntries)).ToArray()));
Assert.IsTrue(File.Exists(outputFile));
var content = File.ReadAllText(outputFile);
var expected = File.ReadAllText(Path.Combine(Context.ExpectedDataFolder, "dump", "CAB-5d40f7cad7c871cf2ad2af19ac542994.txt"));
// Normalize line endings.
content = Regex.Replace(content, @"\r\n|\n\r|\r", "\n");
expected = Regex.Replace(expected, @"\r\n|\n\r|\r", "\n");
Assert.AreEqual(expected, content);
}
[Test]
public async Task DumpText_SkipLargeArrays_TextFileCreatedCorrectly(
[Values("-s", "--skip-large-arrays")] string options)
{
var path = Path.Combine(Context.UnityDataFolder, "assetbundle");
var outputFile = Path.Combine(m_TestOutputFolder, "CAB-5d40f7cad7c871cf2ad2af19ac542994.txt");
Assert.AreEqual(0, await Program.Main(new string[] { "dump", path }.Concat(options.Split(" ", StringSplitOptions.RemoveEmptyEntries)).ToArray()));
Assert.IsTrue(File.Exists(outputFile));
var content = File.ReadAllText(outputFile);
var expected = File.ReadAllText(Path.Combine(Context.ExpectedDataFolder, "dump-s", "CAB-5d40f7cad7c871cf2ad2af19ac542994.txt"));
// Normalize line endings.
content = Regex.Replace(content, @"\r\n|\n\r|\r", "\n");
expected = Regex.Replace(expected, @"\r\n|\n\r|\r", "\n");
Assert.AreEqual(expected, content);
}
[Test]
public async Task Analyze_DefaultArgs_DatabaseCorrect()
{
var databasePath = Path.Combine(m_TestOutputFolder, "database.db");
var analyzePath = Path.Combine(Context.UnityDataFolder);
Assert.AreEqual(0, await Program.Main(new string[] { "analyze", analyzePath }));
ValidateDatabase(databasePath, true);
}
[Test]
public async Task Analyze_WithoutRefs_DatabaseCorrect(
[Values("-s", "--skip-references")] string options)
{
var databasePath = Path.Combine(m_TestOutputFolder, "database.db");
var analyzePath = Path.Combine(Context.UnityDataFolder);
Assert.AreEqual(0, await Program.Main(new string[] { "analyze", analyzePath }.Concat(options.Split(" ")).ToArray()));
ValidateDatabase(databasePath, false);
}
[Test]
public async Task Analyze_WithPattern_DatabaseCorrect(
[Values("-p *.", "--search-pattern *.")] string options)
{
var databasePath = Path.Combine(m_TestOutputFolder, "database.db");
var analyzePath = Path.Combine(Context.UnityDataFolder);
Assert.AreEqual(0, await Program.Main(new string[] { "analyze", analyzePath }.Concat(options.Split(" ")).ToArray()));
ValidateDatabase(databasePath, true);
}
[Test]
public async Task Analyze_WithPatternNoMatch_DatabaseEmpty(
[Values("-p *.x", "--search-pattern *.x")] string options)
{
var databasePath = Path.Combine(m_TestOutputFolder, "database.db");
var analyzePath = Path.Combine(Context.UnityDataFolder);
Assert.AreEqual(0, await Program.Main(new string[] { "analyze", analyzePath }.Concat(options.Split(" ")).ToArray()));
using var db = new SQLiteConnection($"Data Source={databasePath};Version=3;New=True;Foreign Keys=False;");
db.Open();
using (var cmd = db.CreateCommand())
{
cmd.CommandText = "SELECT COUNT(*) FROM objects";
Assert.AreEqual(0, cmd.ExecuteScalar());
}
}
[Test]
public async Task Analyze_WithOutputFile_DatabaseCorrect(
[Values("-o my_database", "--output-file my_database")] string options)
{
var databasePath = Path.Combine(m_TestOutputFolder, "my_database");
var analyzePath = Path.Combine(Context.UnityDataFolder);
Assert.AreEqual(0, await Program.Main(new string[] { "analyze", analyzePath }.Concat(options.Split(" ")).ToArray()));
ValidateDatabase(databasePath, true);
}
private void ValidateDatabase(string databasePath, bool withRefs)
{
using var db = new SQLiteConnection($"Data Source={databasePath};Version=3;New=True;Foreign Keys=False;");
db.Open();
using (var cmd = db.CreateCommand())
{
cmd.CommandText =
@"SELECT
(SELECT COUNT(*) FROM animation_clips),
(SELECT COUNT(*) FROM asset_bundles),
(SELECT COUNT(*) FROM assets),
(SELECT COUNT(*) FROM audio_clips),
(SELECT COUNT(*) FROM meshes),
(SELECT COUNT(*) FROM objects),
(SELECT COUNT(*) FROM refs),
(SELECT COUNT(*) FROM serialized_files),
(SELECT COUNT(*) FROM shader_subprograms),
(SELECT COUNT(*) FROM shaders),
(SELECT COUNT(*) FROM shader_keywords),
(SELECT COUNT(*) FROM shader_subprogram_keywords),
(SELECT COUNT(*) FROM textures),
(SELECT COUNT(*) FROM types)";
using var reader = cmd.ExecuteReader();
reader.Read();
Assert.AreEqual(Context.ExpectedData.Get("animation_clips_count"), reader.GetInt32(0));
Assert.AreEqual(Context.ExpectedData.Get("asset_bundles_count"), reader.GetInt32(1));
Assert.AreEqual(Context.ExpectedData.Get("assets_count"), reader.GetInt32(2));
Assert.AreEqual(Context.ExpectedData.Get("audio_clips_count"), reader.GetInt32(3));
Assert.AreEqual(Context.ExpectedData.Get("meshes_count"), reader.GetInt32(4));
Assert.AreEqual(Context.ExpectedData.Get("objects_count"), reader.GetInt32(5));
Assert.AreEqual(withRefs ? Context.ExpectedData.Get("refs_count") : 0, reader.GetInt32(6));
Assert.AreEqual(Context.ExpectedData.Get("serialized_files_count"), reader.GetInt32(7));
Assert.AreEqual(Context.ExpectedData.Get("shader_subprograms_count"), reader.GetInt32(8));
Assert.AreEqual(Context.ExpectedData.Get("shaders_count"), reader.GetInt32(9));
Assert.AreEqual(Context.ExpectedData.Get("shader_keywords_count"), reader.GetInt32(10));
Assert.AreEqual(Context.ExpectedData.Get("shader_subprogram_keywords_count"), reader.GetInt32(11));
Assert.AreEqual(Context.ExpectedData.Get("textures_count"), reader.GetInt32(12));
Assert.AreEqual(Context.ExpectedData.Get("types_count"), reader.GetInt32(13));
}
}
}
public class UnityDataToolPlayerDataTests : PlayerDataTestFixture
{
private string m_TestOutputFolder;
public UnityDataToolPlayerDataTests(Context context) : base(context)
{
}
[OneTimeSetUp]
public void OneTimeSetup()
{
m_TestOutputFolder = Path.Combine(TestContext.CurrentContext.TestDirectory, "test_folder");
Directory.CreateDirectory(m_TestOutputFolder);
Directory.SetCurrentDirectory(m_TestOutputFolder);
}
[TearDown]
public void Teardown()
{
foreach (var file in new DirectoryInfo(m_TestOutputFolder).EnumerateFiles())
{
file.Delete();
}
}
[Test]
public async Task Analyze_PlayerData_DatabaseCorrect()
{
var databasePath = Path.Combine(m_TestOutputFolder, "database.db");
var analyzePath = Path.Combine(Context.UnityDataFolder);
Assert.AreEqual(0, await Program.Main(new string[] { "analyze", analyzePath, "-p", "*." }));
using var db = new SQLiteConnection($"Data Source={databasePath};Version=3;New=True;Foreign Keys=False;");
db.Open();
using var cmd = db.CreateCommand();
cmd.CommandText =
@"SELECT
(SELECT COUNT(*) FROM asset_bundles),
(SELECT COUNT(*) FROM assets),
(SELECT COUNT(*) FROM objects),
(SELECT COUNT(*) FROM refs),
(SELECT COUNT(*) FROM serialized_files)";
using var reader = cmd.ExecuteReader();
reader.Read();
Assert.AreEqual(0, reader.GetInt32(0));
Assert.AreEqual(0, reader.GetInt32(1));
Assert.Greater(reader.GetInt32(2), 0);
Assert.Greater(reader.GetInt32(3), 0);
Assert.AreEqual(1, reader.GetInt32(4));
}
[Test]
public async Task DumpText_PlayerData_TextFileCreatedCorrectly()
{
var path = Path.Combine(Context.UnityDataFolder, "level0");
var outputFile = Path.Combine(m_TestOutputFolder, "level0.txt");
Assert.AreEqual(0, await Program.Main(new string[] { "dump", path }));
Assert.IsTrue(File.Exists(outputFile));
var content = File.ReadAllText(outputFile);
var expected = File.ReadAllText(Path.Combine(Context.ExpectedDataFolder, "level0.txt"));
// Normalize line endings.
content = Regex.Replace(content, @"\r\n|\n\r|\r", "\n");
expected = Regex.Replace(expected, @"\r\n|\n\r|\r", "\n");
Assert.AreEqual(expected, content);
}
}
You can’t perform that action at this time.
