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 #pragma once
18 
19 /*
20  * Utilities used to help with testing.  Not used in production code.
21  */
22 
23 #include <gtest/gtest.h>
24 
25 #include <keymaster/authorization_set.h>
26 #include <keymaster/logger.h>
27 
28 std::ostream& operator<<(std::ostream& os, const keymaster_key_param_t& param);
29 bool operator==(const keymaster_key_param_t& a, const keymaster_key_param_t& b);
30 std::string hex2str(std::string);
31 
32 namespace keymaster {
33 
34 bool operator==(const AuthorizationSet& a, const AuthorizationSet& b);
35 bool operator!=(const AuthorizationSet& a, const AuthorizationSet& b);
36 
37 std::ostream& operator<<(std::ostream& os, const AuthorizationSet& set);
38 
39 namespace test {
40 
41 template <keymaster_tag_t Tag, typename KeymasterEnum>
contains(const AuthorizationSet & set,TypedEnumTag<KM_ENUM,Tag,KeymasterEnum> tag,KeymasterEnum val)42 bool contains(const AuthorizationSet& set, TypedEnumTag<KM_ENUM, Tag, KeymasterEnum> tag,
43               KeymasterEnum val) {
44     int pos = set.find(tag);
45     return pos != -1 && static_cast<KeymasterEnum>(set[pos].enumerated) == val;
46 }
47 
48 template <keymaster_tag_t Tag, typename KeymasterEnum>
contains(const AuthorizationSet & set,TypedEnumTag<KM_ENUM_REP,Tag,KeymasterEnum> tag,KeymasterEnum val)49 bool contains(const AuthorizationSet& set, TypedEnumTag<KM_ENUM_REP, Tag, KeymasterEnum> tag,
50               KeymasterEnum val) {
51     int pos = -1;
52     while ((pos = set.find(tag, pos)) != -1)
53         if (static_cast<KeymasterEnum>(set[pos].enumerated) == val) return true;
54     return false;
55 }
56 
57 template <keymaster_tag_t Tag>
contains(const AuthorizationSet & set,TypedTag<KM_UINT,Tag> tag,uint32_t val)58 bool contains(const AuthorizationSet& set, TypedTag<KM_UINT, Tag> tag, uint32_t val) {
59     int pos = set.find(tag);
60     return pos != -1 && set[pos].integer == val;
61 }
62 
63 template <keymaster_tag_t Tag>
contains(const AuthorizationSet & set,TypedTag<KM_UINT_REP,Tag> tag,uint32_t val)64 bool contains(const AuthorizationSet& set, TypedTag<KM_UINT_REP, Tag> tag, uint32_t val) {
65     int pos = -1;
66     while ((pos = set.find(tag, pos)) != -1)
67         if (set[pos].integer == val) return true;
68     return false;
69 }
70 
71 template <keymaster_tag_t Tag>
contains(const AuthorizationSet & set,TypedTag<KM_ULONG,Tag> tag,uint64_t val)72 bool contains(const AuthorizationSet& set, TypedTag<KM_ULONG, Tag> tag, uint64_t val) {
73     int pos = set.find(tag);
74     return pos != -1 && set[pos].long_integer == val;
75 }
76 
77 template <keymaster_tag_t Tag>
contains(const AuthorizationSet & set,TypedTag<KM_BYTES,Tag> tag,const std::string & val)78 bool contains(const AuthorizationSet& set, TypedTag<KM_BYTES, Tag> tag, const std::string& val) {
79     int pos = set.find(tag);
80     return pos != -1 && std::string(reinterpret_cast<const char*>(set[pos].blob.data),
81                                     set[pos].blob.data_length) == val;
82 }
83 
84 template <keymaster_tag_t Tag>
contains(const AuthorizationSet & set,TypedTag<KM_BIGNUM,Tag> tag,const std::string & val)85 bool contains(const AuthorizationSet& set, TypedTag<KM_BIGNUM, Tag> tag, const std::string& val) {
86     int pos = set.find(tag);
87     return pos != -1 && std::string(reinterpret_cast<const char*>(set[pos].blob.data),
88                                     set[pos].blob.data_length) == val;
89 }
90 
contains(const AuthorizationSet & set,keymaster_tag_t tag)91 inline bool contains(const AuthorizationSet& set, keymaster_tag_t tag) {
92     return set.find(tag) != -1;
93 }
94 
95 class StdoutLogger : public Logger {
96   public:
StdoutLogger()97     StdoutLogger() { set_instance(this); }
98 
log_msg(LogLevel level,const char * fmt,va_list args)99     int log_msg(LogLevel level, const char* fmt, va_list args) const {
100         int output_len = 0;
101         switch (level) {
102         case DEBUG_LVL:
103             output_len = printf("DEBUG: ");
104             break;
105         case INFO_LVL:
106             output_len = printf("INFO: ");
107             break;
108         case WARNING_LVL:
109             output_len = printf("WARNING: ");
110             break;
111         case ERROR_LVL:
112             output_len = printf("ERROR: ");
113             break;
114         case SEVERE_LVL:
115             output_len = printf("SEVERE: ");
116             break;
117         }
118 
119         output_len += vprintf(fmt, args);
120         output_len += printf("\n");
121         return output_len;
122     }
123 };
124 
make_string(const uint8_t * data,size_t length)125 inline std::string make_string(const uint8_t* data, size_t length) {
126     return std::string(reinterpret_cast<const char*>(data), length);
127 }
128 
make_string(const uint8_t (& a)[N])129 template <size_t N> std::string make_string(const uint8_t (&a)[N]) {
130     return make_string(a, N);
131 }
132 
133 }  // namespace test
134 }  // namespace keymaster
135