1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 #ifndef GOOGLE_PROTOBUF_TEST_UTIL2_H__
32 #define GOOGLE_PROTOBUF_TEST_UTIL2_H__
33
34 #include <google/protobuf/stubs/strutil.h>
35
36 #include <google/protobuf/testing/googletest.h>
37 #include <google/protobuf/io/zero_copy_stream.h>
38 #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
39 #include <google/protobuf/util/message_differencer.h>
40
41 namespace google {
42 namespace protobuf {
43 namespace TestUtil {
44
45 // Translate net/proto2/* -> google/protobuf/*
TranslatePathToOpensource(const std::string & google3_path)46 inline std::string TranslatePathToOpensource(const std::string& google3_path) {
47 const std::string prefix = "net/proto2/";
48 GOOGLE_CHECK(google3_path.find(prefix) == 0) << google3_path;
49 std::string path = google3_path.substr(prefix.size());
50
51 path = StringReplace(path, "internal/", "", false);
52 path = StringReplace(path, "proto/", "", false);
53 path = StringReplace(path, "public/", "", false);
54 return "google/protobuf/" + path;
55 }
56
MaybeTranslatePath(const std::string & google3_path)57 inline std::string MaybeTranslatePath(const std::string& google3_path) {
58 std::string path = google3_path;
59 path = TranslatePathToOpensource(path);
60 return path;
61 }
62
TestSourceDir()63 inline std::string TestSourceDir() {
64 return google::protobuf::TestSourceDir();
65 }
66
GetTestDataPath(const std::string & google3_path)67 inline std::string GetTestDataPath(const std::string& google3_path) {
68 return TestSourceDir() + "/" + MaybeTranslatePath(google3_path);
69 }
70
71 // Checks the equality of "message" and serialized proto of type "ProtoType".
72 // Do not directly compare two serialized protos.
73 template <typename ProtoType>
EqualsToSerialized(const ProtoType & message,const std::string & data)74 bool EqualsToSerialized(const ProtoType& message, const std::string& data) {
75 ProtoType other;
76 other.ParsePartialFromString(data);
77 return util::MessageDifferencer::Equals(message, other);
78 }
79
80 // Wraps io::ArrayInputStream while checking against bound. When a blocking
81 // stream is used with bounded length, proto parsing must not access beyond the
82 // bound. Otherwise, it can result in unintended block, then deadlock.
83 class BoundedArrayInputStream : public io::ZeroCopyInputStream {
84 public:
BoundedArrayInputStream(const void * data,int size)85 BoundedArrayInputStream(const void* data, int size)
86 : stream_(data, size), bound_(size) {}
~BoundedArrayInputStream()87 ~BoundedArrayInputStream() override {}
88
Next(const void ** data,int * size)89 bool Next(const void** data, int* size) override {
90 GOOGLE_CHECK_LT(stream_.ByteCount(), bound_);
91 return stream_.Next(data, size);
92 }
BackUp(int count)93 void BackUp(int count) override { stream_.BackUp(count); }
Skip(int count)94 bool Skip(int count) override { return stream_.Skip(count); }
ByteCount()95 int64_t ByteCount() const override { return stream_.ByteCount(); }
96
97 private:
98 io::ArrayInputStream stream_;
99 int bound_;
100 };
101
102 } // namespace TestUtil
103 } // namespace protobuf
104 } // namespace google
105
106 #endif // GOOGLE_PROTOBUF_TEST_UTIL2_H__
107