diff --git a/CHANGES b/CHANGES index a2a66cf53e..4d667c8454 100644 --- a/CHANGES +++ b/CHANGES @@ -1,4 +1,12 @@ +2.2-271 | 2014-03-30 20:25:17 +0200 + + * Add unit tests covering vector/set/table ctors/inits. (Jon Siwek) + + * Fix parsing of "local" named table constructors. (Jon Siwek) + + * Improve type checking of records. Addresses BIT-1159. (Jon Siwek) + 2.2-267 | 2014-03-30 20:21:43 +0200 * Improve documentation of Bro clusters. Addresses BIT-1160. diff --git a/VERSION b/VERSION index 2794fa367f..84e92344d3 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.2-267 +2.2-271 diff --git a/scripts/base/frameworks/files/main.bro b/scripts/base/frameworks/files/main.bro index cf2c11be45..d481e0b28f 100644 --- a/scripts/base/frameworks/files/main.bro +++ b/scripts/base/frameworks/files/main.bro @@ -41,15 +41,15 @@ export { ## If this file was transferred over a network ## connection this should show the host or hosts that ## the data sourced from. - tx_hosts: set[addr] &log; + tx_hosts: set[addr] &default=addr_set() &log; ## If this file was transferred over a network ## connection this should show the host or hosts that ## the data traveled to. - rx_hosts: set[addr] &log; + rx_hosts: set[addr] &default=addr_set() &log; ## Connection UIDs over which the file was transferred. - conn_uids: set[string] &log; + conn_uids: set[string] &default=string_set() &log; ## An identification of the source of the file data. E.g. it ## may be a network protocol over which it was transferred, or a @@ -63,7 +63,7 @@ export { depth: count &default=0 &log; ## A set of analysis types done during the file analysis. - analyzers: set[string] &log; + analyzers: set[string] &default=string_set() &log; ## A mime type provided by libmagic against the *bof_buffer* ## field of :bro:see:`fa_file`, or in the cases where no diff --git a/src/Expr.cc b/src/Expr.cc index a5315b533d..b3a4b8b096 100644 --- a/src/Expr.cc +++ b/src/Expr.cc @@ -3392,22 +3392,12 @@ bool HasFieldExpr::DoUnserialize(UnserialInfo* info) return UNSERIALIZE(¬_used) && UNSERIALIZE_STR(&field_name, 0) && UNSERIALIZE(&field); } -RecordConstructorExpr::RecordConstructorExpr(ListExpr* constructor_list, - BroType* arg_type) +RecordConstructorExpr::RecordConstructorExpr(ListExpr* constructor_list) : UnaryExpr(EXPR_RECORD_CONSTRUCTOR, constructor_list) { - ctor_type = 0; - if ( IsError() ) return; - if ( arg_type && arg_type->Tag() != TYPE_RECORD ) - { - Error("bad record constructor type", arg_type); - SetError(); - return; - } - // Spin through the list, which should be comprised of // either record's or record-field-assign, and build up a // record type to associate with this constructor. @@ -3447,17 +3437,11 @@ RecordConstructorExpr::RecordConstructorExpr(ListExpr* constructor_list, } } - ctor_type = new RecordType(record_types); - - if ( arg_type ) - SetType(arg_type->Ref()); - else - SetType(ctor_type->Ref()); + SetType(new RecordType(record_types)); } RecordConstructorExpr::~RecordConstructorExpr() { - Unref(ctor_type); } Val* RecordConstructorExpr::InitVal(const BroType* t, Val* aggr) const @@ -3483,7 +3467,7 @@ Val* RecordConstructorExpr::InitVal(const BroType* t, Val* aggr) const Val* RecordConstructorExpr::Fold(Val* v) const { ListVal* lv = v->AsListVal(); - RecordType* rt = ctor_type->AsRecordType(); + RecordType* rt = type->AsRecordType(); if ( lv->Length() != rt->NumFields() ) Internal("inconsistency evaluating record constructor"); @@ -3493,19 +3477,6 @@ Val* RecordConstructorExpr::Fold(Val* v) const for ( int i = 0; i < lv->Length(); ++i ) rv->Assign(i, lv->Index(i)->Ref()); - if ( ! same_type(rt, type) ) - { - RecordVal* new_val = rv->CoerceTo(type->AsRecordType()); - - if ( new_val ) - { - Unref(rv); - rv = new_val; - } - else - Internal("record constructor coercion failed"); - } - return rv; } @@ -3521,16 +3492,12 @@ IMPLEMENT_SERIAL(RecordConstructorExpr, SER_RECORD_CONSTRUCTOR_EXPR); bool RecordConstructorExpr::DoSerialize(SerialInfo* info) const { DO_SERIALIZE(SER_RECORD_CONSTRUCTOR_EXPR, UnaryExpr); - SERIALIZE_OPTIONAL(ctor_type); return true; } bool RecordConstructorExpr::DoUnserialize(UnserialInfo* info) { DO_UNSERIALIZE(UnaryExpr); - BroType* t = 0; - UNSERIALIZE_OPTIONAL(t, RecordType::Unserialize(info)); - ctor_type = t->AsRecordType(); return true; } @@ -4214,6 +4181,26 @@ RecordCoerceExpr::~RecordCoerceExpr() delete [] map; } +Val* RecordCoerceExpr::InitVal(const BroType* t, Val* aggr) const + { + Val* v = Eval(0); + + if ( v ) + { + RecordVal* rv = v->AsRecordVal(); + RecordVal* ar = rv->CoerceTo(t->AsRecordType(), aggr); + + if ( ar ) + { + Unref(rv); + return ar; + } + } + + Error("bad record initializer"); + return 0; + } + Val* RecordCoerceExpr::Fold(Val* v) const { RecordVal* val = new RecordVal(Type()->AsRecordType()); @@ -4238,6 +4225,13 @@ Val* RecordCoerceExpr::Fold(Val* v) const assert(rhs || Type()->AsRecordType()->FieldDecl(i)->FindAttr(ATTR_OPTIONAL)); + if ( ! rhs ) + { + // Optional field is missing. + val->Assign(i, 0); + continue; + } + BroType* rhs_type = rhs->Type(); RecordType* val_type = val->Type()->AsRecordType(); BroType* field_type = val_type->FieldType(i); diff --git a/src/Expr.h b/src/Expr.h index 26f20fcbe9..fafa79025b 100644 --- a/src/Expr.h +++ b/src/Expr.h @@ -753,7 +753,7 @@ protected: class RecordConstructorExpr : public UnaryExpr { public: - RecordConstructorExpr(ListExpr* constructor_list, BroType* arg_type = 0); + RecordConstructorExpr(ListExpr* constructor_list); ~RecordConstructorExpr(); protected: @@ -766,8 +766,6 @@ protected: void ExprDescribe(ODesc* d) const; DECLARE_SERIAL(RecordConstructorExpr); - - RecordType* ctor_type; // type inferred from the ctor expression list args }; class TableConstructorExpr : public UnaryExpr { @@ -878,6 +876,7 @@ protected: friend class Expr; RecordCoerceExpr() { map = 0; } + Val* InitVal(const BroType* t, Val* aggr) const; Val* Fold(Val* v) const; DECLARE_SERIAL(RecordCoerceExpr); diff --git a/src/Var.cc b/src/Var.cc index 1d4f1f4620..52754e3265 100644 --- a/src/Var.cc +++ b/src/Var.cc @@ -169,8 +169,14 @@ static void make_var(ID* id, BroType* t, init_class c, Expr* init, { Val* aggr; if ( t->Tag() == TYPE_RECORD ) + { aggr = new RecordVal(t->AsRecordType()); + if ( init && t ) + // Have an initialization and type is not deduced. + init = new RecordCoerceExpr(init, t->AsRecordType()); + } + else if ( t->Tag() == TYPE_TABLE ) aggr = new TableVal(t->AsTableType(), id->Attrs()); diff --git a/src/parse.y b/src/parse.y index 5835c57143..aa01171a24 100644 --- a/src/parse.y +++ b/src/parse.y @@ -207,6 +207,22 @@ static void extend_record(ID* id, type_decl_list* fields, attr_list* attrs) } } } + +static bool expr_is_table_type_name(const Expr* expr) + { + if ( expr->Tag() != EXPR_NAME ) + return false; + + BroType* type = expr->Type(); + + if ( type->IsTable() ) + return true; + + if ( type->Tag() == TYPE_TYPE ) + return type->AsTypeType()->Type()->IsTable(); + + return false; + } %} %union { @@ -538,13 +554,13 @@ expr: | expr '(' { - if ( $1->Tag() == EXPR_NAME && $1->Type()->IsTable() ) + if ( expr_is_table_type_name($1) ) ++in_init; } opt_expr_list { - if ( $1->Tag() == EXPR_NAME && $1->Type()->IsTable() ) + if ( expr_is_table_type_name($1) ) --in_init; } @@ -559,7 +575,8 @@ expr: { switch ( ctor_type->Tag() ) { case TYPE_RECORD: - $$ = new RecordConstructorExpr($4, ctor_type); + $$ = new RecordCoerceExpr(new RecordConstructorExpr($4), + ctor_type->AsRecordType()); break; case TYPE_TABLE: diff --git a/testing/btest/Baseline/language.named-table-ctors/out b/testing/btest/Baseline/language.named-table-ctors/out index 23554d10f6..273afdd205 100644 --- a/testing/btest/Baseline/language.named-table-ctors/out +++ b/testing/btest/Baseline/language.named-table-ctors/out @@ -17,3 +17,7 @@ [three] = 3.0 } 0 +{ +[42] = forty-two, +[37] = thirty-seven +} diff --git a/testing/btest/Baseline/language.record-bad-ctor/out b/testing/btest/Baseline/language.record-bad-ctor/out index 2b890419ae..97f853b5a8 100644 --- a/testing/btest/Baseline/language.record-bad-ctor/out +++ b/testing/btest/Baseline/language.record-bad-ctor/out @@ -1,3 +1,3 @@ error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.record-bad-ctor/record-bad-ctor.bro, line 6: no type given (asdfasdf) error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.record-bad-ctor/record-bad-ctor.bro, line 7: uninitialized list value ($ports=asdfasdf) -error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.record-bad-ctor/record-bad-ctor.bro, line 7: bad record initializer ([$ports=asdfasdf]) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.record-bad-ctor/record-bad-ctor.bro, line 7: bad record initializer ((coerce [$ports=asdfasdf] to record { ports:error; })) diff --git a/testing/btest/Baseline/language.record-type-checking/out b/testing/btest/Baseline/language.record-type-checking/out new file mode 100644 index 0000000000..ecd5d7b8bb --- /dev/null +++ b/testing/btest/Baseline/language.record-type-checking/out @@ -0,0 +1,11 @@ +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.record-type-checking/record-type-checking.bro, line 9 and count: type clash for field "a" ((coerce [$a=0] to MyRec) and count) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.record-type-checking/record-type-checking.bro, line 9: bad record initializer ((coerce [$a=0] to error)) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.record-type-checking/record-type-checking.bro, line 12 and count: type clash for field "a" ((coerce [$a=1] to MyRec) and count) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.record-type-checking/record-type-checking.bro, line 12: bad record initializer ((coerce (coerce [$a=1] to error) to error)) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.record-type-checking/record-type-checking.bro, line 18 and count: type clash for field "a" ((coerce [$a=2] to MyRec) and count) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.record-type-checking/record-type-checking.bro, line 22 and count: type clash for field "a" ((coerce [$a=3] to MyRec) and count) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.record-type-checking/record-type-checking.bro, line 22: bad record initializer ((coerce [$a=3] to error)) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.record-type-checking/record-type-checking.bro, line 27 and count: type clash for field "a" ((coerce [$a=1000] to MyRec) and count) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.record-type-checking/record-type-checking.bro, line 33 and count: type clash for field "a" ((coerce [$a=1001] to MyRec) and count) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.record-type-checking/record-type-checking.bro, line 40 and count: type clash for field "a" ((coerce [$a=1002] to MyRec) and count) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.record-type-checking/record-type-checking.bro, line 46 and count: type clash for field "a" ((coerce [$a=1003] to MyRec) and count) diff --git a/testing/btest/Baseline/language.set-type-checking/out b/testing/btest/Baseline/language.set-type-checking/out new file mode 100644 index 0000000000..d93db19abd --- /dev/null +++ b/testing/btest/Baseline/language.set-type-checking/out @@ -0,0 +1,21 @@ +error in port and /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-checking/set-type-checking.bro, line 7: arithmetic mixed with non-arithmetic (port and 0) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-checking/set-type-checking.bro, line 7 and port: type mismatch (0 and port) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-checking/set-type-checking.bro, line 7: inconsistent type in set constructor (set(0)) +error in port and /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-checking/set-type-checking.bro, line 10: arithmetic mixed with non-arithmetic (port and 1) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-checking/set-type-checking.bro, line 10 and port: type mismatch (1 and port) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-checking/set-type-checking.bro, line 10: inconsistent type in set constructor (set(1)) +error in port and /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-checking/set-type-checking.bro, line 16: arithmetic mixed with non-arithmetic (port and 2) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-checking/set-type-checking.bro, line 16 and port: type mismatch (2 and port) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-checking/set-type-checking.bro, line 16: inconsistent type in set constructor (set(2)) +error in port and /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-checking/set-type-checking.bro, line 20: arithmetic mixed with non-arithmetic (port and 3) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-checking/set-type-checking.bro, line 20: initialization type mismatch in set (set(3) and 3) +error in port and /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-checking/set-type-checking.bro, line 25: arithmetic mixed with non-arithmetic (port and 1000) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-checking/set-type-checking.bro, line 25 and port: type mismatch (1000 and port) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-checking/set-type-checking.bro, line 25: inconsistent type in set constructor (set(1000)) +error in port and /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-checking/set-type-checking.bro, line 31: arithmetic mixed with non-arithmetic (port and 1001) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-checking/set-type-checking.bro, line 31 and port: type mismatch (1001 and port) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-checking/set-type-checking.bro, line 31: inconsistent type in set constructor (set(1001)) +error in port and /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-checking/set-type-checking.bro, line 38: arithmetic mixed with non-arithmetic (port and 1002) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-checking/set-type-checking.bro, line 38 and port: type mismatch (1002 and port) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-checking/set-type-checking.bro, line 38: inconsistent type in set constructor (set(1002)) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.set-type-checking/set-type-checking.bro, line 44: type clash in assignment (lea = set(1003)) diff --git a/testing/btest/Baseline/language.table-type-checking/out b/testing/btest/Baseline/language.table-type-checking/out new file mode 100644 index 0000000000..00a4bb2491 --- /dev/null +++ b/testing/btest/Baseline/language.table-type-checking/out @@ -0,0 +1,14 @@ +error in port and /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.table-type-checking/table-type-checking.bro, line 7: type clash (port and zero) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.table-type-checking/table-type-checking.bro, line 7: inconsistent types in table constructor (table(zero = 0)) +error in port and /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.table-type-checking/table-type-checking.bro, line 10: type clash (port and one) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.table-type-checking/table-type-checking.bro, line 10: inconsistent types in table constructor (table(one = 1)) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.table-type-checking/table-type-checking.bro, line 17: type clash in assignment (gda = gda2) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.table-type-checking/table-type-checking.bro, line 21 and /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.table-type-checking/table-type-checking.bro, line 4: index type doesn't match table (three and list of port) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.table-type-checking/table-type-checking.bro, line 21: type clash in table assignment (three = 3) +error in port and /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.table-type-checking/table-type-checking.bro, line 26: type clash (port and thousand) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.table-type-checking/table-type-checking.bro, line 26: inconsistent types in table constructor (table(thousand = 1000)) +error in port and /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.table-type-checking/table-type-checking.bro, line 32: type clash (port and thousand-one) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.table-type-checking/table-type-checking.bro, line 32: inconsistent types in table constructor (table(thousand-one = 1001)) +error in port and /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.table-type-checking/table-type-checking.bro, line 39: type clash (port and thousand-two) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.table-type-checking/table-type-checking.bro, line 39: inconsistent types in table constructor (table(thousand-two = 1002)) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.table-type-checking/table-type-checking.bro, line 45: type clash in assignment (lea = table(thousand-three = 1003)) diff --git a/testing/btest/Baseline/language.vector-type-checking/out b/testing/btest/Baseline/language.vector-type-checking/out new file mode 100644 index 0000000000..e96017082a --- /dev/null +++ b/testing/btest/Baseline/language.vector-type-checking/out @@ -0,0 +1,19 @@ +error in count and /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.vector-type-checking/vector-type-checking.bro, line 7: arithmetic mixed with non-arithmetic (count and zero) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.vector-type-checking/vector-type-checking.bro, line 7 and count: type mismatch (zero and count) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.vector-type-checking/vector-type-checking.bro, line 7: inconsistent types in vector constructor (vector(zero)) +error in count and /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.vector-type-checking/vector-type-checking.bro, line 10: arithmetic mixed with non-arithmetic (count and one) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.vector-type-checking/vector-type-checking.bro, line 10 and count: type mismatch (one and count) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.vector-type-checking/vector-type-checking.bro, line 10: inconsistent types in vector constructor (vector(one)) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.vector-type-checking/vector-type-checking.bro, line 17: type clash in assignment (gda = gda2) +error in count and /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.vector-type-checking/vector-type-checking.bro, line 21: arithmetic mixed with non-arithmetic (count and three) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.vector-type-checking/vector-type-checking.bro, line 21: initialization type mismatch at index 0 (vector(three) and three) +error in count and /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.vector-type-checking/vector-type-checking.bro, line 26: arithmetic mixed with non-arithmetic (count and thousand) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.vector-type-checking/vector-type-checking.bro, line 26 and count: type mismatch (thousand and count) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.vector-type-checking/vector-type-checking.bro, line 26: inconsistent types in vector constructor (vector(thousand)) +error in count and /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.vector-type-checking/vector-type-checking.bro, line 32: arithmetic mixed with non-arithmetic (count and thousand-one) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.vector-type-checking/vector-type-checking.bro, line 32 and count: type mismatch (thousand-one and count) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.vector-type-checking/vector-type-checking.bro, line 32: inconsistent types in vector constructor (vector(thousand-one)) +error in count and /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.vector-type-checking/vector-type-checking.bro, line 39: arithmetic mixed with non-arithmetic (count and thousand-two) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.vector-type-checking/vector-type-checking.bro, line 39 and count: type mismatch (thousand-two and count) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.vector-type-checking/vector-type-checking.bro, line 39: inconsistent types in vector constructor (vector(thousand-two)) +error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/language.vector-type-checking/vector-type-checking.bro, line 45: type clash in assignment (lea = vector(thousand-three)) diff --git a/testing/btest/language/named-record-ctors.bro b/testing/btest/language/named-record-ctors.bro index d0a6fc70e5..40a79d86b3 100644 --- a/testing/btest/language/named-record-ctors.bro +++ b/testing/btest/language/named-record-ctors.bro @@ -1,6 +1,8 @@ -# @TEST-EXEC: bro -b frameworks/software/vulnerable %INPUT >out +# @TEST-EXEC: bro -b %INPUT >out # @TEST-EXEC: btest-diff out +@load frameworks/software/vulnerable + type MyRec: record { min: count &optional; max: count; diff --git a/testing/btest/language/named-table-ctors.bro b/testing/btest/language/named-table-ctors.bro index 83500488f1..1fad56e30f 100644 --- a/testing/btest/language/named-table-ctors.bro +++ b/testing/btest/language/named-table-ctors.bro @@ -17,8 +17,14 @@ global mytablecomp: FooTableComp = FooTableComp(["test", 1] = "test1", ["cool", 2] = "cool2"); global mytabley: FooTableY = FooTableY(["one"] = 1, ["two"] = 2, ["three"] = 3) &default=0; -print mytable; -print mytablerec; -print mytablecomp; -print mytabley; -print mytabley["test"]; +event bro_init() + { + print mytable; + print mytablerec; + print mytablecomp; + print mytabley; + print mytabley["test"]; + + local loctable = FooTable([42] = "forty-two", [37] = "thirty-seven"); + print loctable; + } diff --git a/testing/btest/language/record-extension.bro b/testing/btest/language/record-extension.bro index 78ef929a86..02b4c3bbe7 100644 --- a/testing/btest/language/record-extension.bro +++ b/testing/btest/language/record-extension.bro @@ -9,7 +9,7 @@ type Foo: record { redef record Foo += { c: count &default=42; - d: count &optional; + d: string &optional; anotherset: set[count] &default=set(); }; diff --git a/testing/btest/language/record-type-checking.bro b/testing/btest/language/record-type-checking.bro new file mode 100644 index 0000000000..d58937d577 --- /dev/null +++ b/testing/btest/language/record-type-checking.bro @@ -0,0 +1,47 @@ +# @TEST-EXEC-FAIL: bro -b %INPUT >out 2>&1 +# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff out + +type MyRec: record { + a: port &default = 1/tcp; +}; + +# global, type deduction, named ctor +global grdn = MyRec($a = 0); # type clash in init + +# global, type explicit, named ctor +global gren: MyRec = MyRec($a = 1); # type clash in init + +# global, type deduction, anon ctor +global grda = [$a = 2]; # fine +event bro_init() + { + grda = MyRec($a = 2); # type clash in assignment + } + +# global, type explicit, anon ctor +global grea: MyRec = [$a = 3]; # type clash + +# local, type deduction, named ctor +event bro_init() + { + local lrdn = MyRec($a = 1000); # type clash + } + +# local, type explicit, named ctor +event bro_init() + { + local lren: MyRec = MyRec($a = 1001); # type clash + } + +# local, type deduction, anon ctor +event bro_init() + { + local lrda = [$a = 1002]; # fine + lrda = MyRec($a = 1002); # type clash + } + +# local, type explicit, anon ctor +event bro_init() + { + local lrea: MyRec = [$a = 1003]; # type clash + } diff --git a/testing/btest/language/set-type-checking.bro b/testing/btest/language/set-type-checking.bro new file mode 100644 index 0000000000..bf774fb9f9 --- /dev/null +++ b/testing/btest/language/set-type-checking.bro @@ -0,0 +1,45 @@ +# @TEST-EXEC-FAIL: bro -b %INPUT >out 2>&1 +# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff out + +type MySet: set[port]; + +# global, type deduction, named ctor +global gdn = MySet(0); # type clash in init + +# global, type explicit, named ctor +global gen: MySet = MySet(1); # type clash in init + +# global, type deduction, anon ctor +global gda = set(2); # fine +event bro_init() + { + gda = MySet(2); # type clash in assignment + } + +# global, type explicit, anon ctor +global gea: MySet = set(3); # type clash + +# local, type deduction, named ctor +event bro_init() + { + local ldn = MySet(1000); # type clash + } + +# local, type explicit, named ctor +event bro_init() + { + local len: MySet = MySet(1001); # type clash + } + +# local, type deduction, anon ctor +event bro_init() + { + local lda = set(1002); # fine + lda = MySet(1002); # type clash + } + +# local, type explicit, anon ctor +event bro_init() + { + local lea: MySet = set(1003); # type clash + } diff --git a/testing/btest/language/sizeof.bro b/testing/btest/language/sizeof.bro index 8b29e119bd..4f5833aa30 100644 --- a/testing/btest/language/sizeof.bro +++ b/testing/btest/language/sizeof.bro @@ -28,7 +28,7 @@ global f: file = open_log_file("sizeof_demo"); global i: int = -10; global iv: interval = -5sec; global p: port = 80/tcp; -global r: example_record [ $i = 10 ]; +global r: example_record = [ $i = +10 ]; global si: set[int]; global s: string = "Hello"; global sn: subnet = 192.168.0.0/24; diff --git a/testing/btest/language/table-type-checking.bro b/testing/btest/language/table-type-checking.bro new file mode 100644 index 0000000000..f579a83d37 --- /dev/null +++ b/testing/btest/language/table-type-checking.bro @@ -0,0 +1,46 @@ +# @TEST-EXEC-FAIL: bro -b %INPUT >out 2>&1 +# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff out + +type MyTable: table[port] of count; + +# global, type deduction, named ctor +global gdn = MyTable(["zero"] = 0); # type clash in init + +# global, type explicit, named ctor +global gen: MyTable = MyTable(["one"] = 1); # type clash in init + +# global, type deduction, anon ctor +global gda = table(["two"] = 2); # fine +global gda2 = MyTable([2/tcp] = 2); # fine +event bro_init() + { + gda = gda2; # type clash + } + +# global, type explicit, anon ctor +global gea: MyTable = table(["three"] = 3); # type clash + +# local, type deduction, named ctor +event bro_init() + { + local ldn = MyTable(["thousand"] = 1000); # type clash + } + +# local, type explicit, named ctor +event bro_init() + { + local len: MyTable = MyTable(["thousand-one"] = 1001); # type clash + } + +# local, type deduction, anon ctor +event bro_init() + { + local lda = table(["thousand-two"] = 1002); # fine + lda = MyTable(["thousand-two"] = 1002); # type clash + } + +# local, type explicit, anon ctor +event bro_init() + { + local lea: MyTable = table(["thousand-three"] = 1003); # type clash + } diff --git a/testing/btest/language/vector-type-checking.bro b/testing/btest/language/vector-type-checking.bro new file mode 100644 index 0000000000..b4c75118d1 --- /dev/null +++ b/testing/btest/language/vector-type-checking.bro @@ -0,0 +1,46 @@ +# @TEST-EXEC-FAIL: bro -b %INPUT >out 2>&1 +# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff out + +type MyVec: vector of count; + +# global, type deduction, named ctor +global gdn = MyVec("zero"); # type clash in init + +# global, type explicit, named ctor +global gen: MyVec = MyVec("one"); # type clash in init + +# global, type deduction, anon ctor +global gda = vector("two"); # fine +global gda2 = MyVec(2); # fine +event bro_init() + { + gda = gda2; # type clash + } + +# global, type explicit, anon ctor +global gea: MyVec = vector("three"); # type clash + +# local, type deduction, named ctor +event bro_init() + { + local ldn = MyVec("thousand"); # type clash + } + +# local, type explicit, named ctor +event bro_init() + { + local len: MyVec = MyVec("thousand-one"); # type clash + } + +# local, type deduction, anon ctor +event bro_init() + { + local lda = vector("thousand-two"); # fine + lda = MyVec("thousand-two"); # type clash + } + +# local, type explicit, anon ctor +event bro_init() + { + local lea: MyVec = vector("thousand-three"); # type clash + } diff --git a/testing/btest/scripts/base/utils/conn-ids.test b/testing/btest/scripts/base/utils/conn-ids.test index a7d2ce0939..affe746e35 100644 --- a/testing/btest/scripts/base/utils/conn-ids.test +++ b/testing/btest/scripts/base/utils/conn-ids.test @@ -4,8 +4,8 @@ # This is loaded by default. #@load base/utils/conn-ids -global c: conn_id = [ $orig_h = 10.0.0.100, $orig_p = 10000, - $resp_h = 10.0.0.200, $resp_p = 20000 ]; +global c: conn_id = [ $orig_h = 10.0.0.100, $orig_p = 10000/tcp, + $resp_h = 10.0.0.200, $resp_p = 20000/tcp ]; print id_string(c); print reverse_id_string(c); diff --git a/testing/btest/scripts/base/utils/directions-and-hosts.test b/testing/btest/scripts/base/utils/directions-and-hosts.test index 6f4f781c72..92d1b48d3a 100644 --- a/testing/btest/scripts/base/utils/directions-and-hosts.test +++ b/testing/btest/scripts/base/utils/directions-and-hosts.test @@ -11,20 +11,20 @@ global local_ip = 10.0.0.100; global remote_ip = 192.168.1.100; global local2local: conn_id = [ - $orig_h = 10.0.0.100, $orig_p = 10000, - $resp_h = 10.0.0.200, $resp_p = 20000 ]; + $orig_h = 10.0.0.100, $orig_p = 10000/tcp, + $resp_h = 10.0.0.200, $resp_p = 20000/tcp ]; global local2remote: conn_id = [ - $orig_h = 10.0.0.100, $orig_p = 10000, - $resp_h = 192.168.1.100, $resp_p = 20000 ]; + $orig_h = 10.0.0.100, $orig_p = 10000/tcp, + $resp_h = 192.168.1.100, $resp_p = 20000/tcp ]; global remote2local: conn_id = [ - $orig_h = 192.168.1.100, $orig_p = 10000, - $resp_h = 10.0.0.100, $resp_p = 20000 ]; + $orig_h = 192.168.1.100, $orig_p = 10000/tcp, + $resp_h = 10.0.0.100, $resp_p = 20000/tcp ]; global remote2remote: conn_id = [ - $orig_h = 192.168.1.100, $orig_p = 10000, - $resp_h = 192.168.1.200, $resp_p = 20000 ]; + $orig_h = 192.168.1.100, $orig_p = 10000/tcp, + $resp_h = 192.168.1.200, $resp_p = 20000/tcp ]; function test_host(ip: addr, h: Host, expect: bool) {