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 : #ifndef SYZYGY_KASKO_TESTING_UPLOAD_OBSERVER_H_
16 : #define SYZYGY_KASKO_TESTING_UPLOAD_OBSERVER_H_
17 :
18 : #include <map>
19 : #include <string>
20 :
21 : #include "base/macros.h"
22 : #include "base/files/file_path.h"
23 : #include "base/synchronization/waitable_event.h"
24 : #include "base/threading/simple_thread.h"
25 :
26 m : namespace kasko {
27 m : namespace testing {
28 :
29 : // Observes an upload directory and a permanent failure directory to allow tests
30 : // to observe when a crash report has either been successfully uploaded or has
31 : // permanently failed. Requires the observed directories to be empty before
32 : // beginning observation.
33 m : class UploadObserver {
34 m : public:
35 : // Instantiates an instance to watch the supplied directories. The instance
36 : // must be created before upload attempts begin. The instance is actively
37 : // observing by the time the constructor returns.
38 : // @param upload_directory The 'incoming' directory of the TestServer instance
39 : // that is listening for crash reports.
40 : // @param permanent_failure_directory The permanent failure directory
41 : // parameter of the reporter under test.
42 m : UploadObserver(const base::FilePath& upload_directory,
43 m : const base::FilePath& permanent_failure_directory);
44 m : ~UploadObserver();
45 :
46 : // Blocks until a crash report appears in either the upload or permanent
47 : // failure directory. Returns immediately if the report already appeared since
48 : // the constructor invocation.
49 : // @param minidump_path Receives the path to the minidump from the uploaded
50 : // crash report.
51 : // @param crash_keys Receives the crash keys from the uploaded crash
52 : // report.
53 : // @param upload_success Will be set to true if the report appeared in the
54 : // upload directory and false if it appeared in the permanent failure
55 : // directory.
56 m : void WaitForUpload(base::FilePath* minidump_path,
57 m : std::map<std::string, std::string>* crash_keys,
58 m : bool* upload_success);
59 :
60 m : private:
61 m : class UploadObserverThread : public base::SimpleThread {
62 m : public:
63 m : UploadObserverThread(const base::FilePath& upload_directory,
64 m : const base::FilePath& permanent_failure_directory);
65 m : ~UploadObserverThread() override;
66 :
67 m : const base::FilePath& minidump_path() { return minidump_path_; }
68 m : const std::map<std::string, std::string>& crash_keys() {
69 m : return crash_keys_;
70 m : }
71 m : bool upload_success() { return upload_success_; }
72 :
73 m : void WaitUntilReady();
74 :
75 : // base::SimpleThread implementation.
76 m : void Run() override;
77 :
78 m : private:
79 m : void WatchForPermanentFailure(const base::FilePath& path, bool error);
80 m : void WatchForUpload(const base::FilePath& path, bool error);
81 :
82 m : base::WaitableEvent ready_;
83 :
84 m : base::FilePath upload_directory_;
85 m : base::FilePath permanent_failure_directory_;
86 :
87 m : base::FilePath minidump_path_;
88 m : std::map<std::string, std::string> crash_keys_;
89 m : bool upload_success_ = false;
90 :
91 m : DISALLOW_COPY_AND_ASSIGN(UploadObserverThread);
92 m : };
93 :
94 m : UploadObserverThread thread_;
95 :
96 m : DISALLOW_COPY_AND_ASSIGN(UploadObserver);
97 m : };
98 :
99 m : } // namespace testing
100 m : } // namespace kasko
101 :
102 : #endif // SYZYGY_KASKO_TESTING_UPLOAD_OBSERVER_H_
|