blob: b5a5c51d043c7a6e686e2d0a08f54ebd84129c61 [file] [log] [blame]
Dave MacLachlan74956e12019-12-17 17:32:09 -08001// Protocol Buffers - Google's data interchange format
2// Copyright 2015 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#import "GPBTestUtilities.h"
32
33#import <objc/runtime.h>
34
35#import "GPBDescriptor_PackagePrivate.h"
36#import "GPBExtensionRegistry.h"
37#import "GPBMessage.h"
38#import "GPBRootObject_PackagePrivate.h"
39
40// Support classes for tests using old class name (vs classrefs) interfaces.
Dave MacLachlanf4a6b992020-01-22 10:16:21 -080041GPB_FINAL @interface MessageLackingClazzRoot : GPBRootObject
Dave MacLachlan74956e12019-12-17 17:32:09 -080042@end
43
44@interface MessageLackingClazzRoot (DynamicMethods)
45+ (GPBExtensionDescriptor *)ext1;
46@end
47
Dave MacLachlanf4a6b992020-01-22 10:16:21 -080048GPB_FINAL @interface MessageLackingClazz : GPBMessage
Dave MacLachlan74956e12019-12-17 17:32:09 -080049@property(copy, nonatomic) NSString *foo;
50@end
51
52@implementation MessageLackingClazz
53
54@dynamic foo;
55
56typedef struct MessageLackingClazz_storage_ {
57 uint32_t _has_storage_[1];
58 NSString *foo;
59} MessageLackingClazz_storage_;
60
61+ (GPBDescriptor *)descriptor {
62 static GPBDescriptor *descriptor = nil;
63 if (!descriptor) {
64 static GPBMessageFieldDescription fields[] = {
65 {
66 .name = "foo",
67 .dataTypeSpecific.className = "NSString",
68 .number = 1,
69 .hasIndex = 0,
70 .offset = (uint32_t)offsetof(MessageLackingClazz_storage_, foo),
71 .flags = (GPBFieldFlags)(GPBFieldOptional),
72 .dataType = GPBDataTypeMessage,
73 },
74 };
75 GPBFileDescriptor *desc =
76 [[[GPBFileDescriptor alloc] initWithPackage:@"test"
77 objcPrefix:@"TEST"
78 syntax:GPBFileSyntaxProto3] autorelease];
79
80 // GPBDescriptorInitializationFlag_UsesClassRefs intentionally not set here
81 descriptor =
82 [GPBDescriptor allocDescriptorForClass:[MessageLackingClazz class]
83 rootClass:[MessageLackingClazzRoot class]
84 file:desc
85 fields:fields
86 fieldCount:(uint32_t)(sizeof(fields) / sizeof(GPBMessageFieldDescription))
87 storageSize:sizeof(MessageLackingClazz_storage_)
88 flags:GPBDescriptorInitializationFlag_None];
89 [descriptor setupContainingMessageClassName:"MessageLackingClazz"];
90 }
91 return descriptor;
92}
93@end
94
95@implementation MessageLackingClazzRoot
96
97+ (GPBExtensionRegistry*)extensionRegistry {
98 // This is called by +initialize so there is no need to worry
99 // about thread safety and initialization of registry.
100 static GPBExtensionRegistry* registry = nil;
101 if (!registry) {
102 registry = [[GPBExtensionRegistry alloc] init];
103 static GPBExtensionDescription descriptions[] = {
104 {
105 .defaultValue.valueMessage = NULL,
106 .singletonName = "MessageLackingClazzRoot_ext1",
107 .extendedClass.name = "MessageLackingClazz",
108 .messageOrGroupClass.name = "MessageLackingClazz",
109 .enumDescriptorFunc = NULL,
110 .fieldNumber = 1,
111 .dataType = GPBDataTypeMessage,
112 // GPBExtensionUsesClazz Intentionally not set
113 .options = 0,
114 },
115 };
116 for (size_t i = 0; i < sizeof(descriptions) / sizeof(descriptions[0]); ++i) {
117 // Intentionall using `-initWithExtensionDescription:` and not `
118 // -initWithExtensionDescription:usesClassRefs:` to test backwards
119 // compatibility
120 GPBExtensionDescriptor *extension =
121 [[GPBExtensionDescriptor alloc] initWithExtensionDescription:&descriptions[i]];
122 [registry addExtension:extension];
123 [self globallyRegisterExtension:extension];
124 [extension release];
125 }
126 // None of the imports (direct or indirect) defined extensions, so no need to add
127 // them to this registry.
128 }
129 return registry;
130}
131@end
132
133@interface MessageClassNameTests : GPBTestCase
134@end
135
136@implementation MessageClassNameTests
137
138- (void)testClassNameSupported {
139 // This tests backwards compatibility to make sure we support older sources
140 // that use class names instead of references.
141 GPBDescriptor *desc = [MessageLackingClazz descriptor];
142 GPBFieldDescriptor *fieldDesc = [desc fieldWithName:@"foo"];
143 XCTAssertEqualObjects(fieldDesc.msgClass, [NSString class]);
144}
145
146- (void)testSetupContainingMessageClassNameSupported {
147 // This tests backwards compatibility to make sure we support older sources
148 // that use class names instead of references.
149 GPBDescriptor *desc = [MessageLackingClazz descriptor];
150 GPBDescriptor *container = [desc containingType];
151 XCTAssertEqualObjects(container.messageClass, [MessageLackingClazz class]);
152}
153
154- (void)testExtensionsNameSupported {
155 // This tests backwards compatibility to make sure we support older sources
156 // that use class names instead of references.
157 GPBExtensionDescriptor *desc = [MessageLackingClazzRoot ext1];
158 Class containerClass = [desc containingMessageClass];
159 XCTAssertEqualObjects(containerClass, [MessageLackingClazz class]);
160 Class msgClass = [desc msgClass];
161 XCTAssertEqualObjects(msgClass, [MessageLackingClazz class]);
162}
163
164@end