-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathparse_editions.js
More file actions
251 lines (214 loc) · 9.87 KB
/
Copy pathparse_editions.js
File metadata and controls
251 lines (214 loc) · 9.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
var tape = require("tape");
var protobuf = require("..");
tape.test("invalid edition", function(test) {
test.throws(function() {
protobuf.parse(`edition = "2022"; message A {}`);
}, Error, /Error: illegal edition '2022'/, "unknown past edition should be rejected");
test.throws(function() {
protobuf.parse(`edition = "2030"; message A {}`);
}, Error, /Error: illegal edition '2030'/, "unknown future edition should be rejected");
test.end();
});
tape.test("edition 2023 banned keywords", function(test) {
test.throws(function() {
protobuf.parse(`edition = "2023";
message A {\
required uint32 a = 1;\
}`);
}, Error, /Error: illegal token 'required'/, "required should be banned");
test.throws(function() {
protobuf.parse(`edition = "2023";
message A {\
optional uint32 a = 1;\
}`);
}, Error, /Error: illegal token 'optional'/, "optional should be banned");
test.throws(function() {
protobuf.parse(`edition = "2023";
message A {\
group uint32 a = 1;\
}`);
}, Error, /Error: illegal token 'group'/);
test.end();
});
tape.test("edition 2023 reserved", function(test) {
var root = protobuf.parse(`edition = "2023";
message Foo {
reserved bar, baz;
}`).root.resolveAll();
test.same(root.Foo.reserved, ["bar", "baz"], "reserved fields should be parsed");
root = protobuf.parse(`edition = "2023";
enum Foo {
reserved BAR, BAZ_BAZ;
}`).root.resolveAll();
test.same(root.nested.Foo.reserved, ["BAR", "BAZ_BAZ"], "reserved values should be parsed");
test.throws(function() {
protobuf.parse(`edition = "2023";
message Foo {
reserved "bar", "baz";
}`);
}, /Error: illegal id 'bar'/, "reserved field strings should be banned");
test.throws(function() {
protobuf.parse(`edition = "2023";
enum Foo {
reserved "BAR", "BAZ";
}`);
}, /Error: illegal id 'BAR'/, "reserved enum value strings should be banned");
test.throws(function() {
protobuf.parse(`syntax = "proto3";
message Foo {
reserved bar, baz;
}`);
}, /Error: illegal id 'bar'/, "reserved field strings should be banned");
test.throws(function() {
protobuf.parse(`syntax = "proto3";
enum Foo {
reserved BAR, BAZ;
}`);
}, /Error: illegal id 'BAR'/, "reserved enum value strings should be banned");
test.end();
});
tape.test("edition 2024 visibility", function(test) {
var root = protobuf.parse(`edition = "2024";
local message LocalMessage {}
export enum ExportedEnum {}
message Outer {
export message ExportedMessage {}
local enum LocalEnum { ZERO = 0; }
}
`).root;
test.equal(root.lookupType("LocalMessage").visibility, "local", "messages should preserve local modifier");
test.equal(root.lookupEnum("ExportedEnum").visibility, "export", "enums should preserve export modifier");
test.equal(root.lookupType("Outer.ExportedMessage").visibility, "export", "nested messages should preserve export modifier");
test.equal(root.lookupEnum("Outer.LocalEnum").visibility, "local", "nested enums should preserve local modifier");
var roundtrip = protobuf.Root.fromJSON(root.toJSON());
test.equal(roundtrip.lookupType("LocalMessage").visibility, "local", "reflection JSON should preserve message visibility");
test.equal(roundtrip.lookupEnum("ExportedEnum").visibility, "export", "reflection JSON should preserve enum visibility");
test.throws(function() {
protobuf.parse(`edition = "2023"; export message Foo {}`)
}, /Error: illegal token 'export'/, "export should be banned before edition 2024");
test.throws(function() {
protobuf.parse(`edition = "2024"; export service Foo {}`)
}, /Error: illegal token 'export'/, "export should be banned on services");
test.throws(function() {
protobuf.parse(`edition = "2024";
message Empty {}
service Foo {
export rpc Method(Empty) returns (Empty);
}`)
}, /Error: illegal token 'export'/, "export should be banned on methods");
test.throws(function() {
protobuf.parse(`edition = "2024"; export option foo = 1;`)
}, /Error: illegal token 'export'/, "export should be banned on options");
test.throws(function() {
protobuf.parse(`edition = "2023"; local message Foo {}`)
}, /Error: illegal token 'local'/, "local should be banned before edition 2024");
test.throws(function() {
protobuf.parse(`edition = "2024"; local service Foo {}`)
}, /Error: illegal token 'local'/, "local should be banned on services");
test.throws(function() {
protobuf.parse(`edition = "2024";
message Empty {}
service Foo {
local rpc Method(Empty) returns (Empty);
}`)
}, /Error: illegal token 'local'/, "local should be banned on methods");
test.throws(function() {
protobuf.parse(`edition = "2024"; local option foo = 1;`)
}, /Error: illegal token 'local'/, "local should be banned on options");
test.end();
});
tape.test("edition 2024 import option", function(test) {
test.same(protobuf.parse(`edition = "2024"; import "foo.proto";`).imports, ["foo.proto"], "regular options should fetch");
test.equals(protobuf.parse(`edition = "2024"; import option "foo.proto";`).imports, undefined, "import option should not fetch");
test.same(protobuf.parse(`edition = "2024";
import option "foo.proto";
import "bar.proto";
import option "foo2.proto";
`).imports, ["bar.proto"], "multiple import options should not fetch");
test.throws(function() {
protobuf.parse(`edition = "2023"; import option "foo.proto";`);
}, /Error: illegal token 'option'/, "import option should be banned before edition 2024");
var root = new protobuf.Root();
root.loadSync("tests/data/import-option-bad.proto");
root.resolveAll();
test.end();
});
tape.test("edition 2024 local visibility enforcement", function(test) {
// Parses several sources into one Root, each under its own file name, the
// way Root#load does (it sets parse.filename before each parse call).
function parseFiles(files) {
var root = new protobuf.Root();
Object.keys(files).forEach(function(filename) {
protobuf.parse.filename = filename;
protobuf.parse(files[filename], root);
});
return root;
}
test.doesNotThrow(function() {
parseFiles({
"a.proto": `edition = "2024";
local message Hidden { int32 value = 1; }
message User { Hidden hidden = 1; }`
}).resolveAll();
}, "local message should resolve within its own file");
test.doesNotThrow(function() {
parseFiles({
"a.proto": `edition = "2024";
local enum Hidden { ZERO = 0; }
message User { Hidden hidden = 1; }`
}).resolveAll();
}, "local enum should resolve within its own file");
test.throws(function() {
parseFiles({
"a.proto": `edition = "2024"; local message Hidden { int32 value = 1; }`,
"b.proto": `edition = "2024"; message User { Hidden hidden = 1; }`
}).resolveAll();
}, /is local to 'a\.proto' and cannot be referenced from 'b\.proto'/, "local message should not resolve from another file");
test.throws(function() {
parseFiles({
"a.proto": `edition = "2024"; local enum Hidden { ZERO = 0; }`,
"b.proto": `edition = "2024"; message User { Hidden hidden = 1; }`
}).resolveAll();
}, /is local to 'a\.proto' and cannot be referenced from 'b\.proto'/, "local enum should not resolve from another file");
test.throws(function() {
parseFiles({
"a.proto": `edition = "2024"; message Outer { local message Hidden { int32 value = 1; } }`,
"b.proto": `edition = "2024"; message User { Outer.Hidden hidden = 1; }`
}).resolveAll();
}, /is local to 'a\.proto' and cannot be referenced from 'b\.proto'/, "nested local message should not resolve from another file");
test.doesNotThrow(function() {
parseFiles({
"a.proto": `edition = "2024"; export message Shared { int32 value = 1; }`,
"b.proto": `edition = "2024"; message User { Shared shared = 1; }`
}).resolveAll();
}, "export message should resolve from another file");
test.doesNotThrow(function() {
parseFiles({
"a.proto": `edition = "2024"; message Plain { int32 value = 1; }`,
"b.proto": `edition = "2024"; message User { Plain plain = 1; }`
}).resolveAll();
}, "unmodified message should resolve from another file");
test.doesNotThrow(function() {
parseFiles({
"a.proto": `edition = "2023"; message Plain { int32 value = 1; }`,
"b.proto": `edition = "2023"; message User { Plain plain = 1; }`
}).resolveAll();
}, "edition 2023 cross-file references should be untouched");
test.doesNotThrow(function() {
parseFiles({
"a.proto": `syntax = "proto2"; message Plain { optional int32 value = 1; }`,
"b.proto": `syntax = "proto3"; message User { Plain plain = 1; }`
}).resolveAll();
}, "proto2/proto3 cross-file references should be untouched");
// Objects built without a file name cannot be proven to be cross-file, so
// they must keep resolving (programmatic construction, fromJSON, commons).
test.doesNotThrow(function() {
protobuf.Root.fromJSON({
nested: {
Hidden: { fields: { value: { type: "int32", id: 1 } } },
User: { fields: { hidden: { type: "Hidden", id: 1 } } }
}
}).resolveAll();
}, "objects without a file name should keep resolving");
test.end();
});