1 : // Copyright 2014 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/crash_keys_serialization.h"
16 :
17 : #include <map>
18 : #include <memory>
19 :
20 : #include "base/values.h"
21 : #include "base/files/file_path.h"
22 : #include "base/files/file_util.h"
23 : #include "base/files/scoped_temp_dir.h"
24 : #include "base/json/json_reader.h"
25 : #include "base/json/json_writer.h"
26 : #include "base/strings/string16.h"
27 : #include "gtest/gtest.h"
28 :
29 m : namespace kasko {
30 :
31 m : TEST(CrashKeysSerializationTest, BasicTest) {
32 m : base::ScopedTempDir temp_dir;
33 m : ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
34 m : base::FilePath temp_file = temp_dir.path().Append(L"test.dat");
35 m : std::map<base::string16, base::string16> crash_keys;
36 m : crash_keys[L"name"] = L"value";
37 m : ASSERT_TRUE(WriteCrashKeysToFile(temp_file, crash_keys));
38 m : std::map<base::string16, base::string16> crash_keys_from_disk;
39 m : ASSERT_TRUE(ReadCrashKeysFromFile(temp_file, &crash_keys_from_disk));
40 m : ASSERT_EQ(crash_keys, crash_keys_from_disk);
41 m : }
42 :
43 m : TEST(CrashKeysSerializationTest, MissingFile) {
44 m : base::ScopedTempDir temp_dir;
45 m : ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
46 m : std::map<base::string16, base::string16> crash_keys_from_disk;
47 m : ASSERT_FALSE(ReadCrashKeysFromFile(
48 m : temp_dir.path().Append(L"some_other_path.dat"), &crash_keys_from_disk));
49 m : }
50 :
51 m : TEST(CrashKeysSerializationTest, InvalidFile) {
52 m : base::ScopedTempDir temp_dir;
53 m : ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
54 m : base::FilePath temp_file = temp_dir.path().Append(L"test.dat");
55 m : std::string invalid_file_contents =
56 m : "These aren't the bytes you're looking for.";
57 m : ASSERT_TRUE(base::WriteFile(temp_file, invalid_file_contents.data(),
58 m : invalid_file_contents.length()));
59 m : std::map<base::string16, base::string16> crash_keys_from_disk;
60 m : ASSERT_FALSE(ReadCrashKeysFromFile(temp_file, &crash_keys_from_disk));
61 m : }
62 :
63 m : TEST(CrashKeysSerializationTest, IllegalDictionaryContents) {
64 m : base::DictionaryValue dictionary;
65 m : std::unique_ptr<base::ListValue> list(new base::ListValue);
66 m : list->AppendString("value 1");
67 m : dictionary.Set("name", list.release());
68 m : base::ScopedTempDir temp_dir;
69 m : ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
70 m : base::FilePath temp_file = temp_dir.path().Append(L"test.dat");
71 m : std::string file_contents;
72 m : ASSERT_TRUE(base::JSONWriter::Write(dictionary, &file_contents));
73 m : ASSERT_TRUE(
74 m : base::WriteFile(temp_file, file_contents.data(), file_contents.length()));
75 m : std::map<base::string16, base::string16> crash_keys_from_disk;
76 m : ASSERT_FALSE(ReadCrashKeysFromFile(temp_file, &crash_keys_from_disk));
77 m : }
78 :
79 m : TEST(CrashKeysSerializationTest, NotADictionary) {
80 m : base::ListValue list;
81 m : list.AppendString("value 1");
82 m : base::ScopedTempDir temp_dir;
83 m : ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
84 m : base::FilePath temp_file = temp_dir.path().Append(L"test.dat");
85 m : std::string file_contents;
86 m : ASSERT_TRUE(base::JSONWriter::Write(list, &file_contents));
87 m : ASSERT_TRUE(
88 m : base::WriteFile(temp_file, file_contents.data(), file_contents.length()));
89 m : std::map<base::string16, base::string16> crash_keys_from_disk;
90 m : ASSERT_FALSE(ReadCrashKeysFromFile(temp_file, &crash_keys_from_disk));
91 m : }
92 :
93 m : } // namespace kasko
|