blob: d6d264a0b9016478cbd72ecd9ddeb7b3b7ae9e35 [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 Willden98d9b922014-08-26 08:14:10 -060028#include <keymaster/authorization_set.h>
29#include <keymaster/keymaster_defs.h>
30#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 Willden52c8b5d2014-09-11 14:51:54 -0600103 int debug(const char* fmt, ...) const {
Shawn Willden2f3be362014-08-25 11:31:39 -0600104 va_list args;
105 va_start(args, fmt);
106 int result = vprintf(fmt, args);
Shawn Willden52c8b5d2014-09-11 14:51:54 -0600107 result += printf("\n");
Shawn Willden2f3be362014-08-25 11:31:39 -0600108 va_end(args);
109 return result;
110 }
Shawn Willden83804622014-08-28 13:47:40 -0600111
112 int info(const char* fmt, ...) const {
113 va_list args;
114 va_start(args, fmt);
115 int result = vprintf(fmt, args);
Shawn Willden52c8b5d2014-09-11 14:51:54 -0600116 result += printf("\n");
Shawn Willden83804622014-08-28 13:47:40 -0600117 va_end(args);
118 return result;
119 }
120
121 int error(const char* fmt, ...) const {
122 va_list args;
123 va_start(args, fmt);
124 int result = vfprintf(stderr, fmt, args);
Shawn Willden52c8b5d2014-09-11 14:51:54 -0600125 result += printf("\n");
Shawn Willden83804622014-08-28 13:47:40 -0600126 va_end(args);
127 return result;
128 }
129
130 int severe(const char* fmt, ...) const {
131 va_list args;
132 va_start(args, fmt);
133 int result = vfprintf(stderr, fmt, args);
Shawn Willden52c8b5d2014-09-11 14:51:54 -0600134 result += printf("\n");
Shawn Willden83804622014-08-28 13:47:40 -0600135 va_end(args);
136 return result;
137 }
Shawn Willden2f3be362014-08-25 11:31:39 -0600138};
139
140} // namespace test
Shawn Willden76364712014-08-11 17:48:04 -0600141} // namespace keymaster
142
143#endif // SYSTEM_KEYMASTER_GOOGLE_KEYMASTER_TEST_UTILS_H_