Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 1 | /* Copyright 2019 The Chromium OS 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 | |
| 6 | #ifndef HARDWARE_VERIFIER_CLI_H_ |
| 7 | #define HARDWARE_VERIFIER_CLI_H_ |
| 8 | |
| 9 | #include <memory> |
| 10 | #include <ostream> |
| 11 | #include <string> |
| 12 | |
| 13 | #include <base/macros.h> |
| 14 | |
| 15 | #include "hardware_verifier/hw_verification_spec_getter.h" |
| 16 | #include "hardware_verifier/probe_result_getter.h" |
| 17 | #include "hardware_verifier/verifier.h" |
| 18 | |
| 19 | namespace hardware_verifier { |
| 20 | |
| 21 | enum CLIVerificationResult { |
| 22 | kPass = 0, |
| 23 | |
| 24 | // The whole process works without errors, but the verification |
| 25 | // report shows the device is not compliant. |
| 26 | kFail, |
| 27 | |
| 28 | // Failed to load the probe result from the specific file. |
| 29 | kInvalidProbeResultFile, |
| 30 | |
| 31 | // Failed to load the verification payload from either the default one |
| 32 | // or the specific one. |
| 33 | kInvalidHwVerificationSpecFile, |
| 34 | |
| 35 | // The |runtime_probe| fails to return a valid probe result. |
| 36 | kProbeFail, |
| 37 | |
| 38 | // Content in the verification payload and the probe result are not matched |
| 39 | // to each other. |
| 40 | kProbeResultHwVerificationSpecMisalignment, |
| 41 | |
| 42 | kUnknownError |
| 43 | }; |
| 44 | |
| 45 | enum CLIOutputFormat { |
| 46 | kProtoBin = 0, // Protobuf binary format. |
| 47 | kProtoText = 1 // Human readable protobuf text format for debug purpose. |
| 48 | }; |
| 49 | |
| 50 | // A class that holds the core logic of the program if runs in CLI mode. |
| 51 | class CLI { |
| 52 | public: |
| 53 | // Constructor, it sets the dependent classes to the default implementation. |
| 54 | CLI(); |
| 55 | |
| 56 | // Verifies the probe result with the verification payload and then outputs |
| 57 | // the report. |
| 58 | // |
| 59 | // @param probe_result_file: Path to the file that contains the probe result. |
| 60 | // If the string is empty, it invokes |runtime_probe| to get the probe |
| 61 | // result. |
| 62 | // @param hw_verification_spec_file: Path to the file that contains the |
| 63 | // verification payload. If the string is empty, it reads the default |
| 64 | // verification payload file in the rootfs. |
| 65 | // @param output_format: The format of the output data. |
| 66 | // |
| 67 | // @return Execution result, can be either the verification result or the |
| 68 | // failure code. |
| 69 | CLIVerificationResult Run(const std::string& probe_result_file, |
| 70 | const std::string& hw_verification_spec_file, |
| 71 | const CLIOutputFormat output_format); |
| 72 | |
| 73 | private: |
| 74 | friend class CLITest; |
| 75 | |
| 76 | // Dependent classes. |
| 77 | std::unique_ptr<ProbeResultGetter> pr_getter_; |
| 78 | std::unique_ptr<HwVerificationSpecGetter> vp_getter_; |
| 79 | std::unique_ptr<Verifier> verifier_; |
| 80 | |
| 81 | // Instance to the output stream, default to |std::cout|. |
| 82 | std::ostream* output_stream_; |
| 83 | |
| 84 | DISALLOW_COPY_AND_ASSIGN(CLI); |
| 85 | }; |
| 86 | |
| 87 | } // namespace hardware_verifier |
| 88 | |
| 89 | #endif // HARDWARE_VERIFIER_CLI_H_ |