1 /*
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
24 #include <stdarg.h>
25
26 #include <ostream>
27
28 #include <keymaster/authorization_set.h>
29 #include <keymaster/keymaster_defs.h>
30 #include <keymaster/logger.h>
31
32 std::ostream& operator<<(std::ostream& os, const keymaster_key_param_t& param);
33 bool operator==(const keymaster_key_param_t& a, const keymaster_key_param_t& b);
34
35 namespace keymaster {
36
37 bool operator==(const AuthorizationSet& a, const AuthorizationSet& b);
38
39 std::ostream& operator<<(std::ostream& os, const AuthorizationSet& set);
40
41 namespace test {
42
43 template <keymaster_tag_t Tag, typename KeymasterEnum>
contains(const AuthorizationSet & set,TypedEnumTag<KM_ENUM,Tag,KeymasterEnum> tag,KeymasterEnum val)44 bool 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
50 template <keymaster_tag_t Tag, typename KeymasterEnum>
contains(const AuthorizationSet & set,TypedEnumTag<KM_ENUM_REP,Tag,KeymasterEnum> tag,KeymasterEnum val)51 bool 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
60 template <keymaster_tag_t Tag>
contains(const AuthorizationSet & set,TypedTag<KM_INT,Tag> tag,uint32_t val)61 bool 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
66 template <keymaster_tag_t Tag>
contains(const AuthorizationSet & set,TypedTag<KM_INT_REP,Tag> tag,uint32_t val)67 bool 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
75 template <keymaster_tag_t Tag>
contains(const AuthorizationSet & set,TypedTag<KM_LONG,Tag> tag,uint64_t val)76 bool 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
81 template <keymaster_tag_t Tag>
contains(const AuthorizationSet & set,TypedTag<KM_BYTES,Tag> tag,const std::string & val)82 bool 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
contains(const AuthorizationSet & set,keymaster_tag_t tag)89 inline bool contains(const AuthorizationSet& set, keymaster_tag_t tag) {
90 return set.find(tag) != -1;
91 }
92
93 class StdoutLogger : public Logger {
94 public:
debug(const char * fmt,...)95 int debug(const char* fmt, ...) const {
96 va_list args;
97 va_start(args, fmt);
98 int result = vprintf(fmt, args);
99 result += printf("\n");
100 va_end(args);
101 return result;
102 }
103
info(const char * fmt,...)104 int info(const char* fmt, ...) const {
105 va_list args;
106 va_start(args, fmt);
107 int result = vprintf(fmt, args);
108 result += printf("\n");
109 va_end(args);
110 return result;
111 }
112
error(const char * fmt,...)113 int error(const char* fmt, ...) const {
114 va_list args;
115 va_start(args, fmt);
116 int result = vfprintf(stderr, fmt, args);
117 result += printf("\n");
118 va_end(args);
119 return result;
120 }
121
severe(const char * fmt,...)122 int severe(const char* fmt, ...) const {
123 va_list args;
124 va_start(args, fmt);
125 int result = vfprintf(stderr, fmt, args);
126 result += printf("\n");
127 va_end(args);
128 return result;
129 }
130 };
131
132 } // namespace test
133 } // namespace keymaster
134
135 #endif // SYSTEM_KEYMASTER_GOOGLE_KEYMASTER_TEST_UTILS_H_
136