network-infra-utility 0.2.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +21 -0
  3. data/CHANGELOG.md +31 -4
  4. data/Gemfile +2 -0
  5. data/Gemfile.lock +70 -0
  6. data/Rakefile +1 -1
  7. data/bin/dns-query +834 -0
  8. data/bin/geo-doc +135 -0
  9. data/bin/geo-get +1 -1
  10. data/document/ASNum/346/250/241/345/235/227/345/212/237/350/203/275/350/257/264/346/230/216.md +242 -0
  11. data/document/DNSQuery/345/267/245/345/205/267/344/275/277/347/224/250/346/226/271/346/263/225.md +248 -0
  12. data/document/Geo/345/221/275/344/273/244/345/267/245/345/205/267/344/275/277/347/224/250/346/226/271/346/263/225.md +441 -0
  13. data/document/IP/346/250/241/345/235/227/345/212/237/350/203/275/350/257/264/346/230/216.md +297 -0
  14. data/document/MAC/346/250/241/345/235/227/345/212/237/350/203/275/350/257/264/346/230/216.md +296 -0
  15. data/document/SSH/350/277/236/346/216/245/345/256/242/346/210/267/347/253/257/344/275/277/347/224/250/346/226/271/346/263/225.md +764 -0
  16. data/network-infra-utility.gemspec +4 -2
  17. data/network.rb +3 -1
  18. data/service/geodb/GeoAPI.md +1 -0
  19. data/service/geodb/geodb.rb +278 -1
  20. data/service/ssh/README.md +942 -0
  21. data/service/ssh/bin/ssh-client +198 -0
  22. data/service/ssh/design/SSH/350/277/236/346/216/245/345/256/242/346/210/267/347/253/257/345/212/237/350/203/275/351/234/200/346/261/202/346/226/207/346/241/243.md +292 -0
  23. data/service/ssh/design/SSH/350/277/236/346/216/245/345/256/242/346/210/267/347/253/257/350/257/246/347/273/206/350/256/276/350/256/241/346/226/207/346/241/243.md +1521 -0
  24. data/service/ssh/design/SSH/350/277/236/346/216/245/345/256/242/346/210/267/347/253/257/350/275/257/344/273/266/350/256/276/350/256/241/346/226/207/346/241/243.md +2493 -0
  25. data/service/ssh/ext/ssh_core/bin/ssh_core.cmd +28 -0
  26. data/service/ssh/ext/ssh_core/config/sys.config +0 -0
  27. data/service/ssh/ext/ssh_core/config/vm.args +0 -0
  28. data/service/ssh/ext/ssh_core/local_deps/jsx/CHECKSUM +1 -0
  29. data/service/ssh/ext/ssh_core/local_deps/jsx/LICENSE +21 -0
  30. data/service/ssh/ext/ssh_core/local_deps/jsx/README.md +696 -0
  31. data/service/ssh/ext/ssh_core/local_deps/jsx/VERSION +1 -0
  32. data/service/ssh/ext/ssh_core/local_deps/jsx/contents.tar.gz +0 -0
  33. data/service/ssh/ext/ssh_core/local_deps/jsx/metadata.config +15 -0
  34. data/service/ssh/ext/ssh_core/local_deps/jsx/rebar.config +17 -0
  35. data/service/ssh/ext/ssh_core/local_deps/jsx/rebar.lock +1 -0
  36. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx.app.src +10 -0
  37. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx.erl +506 -0
  38. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_config.erl +393 -0
  39. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_config.hrl +18 -0
  40. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_consult.erl +81 -0
  41. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_decoder.erl +1909 -0
  42. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_encoder.erl +116 -0
  43. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_parser.erl +1214 -0
  44. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_to_json.erl +408 -0
  45. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_to_term.erl +389 -0
  46. data/service/ssh/ext/ssh_core/local_deps/jsx/src/jsx_verify.erl +121 -0
  47. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx.erl +506 -0
  48. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_config.erl +393 -0
  49. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_config.hrl +18 -0
  50. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_consult.erl +81 -0
  51. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_decoder.erl +1909 -0
  52. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_encoder.erl +116 -0
  53. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_parser.erl +1214 -0
  54. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_to_json.erl +408 -0
  55. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_to_term.erl +389 -0
  56. data/service/ssh/ext/ssh_core/local_deps/jsx/src/src/jsx_verify.erl +121 -0
  57. data/service/ssh/ext/ssh_core/rebar.config +24 -0
  58. data/service/ssh/ext/ssh_core/rebar.lock +1 -0
  59. data/service/ssh/ext/ssh_core/src/ssh_auth_engine.erl +156 -0
  60. data/service/ssh/ext/ssh_core/src/ssh_channel_stm.erl +232 -0
  61. data/service/ssh/ext/ssh_core/src/ssh_codec.erl +83 -0
  62. data/service/ssh/ext/ssh_core/src/ssh_conn_sup.erl +48 -0
  63. data/service/ssh/ext/ssh_core/src/ssh_conn_worker.erl +535 -0
  64. data/service/ssh/ext/ssh_core/src/ssh_core.app.src +36 -0
  65. data/service/ssh/ext/ssh_core/src/ssh_core_app.erl +11 -0
  66. data/service/ssh/ext/ssh_core/src/ssh_core_sup.erl +46 -0
  67. data/service/ssh/ext/ssh_core/src/ssh_infra_sup.erl +104 -0
  68. data/service/ssh/ext/ssh_core/src/ssh_ipc.hrl +80 -0
  69. data/service/ssh/ext/ssh_core/src/ssh_ipc_coalesce.erl +94 -0
  70. data/service/ssh/ext/ssh_core/src/ssh_ipc_gateway.erl +467 -0
  71. data/service/ssh/ext/ssh_core/src/ssh_ipc_proto.erl +95 -0
  72. data/service/ssh/ext/ssh_core/src/ssh_jump_chain.erl +101 -0
  73. data/service/ssh/ext/ssh_core/src/ssh_keepalive_mgr.erl +222 -0
  74. data/service/ssh/ext/ssh_core/src/ssh_known_hosts_proxy.erl +67 -0
  75. data/service/ssh/ext/ssh_core/src/ssh_port_fwd.erl +225 -0
  76. data/service/ssh/ext/ssh_core/src/ssh_sftp_session.erl +250 -0
  77. data/service/ssh/ext/ssh_core/src/ssh_sftp_sup.erl +62 -0
  78. data/service/ssh/ext/ssh_core_rs/Cargo.lock +2345 -0
  79. data/service/ssh/ext/ssh_core_rs/Cargo.toml +30 -0
  80. data/service/ssh/ext/ssh_core_rs/bin/ssh_core_rs +34 -0
  81. data/service/ssh/ext/ssh_core_rs/bin/ssh_core_rs.cmd +40 -0
  82. data/service/ssh/ext/ssh_core_rs/src/channel.rs +296 -0
  83. data/service/ssh/ext/ssh_core_rs/src/coalesce.rs +143 -0
  84. data/service/ssh/ext/ssh_core_rs/src/codec.rs +71 -0
  85. data/service/ssh/ext/ssh_core_rs/src/conn.rs +628 -0
  86. data/service/ssh/ext/ssh_core_rs/src/gateway.rs +389 -0
  87. data/service/ssh/ext/ssh_core_rs/src/handler.rs +293 -0
  88. data/service/ssh/ext/ssh_core_rs/src/keepalive.rs +194 -0
  89. data/service/ssh/ext/ssh_core_rs/src/main.rs +351 -0
  90. data/service/ssh/ext/ssh_core_rs/src/portfwd.rs +378 -0
  91. data/service/ssh/ext/ssh_core_rs/src/proto.rs +198 -0
  92. data/service/ssh/ext/ssh_core_rs/src/sftp.rs +294 -0
  93. data/service/ssh/lib/network_infra_utility/ssh/automation/macro_engine.rb +213 -0
  94. data/service/ssh/lib/network_infra_utility/ssh/client.rb +257 -0
  95. data/service/ssh/lib/network_infra_utility/ssh/config/schema.rb +90 -0
  96. data/service/ssh/lib/network_infra_utility/ssh/config/settings.rb +103 -0
  97. data/service/ssh/lib/network_infra_utility/ssh/config/store.rb +90 -0
  98. data/service/ssh/lib/network_infra_utility/ssh/ipc/coalesce.rb +83 -0
  99. data/service/ssh/lib/network_infra_utility/ssh/ipc/errors.rb +36 -0
  100. data/service/ssh/lib/network_infra_utility/ssh/ipc/router.rb +212 -0
  101. data/service/ssh/lib/network_infra_utility/ssh/ipc/transport.rb +81 -0
  102. data/service/ssh/lib/network_infra_utility/ssh/security/host_key.rb +211 -0
  103. data/service/ssh/lib/network_infra_utility/ssh/security/vault.rb +211 -0
  104. data/service/ssh/lib/network_infra_utility/ssh/session/history.rb +56 -0
  105. data/service/ssh/lib/network_infra_utility/ssh/session/manager.rb +92 -0
  106. data/service/ssh/lib/network_infra_utility/ssh/session/session.rb +109 -0
  107. data/service/ssh/lib/network_infra_utility/ssh/session/tree.rb +95 -0
  108. data/service/ssh/lib/network_infra_utility/ssh/terminal/ansi_parser.rb +435 -0
  109. data/service/ssh/lib/network_infra_utility/ssh/terminal/buffer.rb +78 -0
  110. data/service/ssh/lib/network_infra_utility/ssh/terminal/emulator.rb +159 -0
  111. data/service/ssh/lib/network_infra_utility/ssh/terminal/logger.rb +195 -0
  112. data/service/ssh/lib/network_infra_utility/ssh/terminal/screen.rb +212 -0
  113. data/service/ssh/lib/network_infra_utility/ssh/terminal/theme.rb +127 -0
  114. data/service/ssh/lib/network_infra_utility/ssh/version.rb +7 -0
  115. data/service/ssh/lib/network_infra_utility/ssh.rb +44 -0
  116. data/support/basic/as_num.rb +221 -0
  117. data/support/basic/mac_address.rb +281 -0
  118. data/version.rb +1 -1
  119. metadata +138 -1
