Merge remote-tracking branch 'origin/topic/dnthayer/bif-tests'

* origin/topic/dnthayer/bif-tests:
  Improve "fmt" BIF documentation comment
  Improve tests of the type_name BIF
  Improve test cases for "order" BIF
  Fix documentation of sort BIF and add more tests
  Fix documentation for system_env BIF
  Deprecate the parse_dotted_addr BIF (use to_addr instead)
  Improve tests for to_port and type_name BIFs
  Improve tests for sort, order, and system_env BIFs
  Fix the join_string_vec BIF and add more tests
  Add more tests for previously-untested BIFs
  Add more tests for previously-untested BIFs
  Add more tests for previously-untested BIFs
  Add more tests for previously-untested BIFs
  Add tests for previously-untested strings BIFs
This commit is contained in:
Robin Sommer 2012-06-06 11:41:00 -07:00
commit 73cefcc11c
135 changed files with 1707 additions and 68 deletions

View file

@ -1,4 +1,10 @@
2.0-596 | 2012-06-06 11:41:00 -0700
* Fixes for some BiFs and their documentation. (Daniel Thayer)
* Many new unit tests for BiFs. (Daniel Thayer)
2.0-579 | 2012-06-06 11:04:46 -0700 2.0-579 | 2012-06-06 11:04:46 -0700
* Memory leak fixes for bad usages of VectorVal ctor. (Jon Siwek) * Memory leak fixes for bad usages of VectorVal ctor. (Jon Siwek)

View file

@ -1 +1 @@
2.0-579 2.0-596

View file

