Andres Morales | 6c9fe69 | 2015-02-23 10:44:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 <gtest/gtest.h> |
| 18 | #include <string.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <stdio.h> |
| 21 | |
| 22 | #include <keyguard/keyguard_messages.h> |
| 23 | |
| 24 | using ::keyguard::SizedBuffer; |
| 25 | using ::testing::Test; |
| 26 | using ::keyguard::EnrollRequest; |
| 27 | using ::keyguard::EnrollResponse; |
| 28 | using ::keyguard::VerifyRequest; |
| 29 | using ::keyguard::VerifyResponse; |
| 30 | using std::cout; |
| 31 | using std::endl; |
| 32 | |
Andres Morales | ac80818 | 2015-02-26 14:11:04 -0800 | [diff] [blame^] | 33 | static const uint32_t USER_ID = 3857; |
| 34 | |
Andres Morales | 6c9fe69 | 2015-02-23 10:44:41 -0800 | [diff] [blame] | 35 | static SizedBuffer *make_buffer(size_t size) { |
| 36 | SizedBuffer *result = new SizedBuffer; |
| 37 | result->length = size; |
| 38 | uint8_t *buffer = new uint8_t[size]; |
| 39 | srand(size); |
| 40 | |
| 41 | for (size_t i = 0; i < size; i++) { |
| 42 | buffer[i] = rand(); |
| 43 | } |
| 44 | |
| 45 | result->buffer.reset(buffer); |
| 46 | return result; |
| 47 | } |
| 48 | |
| 49 | TEST(RoundTripTest, EnrollRequest) { |
| 50 | const size_t password_size = 512; |
| 51 | SizedBuffer *provided_password = make_buffer(password_size); |
| 52 | const SizedBuffer *deserialized_password; |
| 53 | // create request, serialize, deserialize, and validate |
Andres Morales | ac80818 | 2015-02-26 14:11:04 -0800 | [diff] [blame^] | 54 | EnrollRequest req(USER_ID, provided_password); |
Andres Morales | 6c9fe69 | 2015-02-23 10:44:41 -0800 | [diff] [blame] | 55 | uint8_t *serialized_req = req.Serialize(); |
| 56 | EnrollRequest deserialized_req; |
| 57 | deserialized_req.Deserialize(serialized_req, serialized_req + req.GetSerializedSize()); |
| 58 | delete[] serialized_req; |
| 59 | |
| 60 | ASSERT_EQ(keyguard::keyguard_error_t::KG_ERROR_OK, |
| 61 | deserialized_req.GetError()); |
| 62 | |
| 63 | deserialized_password = deserialized_req.GetProvidedPassword(); |
Andres Morales | ac80818 | 2015-02-26 14:11:04 -0800 | [diff] [blame^] | 64 | ASSERT_EQ(USER_ID, deserialized_req.GetUserId()); |
Andres Morales | 6c9fe69 | 2015-02-23 10:44:41 -0800 | [diff] [blame] | 65 | ASSERT_EQ((uint32_t) password_size, deserialized_password->length); |
| 66 | ASSERT_EQ(0, memcmp(req.GetProvidedPassword()->buffer.get(), deserialized_password->buffer.get(), password_size)); |
| 67 | } |
| 68 | |
| 69 | TEST(RoundTripTest, EnrollResponse) { |
| 70 | const size_t password_size = 512; |
| 71 | SizedBuffer *enrolled_password = make_buffer(password_size); |
| 72 | const SizedBuffer *deserialized_password; |
| 73 | // create request, serialize, deserialize, and validate |
Andres Morales | ac80818 | 2015-02-26 14:11:04 -0800 | [diff] [blame^] | 74 | EnrollResponse req(USER_ID, enrolled_password); |
Andres Morales | 6c9fe69 | 2015-02-23 10:44:41 -0800 | [diff] [blame] | 75 | uint8_t *serialized_req = req.Serialize(); |
| 76 | EnrollResponse deserialized_req; |
| 77 | deserialized_req.Deserialize(serialized_req, serialized_req + req.GetSerializedSize()); |
| 78 | delete[] serialized_req; |
| 79 | |
| 80 | ASSERT_EQ(keyguard::keyguard_error_t::KG_ERROR_OK, |
| 81 | deserialized_req.GetError()); |
| 82 | |
| 83 | deserialized_password = deserialized_req.GetEnrolledPasswordHandle(); |
Andres Morales | ac80818 | 2015-02-26 14:11:04 -0800 | [diff] [blame^] | 84 | ASSERT_EQ(USER_ID, deserialized_req.GetUserId()); |
Andres Morales | 6c9fe69 | 2015-02-23 10:44:41 -0800 | [diff] [blame] | 85 | ASSERT_EQ((uint32_t) password_size, deserialized_password->length); |
| 86 | ASSERT_EQ(0, memcmp(req.GetEnrolledPasswordHandle()->buffer.get(), deserialized_password->buffer.get(), password_size)); |
| 87 | } |
| 88 | |
| 89 | TEST(RoundTripTest, VerifyRequest) { |
Andres Morales | ac80818 | 2015-02-26 14:11:04 -0800 | [diff] [blame^] | 90 | const size_t password_size = 512; |
Andres Morales | 6c9fe69 | 2015-02-23 10:44:41 -0800 | [diff] [blame] | 91 | SizedBuffer *provided_password = make_buffer(password_size), |
| 92 | *password_handle = make_buffer(password_size); |
| 93 | const SizedBuffer *deserialized_password; |
| 94 | // create request, serialize, deserialize, and validate |
Andres Morales | ac80818 | 2015-02-26 14:11:04 -0800 | [diff] [blame^] | 95 | VerifyRequest req(USER_ID, password_handle, provided_password); |
Andres Morales | 6c9fe69 | 2015-02-23 10:44:41 -0800 | [diff] [blame] | 96 | uint8_t *serialized_req = req.Serialize(); |
| 97 | VerifyRequest deserialized_req; |
| 98 | deserialized_req.Deserialize(serialized_req, serialized_req + req.GetSerializedSize()); |
| 99 | |
| 100 | ASSERT_EQ(keyguard::keyguard_error_t::KG_ERROR_OK, |
| 101 | deserialized_req.GetError()); |
| 102 | |
Andres Morales | ac80818 | 2015-02-26 14:11:04 -0800 | [diff] [blame^] | 103 | ASSERT_EQ(USER_ID, deserialized_req.GetUserId()); |
Andres Morales | 6c9fe69 | 2015-02-23 10:44:41 -0800 | [diff] [blame] | 104 | deserialized_password = deserialized_req.GetProvidedPassword(); |
| 105 | ASSERT_EQ((uint32_t) password_size, deserialized_password->length); |
| 106 | ASSERT_EQ(0, memcmp(req.GetProvidedPassword()->buffer.get(), deserialized_password->buffer.get(), password_size)); |
| 107 | |
| 108 | deserialized_password = deserialized_req.GetPasswordHandle(); |
| 109 | ASSERT_EQ((uint32_t) password_size, deserialized_password->length); |
| 110 | ASSERT_EQ(0, memcmp(req.GetPasswordHandle()->buffer.get(), deserialized_password->buffer.get(), password_size)); |
| 111 | } |
| 112 | |
| 113 | TEST(RoundTripTest, VerifyResponse) { |
| 114 | const size_t password_size = 512; |
| 115 | SizedBuffer *verification_token = make_buffer(password_size); |
| 116 | const SizedBuffer *deserialized_password; |
| 117 | // create request, serialize, deserialize, and validate |
Andres Morales | ac80818 | 2015-02-26 14:11:04 -0800 | [diff] [blame^] | 118 | VerifyResponse req(USER_ID, verification_token); |
Andres Morales | 6c9fe69 | 2015-02-23 10:44:41 -0800 | [diff] [blame] | 119 | uint8_t *serialized_req = req.Serialize(); |
| 120 | VerifyResponse deserialized_req; |
| 121 | deserialized_req.Deserialize(serialized_req, serialized_req + req.GetSerializedSize()); |
| 122 | delete[] serialized_req; |
| 123 | |
| 124 | ASSERT_EQ(keyguard::keyguard_error_t::KG_ERROR_OK, |
| 125 | deserialized_req.GetError()); |
| 126 | |
Andres Morales | ac80818 | 2015-02-26 14:11:04 -0800 | [diff] [blame^] | 127 | ASSERT_EQ(USER_ID, deserialized_req.GetUserId()); |
Andres Morales | 6c9fe69 | 2015-02-23 10:44:41 -0800 | [diff] [blame] | 128 | deserialized_password = deserialized_req.GetVerificationToken(); |
| 129 | ASSERT_EQ((uint32_t) password_size, deserialized_password->length); |
| 130 | ASSERT_EQ(0, memcmp(req.GetVerificationToken()->buffer.get(), deserialized_password->buffer.get(), password_size)); |
| 131 | } |
| 132 | |
| 133 | uint8_t msgbuf[] = { |
| 134 | 220, 88, 183, 255, 71, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 135 | 0, 173, 0, 0, 0, 228, 174, 98, 187, 191, 135, 253, 200, 51, 230, 114, 247, 151, 109, |
| 136 | 237, 79, 87, 32, 94, 5, 204, 46, 154, 30, 91, 6, 103, 148, 254, 129, 65, 171, 228, |
| 137 | 167, 224, 163, 9, 15, 206, 90, 58, 11, 205, 55, 211, 33, 87, 178, 149, 91, 28, 236, |
| 138 | 218, 112, 231, 34, 82, 82, 134, 103, 137, 115, 27, 156, 102, 159, 220, 226, 89, 42, 25, |
| 139 | 37, 9, 84, 239, 76, 161, 198, 72, 167, 163, 39, 91, 148, 191, 17, 191, 87, 169, 179, |
| 140 | 136, 10, 194, 154, 4, 40, 107, 109, 61, 161, 20, 176, 247, 13, 214, 106, 229, 45, 17, |
| 141 | 5, 60, 189, 64, 39, 166, 208, 14, 57, 25, 140, 148, 25, 177, 246, 189, 43, 181, 88, |
| 142 | 204, 29, 126, 224, 100, 143, 93, 60, 57, 249, 55, 0, 87, 83, 227, 224, 166, 59, 214, |
| 143 | 81, 144, 129, 58, 6, 57, 46, 254, 232, 41, 220, 209, 230, 167, 138, 158, 94, 180, 125, |
| 144 | 247, 26, 162, 116, 238, 202, 187, 100, 65, 13, 180, 44, 245, 159, 83, 161, 176, 58, 72, |
| 145 | 236, 109, 105, 160, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 146 | 0, 11, 0, 0, 0, 98, 0, 0, 0, 1, 0, 0, 32, 2, 0, 0, 0, 1, 0, |
| 147 | 0, 32, 3, 0, 0, 0, 2, 0, 0, 16, 1, 0, 0, 0, 3, 0, 0, 48, 0, |
| 148 | 1, 0, 0, 200, 0, 0, 80, 3, 0, 0, 0, 0, 0, 0, 0, 244, 1, 0, 112, |
| 149 | 1, 246, 1, 0, 112, 1, 189, 2, 0, 96, 144, 178, 236, 250, 255, 255, 255, 255, 145, |
| 150 | 1, 0, 96, 144, 226, 33, 60, 222, 2, 0, 0, 189, 2, 0, 96, 0, 0, 0, 0, |
| 151 | 0, 0, 0, 0, 190, 2, 0, 16, 1, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, |
| 152 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 110, 0, 0, 0, 0, 0, 0, 0, 11, 0, |
| 153 | 0, 0, 98, 0, 0, 0, 1, 0, 0, 32, 2, 0, 0, 0, 1, 0, 0, 32, 3, |
| 154 | 0, 0, 0, 2, 0, 0, 16, 1, 0, 0, 0, 3, 0, 0, 48, 0, 1, 0, 0, |
| 155 | 200, 0, 0, 80, 3, 0, 0, 0, 0, 0, 0, 0, 244, 1, 0, 112, 1, 246, 1, |
| 156 | 0, 112, 1, 189, 2, 0, 96, 144, 178, 236, 250, 255, 255, 255, 255, 145, 1, 0, 96, |
| 157 | 144, 226, 33, 60, 222, 2, 0, 0, 189, 2, 0, 96, 0, 0, 0, 0, 0, 0, 0, |
| 158 | 0, 190, 2, 0, 16, 1, 0, 0, 0, |
| 159 | }; |
| 160 | |
| 161 | |
| 162 | /* |
| 163 | * These tests don't have any assertions or expectations. They just try to parse garbage, to see if |
| 164 | * the result will be a crash. This is especially informative when run under Valgrind memcheck. |
| 165 | */ |
| 166 | |
| 167 | template <typename Message> void parse_garbage() { |
| 168 | Message msg; |
| 169 | size_t array_length = sizeof(msgbuf) / sizeof(msgbuf[0]); |
| 170 | const uint8_t* end = msgbuf + array_length; |
| 171 | for (size_t i = 0; i < array_length; ++i) { |
| 172 | const uint8_t* begin = msgbuf + i; |
| 173 | const uint8_t* p = begin; |
| 174 | msg.Deserialize(p, end); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | #define GARBAGE_TEST(Message) \ |
| 179 | TEST(GarbageTest, Message) { parse_garbage<Message>(); } |
| 180 | |
| 181 | GARBAGE_TEST(VerifyRequest); |
| 182 | GARBAGE_TEST(VerifyResponse); |
| 183 | GARBAGE_TEST(EnrollRequest); |
| 184 | GARBAGE_TEST(EnrollResponse); |