1 // Copyright (c) 2009-2021, Google LLC
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //     * Redistributions of source code must retain the above copyright
7 //       notice, this list of conditions and the following disclaimer.
8 //     * Redistributions in binary form must reproduce the above copyright
9 //       notice, this list of conditions and the following disclaimer in the
10 //       documentation and/or other materials provided with the distribution.
11 //     * Neither the name of Google LLC nor the
12 //       names of its contributors may be used to endorse or promote products
13 //       derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 // ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
19 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 // Tests for C++ wrappers.
27 
28 #include <string.h>
29 
30 #include <fstream>
31 #include <iostream>
32 #include <set>
33 #include <sstream>
34 
35 #include "google/protobuf/timestamp.upb.h"
36 #include "google/protobuf/timestamp.upbdefs.h"
37 #include "gtest/gtest.h"
38 #include "upb/json/decode.h"
39 #include "upb/json/encode.h"
40 #include "upb/reflection/def.h"
41 #include "upb/reflection/def.hpp"
42 #include "upb/test/test_cpp.upb.h"
43 #include "upb/test/test_cpp.upbdefs.h"
44 
45 // Must be last.
46 #include "upb/port/def.inc"
47 
TEST(Cpp,Iteration)48 TEST(Cpp, Iteration) {
49   upb::DefPool defpool;
50   upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(defpool.ptr()));
51 
52   // Test range-based for on both fields and oneofs (with the iterator adaptor).
53   int field_count = 0;
54   for (auto field : md.fields()) {
55     UPB_UNUSED(field);
56     field_count++;
57   }
58   EXPECT_EQ(field_count, md.field_count());
59 
60   int oneof_count = 0;
61   for (auto oneof : md.oneofs()) {
62     UPB_UNUSED(oneof);
63     oneof_count++;
64   }
65   EXPECT_EQ(oneof_count, md.oneof_count());
66 }
67 
TEST(Cpp,InlinedArena2)68 TEST(Cpp, InlinedArena2) {
69   upb::InlinedArena<64> arena;
70   upb_Arena_Malloc(arena.ptr(), sizeof(int));
71 }
72 
TEST(Cpp,Default)73 TEST(Cpp, Default) {
74   upb::DefPool defpool;
75   upb::Arena arena;
76   upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(defpool.ptr()));
77   upb_test_TestMessage* msg = upb_test_TestMessage_new(arena.ptr());
78   size_t size = upb_JsonEncode(msg, md.ptr(), NULL, 0, NULL, 0, NULL);
79   EXPECT_EQ(2, size);  // "{}"
80 }
81 
TEST(Cpp,JsonNull)82 TEST(Cpp, JsonNull) {
83   upb::DefPool defpool;
84   upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(defpool.ptr()));
85   upb::FieldDefPtr i32_f = md.FindFieldByName("i32");
86   upb::FieldDefPtr str_f = md.FindFieldByName("str");
87   ASSERT_TRUE(i32_f);
88   ASSERT_TRUE(str_f);
89   EXPECT_EQ(5, i32_f.default_value().int32_val);
90   EXPECT_EQ(0, strcmp(str_f.default_value().str_val.data, "abc"));
91   EXPECT_EQ(3, str_f.default_value().str_val.size);
92 }
93 
TEST(Cpp,TimestampEncoder)94 TEST(Cpp, TimestampEncoder) {
95   upb::DefPool defpool;
96   upb::Arena arena;
97   upb::MessageDefPtr md(google_protobuf_Timestamp_getmsgdef(defpool.ptr()));
98   google_protobuf_Timestamp* timestamp_upb =
99       google_protobuf_Timestamp_new(arena.ptr());
100   google_protobuf_Timestamp* timestamp_upb_decoded =
101       google_protobuf_Timestamp_new(arena.ptr());
102 
103   int64_t timestamps[] = {
104       253402300799,  // 9999-12-31T23:59:59Z
105       1641006000,    // 2022-01-01T03:00:00Z
106       0,             // 1970-01-01T00:00:00Z
107       -31525200,     // 1969-01-01T03:00:00Z
108       -2208988800,   // 1900-01-01T00:00:00Z
109       -62135596800,  // 0000-01-01T00:00:00Z
110   };
111 
112   for (int64_t timestamp : timestamps) {
113     google_protobuf_Timestamp_set_seconds(timestamp_upb, timestamp);
114 
115     char json[128];
116     size_t size = upb_JsonEncode(timestamp_upb, md.ptr(), NULL, 0, json,
117                                  sizeof(json), NULL);
118     bool result = upb_JsonDecode(json, size, timestamp_upb_decoded, md.ptr(),
119                                  NULL, 0, arena.ptr(), NULL);
120     const int64_t timestamp_decoded =
121         google_protobuf_Timestamp_seconds(timestamp_upb_decoded);
122 
123     ASSERT_TRUE(result);
124     EXPECT_EQ(timestamp, timestamp_decoded);
125   }
126 }
127