@ -1467,6 +1467,16 @@ bool VectorType::DoUnserialize(UnserialInfo* info)
return yield_type != 0; return yield_type != 0;
} }
void VectorType::Describe(ODesc* d) const
{
if ( d->IsReadable() )
d->AddSP("vector of");
else
d->Add(int(Tag()));
yield_type->Describe(d);
}
BroType* base_type(TypeTag tag) BroType* base_type(TypeTag tag)
{ {
static BroType* base_types[NUM_TYPES]; static BroType* base_types[NUM_TYPES];

View file

@ -564,6 +564,8 @@ public:
// gets using an empty "vector()" constructor. // gets using an empty "vector()" constructor.
bool IsUnspecifiedVector() const; bool IsUnspecifiedVector() const;
void Describe(ODesc* d) const;
protected: protected:
VectorType() { yield_type = 0; } VectorType() { yield_type = 0; }

View file

@ -466,17 +466,18 @@ function system%(str: string%): int
## ##
## str: The command to execute. ## str: The command to execute.
## ##
## env: A :bro:type:`set` or :bro:type:`table` with the environment variables ## env: A :bro:type:`table` with the environment variables in the form
## in the form of key-value pairs (where the value is optional). ## of key-value pairs. Each specified environment variable name
## will be automatically prepended with ``BRO_ARG_``.
## ##
## Returns: The return value from the OS ``system`` function. ## Returns: The return value from the OS ``system`` function.
## ##
## .. bro:see:: system str_shell_escape piped_exec ## .. bro:see:: system str_shell_escape piped_exec
function system_env%(str: string, env: any%): int function system_env%(str: string, env: table_string_of_string%): int
%{ %{
if ( env->Type()->Tag() != TYPE_TABLE ) if ( env->Type()->Tag() != TYPE_TABLE )
{ {
builtin_error("system_env() requires a table/set argument"); builtin_error("system_env() requires a table argument");
return new Val(-1, TYPE_INT); return new Val(-1, TYPE_INT);
} }
@ -1422,12 +1423,15 @@ bool indirect_int_sort_function(int a, int b)
## Sorts a vector in place. The second argument is a comparison function that ## Sorts a vector in place. The second argument is a comparison function that
## takes two arguments: if the vector type is ``vector of T``, then the ## takes two arguments: if the vector type is ``vector of T``, then the
## comparison function must be ``function(a: T, b: T): bool``, which returns ## comparison function must be ``function(a: T, b: T): int``, which returns
## ``a < b`` for some type-specific notion of the less-than operator. ## a value less than zero if ``a < b`` for some type-specific notion of the
## less-than operator. The comparison function is optional if the type
## is an integral type (int, count, etc.).
## ##
## v: The vector instance to sort. ## v: The vector instance to sort.
## ##
## Returns: The original vector. ## Returns: The vector, sorted from minimum to maximum value. If the vector
## could not be sorted, then the original vector is returned instead.
## ##
## .. bro:see:: order ## .. bro:see:: order
function sort%(v: any, ...%) : any function sort%(v: any, ...%) : any
@ -1522,13 +1526,13 @@ function order%(v: any, ...%) : index_vec
} }
if ( ! comp && ! IsIntegral(elt_type->Tag()) ) if ( ! comp && ! IsIntegral(elt_type->Tag()) )
builtin_error("comparison function required for sort() with non-integral types"); builtin_error("comparison function required for order() with non-integral types");
vector<Val*>& vv = *v->AsVector(); vector<Val*>& vv = *v->AsVector();
int n = vv.size(); int n = vv.size();
// Set up initial mapping of indices directly to corresponding // Set up initial mapping of indices directly to corresponding
// elements. We stay zero-based until after the sorting. // elements.
vector<int> ind_vv(n); vector<int> ind_vv(n);
index_map = new Val*[n]; index_map = new Val*[n];
int i; int i;
@ -1544,7 +1548,7 @@ function order%(v: any, ...%) : index_vec
if ( comp_type->YieldType()->Tag() != TYPE_INT || if ( comp_type->YieldType()->Tag() != TYPE_INT ||
! comp_type->ArgTypes()->AllMatch(elt_type, 0) ) ! comp_type->ArgTypes()->AllMatch(elt_type, 0) )
{ {
builtin_error("invalid comparison function in call to sort()"); builtin_error("invalid comparison function in call to order()");
return v; return v;
} }
@ -1558,8 +1562,7 @@ function order%(v: any, ...%) : index_vec
delete [] index_map; delete [] index_map;
index_map = 0; index_map = 0;
// Now spin through ind_vv to read out the rearrangement, // Now spin through ind_vv to read out the rearrangement.
// adjusting indices as we do so.
for ( i = 0; i < n; ++i ) for ( i = 0; i < n; ++i )
{ {
int ind = ind_vv[i]; int ind = ind_vv[i];
@ -1649,7 +1652,7 @@ function cat_sep%(sep: string, def: string, ...%): string
## ##
## - ``.``: Precision of floating point specifiers ``[efg]`` (< 128) ## - ``.``: Precision of floating point specifiers ``[efg]`` (< 128)
## ##
## - ``A``: Escape NUL bytes, i.e., replace ``0`` with ``\0`` ## - ``A``: Escape only NUL bytes (each one replaced with ``\0``) in a string
## ##
## - ``[DTdxsefg]``: Format specifier ## - ``[DTdxsefg]``: Format specifier
## ##
@ -1661,15 +1664,14 @@ function cat_sep%(sep: string, def: string, ...%): string
## - ``x``: Unsigned hexadecimal (using C-style ``%llx``); ## - ``x``: Unsigned hexadecimal (using C-style ``%llx``);
## addresses/ports are converted to host-byte order ## addresses/ports are converted to host-byte order
## ##
## - ``s``: Escaped string ## - ``s``: String (byte values less than 32 or greater than 126
## will be escaped)
## ##
## - ``[efg]``: Double ## - ``[efg]``: Double
## ##
## Returns: Given no arguments, :bro:id:`fmt` returns an empty string. Given a ## Returns: Returns the formatted string. Given no arguments, :bro:id:`fmt`. Given
## non-string first argument, :bro:id:`fmt` returns the concatenation ## no format string or the wrong number of additional arguments for the
## of all its arguments, per :bro:id:`cat`. Finally, given the wrong ## given format specifier, :bro:id:`fmt` generates a run-time error.
## number of additional arguments for the given format specifier,
## :bro:id:`fmt` generates a run-time error.
## ##
## .. bro:see:: cat cat_sep string_cat cat_string_array cat_string_array_n ## .. bro:see:: cat cat_sep string_cat cat_string_array cat_string_array_n
function fmt%(...%): string function fmt%(...%): string
@ -1678,8 +1680,9 @@ function fmt%(...%): string
return new StringVal(""); return new StringVal("");
Val* fmt_v = @ARG@[0]; Val* fmt_v = @ARG@[0];
if ( fmt_v->Type()->Tag() != TYPE_STRING )
return bro_cat(frame, @ARGS@); // Type of fmt_v will be string here, check_built_in_call() in Func.cc
// checks that.
const char* fmt = fmt_v->AsString()->CheckString(); const char* fmt = fmt_v->AsString()->CheckString();
ODesc d; ODesc d;
@ -1689,10 +1692,16 @@ function fmt%(...%): string
; ;
if ( n < @ARGC@ - 1 ) if ( n < @ARGC@ - 1 )
{
builtin_error("too many arguments for format", fmt_v); builtin_error("too many arguments for format", fmt_v);
return new StringVal("");
}
else if ( n >= @ARGC@ ) else if ( n >= @ARGC@ )
{
builtin_error("too few arguments for format", fmt_v); builtin_error("too few arguments for format", fmt_v);
return new StringVal("");
}
BroString* s = new BroString(1, d.TakeBytes(), d.Len()); BroString* s = new BroString(1, d.TakeBytes(), d.Len());
s->SetUseFreeToDelete(true); s->SetUseFreeToDelete(true);
@ -2670,7 +2679,7 @@ function to_port%(s: string%): port
## ##
## Returns: The IP address corresponding to *s*. ## Returns: The IP address corresponding to *s*.
## ##
## .. bro:see:: addr_to_ptr_name parse_dotted_addr ## .. bro:see:: addr_to_ptr_name to_addr
function ptr_name_to_addr%(s: string%): addr function ptr_name_to_addr%(s: string%): addr
%{ %{
if ( s->Len() != 72 ) if ( s->Len() != 72 )
@ -2734,27 +2743,12 @@ function ptr_name_to_addr%(s: string%): addr
## ##
## Returns: The reverse pointer representation of *a*. ## Returns: The reverse pointer representation of *a*.
## ##
## .. bro:see:: ptr_name_to_addr parse_dotted_addr ## .. bro:see:: ptr_name_to_addr to_addr
function addr_to_ptr_name%(a: addr%): string function addr_to_ptr_name%(a: addr%): string
%{ %{
return new StringVal(a->AsAddr().PtrName().c_str()); return new StringVal(a->AsAddr().PtrName().c_str());
%} %}
# Transforms n0.n1.n2.n3 -> addr.
## Converts a decimal dotted IP address in a :bro:type:`string` to an
## :bro:type:`addr` type.
##
## s: The IP address in the form ``n0.n1.n2.n3``.
##
## Returns: The IP address as type :bro:type:`addr`.
##
## .. bro:see:: addr_to_ptr_name parse_dotted_addr
function parse_dotted_addr%(s: string%): addr
%{
IPAddr a(s->CheckString());
return new AddrVal(a);
%}
%%{ %%{
static Val* parse_port(const char* line) static Val* parse_port(const char* line)
@ -5659,6 +5653,14 @@ function match_signatures%(c: connection, pattern_type: int, s: string,
# #
# =========================================================================== # ===========================================================================
## Deprecated. Will be removed.
function parse_dotted_addr%(s: string%): addr
%{
IPAddr a(s->CheckString());
return new AddrVal(a);
%}
%%{ %%{
#include "Anon.h" #include "Anon.h"
%%} %%}

View file

@ -175,7 +175,7 @@ function join_string_vec%(vec: string_vec, sep: string%): string
if ( i > 0 ) if ( i > 0 )
d.Add(sep->CheckString(), 0); d.Add(sep->CheckString(), 0);
v->Lookup(i+1)->Describe(&d); v->Lookup(i)->Describe(&d);
} }
BroString* s = new BroString(1, d.TakeBytes(), d.Len()); BroString* s = new BroString(1, d.TakeBytes(), d.Len());

View file

@ -0,0 +1,3 @@
F
F
T

View file

@ -0,0 +1,3 @@
T
F
F

View file

@ -0,0 +1 @@
11

View file

@ -0,0 +1,3 @@
3034
00

View file

@ -0,0 +1,6 @@
foo3T
3T
foo|3|T
<empty>|3|T

View file

@ -0,0 +1,3 @@
isatest
thisisatest
isa

View file

@ -0,0 +1,2 @@
1
0

View file

@ -0,0 +1,3 @@
foo
b\[a\-z\]\+

View file

@ -0,0 +1,15 @@
T
testfile
F
15.0
T
F
28.0
-1.0
15.0
0.0
T
15.0
T
testdir/testfile4
F

View file

@ -0,0 +1,2 @@
This is a test
another test

View file

@ -0,0 +1 @@
new text

View file

@ -0,0 +1 @@
llo t

View file

@ -0,0 +1,10 @@
12
Test \0string
13
Test \0string
15
Test \x00string
13
Test \0string
24
546573742000737472696e67

View file

@ -0,0 +1 @@
hello

View file

@ -0,0 +1,10 @@
rw-r--r--
rwxrwxrwx
rwxrwxrwt
rwxr-x--T
rwsr-xr-x
r-S------
rwxr-sr-x
r--r-S---
--xr-xrwx
---------

View file

@ -0,0 +1,4 @@
es
hi
-------------------
0

View file

@ -0,0 +1,2 @@
[entropy=4.715374, chi_square=591.981818, mean=75.472727, monte_carlo_pi=4.0, serial_correlation=-0.11027]
[entropy=2.083189, chi_square=3906.018182, mean=69.054545, monte_carlo_pi=4.0, serial_correlation=0.849402]

View file

@ -0,0 +1,3 @@
es
-------------------
0

View file

@ -0,0 +1,55 @@
test
%
*test *
* test*
* T*
*T *
* 3.14e+00*
*3.14e+00 *
* 3.14*
* 3.1*
* -3.14e+00*
* -3.14*
* -3.1*
*-3.14e+00 *
*-3.14 *
*-3.1 *
* -128*
*-128 *
* 128*
*0000000128*
*128 *
* a0*
*00000000a0*
* a0*
* 160/tcp*
* 127.0.0.1*
* 7f000001*
*192.168.0.0/16*
* ::1*
*fe000000000000000000000000000001*
*fe80:1234::1*
*fe80:1234::/32*
* 3.0 hrs*
*/^?(^foo|bar)$?/*
* Blue*
* [1, 2, 3]*
*{^J^I2,^J^I1,^J^I3^J}*
*{^J^I[2] = bro,^J^I[1] = test^J}*
3.100000e+02
310.000000
310
3.100e+02
310.000
310
310
2
3
4
2
2
6
2
2
6

View file

@ -0,0 +1,2 @@
192,168,0,2,1,1

View file

@ -0,0 +1,3 @@
tcp
udp
icmp

View file

@ -0,0 +1,3 @@
OK
OK
OK

View file

@ -0,0 +1 @@
func

View file

@ -0,0 +1 @@
0000 61 62 63 ff 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f abc.defg hijklmno^J0010 70 71 72 73 74 75 76 77 78 79 7a pqrstuvw xyz^J

View file

@ -0,0 +1,2 @@
F
T

View file

@ -0,0 +1,9 @@
T
F
F
F
T
F
F
F
T

View file

@ -0,0 +1,6 @@
this * is * a * test
thisisatest
mytest
this__is__another__test
thisisanothertest
Test

View file

@ -0,0 +1,6 @@
1
4
2
0
0
0

View file

@ -0,0 +1,5 @@
bro test
<unknown id>
<unknown id>
<unknown id>
event()

View file

@ -0,0 +1,2 @@
this is a test
THIS IS A TEST

View file

@ -0,0 +1,8 @@
3.0
2.0
-4.0
-3.0
1.772005
23.103867
1.144223
0.49693

View file

@ -2,3 +2,5 @@ f97c5d29941bfb1b2fdab0874906ab82
7b0391feb2e0cd271f1cf39aafb4376f 7b0391feb2e0cd271f1cf39aafb4376f
f97c5d29941bfb1b2fdab0874906ab82 f97c5d29941bfb1b2fdab0874906ab82
7b0391feb2e0cd271f1cf39aafb4376f 7b0391feb2e0cd271f1cf39aafb4376f
571c0a35c7858ad5a0e16b8fdb41adcd
1751cbd623726f423f734e23a8c7ec06

View file

@ -0,0 +1,2 @@
match
match

View file

@ -0,0 +1,8 @@
[5, 2, 8, 3]
[1, 3, 0, 2]
[5.0 hrs, 2.0 days, 1.0 sec, -7.0 mins]
[3, 2, 0, 1]
[192.168.123.200, 10.0.0.157, 192.168.0.3]
[1, 2, 0]
[3.03, 3.01, 3.02, 3.015]
[1, 3, 2, 0]

View file

@ -0,0 +1,5 @@
[h=192.168.0.2, p=257/tcp, valid=T]
[h=192.168.0.2, p=257/tcp, valid=T]
[h=fe80::12, p=1234/tcp, valid=T]
[h=192.168.0.2, p=257/tcp, valid=T]
[h=::, p=1234/tcp, valid=T]

View file

@ -0,0 +1,6 @@
185
236
805
47
996
498

View file

@ -0,0 +1,2 @@
65.66.67.68
0.0.0.0

View file

@ -0,0 +1 @@
[, ct, str1]

View file

@ -0,0 +1,4 @@
3
5
0
7

View file

@ -0,0 +1,3 @@
file rotated
15.0
0.0

View file

@ -0,0 +1,3 @@
file rotated
15.0
0.0

View file

@ -0,0 +1,3 @@
T
F
F

View file

@ -0,0 +1,16 @@
[2, 3, 5, 8]
[2, 3, 5, 8]
[-7.0 mins, 1.0 sec, 5.0 hrs, 2.0 days]
[-7.0 mins, 1.0 sec, 5.0 hrs, 2.0 days]
[F, F, T, T]
[F, F, T, T]
[57/tcp, 123/tcp, 7/udp, 500/udp, 12/icmp]
[57/tcp, 123/tcp, 7/udp, 500/udp, 12/icmp]
[3.03, 3.01, 3.02, 3.015]
[3.03, 3.01, 3.02, 3.015]
[192.168.123.200, 10.0.0.157, 192.168.0.3]
[192.168.123.200, 10.0.0.157, 192.168.0.3]
[10.0.0.157, 192.168.0.3, 192.168.123.200]
[10.0.0.157, 192.168.0.3, 192.168.123.200]
[3.01, 3.015, 3.02, 3.03]
[3.01, 3.015, 3.02, 3.03]

View file

@ -0,0 +1,4 @@
a
is
test
this

View file

@ -0,0 +1,32 @@
t
s is a t
t
---------------------
t
s is a test
---------------------
t
hi
s is a t
es
t
---------------------
t
s is a test
---------------------
t
hi
s is a test
---------------------
[, thi, s i, s a tes, t]
---------------------
X-Mailer
Testing Test (http://www.example.com)
---------------------
A
=
B
=
C
=
D

View file

@ -0,0 +1,4 @@
24
echo ${TEST} > "my file"
27
echo \${TEST} > \"my file\"

View file

@ -0,0 +1,3 @@
T
T
T

View file

@ -0,0 +1,3 @@
*\0* 1
*t\0* 2
*test test\0* 10

View file

@ -1,13 +0,0 @@
{
[2] = Testing Test (http://www.example.com),
[1] = X-Mailer
}
{
[2] = =,
[4] = =,
[6] = =,
[7] = D,
[1] = A ,
[5] = C ,
[3] = B
}

View file

@ -0,0 +1,6 @@
/^?(foo)$?/
/^?()$?/
/^?(b[a-z]+)$?/
/^?(foo)$?/
/^?()$?/
/^?(b\[a\-z\]\+)$?/

View file

@ -0,0 +1,6 @@
* this is a test *
*this is a test*
**
**
* *
**

View file

@ -0,0 +1,2 @@
2
0

View file

@ -0,0 +1,2 @@
that is a test
that at a test

View file

@ -0,0 +1 @@
that at another test

View file

@ -0,0 +1 @@
thistest

View file

@ -0,0 +1 @@
helloworld

View file

@ -0,0 +1,9 @@
0
2
3
4
7
0
18446744073709551611
0
123

View file

@ -0,0 +1,6 @@
0.000001
1.0
-60.0
3600.0
86400.0
1337982322.762159

View file

@ -0,0 +1,3 @@
1
-1
0

View file

@ -0,0 +1,2 @@
1234563.14
-1234563.14

View file

@ -0,0 +1,7 @@
123/tcp
123/udp
123/icmp
0/unknown
256/tcp
256/udp
256/icmp

View file

@ -0,0 +1,2 @@
1234563.14
-1234563.14

View file

@ -0,0 +1,26 @@
string
count
int
double
bool
time
interval
pattern
enum
port
addr
addr
subnet
subnet
vector of count
vector of table[count] of string
set[count]
set[port,string]
table[count] of string
table[string] of table[addr,port] of string
record { c:count; s:string; }
function(aa:int; bb:int;) : bool
function() : any
function() : void
file of string
event()

View file

@ -0,0 +1,2 @@
626180fe-6463-6665-6730-313233343536
<Invalid UUID>

View file

@ -0,0 +1,2 @@
72
72

View file

@ -0,0 +1,15 @@
#
# @TEST-EXEC: bro %INPUT >out
# @TEST-EXEC: btest-diff out
event bro_init()
{
local a = vector( T, F, T );
print all_set(a);
local b = vector();
print all_set(b);
local c = vector( T );
print all_set(c);
}

View file

@ -0,0 +1,15 @@
#
# @TEST-EXEC: bro %INPUT >out
# @TEST-EXEC: btest-diff out
event bro_init()
{
local a = vector( F, T, F );
print any_set(a);
local b = vector();
print any_set(b);
local c = vector( F );
print any_set(c);
}

View file

@ -0,0 +1,10 @@
#
# @TEST-EXEC: bro %INPUT >out
# @TEST-EXEC: btest-diff out
event bro_init()
{
local a = "hello\0there";
print byte_len(a);
}

View file

@ -0,0 +1,10 @@
#
# @TEST-EXEC: bro %INPUT >out
# @TEST-EXEC: btest-diff out
event bro_init()
{
print bytestring_to_hexstr("04");
print bytestring_to_hexstr("");
print bytestring_to_hexstr("\0");
}

View file

@ -0,0 +1,22 @@
#
# @TEST-EXEC: bro %INPUT >out
# @TEST-EXEC: btest-diff out
event bro_init()
{
local a = "foo";
local b = 3;
local c = T;
print cat(a, b, c);
print cat();
print cat("", 3, T);
print cat_sep("|", "<empty>", a, b, c);
print cat_sep("|", "<empty>");
print cat_sep("|", "<empty>", "", b, c);
}

View file

@ -0,0 +1,14 @@
#
# @TEST-EXEC: bro %INPUT >out
# @TEST-EXEC: btest-diff out
event bro_init()
{
local a: string_array = {
[0] = "this", [1] = "is", [2] = "a", [3] = "test"
};
print cat_string_array(a);
print cat_string_array_n(a, 0, |a|-1);
print cat_string_array_n(a, 1, 2);
}

View file

@ -0,0 +1,14 @@
#
# @TEST-EXEC: bro %INPUT > out
# @TEST-EXEC: btest-diff out
event bro_init()
{
local mytable: table[string] of string = { ["key1"] = "val1" };
print |mytable|;
clear_table(mytable);
print |mytable|;
}

View file

@ -0,0 +1,10 @@
#
# @TEST-EXEC: bro %INPUT >out
# @TEST-EXEC: btest-diff out
event bro_init()
{
print convert_for_pattern("foo");
print convert_for_pattern("");
print convert_for_pattern("b[a-z]+");
}

View file

@ -0,0 +1,65 @@
#
# @TEST-EXEC: bro %INPUT >out
# @TEST-EXEC: btest-diff out
# @TEST-EXEC: btest-diff testfile
# @TEST-EXEC: btest-diff testfile2
# @TEST-EXEC: test -f testdir/testfile4
event bro_init()
{
# Test that creating a file works as expected
local a = open("testfile");
print active_file(a);
print get_file_name(a);
write_file(a, "This is a test\n");
close(a);
print active_file(a);
print file_size("testfile");
# Test that "open_for_append" doesn't overwrite an existing file
a = open_for_append("testfile");
print active_file(a);
write_file(a, "another test\n");
close(a);
print active_file(a);
print file_size("testfile");
# This should fail
print file_size("doesnotexist");
# Test that "open" overwrites existing file
a = open("testfile2");
write_file(a, "this will be overwritten\n");
close(a);
a = open("testfile2");
write_file(a, "new text\n");
close(a);
# Test that set_buf and flush_all work correctly
a = open("testfile3");
set_buf(a, F);
write_file(a, "This is a test\n");
print file_size("testfile3");
close(a);
a = open("testfile3");
set_buf(a, T);
write_file(a, "This is a test\n");
print file_size("testfile3");
print flush_all();
print file_size("testfile3");
close(a);
# Create a new directory
print mkdir("testdir");
# Create a file in the new directory
a = open("testdir/testfile4");
print get_file_name(a);
write_file(a, "This is a test\n");
close(a);
# This should fail
print mkdir("/thisdoesnotexist/dir");
}

View file

@ -0,0 +1,10 @@
#
# @TEST-EXEC: bro %INPUT >out
# @TEST-EXEC: btest-diff out
event bro_init()
{
local a = "hello there";
print edit(a, "e");
}

View file

@ -0,0 +1,27 @@
#
# @TEST-EXEC: bro %INPUT >out
# @TEST-EXEC: btest-diff out
event bro_init()
{
local a = "Test \0string";
print |a|;
print a;
local b = clean(a);
print |b|;
print b;
local c = to_string_literal(a);
print |c|;
print c;
local d = escape_string(a);
print |d|;
print d;
local e = string_to_ascii_hex(a);
print |e|;
print e;
}

View file

@ -0,0 +1,9 @@
#
# @TEST-EXEC: bro %INPUT >out || test $? -eq 7
# @TEST-EXEC: btest-diff out
event bro_init()
{
print "hello";
exit(7);
}

View file

@ -0,0 +1,36 @@
#
# @TEST-EXEC: bro %INPUT >out
# @TEST-EXEC: btest-diff out
event bro_init()
{
local a = 420; # octal: 0644
print file_mode(a);
a = 511; # octal: 0777
print file_mode(a);
a = 1023; # octal: 01777
print file_mode(a);
a = 1000; # octal: 01750
print file_mode(a);
a = 2541; # octal: 04755
print file_mode(a);
a = 2304; # octal: 04400
print file_mode(a);
a = 1517; # octal: 02755
print file_mode(a);
a = 1312; # octal: 02440
print file_mode(a);
a = 111; # octal: 0157
print file_mode(a);
a = 0;
print file_mode(a);
}

View file

@ -0,0 +1,18 @@
#
# @TEST-EXEC: bro %INPUT >out
# @TEST-EXEC: btest-diff out
event bro_init()
{
local a = "this is a test";
local pat = /hi|es/;
local pat2 = /aa|bb/;
local b = find_all(a, pat);
local b2 = find_all(a, pat2);
for (i in b)
print i;
print "-------------------";
print |b2|;
}

View file

@ -0,0 +1,13 @@
#
# @TEST-EXEC: bro %INPUT >out
# @TEST-EXEC: btest-diff out
event bro_init()
{
local a = "dh3Hie02uh^s#Sdf9L3frd243h$d78r2G4cM6*Q05d(7rh46f!0|4-f";
local b = "0011000aaabbbbcccc000011111000000000aaaabbbbcccc0000000";
print find_entropy(a);
print find_entropy(b);
}

View file

@ -0,0 +1,17 @@
#
# @TEST-EXEC: bro %INPUT >out
# @TEST-EXEC: btest-diff out
event bro_init()
{
local a = "this is a test";
local pat = /hi|es/;
local pat2 = /aa|bb/;
local b = find_last(a, pat);
local b2 = find_last(a, pat2);
print b;
print "-------------------";
print |b2|;
}

View file

@ -0,0 +1,90 @@
#
# @TEST-EXEC: bro %INPUT >out
# @TEST-EXEC: btest-diff out
type color: enum { Red, Blue };
event bro_init()
{
local a = Blue;
local b = vector( 1, 2, 3);
local c = set( 1, 2, 3);
local d: table[count] of string = { [1] = "test", [2] = "bro" };
# tests with only a format string (no additional args)
print fmt("test");
print fmt("%%");
# no arguments
print fmt();
# tests of various data types with field width specified
print fmt("*%-10s*", "test");
print fmt("*%10s*", "test");
print fmt("*%10s*", T);
print fmt("*%-10s*", T);
print fmt("*%10.2e*", 3.14159265);
print fmt("*%-10.2e*", 3.14159265);
print fmt("*%10.2f*", 3.14159265);
print fmt("*%10.2g*", 3.14159265);
print fmt("*%10.2e*", -3.14159265);
print fmt("*%10.2f*", -3.14159265);
print fmt("*%10.2g*", -3.14159265);
print fmt("*%-10.2e*", -3.14159265);
print fmt("*%-10.2f*", -3.14159265);
print fmt("*%-10.2g*", -3.14159265);
print fmt("*%10d*", -128);
print fmt("*%-10d*", -128);
print fmt("*%10d*", 128);
print fmt("*%010d*", 128);
print fmt("*%-10d*", 128);
print fmt("*%10x*", 160);
print fmt("*%010x*", 160);
print fmt("*%10x*", 160/tcp);
print fmt("*%10s*", 160/tcp);
print fmt("*%10s*", 127.0.0.1);
print fmt("*%10x*", 127.0.0.1);
print fmt("*%10s*", 192.168.0.0/16);
print fmt("*%10s*", [::1]);
print fmt("*%10x*", [fe00::1]);
print fmt("*%10s*", [fe80:1234::1]);
print fmt("*%10s*", [fe80:1234::]/32);
print fmt("*%10s*", 3hr);
print fmt("*%10s*", /^foo|bar/);
print fmt("*%10s*", a);
print fmt("*%10s*", b);
print fmt("*%10s*", c);
print fmt("*%10s*", d);
# tests of various data types without field width
print fmt("%e", 3.1e+2);
print fmt("%f", 3.1e+2);
print fmt("%g", 3.1e+2);
print fmt("%.3e", 3.1e+2);
print fmt("%.3f", 3.1e+2);
print fmt("%.3g", 3.1e+2);
print fmt("%.7g", 3.1e+2);
# Tests comparing "%As" and "%s" (the string length is printed instead
# of the string itself because the print command does its own escaping)
local s0 = "\x00\x07";
local s1 = fmt("%As", s0); # expands \x00 to "\0"
local s2 = fmt("%s", s0); # expands \x00 to "\0", and \x07 to "^G"
print |s0|;
print |s1|;
print |s2|;
s0 = "\x07\x1f";
s1 = fmt("%As", s0);
s2 = fmt("%s", s0); # expands \x07 to "^G", and \x1f to "\x1f"
print |s0|;
print |s1|;
print |s2|;
s0 = "\x7f\xff";
s1 = fmt("%As", s0);
s2 = fmt("%s", s0); # expands \x7f to "^?", and \xff to "\xff"
print |s0|;
print |s1|;
print |s2|;
}

View file

@ -0,0 +1,13 @@
#
# @TEST-EXEC: bro %INPUT >out
# @TEST-EXEC: btest-diff out
event bro_init()
{
local a = 192.168.0.2;
local b = 257/tcp;
print fmt_ftp_port(a, b);
a = [fe80::1234];
print fmt_ftp_port(a, b);
}

View file

@ -0,0 +1,13 @@
#
# @TEST-EXEC: bro %INPUT >out
# @TEST-EXEC: btest-diff out
event bro_init()
{
local a = 123/tcp;
local b = 123/udp;
local c = 123/icmp;
print get_port_transport_proto(a);
print get_port_transport_proto(b);
print get_port_transport_proto(c);
}

View file

@ -0,0 +1,20 @@
#
# @TEST-EXEC: TESTBRO=testvalue bro %INPUT >out
# @TEST-EXEC: btest-diff out
event bro_init()
{
local a = getenv("NOTDEFINED");
local b = getenv("TESTBRO");
if ( |a| == 0 )
print "OK";
if ( b == "testvalue" )
print "OK";
if ( setenv("NOTDEFINED", "now defined" ) == T )
{
if ( getenv("NOTDEFINED") == "now defined" )
print "OK";
}
}

View file

@ -0,0 +1,16 @@
#
# @TEST-EXEC: bro %INPUT >out
# @TEST-EXEC: btest-diff out
event bro_init()
{
local a = global_ids();
for ( i in a )
{
# the table is quite large, so just print one item we expect
if ( i == "bro_init" )
print a[i]$type_name;
}
}

View file

@ -0,0 +1,10 @@
#
# @TEST-EXEC: bro %INPUT >out
# @TEST-EXEC: btest-diff out
event bro_init()
{
local a = "abc\xffdefghijklmnopqrstuvwxyz";
print hexdump(a);
}

View file

@ -0,0 +1,12 @@
#
# @TEST-EXEC: bro %INPUT >out
# @TEST-EXEC: btest-diff out
event bro_init()
{
local a = "this is a test\xfe";
local b = "this is a test\x7f";
print is_ascii(a);
print is_ascii(b);
}

View file

@ -0,0 +1,22 @@
#
# @TEST-EXEC: bro %INPUT >out
# @TEST-EXEC: btest-diff out
event bro_init()
{
local a = 123/tcp;
local b = 123/udp;
local c = 123/icmp;
print is_tcp_port(a);
print is_tcp_port(b);
print is_tcp_port(c);
print is_udp_port(a);
print is_udp_port(b);
print is_udp_port(c);
print is_icmp_port(a);
print is_icmp_port(b);
print is_icmp_port(c);
}

View file

@ -0,0 +1,21 @@
#
# @TEST-EXEC: bro %INPUT >out
# @TEST-EXEC: btest-diff out
event bro_init()
{
local a: string_array = {
[1] = "this", [2] = "is", [3] = "a", [4] = "test"
};
local b: string_array = { [1] = "mytest" };
local c: string_vec = vector( "this", "is", "another", "test" );
local d: string_vec = vector( "Test" );
print join_string_array(" * ", a);
print join_string_array("", a);
print join_string_array("x", b);
print join_string_vec(c, "__");
print join_string_vec(c, "");
print join_string_vec(d, "-");
}

View file

@ -0,0 +1,22 @@
#
# @TEST-EXEC: bro %INPUT > out
# @TEST-EXEC: btest-diff out
event bro_init()
{
local mytable: table[string] of string = { ["key1"] = "val1" };
local myset: set[count] = set( 3, 6, 2, 7 );
local myvec: vector of string = vector( "value1", "value2" );
print length(mytable);
print length(myset);
print length(myvec);
mytable = table();
myset = set();
myvec = vector();
print length(mytable);
print length(myset);
print length(myvec);
}

View file

@ -0,0 +1,16 @@
#
# @TEST-EXEC: bro %INPUT >out
# @TEST-EXEC: btest-diff out
global a = "bro test";
event bro_init()
{
local b = "local value";
print lookup_ID("a");
print lookup_ID("");
print lookup_ID("xyz");
print lookup_ID("b");
print type_name( lookup_ID("bro_init") );
}

View file

@ -0,0 +1,11 @@
#
# @TEST-EXEC: bro %INPUT >out
# @TEST-EXEC: btest-diff out
event bro_init()
{
local a = "this is a Test";
print to_lower(a);
print to_upper(a);
}

View file

@ -0,0 +1,24 @@
#
# @TEST-EXEC: bro %INPUT >out
# @TEST-EXEC: btest-diff out
event bro_init()
{
local a = 3.14;
local b = 2.71;
local c = -3.14;
local d = -2.71;
print floor(a);
print floor(b);
print floor(c);
print floor(d);
print sqrt(a);
print exp(a);
print ln(a);
print log10(a);
}

Some files were not shown because too many files have changed in this diff Show more