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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
media / cdm / library_cdm / clear_key_cdm / cdm_file_io_test.cc [blame]
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "media/cdm/library_cdm/clear_key_cdm/cdm_file_io_test.h"
#include <algorithm>
#include <limits>
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
namespace media {
#define FILE_IO_DVLOG(level) DVLOG(level) << "File IO Test: "
const uint8_t kData[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
const uint32_t kDataSize = std::size(kData);
const uint8_t kBigData[] = {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa,
0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55,
0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00};
const uint32_t kBigDataSize = std::size(kBigData);
// Must be > kReadSize in cdm_file_io_impl.cc.
const uint32_t kLargeDataSize = 20 * 1024 + 7;
// Macros to help add test cases/steps.
// |test_name| is also used as the file name. File name validity tests relies
// on this to work.
#define START_TEST_CASE(test_name) \
do { \
auto test_case = \
std::make_unique<FileIOTest>(create_file_io_cb_, test_name); \
CREATE_FILE_IO // Create FileIO for each test case.
#define ADD_TEST_STEP(type, status, data, data_size) \
test_case->AddTestStep(FileIOTest::type, cdm::FileIOClient::Status::status, \
(data), (data_size));
#define END_TEST_CASE \
remaining_tests_.push_back(std::move(test_case)); \
} while(0);
#define CREATE_FILE_IO \
ADD_TEST_STEP(ACTION_CREATE, kSuccess, nullptr, 0)
#define OPEN_FILE \
ADD_TEST_STEP(ACTION_OPEN, kSuccess, nullptr, 0)
#define EXPECT_FILE_OPENED(status) \
ADD_TEST_STEP(RESULT_OPEN, status, nullptr, 0)
#define READ_FILE \
ADD_TEST_STEP(ACTION_READ, kSuccess, nullptr, 0)
#define EXPECT_FILE_READ(status, data, data_size) \
ADD_TEST_STEP(RESULT_READ, status, data, data_size)
#define EXPECT_FILE_READ_EITHER(status, data, data_size, data2, data2_size) \
test_case->AddResultReadEither(cdm::FileIOClient::Status::status, (data), \
(data_size), (data2), (data2_size));
#define WRITE_FILE(data, data_size) \
ADD_TEST_STEP(ACTION_WRITE, kSuccess, data, data_size)
#define EXPECT_FILE_WRITTEN(status) \
ADD_TEST_STEP(RESULT_WRITE, status, nullptr, 0)
#define CLOSE_FILE \
ADD_TEST_STEP(ACTION_CLOSE, kSuccess, nullptr, 0)
// FileIOTestRunner implementation.
FileIOTestRunner::FileIOTestRunner(const CreateFileIOCB& create_file_io_cb)
: create_file_io_cb_(create_file_io_cb) {
// Generate |large_data_|.
large_data_.resize(kLargeDataSize);
for (size_t i = 0; i < kLargeDataSize; ++i)
large_data_[i] = i % std::numeric_limits<uint8_t>::max();
AddTests();
}
FileIOTestRunner::~FileIOTestRunner() {
if (remaining_tests_.empty())
return;
DCHECK_LT(num_passed_tests_, total_num_tests_);
FILE_IO_DVLOG(1) << "Not Finished (probably due to timeout). "
<< num_passed_tests_ << " passed in "
<< total_num_tests_ << " tests.";
}
// Note: Consecutive expectations (EXPECT*) can happen in any order.
void FileIOTestRunner::AddTests() {
START_TEST_CASE("/FileNameStartsWithForwardSlash")
OPEN_FILE
EXPECT_FILE_OPENED(kError)
END_TEST_CASE
START_TEST_CASE("FileNameContains/ForwardSlash")
OPEN_FILE
EXPECT_FILE_OPENED(kError)
END_TEST_CASE
START_TEST_CASE("\\FileNameStartsWithBackslash")
OPEN_FILE
EXPECT_FILE_OPENED(kError)
END_TEST_CASE
START_TEST_CASE("FileNameContains\\Backslash")
OPEN_FILE
EXPECT_FILE_OPENED(kError)
END_TEST_CASE
START_TEST_CASE("FileNameContains Space")
OPEN_FILE
EXPECT_FILE_OPENED(kError)
END_TEST_CASE
START_TEST_CASE("FileNameContains?QuestionMark")
OPEN_FILE
EXPECT_FILE_OPENED(kError)
END_TEST_CASE
START_TEST_CASE("FileNameContains\xeeNonASCII")
OPEN_FILE
EXPECT_FILE_OPENED(kError)
END_TEST_CASE
START_TEST_CASE(
"FileNameLongThan256Characters1234567890123456789012345678901234567890123"
"456789012345678901234567890123456789012345678901234567890123456789012345"
"678901234567890123456789012345678901234567890123456789012345678901234567"
"890123456789012345678901234567890123456789012345678901234567890")
OPEN_FILE
EXPECT_FILE_OPENED(kError)
END_TEST_CASE
START_TEST_CASE("_FileNameStartsWithUnderscore")
OPEN_FILE
EXPECT_FILE_OPENED(kError)
END_TEST_CASE
START_TEST_CASE("FileNameContains_Underscore")
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
END_TEST_CASE
START_TEST_CASE("ReadBeforeOpeningFile")
READ_FILE
EXPECT_FILE_READ(kError, nullptr, 0)
END_TEST_CASE
START_TEST_CASE("WriteBeforeOpeningFile")
WRITE_FILE(kData, kDataSize)
EXPECT_FILE_WRITTEN(kError)
END_TEST_CASE
START_TEST_CASE("ReadBeforeFileOpened")
OPEN_FILE
READ_FILE
EXPECT_FILE_OPENED(kSuccess)
EXPECT_FILE_READ(kError, nullptr, 0)
// After file opened, we can still do normal operations.
WRITE_FILE(kData, kDataSize)
EXPECT_FILE_WRITTEN(kSuccess)
READ_FILE
EXPECT_FILE_READ(kSuccess, kData, kDataSize)
END_TEST_CASE
START_TEST_CASE("WriteBeforeFileOpened")
OPEN_FILE
WRITE_FILE(kData, kDataSize)
EXPECT_FILE_WRITTEN(kError)
EXPECT_FILE_OPENED(kSuccess)
// After file opened, we can still do normal operations.
WRITE_FILE(kData, kDataSize)
EXPECT_FILE_WRITTEN(kSuccess)
READ_FILE
EXPECT_FILE_READ(kSuccess, kData, kDataSize)
END_TEST_CASE
START_TEST_CASE("ReadDuringPendingRead")
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
WRITE_FILE(kData, kDataSize)
EXPECT_FILE_WRITTEN(kSuccess)
READ_FILE
READ_FILE
EXPECT_FILE_READ(kInUse, nullptr, 0)
EXPECT_FILE_READ(kSuccess, kData, kDataSize)
// Read again.
READ_FILE
EXPECT_FILE_READ(kSuccess, kData, kDataSize)
END_TEST_CASE
START_TEST_CASE("ReadDuringPendingWrite")
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
WRITE_FILE(kData, kDataSize)
READ_FILE
EXPECT_FILE_READ(kInUse, nullptr, 0)
EXPECT_FILE_WRITTEN(kSuccess)
// Read again.
READ_FILE
EXPECT_FILE_READ(kSuccess, kData, kDataSize)
END_TEST_CASE
START_TEST_CASE("WriteDuringPendingRead")
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
READ_FILE
WRITE_FILE(kData, kDataSize)
EXPECT_FILE_WRITTEN(kInUse)
EXPECT_FILE_READ(kSuccess, nullptr, 0)
// We can still do normal operations.
WRITE_FILE(kData, kDataSize)
EXPECT_FILE_WRITTEN(kSuccess)
READ_FILE
EXPECT_FILE_READ(kSuccess, kData, kDataSize)
END_TEST_CASE
START_TEST_CASE("WriteDuringPendingWrite")
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
WRITE_FILE(kData, kDataSize)
WRITE_FILE(kBigData, kBigDataSize)
EXPECT_FILE_WRITTEN(kInUse)
EXPECT_FILE_WRITTEN(kSuccess)
// Read to make sure original data (kData) is written.
READ_FILE
EXPECT_FILE_READ(kSuccess, kData, kDataSize)
END_TEST_CASE
START_TEST_CASE("ReadFileThatDoesNotExist")
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
READ_FILE
EXPECT_FILE_READ(kSuccess, nullptr, 0)
END_TEST_CASE
START_TEST_CASE("WriteAndRead")
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
WRITE_FILE(kData, kDataSize)
EXPECT_FILE_WRITTEN(kSuccess)
READ_FILE
EXPECT_FILE_READ(kSuccess, kData, kDataSize)
END_TEST_CASE
START_TEST_CASE("WriteAndReadEmptyFile")
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
WRITE_FILE(nullptr, 0)
EXPECT_FILE_WRITTEN(kSuccess)
READ_FILE
EXPECT_FILE_READ(kSuccess, nullptr, 0)
END_TEST_CASE
START_TEST_CASE("WriteAndReadLargeData")
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
WRITE_FILE(&large_data_[0], kLargeDataSize)
EXPECT_FILE_WRITTEN(kSuccess)
READ_FILE
EXPECT_FILE_READ(kSuccess, &large_data_[0], kLargeDataSize)
END_TEST_CASE
START_TEST_CASE("OverwriteZeroBytes")
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
WRITE_FILE(kData, kDataSize)
EXPECT_FILE_WRITTEN(kSuccess)
READ_FILE
EXPECT_FILE_READ(kSuccess, kData, kDataSize)
WRITE_FILE(nullptr, 0)
EXPECT_FILE_WRITTEN(kSuccess)
READ_FILE
EXPECT_FILE_READ(kSuccess, nullptr, 0)
END_TEST_CASE
START_TEST_CASE("OverwriteWithSmallerData")
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
WRITE_FILE(kBigData, kBigDataSize)
EXPECT_FILE_WRITTEN(kSuccess)
WRITE_FILE(kData, kDataSize)
EXPECT_FILE_WRITTEN(kSuccess)
READ_FILE
EXPECT_FILE_READ(kSuccess, kData, kDataSize)
END_TEST_CASE
START_TEST_CASE("OverwriteWithLargerData")
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
WRITE_FILE(kData, kDataSize)
EXPECT_FILE_WRITTEN(kSuccess)
WRITE_FILE(kBigData, kBigDataSize)
EXPECT_FILE_WRITTEN(kSuccess)
READ_FILE
EXPECT_FILE_READ(kSuccess, kBigData, kBigDataSize)
END_TEST_CASE
START_TEST_CASE("ReadExistingFile")
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
WRITE_FILE(kData, kDataSize)
EXPECT_FILE_WRITTEN(kSuccess)
CLOSE_FILE
CREATE_FILE_IO
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
READ_FILE
EXPECT_FILE_READ(kSuccess, kData, kDataSize)
END_TEST_CASE
START_TEST_CASE("WriteExistingFile")
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
// Write big data first
WRITE_FILE(kBigData, kBigDataSize)
EXPECT_FILE_WRITTEN(kSuccess)
CLOSE_FILE
CREATE_FILE_IO
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
READ_FILE
EXPECT_FILE_READ(kSuccess, kBigData, kBigDataSize)
// Now overwrite the file with normal data
WRITE_FILE(kData, kDataSize)
EXPECT_FILE_WRITTEN(kSuccess)
CLOSE_FILE
CREATE_FILE_IO
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
READ_FILE
EXPECT_FILE_READ(kSuccess, kData, kDataSize)
// Now overwrite the file with big data again
WRITE_FILE(kBigData, kBigDataSize)
EXPECT_FILE_WRITTEN(kSuccess)
CLOSE_FILE
CREATE_FILE_IO
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
READ_FILE
EXPECT_FILE_READ(kSuccess, kBigData, kBigDataSize)
END_TEST_CASE
START_TEST_CASE("MultipleReadsAndWrites")
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
// Read file which doesn't exist.
READ_FILE
EXPECT_FILE_READ(kSuccess, nullptr, 0)
// Write kData to file.
WRITE_FILE(kData, kDataSize)
EXPECT_FILE_WRITTEN(kSuccess)
// Read file.
READ_FILE
EXPECT_FILE_READ(kSuccess, kData, kDataSize)
// Read file again.
READ_FILE
EXPECT_FILE_READ(kSuccess, kData, kDataSize)
// Overwrite file with large data.
WRITE_FILE(&large_data_[0], kLargeDataSize)
EXPECT_FILE_WRITTEN(kSuccess)
// Read file.
READ_FILE
EXPECT_FILE_READ(kSuccess, &large_data_[0], kLargeDataSize)
// Overwrite file with kData.
WRITE_FILE(kData, kDataSize)
EXPECT_FILE_WRITTEN(kSuccess)
// Read file.
READ_FILE
EXPECT_FILE_READ(kSuccess, kData, kDataSize)
// Overwrite file with zero bytes.
WRITE_FILE(nullptr, 0)
EXPECT_FILE_WRITTEN(kSuccess)
// Read file.
READ_FILE
EXPECT_FILE_READ(kSuccess, nullptr, 0)
END_TEST_CASE
START_TEST_CASE("OpenAfterOpen")
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
OPEN_FILE
EXPECT_FILE_OPENED(kError)
END_TEST_CASE
START_TEST_CASE("OpenDuringPendingOpen")
OPEN_FILE
OPEN_FILE
EXPECT_FILE_OPENED(kError) // The second Open() failed.
EXPECT_FILE_OPENED(kSuccess) // The first Open() succeeded.
END_TEST_CASE
START_TEST_CASE("ReopenFileInSeparateFileIO")
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
WRITE_FILE(kData, kDataSize)
EXPECT_FILE_WRITTEN(kSuccess)
CREATE_FILE_IO // Create a second FileIO without closing the first one.
OPEN_FILE
EXPECT_FILE_OPENED(kInUse)
END_TEST_CASE
START_TEST_CASE("CloseAfterCreation")
CLOSE_FILE
END_TEST_CASE
START_TEST_CASE("CloseDuringPendingOpen")
OPEN_FILE
CLOSE_FILE
END_TEST_CASE
START_TEST_CASE("CloseDuringPendingWrite")
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
WRITE_FILE(kData, kDataSize)
CLOSE_FILE
END_TEST_CASE
START_TEST_CASE("CloseDuringPendingOverwriteWithLargerData")
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
WRITE_FILE(kData, kDataSize)
EXPECT_FILE_WRITTEN(kSuccess)
WRITE_FILE(kBigData, kBigDataSize)
CLOSE_FILE
// Write() is async, so it may or may not modify the content of the file.
CREATE_FILE_IO
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
READ_FILE
// As Write() is async, it is possible that the second write above
// succeeds before the file is closed. So check that the contents
// is either data set.
EXPECT_FILE_READ_EITHER(kSuccess,
kData, kDataSize,
kBigData, kBigDataSize)
END_TEST_CASE
START_TEST_CASE("CloseDuringPendingOverwriteWithSmallerData")
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
WRITE_FILE(kBigData, kBigDataSize)
EXPECT_FILE_WRITTEN(kSuccess)
WRITE_FILE(kData, kDataSize)
CLOSE_FILE
// Write() is async, so it may or may not modify the content of the file.
CREATE_FILE_IO
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
READ_FILE
// As Write() is async, it is possible that the second write above
// succeeds before the file is closed. So check that the contents
// is either data set.
EXPECT_FILE_READ_EITHER(kSuccess,
kBigData, kBigDataSize,
kData, kDataSize)
END_TEST_CASE
START_TEST_CASE("CloseDuringPendingRead")
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
WRITE_FILE(kData, kDataSize)
EXPECT_FILE_WRITTEN(kSuccess)
READ_FILE
CLOSE_FILE
// Make sure the file is not modified.
CREATE_FILE_IO
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
READ_FILE
EXPECT_FILE_READ(kSuccess, kData, kDataSize)
END_TEST_CASE
START_TEST_CASE("StressTest")
for (int i = 0; i < 100; ++i) {
CREATE_FILE_IO
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
WRITE_FILE(kData, kDataSize)
EXPECT_FILE_WRITTEN(kSuccess)
WRITE_FILE(kBigData, kBigDataSize)
CLOSE_FILE
// Make sure the file is not modified.
CREATE_FILE_IO
OPEN_FILE
EXPECT_FILE_OPENED(kSuccess)
READ_FILE
// As Write() is async, it is possible that the second write above
// succeeds before the file is closed. So check that the contents
// is either data set.
EXPECT_FILE_READ_EITHER(kSuccess,
kData, kDataSize,
kBigData, kBigDataSize)
CLOSE_FILE
}
END_TEST_CASE
}
void FileIOTestRunner::RunAllTests(CompletionCB completion_cb) {
completion_cb_ = std::move(completion_cb);
total_num_tests_ = remaining_tests_.size();
RunNextTest();
}
void FileIOTestRunner::RunNextTest() {
if (remaining_tests_.empty()) {
FILE_IO_DVLOG(1) << num_passed_tests_ << " passed and "
<< (total_num_tests_ - num_passed_tests_) << " failed in "
<< total_num_tests_ << " tests.";
bool success = (num_passed_tests_ == total_num_tests_);
std::move(completion_cb_).Run(success);
return;
}
remaining_tests_.front()->Run(base::BindOnce(
&FileIOTestRunner::OnTestComplete, base::Unretained(this)));
}
void FileIOTestRunner::OnTestComplete(bool success) {
if (success)
num_passed_tests_++;
remaining_tests_.pop_front();
RunNextTest();
}
// FileIOTest implementation.
FileIOTest::FileIOTest(const CreateFileIOCB& create_file_io_cb,
const std::string& test_name)
: create_file_io_cb_(create_file_io_cb), test_name_(test_name) {}
FileIOTest::~FileIOTest() = default;
void FileIOTest::AddTestStep(StepType type,
Status status,
const uint8_t* data,
uint32_t data_size) {
test_steps_.push_back(TestStep(type, status, data, data_size));
}
void FileIOTest::AddResultReadEither(Status status,
const uint8_t* data,
uint32_t data_size,
const uint8_t* data2,
uint32_t data2_size) {
DCHECK_NE(data_size, data2_size);
test_steps_.push_back(TestStep(FileIOTest::RESULT_READ, status, data,
data_size, data2, data2_size));
}
void FileIOTest::Run(CompletionCB completion_cb) {
FILE_IO_DVLOG(3) << "Run " << test_name_;
completion_cb_ = std::move(completion_cb);
DCHECK(!test_steps_.empty() && !IsResult(test_steps_.front()));
RunNextStep();
}
void FileIOTest::OnOpenComplete(Status status) {
OnResult(TestStep(RESULT_OPEN, status));
}
void FileIOTest::OnReadComplete(Status status,
const uint8_t* data,
uint32_t data_size) {
OnResult(TestStep(RESULT_READ, status, data, data_size));
}
void FileIOTest::OnWriteComplete(Status status) {
OnResult(TestStep(RESULT_WRITE, status));
}
bool FileIOTest::IsResult(const TestStep& test_step) {
switch (test_step.type) {
case RESULT_OPEN:
case RESULT_READ:
case RESULT_WRITE:
return true;
case ACTION_CREATE:
case ACTION_OPEN:
case ACTION_READ:
case ACTION_WRITE:
case ACTION_CLOSE:
return false;
}
NOTREACHED();
}
bool FileIOTest::MatchesResult(const TestStep& a, const TestStep& b) {
DCHECK(IsResult(a) && IsResult(b));
DCHECK(!b.data2);
if (a.type != b.type || a.status != b.status)
return false;
if (a.type != RESULT_READ || a.status != cdm::FileIOClient::Status::kSuccess)
return true;
// If |a| specifies a data2, compare it first. If the size matches, compare
// the contents.
if (a.data2 && b.data_size == a.data2_size)
return std::equal(a.data2, a.data2 + a.data2_size, b.data);
return (a.data_size == b.data_size &&
std::equal(a.data, a.data + a.data_size, b.data));
}
void FileIOTest::RunNextStep() {
// Run all actions in the current action group.
while (!test_steps_.empty()) {
// Start to wait for test results when the next step is a test result.
if (IsResult(test_steps_.front()))
return;
TestStep test_step = test_steps_.front();
test_steps_.pop_front();
cdm::FileIO* file_io =
file_io_stack_.empty() ? nullptr : file_io_stack_.top();
switch (test_step.type) {
case ACTION_CREATE:
file_io = create_file_io_cb_.Run(this);
if (!file_io) {
LOG(WARNING) << test_name_ << " cannot create FileIO object.";
OnTestComplete(false);
return;
}
file_io_stack_.push(file_io);
break;
case ACTION_OPEN:
// Use test name as the test file name.
file_io->Open(test_name_.data(), test_name_.size());
break;
case ACTION_READ:
file_io->Read();
break;
case ACTION_WRITE:
file_io->Write(test_step.data, test_step.data_size);
break;
case ACTION_CLOSE:
file_io->Close();
file_io_stack_.pop();
break;
default:
NOTREACHED();
}
}
OnTestComplete(true);
}
void FileIOTest::OnResult(const TestStep& result) {
DCHECK(IsResult(result));
if (!CheckResult(result)) {
LOG(WARNING) << test_name_ << " got unexpected result. type=" << result.type
<< ", status=" << (uint32_t)result.status
<< ", data_size=" << result.data_size << ", received data="
<< (result.data
? base::HexEncode(result.data, result.data_size)
: "<null>");
for (const auto& step : test_steps_) {
if (IsResult(step)) {
LOG(WARNING) << test_name_ << " expected type=" << step.type
<< ", status=" << (uint32_t)step.status
<< ", data_size=" << step.data_size;
}
}
OnTestComplete(false);
return;
}
RunNextStep();
}
bool FileIOTest::CheckResult(const TestStep& result) {
if (test_steps_.empty() || !IsResult(test_steps_.front()))
return false;
// If there are multiple results expected, the order does not matter.
auto iter = test_steps_.begin();
for (; iter != test_steps_.end(); ++iter) {
if (!IsResult(*iter))
return false;
if (!MatchesResult(*iter, result))
continue;
test_steps_.erase(iter);
return true;
}
return false;
}
void FileIOTest::OnTestComplete(bool success) {
while (!file_io_stack_.empty()) {
file_io_stack_.top()->Close();
file_io_stack_.pop();
}
FILE_IO_DVLOG(3) << test_name_ << (success ? " PASSED" : " FAILED");
LOG_IF(WARNING, !success) << test_name_ << " FAILED";
std::move(completion_cb_).Run(success);
}
} // namespace media