1 : // Copyright 2012 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 : // Declares some unittest helper functions.
16 :
17 : #ifndef SYZYGY_CORE_UNITTEST_UTIL_H_
18 : #define SYZYGY_CORE_UNITTEST_UTIL_H_
19 :
20 : #include "base/file_util.h"
21 : #include "base/files/file_path.h"
22 : #include "gtest/gtest.h"
23 : #include "syzygy/core/serialization.h"
24 :
25 : namespace testing {
26 :
27 : // A simple utility class for creating and cleaning up a temporary file.
28 : class ScopedTempFile {
29 : public:
30 E : ScopedTempFile() {
31 E : file_util::CreateTemporaryFile(&path_);
32 E : }
33 :
34 E : ~ScopedTempFile() {
35 E : file_util::Delete(path_, false);
36 E : }
37 :
38 E : const base::FilePath& path() const { return path_; }
39 :
40 : private:
41 : base::FilePath path_;
42 : };
43 :
44 : // This defines a simple test of serialization for a given object. Returns
45 : // true on success, false otherwise. The data object must be default
46 : // constructible and comparable.
47 E : template<class Data> bool TestSerialization(const Data& data) {
48 E : core::ByteVector bytes;
49 :
50 E : core::ScopedOutStreamPtr out_stream;
51 E : out_stream.reset(core::CreateByteOutStream(std::back_inserter(bytes)));
52 E : core::NativeBinaryOutArchive out_archive(out_stream.get());
53 E : if (!out_archive.Save(data))
54 i : return false;
55 E : if (!out_archive.Flush())
56 i : return false;
57 :
58 E : core::ScopedInStreamPtr in_stream;
59 E : in_stream.reset(core::CreateByteInStream(bytes.begin(), bytes.end()));
60 E : core::NativeBinaryInArchive in_archive(in_stream.get());
61 E : Data data_copy;
62 E : if (!in_archive.Load(&data_copy))
63 i : return false;
64 :
65 : // Ensure the two elements are the same after a roundtrip through the
66 : // serialization engine.
67 E : return (data == data_copy);
68 E : };
69 :
70 : // Same as above, but serializes to the given file, which has to be opened
71 : // in read-write mode.
72 E : template<class Data> bool TestSerialization(const Data& data, FILE* file) {
73 E : core::FileOutStream out_stream(file);
74 E : core::NativeBinaryOutArchive out_archive(&out_stream);
75 E : if (!out_archive.Save(data))
76 i : return false;
77 :
78 : // Flush the output and rewind the file.
79 E : fflush(file);
80 E : fseek(file, 0, SEEK_SET);
81 :
82 E : core::FileInStream in_stream(file);
83 E : core::NativeBinaryInArchive in_archive(&in_stream);
84 E : Data data_copy;
85 E : if (!in_archive.Load(&data_copy))
86 i : return false;
87 :
88 : // Ensure the two elements are the same after a roundtrip through the
89 : // serialization engine.
90 E : return (data == data_copy);
91 E : };
92 :
93 : // Converts a relative path to absolute using the src directory as base.
94 : //
95 : // @path rel_path the relative path to convert.
96 : // @returns an absolute path.
97 : base::FilePath GetSrcRelativePath(const wchar_t* rel_path);
98 :
99 : // Converts a relative path to absolute using the executable directory as base.
100 : //
101 : // @path rel_path the relative path to convert.
102 : // @returns an absolute path.
103 : base::FilePath GetExeRelativePath(const wchar_t* rel_path);
104 :
105 : // Converts a relative path to absolute using the output directory as base.
106 : //
107 : // @path rel_path the relative path to convert.
108 : // @returns an absolute path.
109 : base::FilePath GetOutputRelativePath(const wchar_t* rel_path);
110 :
111 : // Converts a relative path to absolute using the test_data directory as base.
112 : //
113 : // @path rel_path the relative path to convert.
114 : // @returns an absolute path.
115 : base::FilePath GetExeTestDataRelativePath(const wchar_t* rel_path);
116 :
117 : // Converts an absolute path to a relative path using the given root directory
118 : // as a base.
119 : //
120 : // @param abs_path the absolute path to convert.
121 : // @param root_path the root path to use.
122 : // @returns the relative path to abs_path, starting from root. If there is no
123 : // relative path, it returns the empty path.
124 : // @pre Both abs_path and root_path must be absolute paths.
125 : base::FilePath GetRelativePath(const base::FilePath& abs_path,
126 : const base::FilePath& root_path);
127 :
128 : // Converts an absolute path to a relative path using the current working
129 : // directory as a base.
130 : //
131 : // @param abs_path the absolute path to convert.
132 : // @returns the relative path to abs_path, starting from the current working
133 : // directory. If there is no relative path, it returns the empty path.
134 : base::FilePath GetRelativePath(const base::FilePath& abs_path);
135 :
136 : // A utility for ensuring that two file paths point to the same file. Upon
137 : // failure, outputs the actual paths as well. This is not intended to be used
138 : // directly, but rather through the ASSERT_SAME_FILE and EXPECT_SAME_FILE
139 : // macros.
140 : // @param path1_expr the source code expression representing the contents of
141 : // path1.
142 : // @param path2_expr the source code expression representing the contents of
143 : // path2.
144 : // @param path1 the first path to compare.
145 : // @param path2 the second path to compare.
146 : // @returns AssertionSuccess if path1 and path2 refer to the same file on disk,
147 : // even if they have different paths. Otherwise, returns an AssertionFailure
148 : // with an informative error message.
149 : AssertionResult AssertAreSameFile(const char* path1_expr,
150 : const char* path2_expr,
151 : const base::FilePath& path1,
152 : const base::FilePath& path2);
153 :
154 : // GTest macros for ensuring two paths refer to the same file.
155 : #define ASSERT_SAME_FILE(path1, path2) \
156 : ASSERT_PRED_FORMAT2(::testing::AssertAreSameFile, path1, path2)
157 : #define EXPECT_SAME_FILE(path1, path2) \
158 : EXPECT_PRED_FORMAT2(::testing::AssertAreSameFile, path1, path2)
159 :
160 : } // namespace testing
161 :
162 : #endif // SYZYGY_CORE_UNITTEST_UTIL_H_
|