/* * Copyright 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once /* * Utilities used to help with testing. Not used in production code. */ #include #include #include std::ostream& operator<<(std::ostream& os, const keymaster_key_param_t& param); bool operator==(const keymaster_key_param_t& a, const keymaster_key_param_t& b); std::string hex2str(std::string); namespace keymaster { bool operator==(const AuthorizationSet& a, const AuthorizationSet& b); bool operator!=(const AuthorizationSet& a, const AuthorizationSet& b); std::ostream& operator<<(std::ostream& os, const AuthorizationSet& set); namespace test { template bool contains(const AuthorizationSet& set, TypedEnumTag tag, KeymasterEnum val) { int pos = set.find(tag); return pos != -1 && static_cast(set[pos].enumerated) == val; } template bool contains(const AuthorizationSet& set, TypedEnumTag tag, KeymasterEnum val) { int pos = -1; while ((pos = set.find(tag, pos)) != -1) if (static_cast(set[pos].enumerated) == val) return true; return false; } template bool contains(const AuthorizationSet& set, TypedTag tag, uint32_t val) { int pos = set.find(tag); return pos != -1 && set[pos].integer == val; } template bool contains(const AuthorizationSet& set, TypedTag tag, uint32_t val) { int pos = -1; while ((pos = set.find(tag, pos)) != -1) if (set[pos].integer == val) return true; return false; } template bool contains(const AuthorizationSet& set, TypedTag tag, uint64_t val) { int pos = set.find(tag); return pos != -1 && set[pos].long_integer == val; } template bool contains(const AuthorizationSet& set, TypedTag tag, const std::string& val) { int pos = set.find(tag); return pos != -1 && std::string(reinterpret_cast(set[pos].blob.data), set[pos].blob.data_length) == val; } template bool contains(const AuthorizationSet& set, TypedTag tag, const std::string& val) { int pos = set.find(tag); return pos != -1 && std::string(reinterpret_cast(set[pos].blob.data), set[pos].blob.data_length) == val; } inline bool contains(const AuthorizationSet& set, keymaster_tag_t tag) { return set.find(tag) != -1; } class StdoutLogger : public Logger { public: StdoutLogger() { set_instance(this); } int log_msg(LogLevel level, const char* fmt, va_list args) const { int output_len = 0; switch (level) { case DEBUG_LVL: output_len = printf("DEBUG: "); break; case INFO_LVL: output_len = printf("INFO: "); break; case WARNING_LVL: output_len = printf("WARNING: "); break; case ERROR_LVL: output_len = printf("ERROR: "); break; case SEVERE_LVL: output_len = printf("SEVERE: "); break; } output_len += vprintf(fmt, args); output_len += printf("\n"); return output_len; } }; inline std::string make_string(const uint8_t* data, size_t length) { return std::string(reinterpret_cast(data), length); } template std::string make_string(const uint8_t (&a)[N]) { return make_string(a, N); } } // namespace test } // namespace keymaster