blob: c32cd32d339710bea256d17b1b3b54222d8380e8 [file] [log] [blame]
Shawn Willden76364712014-08-11 17:48:04 -06001/*
2 * Copyright 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SYSTEM_KEYMASTER_GOOGLE_KEYMASTER_TEST_UTILS_H_
18#define SYSTEM_KEYMASTER_GOOGLE_KEYMASTER_TEST_UTILS_H_
19
20/*
21 * Utilities used to help with testing. Not used in production code.
22 */
23
Shawn Willden2f3be362014-08-25 11:31:39 -060024#include <stdarg.h>
25
Shawn Willden76364712014-08-11 17:48:04 -060026#include <ostream>
27
Shawn Willdenb9d584d2015-01-22 16:35:00 -070028#include <hardware/keymaster_defs.h>
Shawn Willden98d9b922014-08-26 08:14:10 -060029#include <keymaster/authorization_set.h>
Shawn Willden98d9b922014-08-26 08:14:10 -060030#include <keymaster/logger.h>
Shawn Willden76364712014-08-11 17:48:04 -060031
32std::ostream& operator<<(std::ostream& os, const keymaster_key_param_t& param);
33bool operator==(const keymaster_key_param_t& a, const keymaster_key_param_t& b);
34
35namespace keymaster {
Shawn Willden2f3be362014-08-25 11:31:39 -060036
Shawn Willden76364712014-08-11 17:48:04 -060037bool operator==(const AuthorizationSet& a, const AuthorizationSet& b);
Shawn Willden2f3be362014-08-25 11:31:39 -060038
Shawn Willden76364712014-08-11 17:48:04 -060039std::ostream& operator<<(std::ostream& os, const AuthorizationSet& set);
Shawn Willden2f3be362014-08-25 11:31:39 -060040
41namespace test {
42
Shawn Willden407d4122014-08-25 16:49:13 -060043template <keymaster_tag_t Tag, typename KeymasterEnum>
44bool contains(const AuthorizationSet& set, TypedEnumTag<KM_ENUM, Tag, KeymasterEnum> tag,
45 KeymasterEnum val) {
46 int pos = set.find(tag);
47 return pos != -1 && set[pos].enumerated == val;
48}
49
50template <keymaster_tag_t Tag, typename KeymasterEnum>
51bool contains(const AuthorizationSet& set, TypedEnumTag<KM_ENUM_REP, Tag, KeymasterEnum> tag,
52 KeymasterEnum val) {
53 int pos = -1;
54 while ((pos = set.find(tag, pos)) != -1)
55 if (set[pos].enumerated == val)
56 return true;
57 return false;
58}
59
60template <keymaster_tag_t Tag>
61bool contains(const AuthorizationSet& set, TypedTag<KM_INT, Tag> tag, uint32_t val) {
62 int pos = set.find(tag);
63 return pos != -1 && set[pos].integer == val;
64}
65
66template <keymaster_tag_t Tag>
67bool contains(const AuthorizationSet& set, TypedTag<KM_INT_REP, Tag> tag, uint32_t val) {
68 int pos = -1;
69 while ((pos = set.find(tag, pos)) != -1)
70 if (set[pos].integer == val)
71 return true;
72 return false;
73}
74
75template <keymaster_tag_t Tag>
76bool contains(const AuthorizationSet& set, TypedTag<KM_LONG, Tag> tag, uint64_t val) {
77 int pos = set.find(tag);
78 return pos != -1 && set[pos].long_integer == val;
79}
80
81template <keymaster_tag_t Tag>
82bool contains(const AuthorizationSet& set, TypedTag<KM_BYTES, Tag> tag, const std::string& val) {
83 int pos = set.find(tag);
84 return pos != -1 &&
85 std::string(reinterpret_cast<const char*>(set[pos].blob.data),
86 set[pos].blob.data_length) == val;
87}
88
Shawn Willdend0772312014-09-18 12:27:57 -060089template <keymaster_tag_t Tag>
90bool contains(const AuthorizationSet& set, TypedTag<KM_BIGNUM, Tag> tag, const std::string& val) {
91 int pos = set.find(tag);
92 return pos != -1 &&
93 std::string(reinterpret_cast<const char*>(set[pos].blob.data),
94 set[pos].blob.data_length) == val;
95}
96
Shawn Willden407d4122014-08-25 16:49:13 -060097inline bool contains(const AuthorizationSet& set, keymaster_tag_t tag) {
98 return set.find(tag) != -1;
99}
100
Shawn Willden2f3be362014-08-25 11:31:39 -0600101class StdoutLogger : public Logger {
102 public:
Shawn Willden538b0652014-12-30 23:23:40 -0700103 StdoutLogger() { set_instance(this); }
Shawn Willden83804622014-08-28 13:47:40 -0600104
Shawn Willden538b0652014-12-30 23:23:40 -0700105 int log_msg(LogLevel level, const char* fmt, va_list args) const {
106 int output_len = 0;
107 switch (level) {
108 case DEBUG_LVL:
109 output_len = printf("DEBUG: ");
110 break;
111 case INFO_LVL:
112 output_len = printf("INFO: ");
113 break;
114 case WARNING_LVL:
115 output_len = printf("WARNING: ");
116 break;
117 case ERROR_LVL:
118 output_len = printf("ERROR: ");
119 break;
120 case SEVERE_LVL:
121 output_len = printf("SEVERE: ");
122 break;
123 }
Shawn Willden83804622014-08-28 13:47:40 -0600124
Shawn Willden538b0652014-12-30 23:23:40 -0700125 output_len += vprintf(fmt, args);
126 output_len += printf("\n");
127 return output_len;
Shawn Willden83804622014-08-28 13:47:40 -0600128 }
Shawn Willden2f3be362014-08-25 11:31:39 -0600129};
130
131} // namespace test
Shawn Willden76364712014-08-11 17:48:04 -0600132} // namespace keymaster
133
134#endif // SYSTEM_KEYMASTER_GOOGLE_KEYMASTER_TEST_UTILS_H_