1 : // Copyright 2015 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #include "syzygy/kasko/client.h"
16 :
17 : #include <Windows.h> // NOLINT
18 : #include <Rpc.h>
19 :
20 : #include <vector>
21 :
22 : #include "base/bind.h"
23 : #include "base/callback.h"
24 : #include "base/callback_helpers.h"
25 : #include "base/process/process_handle.h"
26 : #include "base/strings/string16.h"
27 : #include "base/strings/string_number_conversions.h"
28 : #include "gtest/gtest.h"
29 : #include "syzygy/kasko/service_bridge.h"
30 : #include "syzygy/kasko/testing/mock_service.h"
31 :
32 : namespace kasko {
33 :
34 : namespace {
35 :
36 : const base::char16* const kValidRpcProtocol = L"ncalrpc";
37 : const base::char16* const kTestRpcEndpointPrefix = L"syzygy-kasko-test-svc";
38 :
39 E : base::string16 GetTestEndpoint() {
40 E : return kTestRpcEndpointPrefix + base::UintToString16(::GetCurrentProcessId());
41 E : }
42 :
43 : } // namespace
44 :
45 E : TEST(ClientTest, BasicTest) {
46 E : std::vector<testing::MockService::CallRecord> call_log;
47 :
48 E : base::string16 protocol = kValidRpcProtocol;
49 E : base::string16 endpoint = GetTestEndpoint();
50 : ServiceBridge instance(
51 : protocol, endpoint,
52 E : scoped_ptr<Service>(new testing::MockService(&call_log)));
53 E : ASSERT_TRUE(instance.Run());
54 :
55 : base::ScopedClosureRunner stop_service_bridge(
56 E : base::Bind(&ServiceBridge::Stop, base::Unretained(&instance)));
57 :
58 E : std::string protobuf = "hello world";
59 :
60 E : base::char16* keys[] = {L"foo", L"hello", nullptr};
61 E : base::char16* values[] = {L"bar", L"world", nullptr};
62 :
63 : // Small dump with crash keys.
64 E : Client(endpoint).SendReport(nullptr, SMALL_DUMP_TYPE, protobuf.data(),
65 : protobuf.length(), keys, values);
66 :
67 : // Larger dump without crash keys.
68 E : Client(endpoint).SendReport(nullptr, LARGER_DUMP_TYPE, protobuf.data(),
69 : protobuf.length(), nullptr, nullptr);
70 :
71 E : ASSERT_EQ(2u, call_log.size());
72 E : ASSERT_EQ(::GetCurrentProcessId(), call_log[0].client_process_id);
73 E : ASSERT_EQ(protobuf, call_log[0].protobuf);
74 E : ASSERT_EQ(2u, call_log[0].crash_keys.size());
75 E : auto entry = call_log[0].crash_keys.find(L"foo");
76 E : ASSERT_NE(call_log[0].crash_keys.end(), entry);
77 E : ASSERT_EQ(L"bar", entry->second);
78 E : entry = call_log[0].crash_keys.find(L"hello");
79 E : ASSERT_NE(call_log[0].crash_keys.end(), entry);
80 E : ASSERT_EQ(L"world", entry->second);
81 E : ASSERT_EQ(SMALL_DUMP_TYPE, call_log[0].minidump_type);
82 :
83 E : ASSERT_EQ(::GetCurrentProcessId(), call_log[1].client_process_id);
84 E : ASSERT_EQ(protobuf, call_log[1].protobuf);
85 E : ASSERT_EQ(0u, call_log[1].crash_keys.size());
86 E : ASSERT_EQ(LARGER_DUMP_TYPE, call_log[1].minidump_type);
87 E : }
88 :
89 : } // namespace kasko
|