@@ -0,0 +1,389 @@
1
+ %% The MIT License
2
+
3
+ %% Copyright (c) 2010-2013 Alisdair Sullivan <alisdairsullivan@yahoo.ca>
4
+
5
+ %% Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ %% of this software and associated documentation files (the "Software"), to deal
7
+ %% in the Software without restriction, including without limitation the rights
8
+ %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ %% copies of the Software, and to permit persons to whom the Software is
10
+ %% furnished to do so, subject to the following conditions:
11
+
12
+ %% The above copyright notice and this permission notice shall be included in
13
+ %% all copies or substantial portions of the Software.
14
+
15
+ %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ %% THE SOFTWARE.
22
+
23
+
24
+ -module(jsx_to_term).
25
+
26
+ -export([to_term/2]).
27
+ -export([init/1, handle_event/2]).
28
+ -export([
29
+ start_term/1,
30
+ start_object/1,
31
+ start_array/1,
32
+ finish/1,
33
+ insert/2,
34
+ get_key/1,
35
+ get_value/1
36
+ ]).
37
+
38
+
39
+ -record(config, {
40
+ labels = binary,
41
+ return_maps = false
42
+ }).
43
+
44
+ -type config() :: proplists:proplist().
45
+
46
+ -spec to_term(Source::binary(), Config::jsx_config:options()) -> jsx:json_term() | {incomplete, jsx:decoder()}.
47
+
48
+ to_term(Source, Config) when is_list(Config) ->
49
+ (jsx:decoder(?MODULE, [return_maps] ++ Config, jsx_config:extract_config(Config)))(Source).
50
+
51
+ parse_config(Config) -> parse_config(Config, #config{}).
52
+
53
+ parse_config([{labels, Val}|Rest], Config)
54
+ when Val == binary; Val == atom; Val == existing_atom; Val == attempt_atom ->
55
+ parse_config(Rest, Config#config{labels = Val});
56
+ parse_config([labels|Rest], Config) ->
57
+ parse_config(Rest, Config#config{labels = binary});
58
+ parse_config([{return_maps, Val}|Rest], Config)
59
+ when Val == true; Val == false ->
60
+ parse_config(Rest, Config#config{return_maps = Val});
61
+ parse_config([return_maps|Rest], Config) ->
62
+ parse_config(Rest, Config#config{return_maps = true});
63
+ parse_config([{K, _}|Rest] = Options, Config) ->
64
+ case lists:member(K, jsx_config:valid_flags()) of
65
+ true -> parse_config(Rest, Config)
66
+ ; false -> erlang:error(badarg, [Options, Config])
67
+ end;
68
+ parse_config([K|Rest] = Options, Config) ->
69
+ case lists:member(K, jsx_config:valid_flags()) of
70
+ true -> parse_config(Rest, Config)
71
+ ; false -> erlang:error(badarg, [Options, Config])
72
+ end;
73
+ parse_config([], Config) ->
74
+ Config.
75
+
76
+
77
+ -type state() :: {list(), #config{}}.
78
+ -spec init(Config::config()) -> state().
79
+
80
+ init(Config) -> start_term(Config).
81
+
82
+ -spec handle_event(Event::any(), State::state()) -> state().
83
+
84
+ handle_event(end_json, State) -> get_value(State);
85
+
86
+ handle_event(start_object, State) -> start_object(State);
87
+ handle_event(end_object, State) -> finish(State);
88
+
89
+ handle_event(start_array, State) -> start_array(State);
90
+ handle_event(end_array, State) -> finish(State);
91
+
92
+ handle_event({key, Key}, {_, Config} = State) -> insert(format_key(Key, Config), State);
93
+
94
+ handle_event({_, Event}, State) -> insert(Event, State).
95
+
96
+
97
+ format_key(Key, Config) ->
98
+ case Config#config.labels of
99
+ binary -> Key
100
+ ; atom -> binary_to_atom(Key, utf8)
101
+ ; existing_atom -> binary_to_existing_atom(Key, utf8)
102
+ ; attempt_atom ->
103
+ try binary_to_existing_atom(Key, utf8) of
104
+ Result -> Result
105
+ catch
106
+ error:badarg -> Key
107
+ end
108
+ end.
109
+
110
+
111
+ %% internal state is a stack and a config object
112
+ %% `{Stack, Config}`
113
+ %% the stack is a list of in progress objects/arrays
114
+ %% `[Current, Parent, Grandparent,...OriginalAncestor]`
115
+ %% an object has the representation on the stack of
116
+ %% `{object, [
117
+ %% {NthKey, NthValue},
118
+ %% {NMinus1Key, NthMinus1Value},
119
+ %% ...,
120
+ %% {FirstKey, FirstValue}
121
+ %% ]}`
122
+ %% or if returning maps
123
+ %% `{object, #{
124
+ %% FirstKey => FirstValue,
125
+ %% SecondKey => SecondValue,
126
+ %% ...,
127
+ %% NthKey => NthValue
128
+ %% }}`
129
+ %% or if there's a key with a yet to be matched value
130
+ %% `{object, Key, ...}`
131
+ %% an array looks like
132
+ %% `{array, [NthValue, NthMinus1Value,...FirstValue]}`
133
+
134
+ start_term(Config) when is_list(Config) -> {[], parse_config(Config)}.
135
+
136
+ %% allocate a new object on top of the stack
137
+ start_object({Stack, Config=#config{return_maps=true}}) ->
138
+ {[{object, #{}}] ++ Stack, Config};
139
+ start_object({Stack, Config}) ->
140
+ {[{object, []}] ++ Stack, Config}.
141
+
142
+
143
+ %% allocate a new array on top of the stack
144
+ start_array({Stack, Config}) -> {[{array, []}] ++ Stack, Config}.
145
+
146
+
147
+ %% finish an object or array and insert it into the parent object if it exists or
148
+ %% return it if it is the root object
149
+ finish({[{object, Map}], Config=#config{return_maps=true}}) -> {Map, Config};
150
+ finish({[{object, Map}|Rest], Config=#config{return_maps=true}}) -> insert(Map, {Rest, Config});
151
+ finish({[{object, []}], Config}) -> {[{}], Config};
152
+ finish({[{object, []}|Rest], Config}) -> insert([{}], {Rest, Config});
153
+ finish({[{object, Pairs}], Config}) -> {lists:reverse(Pairs), Config};
154
+ finish({[{object, Pairs}|Rest], Config}) -> insert(lists:reverse(Pairs), {Rest, Config});
155
+ finish({[{array, Values}], Config}) -> {lists:reverse(Values), Config};
156
+ finish({[{array, Values}|Rest], Config}) -> insert(lists:reverse(Values), {Rest, Config});
157
+ finish(_) -> erlang:error(badarg).
158
+
159
+
160
+ %% insert a value when there's no parent object or array
161
+ insert(Value, {[], Config}) -> {Value, Config};
162
+ %% insert a key or value into an object or array, autodetects the 'right' thing
163
+ insert(Key, {[{object, Map}|Rest], Config=#config{return_maps=true}}) ->
164
+ {[{object, Key, Map}] ++ Rest, Config};
165
+ insert(Key, {[{object, Pairs}|Rest], Config}) ->
166
+ {[{object, Key, Pairs}] ++ Rest, Config};
167
+ insert(Value, {[{object, Key, Map}|Rest], Config=#config{return_maps=true}}) ->
168
+ {[{object, maps:put(Key, Value, Map)}] ++ Rest, Config};
169
+ insert(Value, {[{object, Key, Pairs}|Rest], Config}) ->
170
+ {[{object, [{Key, Value}] ++ Pairs}] ++ Rest, Config};
171
+ insert(Value, {[{array, Values}|Rest], Config}) ->
172
+ {[{array, [Value] ++ Values}] ++ Rest, Config};
173
+ insert(_, _) -> erlang:error(badarg).
174
+
175
+ get_key({[{object, Key, _}|_], _}) -> Key;
176
+ get_key(_) -> erlang:error(badarg).
177
+
178
+
179
+ get_value({Value, _Config}) -> Value;
180
+ get_value(_) -> erlang:error(badarg).
181
+
182
+
183
+
184
+ %% eunit tests
185
+
186
+ -ifdef(TEST).
187
+ -include_lib("eunit/include/eunit.hrl").
188
+
189
+
190
+ config_test_() ->
191
+ [
192
+ {"empty config", ?_assertEqual(#config{}, parse_config([]))},
193
+ {"implicit binary labels", ?_assertEqual(#config{}, parse_config([labels]))},
194
+ {"binary labels", ?_assertEqual(#config{}, parse_config([{labels, binary}]))},
195
+ {"atom labels", ?_assertEqual(#config{labels=atom}, parse_config([{labels, atom}]))},
196
+ {"existing atom labels", ?_assertEqual(
197
+ #config{labels=existing_atom},
198
+ parse_config([{labels, existing_atom}])
199
+ )},
200
+ {"return_maps true", ?_assertEqual(
201
+ #config{return_maps=true},
202
+ parse_config([return_maps])
203
+ )},
204
+ {"invalid opt flag", ?_assertError(badarg, parse_config([error]))},
205
+ {"invalid opt tuple", ?_assertError(badarg, parse_config([{error, true}]))}
206
+ ].
207
+
208
+
209
+ format_key_test_() ->
210
+ [
211
+ {"binary key", ?_assertEqual(<<"key">>, format_key(<<"key">>, #config{labels=binary}))},
212
+ {"atom key", ?_assertEqual(key, format_key(<<"key">>, #config{labels=atom}))},
213
+ {"existing atom key", ?_assertEqual(
214
+ key,
215
+ format_key(<<"key">>, #config{labels=existing_atom})
216
+ )},
217
+ {"nonexisting atom key", ?_assertError(
218
+ badarg,
219
+ format_key(<<"nonexistentatom">>, #config{labels=existing_atom})
220
+ )},
221
+ {"sloppy existing atom key", ?_assertEqual(
222
+ key,
223
+ format_key(<<"key">>, #config{labels=attempt_atom})
224
+ )},
225
+ {"nonexisting atom key", ?_assertEqual(
226
+ <<"nonexistentatom">>,
227
+ format_key(<<"nonexistentatom">>, #config{labels=attempt_atom})
228
+ )}
229
+ ].
230
+
231
+
232
+ rep_manipulation_test_() ->
233
+ [
234
+ {"allocate a new context with option", ?_assertEqual(
235
+ {[], #config{labels=atom}},
236
+ start_term([{labels, atom}])
237
+ )},
238
+ {"allocate a new object on an empty stack", ?_assertEqual(
239
+ {[{object, []}], #config{}},
240
+ start_object({[], #config{}})
241
+ )},
242
+ {"allocate a new object on a stack", ?_assertEqual(
243
+ {[{object, []}, {object, []}], #config{}},
244
+ start_object({[{object, []}], #config{}})
245
+ )},
246
+ {"allocate a new array on an empty stack", ?_assertEqual(
247
+ {[{array, []}], #config{}},
248
+ start_array({[], #config{}})
249
+ )},
250
+ {"allocate a new array on a stack", ?_assertEqual(
251
+ {[{array, []}, {object, []}], #config{}},
252
+ start_array({[{object, []}], #config{}})
253
+ )},
254
+ {"insert a key into an object", ?_assertEqual(
255
+ {[{object, key, []}, junk], #config{}},
256
+ insert(key, {[{object, []}, junk], #config{}})
257
+ )},
258
+ {"get current key", ?_assertEqual(
259
+ key,
260
+ get_key({[{object, key, []}], #config{}})
261
+ )},
262
+ {"try to get non-key from object", ?_assertError(
263
+ badarg,
264
+ get_key({[{object, []}], #config{}})
265
+ )},
266
+ {"try to get key from array", ?_assertError(
267
+ badarg,
268
+ get_key({[{array, []}], #config{}})
269
+ )},
270
+ {"insert a value into an object", ?_assertEqual(
271
+ {[{object, [{key, value}]}, junk], #config{}},
272
+ insert(value, {[{object, key, []}, junk], #config{}})
273
+ )},
274
+ {"insert a value into an array", ?_assertEqual(
275
+ {[{array, [value]}, junk], #config{}},
276
+ insert(value, {[{array, []}, junk], #config{}})
277
+ )},
278
+ {"finish an object with no ancestor", ?_assertEqual(
279
+ {[{a, b}, {x, y}], #config{}},
280
+ finish({[{object, [{x, y}, {a, b}]}], #config{}})
281
+ )},
282
+ {"finish an empty object", ?_assertEqual(
283
+ {[{}], #config{}},
284
+ finish({[{object, []}], #config{}})
285
+ )},
286
+ {"finish an object with an ancestor", ?_assertEqual(
287
+ {[{object, [{key, [{a, b}, {x, y}]}, {foo, bar}]}], #config{}},
288
+ finish({[{object, [{x, y}, {a, b}]}, {object, key, [{foo, bar}]}], #config{}})
289
+ )},
290
+ {"finish an array with no ancestor", ?_assertEqual(
291
+ {[a, b, c], #config{}},
292
+ finish({[{array, [c, b, a]}], #config{}})
293
+ )},
294
+ {"finish an array with an ancestor", ?_assertEqual(
295
+ {[{array, [[a, b, c], d, e, f]}], #config{}},
296
+ finish({[{array, [c, b, a]}, {array, [d, e, f]}], #config{}})
297
+ )}
298
+ ].
299
+
300
+
301
+ rep_manipulation_with_maps_test_() ->
302
+ [
303
+ {"allocate a new object on an empty stack", ?_assertEqual(
304
+ {[{object, #{}}], #config{return_maps=true}},
305
+ start_object({[], #config{return_maps=true}})
306
+ )},
307
+ {"allocate a new object on a stack", ?_assertEqual(
308
+ {[{object, #{}}, {object, #{}}], #config{return_maps=true}},
309
+ start_object({[{object, #{}}], #config{return_maps=true}})
310
+ )},
311
+ {"insert a key into an object", ?_assertEqual(
312
+ {[{object, key, #{}}, junk], #config{return_maps=true}},
313
+ insert(key, {[{object, #{}}, junk], #config{return_maps=true}})
314
+ )},
315
+ {"get current key", ?_assertEqual(
316
+ key,
317
+ get_key({[{object, key, #{}}], #config{return_maps=true}})
318
+ )},
319
+ {"try to get non-key from object", ?_assertError(
320
+ badarg,
321
+ get_key({[{object, #{}}], #config{return_maps=true}})
322
+ )},
323
+ {"insert a value into an object", ?_assertEqual(
324
+ {[{object, #{key => value}}, junk], #config{return_maps=true}},
325
+ insert(value, {[{object, key, #{}}, junk], #config{return_maps=true}})
326
+ )},
327
+ {"finish an object with no ancestor", ?_assertEqual(
328
+ {#{a => b, x => y}, #config{return_maps=true}},
329
+ finish({[{object, #{x => y, a => b}}], #config{return_maps=true}})
330
+ )},
331
+ {"finish an empty object", ?_assertEqual(
332
+ {#{}, #config{return_maps=true}},
333
+ finish({[{object, #{}}], #config{return_maps=true}})
334
+ )},
335
+ {"finish an object with an ancestor", ?_assertEqual(
336
+ {
337
+ [{object, #{key => #{a => b, x => y}, foo => bar}}],
338
+ #config{return_maps=true}
339
+ },
340
+ finish({
341
+ [{object, #{x => y, a => b}}, {object, key, #{foo => bar}}],
342
+ #config{return_maps=true}
343
+ })
344
+ )}
345
+ ].
346
+
347
+
348
+ return_maps_test_() ->
349
+ [
350
+ {"an empty map", ?_assertEqual(
351
+ #{},
352
+ jsx:decode(<<"{}">>, [])
353
+ )},
354
+ {"an empty map", ?_assertEqual(
355
+ #{},
356
+ jsx:decode(<<"{}">>, [])
357
+ )},
358
+ {"an empty map", ?_assertEqual(
359
+ [{}],
360
+ jsx:decode(<<"{}">>, [{return_maps, false}])
361
+ )},
362
+ {"a small map", ?_assertEqual(
363
+ #{<<"awesome">> => true, <<"library">> => <<"jsx">>},
364
+ jsx:decode(<<"{\"library\": \"jsx\", \"awesome\": true}">>, [])
365
+ )},
366
+ {"a recursive map", ?_assertEqual(
367
+ #{<<"key">> => #{<<"key">> => true}},
368
+ jsx:decode(<<"{\"key\": {\"key\": true}}">>, [])
369
+ )},
370
+ {"a map inside a list", ?_assertEqual(
371
+ [#{}],
372
+ jsx:decode(<<"[{}]">>, [])
373
+ )}
374
+ ].
375
+
376
+
377
+ handle_event_test_() ->
378
+ Data = jsx:test_cases(),
379
+ [
380
+ {
381
+ Title, ?_assertEqual(
382
+ Term,
383
+ lists:foldl(fun handle_event/2, init([]), Events ++ [end_json])
384
+ )
385
+ } || {Title, _, Term, Events} <- Data
386
+ ].
387
+
388
+
389
+ -endif.
@@ -0,0 +1,121 @@
1
+ %% The MIT License
2
+
3
+ %% Copyright (c) 2010-2013 alisdair sullivan <alisdairsullivan@yahoo.ca>
4
+
5
+ %% Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ %% of this software and associated documentation files (the "Software"), to deal
7
+ %% in the Software without restriction, including without limitation the rights
8
+ %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ %% copies of the Software, and to permit persons to whom the Software is
10
+ %% furnished to do so, subject to the following conditions:
11
+
12
+ %% The above copyright notice and this permission notice shall be included in
13
+ %% all copies or substantial portions of the Software.
14
+
15
+ %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ %% THE SOFTWARE.
22
+
23
+
24
+ -module(jsx_verify).
25
+
26
+ -export([is_json/2, is_term/2]).
27
+ -export([init/1, handle_event/2]).
28
+
29
+ -type config() :: proplists:proplist().
30
+
31
+ -spec is_json(Source::binary(), Config::jsx_config:options()) -> true | false | {incomplete, jsx:decoder()}.
32
+
33
+ is_json(Source, Config) when is_list(Config) ->
34
+ try (jsx:decoder(?MODULE, Config, jsx_config:extract_config(Config)))(Source)
35
+ catch error:badarg -> false
36
+ end.
37
+
38
+
39
+ -spec is_term(Source::jsx:json_term() | end_stream | end_json,
40
+ Config::jsx_config:options()) -> true | false | {incomplete, jsx:encoder()}.
41
+
42
+ is_term(Source, Config) when is_list(Config) ->
43
+ try (jsx:encoder(?MODULE, Config, jsx_config:extract_config(Config)))(Source)
44
+ catch error:badarg -> false
45
+ end.
46
+
47
+
48
+ parse_config(Config) -> parse_config(Config, []).
49
+
50
+ %% ignore deprecated flags
51
+ parse_config([no_repeated_keys|Rest], Config) ->
52
+ parse_config(Rest, Config);
53
+ parse_config([{repeated_keys, Val}|Rest], Config) when Val == true; Val == false ->
54
+ parse_config(Rest, Config);
55
+ parse_config([repeated_keys|Rest], Config) ->
56
+ parse_config(Rest, Config);
57
+ parse_config([{K, _}|Rest] = Options, Config) ->
58
+ case lists:member(K, jsx_config:valid_flags()) of
59
+ true -> parse_config(Rest, Config);
60
+ false -> erlang:error(badarg, [Options, Config])
61
+ end;
62
+ parse_config([K|Rest] = Options, Config) ->
63
+ case lists:member(K, jsx_config:valid_flags()) of
64
+ true -> parse_config(Rest, Config);
65
+ false -> erlang:error(badarg, [Options, Config])
66
+ end;
67
+ parse_config([], Config) ->
68
+ Config.
69
+
70
+
71
+ %% we don't actually need any state for this
72
+ -type state() :: [].
73
+ -spec init(Config::config()) -> state().
74
+
75
+ init(Config) -> parse_config(Config).
76
+
77
+
78
+ -spec handle_event(Event::any(), State::state()) -> state().
79
+
80
+ handle_event(end_json, _) -> true;
81
+
82
+ handle_event(_, State) -> State.
83
+
84
+
85
+
86
+ %% eunit tests
87
+ -ifdef(TEST).
88
+ -include_lib("eunit/include/eunit.hrl").
89
+
90
+
91
+ config_test_() ->
92
+ [
93
+ {"empty config", ?_assertEqual([], parse_config([]))},
94
+ {"no repeat keys", ?_assertEqual([], parse_config([no_repeated_keys]))},
95
+ {"bare repeated keys", ?_assertEqual([], parse_config([repeated_keys]))},
96
+ {"repeated keys true", ?_assertEqual(
97
+ [],
98
+ parse_config([{repeated_keys, true}])
99
+ )},
100
+ {"repeated keys false", ?_assertEqual(
101
+ [],
102
+ parse_config([{repeated_keys, false}])
103
+ )},
104
+ {"invalid opt flag", ?_assertError(badarg, parse_config([error]))},
105
+ {"invalid opt tuple", ?_assertError(badarg, parse_config([{error, true}]))}
106
+ ].
107
+
108
+
109
+ handle_event_test_() ->
110
+ Data = jsx:test_cases() ++ jsx:special_test_cases(),
111
+ [
112
+ {
113
+ Title, ?_assertEqual(
114
+ true,
115
+ lists:foldl(fun handle_event/2, [], Events ++ [end_json])
116
+ )
117
+ } || {Title, _, _, Events} <- Data
118
+ ].
119
+
120
+
121
+ -endif.