135 lines
4.3 KiB
C++
135 lines
4.3 KiB
C++
/*
|
|
* 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 <gtest/gtest.h>
|
|
|
|
#include <keymaster/authorization_set.h>
|
|
#include <keymaster/logger.h>
|
|
|
|
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 <keymaster_tag_t Tag, typename KeymasterEnum>
|
|
bool contains(const AuthorizationSet& set, TypedEnumTag<KM_ENUM, Tag, KeymasterEnum> tag,
|
|
KeymasterEnum val) {
|
|
int pos = set.find(tag);
|
|
return pos != -1 && static_cast<KeymasterEnum>(set[pos].enumerated) == val;
|
|
}
|
|
|
|
template <keymaster_tag_t Tag, typename KeymasterEnum>
|
|
bool contains(const AuthorizationSet& set, TypedEnumTag<KM_ENUM_REP, Tag, KeymasterEnum> tag,
|
|
KeymasterEnum val) {
|
|
int pos = -1;
|
|
while ((pos = set.find(tag, pos)) != -1)
|
|
if (static_cast<KeymasterEnum>(set[pos].enumerated) == val) return true;
|
|
return false;
|
|
}
|
|
|
|
template <keymaster_tag_t Tag>
|
|
bool contains(const AuthorizationSet& set, TypedTag<KM_UINT, Tag> tag, uint32_t val) {
|
|
int pos = set.find(tag);
|
|
return pos != -1 && set[pos].integer == val;
|
|
}
|
|
|
|
template <keymaster_tag_t Tag>
|
|
bool contains(const AuthorizationSet& set, TypedTag<KM_UINT_REP, Tag> tag, uint32_t val) {
|
|
int pos = -1;
|
|
while ((pos = set.find(tag, pos)) != -1)
|
|
if (set[pos].integer == val) return true;
|
|
return false;
|
|
}
|
|
|
|
template <keymaster_tag_t Tag>
|
|
bool contains(const AuthorizationSet& set, TypedTag<KM_ULONG, Tag> tag, uint64_t val) {
|
|
int pos = set.find(tag);
|
|
return pos != -1 && set[pos].long_integer == val;
|
|
}
|
|
|
|
template <keymaster_tag_t Tag>
|
|
bool contains(const AuthorizationSet& set, TypedTag<KM_BYTES, Tag> tag, const std::string& val) {
|
|
int pos = set.find(tag);
|
|
return pos != -1 && std::string(reinterpret_cast<const char*>(set[pos].blob.data),
|
|
set[pos].blob.data_length) == val;
|
|
}
|
|
|
|
template <keymaster_tag_t Tag>
|
|
bool contains(const AuthorizationSet& set, TypedTag<KM_BIGNUM, Tag> tag, const std::string& val) {
|
|
int pos = set.find(tag);
|
|
return pos != -1 && std::string(reinterpret_cast<const char*>(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<const char*>(data), length);
|
|
}
|
|
|
|
template <size_t N> std::string make_string(const uint8_t (&a)[N]) {
|
|
return make_string(a, N);
|
|
}
|
|
|
|
} // namespace test
|
|
} // namespace keymaster
|