blob: ee59c6e37ba7c47e517344f1fda8fb62c09da699 [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 Willden2c242002015-02-27 07:01:02 -070038bool operator!=(const AuthorizationSet& a, const AuthorizationSet& b);
Shawn Willden2f3be362014-08-25 11:31:39 -060039
Shawn Willden76364712014-08-11 17:48:04 -060040std::ostream& operator<<(std::ostream& os, const AuthorizationSet& set);
Shawn Willden2f3be362014-08-25 11:31:39 -060041
42namespace test {
43
Shawn Willden407d4122014-08-25 16:49:13 -060044template <keymaster_tag_t Tag, typename KeymasterEnum>
45bool contains(const AuthorizationSet& set, TypedEnumTag<KM_ENUM, Tag, KeymasterEnum> tag,
46 KeymasterEnum val) {
47 int pos = set.find(tag);
48 return pos != -1 && set[pos].enumerated == val;
49}
50
51template <keymaster_tag_t Tag, typename KeymasterEnum>
52bool contains(const AuthorizationSet& set, TypedEnumTag<KM_ENUM_REP, Tag, KeymasterEnum> tag,
53 KeymasterEnum val) {
54 int pos = -1;
55 while ((pos = set.find(tag, pos)) != -1)
56 if (set[pos].enumerated == val)
57 return true;
58 return false;
59}
60
61template <keymaster_tag_t Tag>
62bool contains(const AuthorizationSet& set, TypedTag<KM_INT, Tag> tag, uint32_t val) {
63 int pos = set.find(tag);
64 return pos != -1 && set[pos].integer == val;
65}
66
67template <keymaster_tag_t Tag>
68bool contains(const AuthorizationSet& set, TypedTag<KM_INT_REP, Tag> tag, uint32_t val) {
69 int pos = -1;
70 while ((pos = set.find(tag, pos)) != -1)
71 if (set[pos].integer == val)
72 return true;
73 return false;
74}
75
76template <keymaster_tag_t Tag>
77bool contains(const AuthorizationSet& set, TypedTag<KM_LONG, Tag> tag, uint64_t val) {
78 int pos = set.find(tag);
79 return pos != -1 && set[pos].long_integer == val;
80}
81
82template <keymaster_tag_t Tag>
83bool contains(const AuthorizationSet& set, TypedTag<KM_BYTES, Tag> tag, const std::string& val) {
84 int pos = set.find(tag);
85 return pos != -1 &&
86 std::string(reinterpret_cast<const char*>(set[pos].blob.data),
87 set[pos].blob.data_length) == val;
88}
89
Shawn Willdend0772312014-09-18 12:27:57 -060090template <keymaster_tag_t Tag>
91bool contains(const AuthorizationSet& set, TypedTag<KM_BIGNUM, Tag> tag, const std::string& val) {
92 int pos = set.find(tag);
93 return pos != -1 &&
94 std::string(reinterpret_cast<const char*>(set[pos].blob.data),
95 set[pos].blob.data_length) == val;
96}
97
Shawn Willden407d4122014-08-25 16:49:13 -060098inline bool contains(const AuthorizationSet& set, keymaster_tag_t tag) {
99 return set.find(tag) != -1;
100}
101
Shawn Willden2f3be362014-08-25 11:31:39 -0600102class StdoutLogger : public Logger {
103 public:
Shawn Willden538b0652014-12-30 23:23:40 -0700104 StdoutLogger() { set_instance(this); }
Shawn Willden83804622014-08-28 13:47:40 -0600105
Shawn Willden538b0652014-12-30 23:23:40 -0700106 int log_msg(LogLevel level, const char* fmt, va_list args) const {
107 int output_len = 0;
108 switch (level) {
109 case DEBUG_LVL:
110 output_len = printf("DEBUG: ");
111 break;
112 case INFO_LVL:
113 output_len = printf("INFO: ");
114 break;
115 case WARNING_LVL:
116 output_len = printf("WARNING: ");
117 break;
118 case ERROR_LVL:
119 output_len = printf("ERROR: ");
120 break;
121 case SEVERE_LVL:
122 output_len = printf("SEVERE: ");
123 break;
124 }
Shawn Willden83804622014-08-28 13:47:40 -0600125
Shawn Willden538b0652014-12-30 23:23:40 -0700126 output_len += vprintf(fmt, args);
127 output_len += printf("\n");
128 return output_len;
Shawn Willden83804622014-08-28 13:47:40 -0600129 }
Shawn Willden2f3be362014-08-25 11:31:39 -0600130};
131
132} // namespace test
Shawn Willden76364712014-08-11 17:48:04 -0600133} // namespace keymaster
134
135#endif // SYSTEM_KEYMASTER_GOOGLE_KEYMASTER_TEST_UTILS_H_