blob: 17786de403026591b51288d0654a31acd218d260 [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 Willden76364712014-08-11 17:48:04 -060028#include "authorization_set.h"
Shawn Willden2f3be362014-08-25 11:31:39 -060029#include "keymaster_defs.h"
30#include "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 -060043
44template <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
90inline bool contains(const AuthorizationSet& set, keymaster_tag_t tag) {
91 return set.find(tag) != -1;
92}
93
Shawn Willden2f3be362014-08-25 11:31:39 -060094class StdoutLogger : public Logger {
95 public:
96 int log(const char* fmt, ...) const {
97 va_list args;
98 va_start(args, fmt);
99 int result = vprintf(fmt, args);
100 va_end(args);
101 return result;
102 }
103};
104
105} // namespace test
Shawn Willden76364712014-08-11 17:48:04 -0600106} // namespace keymaster
107
108#endif // SYSTEM_KEYMASTER_GOOGLE_KEYMASTER_TEST_UTILS_H_