• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "google_keymaster_test_utils.h"
18 
operator <<(std::ostream & os,const keymaster_key_param_t & param)19 std::ostream& operator<<(std::ostream& os, const keymaster_key_param_t& param) {
20     os << "Tag: " << keymaster_tag_mask_type(param.tag);
21     switch (keymaster_tag_get_type(param.tag)) {
22     case KM_INVALID:
23         os << " Invalid";
24         break;
25     case KM_INT_REP:
26         os << " (Rep)";
27     /* Falls through */
28     case KM_INT:
29         os << " Int: " << param.integer;
30         break;
31     case KM_ENUM_REP:
32         os << " (Rep)";
33     /* Falls through */
34     case KM_ENUM:
35         os << " Enum: " << param.enumerated;
36         break;
37     case KM_LONG:
38         os << " Long: " << param.long_integer;
39         break;
40     case KM_DATE:
41         os << " Date: " << param.date_time;
42         break;
43     case KM_BOOL:
44         os << " Bool: " << param.boolean;
45         break;
46     case KM_BIGNUM:
47         os << " Bignum: ";
48         break;
49     case KM_BYTES:
50         os << " Bytes: ";
51         break;
52     }
53     return os;
54 }
55 
operator ==(const keymaster_key_param_t & a,const keymaster_key_param_t & b)56 bool operator==(const keymaster_key_param_t& a, const keymaster_key_param_t& b) {
57     if (a.tag != b.tag) {
58         return false;
59     }
60 
61     switch (keymaster_tag_get_type(a.tag)) {
62     default:
63         return false;
64     case KM_INVALID:
65         return true;
66     case KM_INT_REP:
67     case KM_INT:
68         return a.integer == b.integer;
69     case KM_ENUM_REP:
70     case KM_ENUM:
71         return a.enumerated == b.enumerated;
72     case KM_LONG:
73         return a.long_integer == b.long_integer;
74     case KM_DATE:
75         return a.date_time == b.date_time;
76     case KM_BOOL:
77         return a.boolean == b.boolean;
78     case KM_BIGNUM:
79     case KM_BYTES:
80         if ((a.blob.data == NULL || b.blob.data == NULL) && a.blob.data != b.blob.data)
81             return false;
82         return a.blob.data_length == b.blob.data_length &&
83                (memcmp(a.blob.data, b.blob.data, a.blob.data_length) == 0);
84     }
85 }
86 
87 namespace keymaster {
88 
operator ==(const AuthorizationSet & a,const AuthorizationSet & b)89 bool operator==(const AuthorizationSet& a, const AuthorizationSet& b) {
90     if (a.size() != b.size())
91         return false;
92 
93     for (size_t i = 0; i < a.size(); ++i)
94         if (!(a[i] == b[i]))
95             return false;
96     return true;
97 }
98 
operator <<(std::ostream & os,const AuthorizationSet & set)99 std::ostream& operator<<(std::ostream& os, const AuthorizationSet& set) {
100     if (set.size() == 0)
101         os << "(Empty)" << std::endl;
102     for (size_t i = 0; i < set.size(); ++i) {
103         os << set[i] << std::endl;
104     }
105     return os;
106 }
107 
108 }  // namespace keymaster
109