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