~ubuntu-branches/ubuntu/trusty/erlang/trusty

« back to all changes in this revision

Viewing changes to lib/erl_interface/test/runner.erl

  • Committer: Bazaar Package Importer
  • Author(s): Clint Byrum
  • Date: 2011-05-05 15:48:43 UTC
  • mfrom: (3.5.13 sid)
  • Revision ID: james.westby@ubuntu.com-20110505154843-0om6ekzg6m7ugj27
Tags: 1:14.b.2-dfsg-3ubuntu1
* Merge from debian unstable.  Remaining changes:
  - Drop libwxgtk2.8-dev build dependency. Wx isn't in main, and not
    supposed to.
  - Drop erlang-wx binary.
  - Drop erlang-wx dependency from -megaco, -common-test, and -reltool, they
    do not really need wx. Also drop it from -debugger; the GUI needs wx,
    but it apparently has CLI bits as well, and is also needed by -megaco,
    so let's keep the package for now.
  - debian/patches/series: Do what I meant, and enable build-options.patch
    instead.
* Additional changes:
  - Drop erlang-wx from -et
* Dropped Changes:
  - patches/pcre-crash.patch: CVE-2008-2371: outer level option with
    alternatives caused crash. (Applied Upstream)
  - fix for ssl certificate verification in newSSL: 
    ssl_cacertfile_fix.patch (Applied Upstream)
  - debian/patches/series: Enable native.patch again, to get stripped beam
    files and reduce the package size again. (build-options is what
    actually accomplished this)
  - Remove build-options.patch on advice from upstream and because it caused
    odd build failures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
%%
 
2
%% %CopyrightBegin%
 
3
%% 
 
4
%% Copyright Ericsson AB 1997-2010. All Rights Reserved.
 
5
%% 
 
6
%% The contents of this file are subject to the Erlang Public License,
 
7
%% Version 1.1, (the "License"); you may not use this file except in
 
8
%% compliance with the License. You should have received a copy of the
 
9
%% Erlang Public License along with this software. If not, it can be
 
10
%% retrieved online at http://www.erlang.org/.
 
11
%% 
 
12
%% Software distributed under the License is distributed on an "AS IS"
 
13
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 
14
%% the License for the specific language governing rights and limitations
 
15
%% under the License.
 
16
%% 
 
17
%% %CopyrightEnd%
 
18
%%
 
19
 
 
20
%%
 
21
-module(runner).
 
22
 
 
23
-export([test/1, test/2,
 
24
         start/1, send_term/2, finish/1, send_eot/1, recv_eot/1,
 
25
         get_term/1, get_term/2]).
 
26
 
 
27
-define(default_timeout, test_server:seconds(5)).
 
28
 
 
29
%% Executes a test case in a C program.
 
30
%%
 
31
%% This function is useful for test cases written in C which requires
 
32
%% no further input, and only returns a result by calling report().
 
33
 
 
34
test(Tc) ->
 
35
    test(Tc, ?default_timeout).
 
36
 
 
37
test(Tc, Timeout) ->
 
38
    Port = start(Tc),
 
39
    
 
40
    case get_term(Port, Timeout) of
 
41
        eot ->
 
42
            ok;
 
43
        Other ->
 
44
            io:format("In this test case, a success/failure result was"),
 
45
            io:format("expected from the C program.\n"),
 
46
            io:format("Received: ~p", [Other]),
 
47
            test_server:fail()
 
48
    end.
 
49
 
 
50
%% Executes a test case in a C program.  Returns the port.
 
51
%%
 
52
%% Use get_term/1,2.
 
53
%%
 
54
%% Returns: {ok, Port}
 
55
 
 
56
start({Prog, Tc}) when is_list(Prog), is_integer(Tc) ->
 
57
    Port = open_port({spawn, Prog}, [{packet, 4}]),
 
58
    Command = [Tc div 256, Tc rem 256],
 
59
    Port ! {self(), {command, Command}},
 
60
    Port.
 
61
 
 
62
%% Finishes a test case by send an 'eot' message to the C program
 
63
%% and waiting for an 'eot'.
 
64
%%
 
65
%% If the C program doesn't require an 'eot', use recv_eot/1 instead.
 
66
 
 
67
finish(Port) when is_port(Port) ->
 
68
    send_eot(Port),
 
69
    recv_eot(Port).
 
70
 
 
71
%% Sends an Erlang term to a C program.
 
72
 
 
73
send_term(Port, Term) when is_port(Port) ->
 
74
    Port ! {self(), {command, [$t, term_to_binary(Term)]}}.
 
75
 
 
76
%% Sends an 'eot' (end-of-test) indication to a C progrm.
 
77
 
 
78
send_eot(Port) when is_port(Port) ->
 
79
    Port ! {self(), {command, [$e]}}.
 
80
 
 
81
%% Waits for an 'eot' indication from the C program.
 
82
%% Either returns 'ok' or invokes test_server:fail().
 
83
 
 
84
recv_eot(Port) when is_port(Port) ->    
 
85
    case get_term(Port) of
 
86
        eot ->
 
87
            ok;
 
88
        Other ->
 
89
            io:format("Error finishing test case.  Expected eof from"),
 
90
            io:format("C program, but got:"),
 
91
            io:format("~p", [Other]),
 
92
            test_server:fail()
 
93
    end.
 
94
 
 
95
%% Reads a term from the C program.
 
96
%%
 
97
%% Returns: {term, Term}|eot|'NULL' or calls test_server:fail/1,2.
 
98
 
 
99
get_term(Port) ->
 
100
    get_term(Port, ?default_timeout).
 
101
 
 
102
get_term(Port, Timeout) ->
 
103
    case get_reply(Port, Timeout) of
 
104
        [$b|Bytes] ->
 
105
            {bytes, Bytes};
 
106
        [$f] ->
 
107
            test_server:fail();
 
108
        [$f|Reason] ->
 
109
            test_server:fail(Reason);
 
110
        [$t|Term] ->
 
111
            {term, binary_to_term(list_to_binary(Term))};
 
112
        [$N] ->
 
113
            'NULL';
 
114
        [$e] ->
 
115
            eot;
 
116
        [$m|Message] ->
 
117
            io:format("~s", [Message]),
 
118
            get_term(Port, Timeout);
 
119
        Other ->
 
120
            io:format("Garbage received from C program: ~p", [Other]),
 
121
            test_server:fail("Illegal response from C program")
 
122
    end.
 
123
 
 
124
get_reply(Port, Timeout) when is_port(Port) ->
 
125
    receive
 
126
        {Port, {data, Reply}} ->
 
127
            Reply
 
128
    after Timeout ->
 
129
            test_server:fail("No response from C program")
 
130
    end.