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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2005-2014. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
-module(send_term_SUITE).
-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1,
init_per_group/2,end_per_group/2,basic/1]).
-export([init_per_testcase/2,end_per_testcase/2]).
-export([generate_external_terms_files/1]).
-include_lib("test_server/include/test_server.hrl").
init_per_testcase(Func, Config) when is_atom(Func), is_list(Config) ->
Dog=?t:timetrap(?t:minutes(3)),
[{watchdog, Dog}|Config].
end_per_testcase(_Func, Config) ->
Dog=?config(watchdog, Config),
?t:timetrap_cancel(Dog).
suite() -> [{ct_hooks,[ts_install_cth]}].
all() ->
[basic].
groups() ->
[].
init_per_suite(Config) ->
Config.
end_per_suite(_Config) ->
ok.
init_per_group(_GroupName, Config) ->
Config.
end_per_group(_GroupName, Config) ->
Config.
basic(Config) when is_list(Config) ->
Drv = "send_term_drv",
?line P = start_driver(Config, Drv),
?line [] = term(P, 0),
?line Self = self(),
{blurf,42,[],[-42,{}|"abc"++P],"kalle",3.1416,Self,#{}} = term(P, 1),
Map41 = maps:from_list([{blurf, 42},
{[], [-42,{}|"abc"++P]},
{"kalle", 3.1416},
{Self, #{}}]),
Map41 = term(P, 41),
Map42 = maps:from_list([{42, []},
{[-42,{}|"abc"++P], "kalle"},
{3.1416, Self},
{#{}, blurf}]),
Map42 = term(P, 42),
?line Deep = lists:seq(0, 199),
?line Deep = term(P, 2),
?line {B1,B2} = term(P, 3),
?line B1 = list_to_binary(lists:seq(0, 255)),
?line B2 = list_to_binary(lists:seq(23, 255-17)),
%% Pid sending. We need another process.
?line Child = spawn_link(fun() ->
erlang:port_command(P, [4])
end),
?line {Self,Child} = receive_any(),
%% ERL_DRV_EXT2TERM
?line ExpectExt2Term = expected_ext2term_drv(?config(data_dir, Config)),
?line ExpectExt2Term = term(P, 5),
%% ERL_DRV_INT, ERL_DRV_UINT
?line case erlang:system_info({wordsize, external}) of
4 ->
?line {-1, 4294967295} = term(P, 6);
8 ->
?line {-1, 18446744073709551615} = term(P, 6)
end,
%% ERL_DRV_BUF2BINARY
?line ExpectedBinTup = {<<>>,
<<>>,
list_to_binary(lists:duplicate(17,17)),
list_to_binary(lists:duplicate(1024,17))},
?line ExpectedBinTup = term(P, 7),
%% single terms
Singles = [{[], 8}, % ERL_DRV_NIL
{'', 9}, % ERL_DRV_ATOM
{an_atom, 10}, % ERL_DRV_ATOM
{-4711, 11}, % ERL_DRV_INT
{4711, 12}, % ERL_DRV_UINT
{P, 13}, % ERL_DRV_PORT
{<<>>, 14}, % ERL_DRV_BINARY
{<<"hejsan">>, 15}, % ERL_DRV_BINARY
{<<>>, 16}, % ERL_DRV_BUF2BINARY
{<<>>, 17}, % ERL_DRV_BUF2BINARY
{<<"hoppsan">>, 18}, % ERL_DRV_BUF2BINARY
{"", 19}, % ERL_DRV_STRING
{"", 20}, % ERL_DRV_STRING
{"hippsan", 21}, % ERL_DRV_STRING
{{}, 22}, % ERL_DRV_TUPLE
{[], 23}, % ERL_DRV_LIST
{Self, 24}, % ERL_DRV_PID
{[], 25}, % ERL_DRV_STRING_CONS
{[], 27}, % ERL_DRV_EXT2TERM
{18446744073709551615, 28}, % ERL_DRV_UINT64
{20233590931456, 29}, % ERL_DRV_UINT64
{4711, 30}, % ERL_DRV_UINT64
{0, 31}, % ERL_DRV_UINT64
{9223372036854775807, 32}, % ERL_DRV_INT64
{20233590931456, 33}, % ERL_DRV_INT64
{4711, 34}, % ERL_DRV_INT64
{0, 35}, % ERL_DRV_INT64
{-1, 36}, % ERL_DRV_INT64
{-4711, 37}, % ERL_DRV_INT64
{-20233590931456, 38}, % ERL_DRV_INT64
{-9223372036854775808, 39},
{#{}, 40}], % ERL_DRV_MAP
?line {Terms, Ops} = lists:unzip(Singles),
?line Terms = term(P,Ops),
AFloat = term(P, 26), % ERL_DRV_FLOAT
?line true = AFloat < 0.001,
?line true = AFloat > -0.001,
%% Failure cases.
?line [] = term(P, 127),
?line receive
Any ->
?line io:format("Unexpected: ~p\n", [Any]),
?line ?t:fail()
after 0 ->
ok
end,
?line ok = chk_temp_alloc(),
%% In a private heap system, verify that there are no binaries
%% left for the process.
?line erlang:garbage_collect(), %Get rid of binaries.
case erlang:system_info(heap_type) of
private ->
?line {binary,[]} = process_info(self(), binary);
_ -> ok
end,
?line stop_driver(P, Drv),
ok.
term(P, Op) ->
erlang:port_command(P, [Op]),
receive_any().
receive_any() ->
receive
Any -> Any
end.
chk_temp_alloc() ->
case erlang:system_info({allocator,temp_alloc}) of
false ->
%% Temp alloc is not enabled
?line ok;
TIL ->
%% Verify that we havn't got anything allocated by temp_alloc
lists:foreach(
fun ({instance, _, TI}) ->
?line {value, {mbcs, MBCInfo}}
= lists:keysearch(mbcs, 1, TI),
?line {value, {blocks, 0, _, _}}
= lists:keysearch(blocks, 1, MBCInfo),
?line {value, {sbcs, SBCInfo}}
= lists:keysearch(sbcs, 1, TI),
?line {value, {blocks, 0, _, _}}
= lists:keysearch(blocks, 1, SBCInfo)
end,
TIL),
?line ok
end.
%% Start/stop drivers.
start_driver(Config, Name) ->
Path = ?config(data_dir, Config),
erl_ddll:start(),
ok = load_driver(Path, Name),
open_port({spawn, Name}, []).
load_driver(Dir, Driver) ->
case erl_ddll:load_driver(Dir, Driver) of
ok -> ok;
{error, Error} = Res ->
io:format("~s\n", [erl_ddll:format_error(Error)]),
Res
end.
stop_driver(Port, Name) ->
?line true = erlang:port_close(Port),
receive
{Port,Message} ->
?t:fail({strange_message_from_port,Message})
after 0 ->
ok
end,
%% Unload the driver.
ok = erl_ddll:unload_driver(Name),
?line ok = erl_ddll:stop().
get_external_terms(DataDir) ->
{ok, Bin} = file:read_file([DataDir, "ext_terms.bin"]),
binary_to_term(Bin).
expected_ext2term_drv(DataDir) ->
make_expected_ext2term_drv(get_external_terms(DataDir)).
make_expected_ext2term_drv([]) ->
[];
make_expected_ext2term_drv([T|Ts]) ->
[{T, T} | make_expected_ext2term_drv(Ts)].
%%
%% Generation of send_term_SUITE_data/ext_terms.h and
%% send_term_SUITE_data/ext_terms.bin
%%
%% These files should normally not need to be regenerated,
%% but we may want that if we introduce new types or make
%% backward incompatible changes to the external format.
%%
generate_external_terms_files(BaseDir) ->
{ok,Node} = slave:start(hostname(), a_node),
RPid = rpc:call(Node, erlang, self, []),
true = is_pid(RPid),
RRef = rpc:call(Node, erlang, make_ref, []),
true = is_reference(RRef),
RPort = hd(rpc:call(Node, erlang, ports, [])),
true = is_port(RPort),
slave:stop(Node),
Terms =
[{4711, -4711, [an_atom, "a list"]},
[1000000000000000000000,-1111111111111111, "blupp!", blipp],
{RPid, {RRef, RPort}, self(), hd(erlang:ports()), make_ref()},
{{}, [], [], fun () -> ok end, <<"hej hopp trallalaaaaaaaaaaaaaaa">>},
[44444444444444444444444,-44444444444, "b!", blippppppp],
{4711, RPid, {RRef, RPort}, -4711, [an_atom, "a list"]},
{RPid, {RRef, RPort}, hd(processes()), hd(erlang:ports())},
{4711, -4711, [an_atom, "a list"]},
{4711, -4711, [atom, "list"]},
{RPid, {RRef, RPort}, hd(processes()), hd(erlang:ports())},
{4444444444444444444,-44444, {{{{{{{{{{{{}}}}}}}}}}}}, make_ref()},
{444444444444444444444,-44444, [[[[[[[[[[[1]]]]]]]]]]], make_ref()},
{444444444444444444,-44444, {{{{{{{{{{{{2}}}}}}}}}}}}, make_ref()},
{4444444444444444444444,-44444, {{{{{{{{{{{{3}}}}}}}}}}}}, make_ref()},
{44444444444444444444,-44444, {{{{{{{{{{{{4}}}}}}}}}}}}, make_ref()},
{4444444444444444,-44444, [[[[[[[[[[[5]]]]]]]]]]], make_ref()},
{444444444444444444444,-44444, {{{{{{{{{{{{6}}}}}}}}}}}}, make_ref()},
{444444444444444,-44444, {{{{{{{{{{{{7}}}}}}}}}}}}, make_ref()},
{4444444444444444444,-44444, {{{{{{{{{{{{8}}}}}}}}}}}}, make_ref()}],
ok = file:write_file(filename:join([BaseDir,
"send_term_SUITE_data",
"ext_terms.bin"]),
term_to_binary(Terms, [compressed])),
{ok, IoDev} = file:open(filename:join([BaseDir,
"send_term_SUITE_data",
"ext_terms.h"]),
[write]),
write_ext_terms_h(IoDev, Terms),
file:close(IoDev).
write_ext_terms_h(IoDev, Terms) ->
write_license(IoDev),
io:format(IoDev, "#ifndef EXT_TERMS_H__~n",[]),
io:format(IoDev, "#define EXT_TERMS_H__~n",[]),
{ExtTerms, MaxSize} = make_ext_terms(Terms),
io:format(IoDev,
"static struct {~n"
" unsigned char ext[~p];~n"
" int ext_size;~n"
" unsigned char cext[~p];~n"
" int cext_size;~n"
"} ext_terms[] = {~n",[MaxSize, MaxSize]),
E = write_ext_terms_h(IoDev, ExtTerms, 0),
io:format(IoDev, "};~n",[]),
io:format(IoDev, "#define NO_OF_EXT_TERMS ~p~n", [E]),
io:format(IoDev, "#endif~n",[]).
make_ext_terms([]) ->
{[], 0};
make_ext_terms([T|Ts]) ->
E = term_to_binary(T),
ESz = size(E),
CE = term_to_binary(T, [compressed]),
CESz = size(CE),
true = CESz =< ESz, % Assertion
{ExtTerms, MaxSize} = make_ext_terms(Ts),
NewMaxSize = case MaxSize < ESz of
true -> ESz;
false -> MaxSize
end,
{[{E, ESz, CE, CESz} | ExtTerms], NewMaxSize}.
write_ext_terms_h(IoDev, [], N) ->
io:format(IoDev, "~n",[]),
N;
write_ext_terms_h(IoDev, [ET|ETs], 0) ->
write_ext_term(IoDev, ET),
write_ext_terms_h(IoDev, ETs, 1);
write_ext_terms_h(IoDev, [ET|ETs], N) ->
io:format(IoDev, ",~n",[]),
write_ext_term(IoDev, ET),
write_ext_terms_h(IoDev, ETs, N+1).
write_ext_term(IoDev, {E, ESz, CE, CESz}) ->
ESz = write_bytes(IoDev, " {{", binary_to_list(E), 0),
io:format(IoDev,
",~n"
" ~p,~n",
[ESz]),
CESz = write_bytes(IoDev, " {", binary_to_list(CE), 0),
io:format(IoDev,
",~n"
" ~p}",
[CESz]).
write_bytes(IoDev, _, [], N) ->
io:format(IoDev, "}",[]),
N;
write_bytes(IoDev, Prefix, [B|Bs], N) ->
io:format(IoDev, "~s~w", [Prefix, B]),
write_bytes(IoDev, ",", Bs, N+1).
write_license(IoDev) ->
S = "/* ``The contents of this file are subject to the Erlang Public License,~n"
" * Version 1.1, (the \"License\"); you may not use this file except in~n"
" * compliance with the License. You should have received a copy of the~n"
" * Erlang Public License along with this software. If not, it can be~n"
" * retrieved via the world wide web at http://www.erlang.org/.~n"
" * ~n"
" * Software distributed under the License is distributed on an \"AS IS\"~n"
" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See~n"
" * the License for the specific language governing rights and limitations~n"
" * under the License.~n"
" * ~n"
" * The Initial Developer of the Original Code is Ericsson AB.~n"
" * Portions created by Ericsson are Copyright 2007, Ericsson AB.~n"
" * All Rights Reserved.''~n"
" * ~n"
" * $Id$~n"
" */~n"
"~n"
"/*~n"
" * Do not modify this file. This file and ext_terms.bin were~n"
" * automatically generated by send_term_SUITE:generate_external_terms_files/1~n"
" * and needs to be consistent with each other.~n"
" */~n",
io:format(IoDev, S, []).
hostname() ->
hostname(atom_to_list(node())).
hostname([$@ | Hostname]) ->
list_to_atom(Hostname);
hostname([_C | Cs]) ->
hostname(Cs).
|