1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef MEDIA_CDM_PPAPI_CDM_FILE_IO_TEST_H_
6#define MEDIA_CDM_PPAPI_CDM_FILE_IO_TEST_H_
7
8#include <list>
9#include <stack>
10#include <string>
11#include <vector>
12
13#include "base/callback.h"
14#include "base/compiler_specific.h"
15#include "media/cdm/ppapi/api/content_decryption_module.h"
16
17namespace media {
18
19typedef base::Callback<void(bool success)> CompletionCB;
20typedef base::Callback<cdm::FileIO*(cdm::FileIOClient* client)> CreateFileIOCB;
21
22// A customizable test class that tests cdm::FileIO implementation.
23// - To create a test, call AddTestStep() to add a test step. A test step can be
24//   either an action to make (use ACTION_* types), or a result to verify (use
25//   RESULT_* types).
26// - To run the test, simply call Run() with a completion callback. The result
27//   will be reported in the completion callback when the test is finished.
28//
29// The following rules apply to the test steps:
30// - Test steps are ordered (with the exception that results in a result group
31//   is not ordered).
32// - Consecutive action steps form an "action group". Consecutively result
33//   steps form a "result group". An action group is followed by a result
34//   group and vice versa.
35// - A test must start with an action group.
36// - To process an action group, the test runner runs (and clears) all steps
37//   in the group in the order they were added. Then it waits for test
38//   results.
39// - When a cdm::FileIOClient method is called, the test runner compares the
40//   result with all results in the current result group. If no result in that
41//   group matches the test result, the test fails. Otherwise, the matching
42//   result is cleared from the group. If the group is empty, the test runner
43//   starts to process the next action group. Otherwise, the test runner keeps
44//   waiting for the next test result.
45// - After all steps are cleared, the test passes.
46class FileIOTest : public cdm::FileIOClient {
47 public:
48  // Types of allowed test steps:
49  // - ACTION_* specifies the next step to test.
50  // - RESULT_* specifies the expected result of the previous step(s).
51  enum StepType {
52    ACTION_CREATE,
53    ACTION_OPEN,  // |test_name_| will be used used as the file name to open.
54    RESULT_OPEN,
55    ACTION_READ,
56    RESULT_READ,
57    ACTION_WRITE,
58    RESULT_WRITE,
59    ACTION_CLOSE  // If ACTION_CLOSE is not specified, FileIO::Close() will be
60                  // automatically called at the end of the test.
61  };
62
63  FileIOTest(const CreateFileIOCB& create_file_io_cb,
64             const std::string& test_name);
65  ~FileIOTest();
66
67  // Adds a test step in this test. |this| object doesn't take the ownership of
68  // |data|, which should be valid throughout the lifetime of |this| object.
69  void AddTestStep(
70      StepType type, Status status, const uint8* data, uint32 data_size);
71
72  // Runs this test case and returns the test result through |completion_cb|.
73  void Run(const CompletionCB& completion_cb);
74
75 private:
76  struct TestStep {
77    // |this| object doesn't take the ownership of |data|, which should be valid
78    // throughout the lifetime of |this| object.
79    TestStep(StepType type, Status status, const uint8* data, uint32 data_size)
80        : type(type), status(status), data(data), data_size(data_size) {}
81
82    StepType type;
83
84    // Expected status for RESULT* steps.
85    Status status;
86
87    // Data to write in ACTION_WRITE, or read data in RESULT_READ.
88    const uint8* data;
89    uint32 data_size;
90  };
91
92  // Returns whether |test_step| is a RESULT_* step.
93  static bool IsResult(const TestStep& test_step);
94
95  // Returns whether two results match.
96  static bool MatchesResult(const TestStep& a, const TestStep& b);
97
98  // cdm::FileIOClient implementation.
99  virtual void OnOpenComplete(Status status) OVERRIDE;
100  virtual void OnReadComplete(Status status,
101                              const uint8_t* data,
102                              uint32_t data_size) OVERRIDE;
103  virtual void OnWriteComplete(Status status) OVERRIDE;
104
105  // Runs the next step in this test case.
106  void RunNextStep();
107
108  void OnResult(const TestStep& result);
109
110  // Checks whether the test result matches this step. This can only be called
111  // when this step is a RESULT_* step.
112  bool CheckResult(const TestStep& result);
113
114  void OnTestComplete(bool success);
115
116  CreateFileIOCB create_file_io_cb_;
117  CompletionCB completion_cb_;
118
119  std::string test_name_;
120  std::list<TestStep> test_steps_;
121
122  // All opened cdm::FileIO objects. We keep multiple cdm::FileIO objects open
123  // so that we can test multiple cdm::FileIO objects accessing the same file.
124  // In the current implementation, all ACTION_* are performed on the latest
125  // opened cdm::FileIO object, hence the stack.
126  std::stack<cdm::FileIO*> file_io_stack_;
127};
128
129// Tests cdm::FileIO implementation.
130class FileIOTestRunner {
131 public:
132  explicit FileIOTestRunner(const CreateFileIOCB& create_file_io_cb);
133  ~FileIOTestRunner();
134
135  void AddTests();
136
137  // Run all tests. When tests are completed, the result will be reported in the
138  // |completion_cb|.
139  void RunAllTests(const CompletionCB& completion_cb);
140
141 private:
142  void OnTestComplete(bool success);
143  void RunNextTest();
144
145  CreateFileIOCB create_file_io_cb_;
146  CompletionCB completion_cb_;
147  std::list<FileIOTest> remaining_tests_;
148  std::vector<uint8> large_data_;
149  size_t total_num_tests_;  // Total number of tests.
150  size_t num_passed_tests_;  // Number of passed tests.
151
152  DISALLOW_COPY_AND_ASSIGN (FileIOTestRunner);
153};
154
155}  // namespace media
156
157#endif  // MEDIA_CDM_PPAPI_CDM_FILE_IO_TEST_H_
158