From 197870a731f18dd9759e9cc97dfd298fda773251 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Tue, 16 Sep 2008 18:05:53 +0200 Subject: Remove remaining embedded JavaScript support. --- source4/scripting/ejs/config.mk | 63 --- source4/scripting/ejs/mprutil.c | 494 -------------------- source4/scripting/ejs/smbcalls.c | 220 --------- source4/scripting/ejs/smbcalls.h | 42 -- source4/scripting/ejs/smbcalls_auth.c | 243 ---------- source4/scripting/ejs/smbcalls_config.c | 228 --------- source4/scripting/ejs/smbcalls_creds.c | 275 ----------- source4/scripting/ejs/smbcalls_ldb.c | 772 ------------------------------- source4/scripting/ejs/smbcalls_options.c | 193 -------- source4/scripting/ejs/smbcalls_string.c | 529 --------------------- source4/scripting/ejs/smbcalls_sys.c | 494 -------------------- source4/scripting/ejs/smbscript.c | 129 ------ 12 files changed, 3682 deletions(-) delete mode 100644 source4/scripting/ejs/config.mk delete mode 100644 source4/scripting/ejs/mprutil.c delete mode 100644 source4/scripting/ejs/smbcalls.c delete mode 100644 source4/scripting/ejs/smbcalls.h delete mode 100644 source4/scripting/ejs/smbcalls_auth.c delete mode 100644 source4/scripting/ejs/smbcalls_config.c delete mode 100644 source4/scripting/ejs/smbcalls_creds.c delete mode 100644 source4/scripting/ejs/smbcalls_ldb.c delete mode 100644 source4/scripting/ejs/smbcalls_options.c delete mode 100644 source4/scripting/ejs/smbcalls_string.c delete mode 100644 source4/scripting/ejs/smbcalls_sys.c delete mode 100644 source4/scripting/ejs/smbscript.c (limited to 'source4/scripting') diff --git a/source4/scripting/ejs/config.mk b/source4/scripting/ejs/config.mk deleted file mode 100644 index 34c0a9678e..0000000000 --- a/source4/scripting/ejs/config.mk +++ /dev/null @@ -1,63 +0,0 @@ -[MODULE::smbcalls_config] -OUTPUT_TYPE = MERGED_OBJ -SUBSYSTEM = smbcalls -INIT_FUNCTION = smb_setup_ejs_config - -smbcalls_config_OBJ_FILES = $(ejsscriptsrcdir)/smbcalls_config.o - -[MODULE::smbcalls_ldb] -OUTPUT_TYPE = MERGED_OBJ -SUBSYSTEM = smbcalls -INIT_FUNCTION = smb_setup_ejs_ldb -PRIVATE_DEPENDENCIES = LIBLDB SAMDB LIBNDR - -smbcalls_ldb_OBJ_FILES = $(ejsscriptsrcdir)/smbcalls_ldb.o - -[MODULE::smbcalls_auth] -OUTPUT_TYPE = MERGED_OBJ -SUBSYSTEM = smbcalls -INIT_FUNCTION = smb_setup_ejs_auth -PRIVATE_DEPENDENCIES = service_auth - -smbcalls_auth_OBJ_FILES = $(ejsscriptsrcdir)/smbcalls_auth.o - -smbcalls_auth_OBJ_FILES = $(ejsscriptsrcdir)/smbcalls_auth.o - -[MODULE::smbcalls_string] -SUBSYSTEM = smbcalls -OUTPUT_TYPE = MERGED_OBJ -INIT_FUNCTION = smb_setup_ejs_string - -smbcalls_string_OBJ_FILES = $(ejsscriptsrcdir)/smbcalls_string.o - -[MODULE::smbcalls_sys] -SUBSYSTEM = smbcalls -OUTPUT_TYPE = MERGED_OBJ -INIT_FUNCTION = smb_setup_ejs_system - -smbcalls_sys_OBJ_FILES = $(ejsscriptsrcdir)/smbcalls_sys.o - -[SUBSYSTEM::smbcalls] -PRIVATE_DEPENDENCIES = \ - EJS LIBSAMBA-UTIL \ - MESSAGING \ - LIBSAMBA-NET LIBCLI_SMB LIBPOPT \ - CREDENTIALS POPT_CREDENTIALS POPT_SAMBA \ - NDR_TABLE - -smbcalls_OBJ_FILES = $(addprefix $(ejsscriptsrcdir)/, \ - smbcalls.o \ - smbcalls_options.o \ - smbcalls_creds.o \ - mprutil.o) - -$(eval $(call proto_header_template,$(ejsscriptsrcdir)/proto.h,$(smbcalls_OBJ_FILES:.o=.c))) - -####################### -# Start BINARY SMBSCRIPT -[BINARY::smbscript] -PRIVATE_DEPENDENCIES = EJS LIBSAMBA-UTIL smbcalls LIBSAMBA-HOSTCONFIG -# End BINARY SMBSCRIPT -####################### - -smbscript_OBJ_FILES = $(ejsscriptsrcdir)/smbscript.o diff --git a/source4/scripting/ejs/mprutil.c b/source4/scripting/ejs/mprutil.c deleted file mode 100644 index 9143947fb8..0000000000 --- a/source4/scripting/ejs/mprutil.c +++ /dev/null @@ -1,494 +0,0 @@ -/* - Unix SMB/CIFS implementation. - - utility functions for manipulating mpr variables in ejs calls - - Copyright (C) Andrew Tridgell 2005 - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#include "includes.h" -#include "lib/appweb/ejs/ejs.h" -#include "lib/ldb/include/ldb.h" -#include "scripting/ejs/smbcalls.h" - -/* - return a default mpr object -*/ -struct MprVar mprObject(const char *name) -{ - return ejsCreateObj(name && *name?name:"(NULL)", MPR_DEFAULT_HASH_SIZE); -} - -/* - return a empty mpr array -*/ -struct MprVar mprArray(const char *name) -{ - return ejsCreateArray(name && *name?name:"(NULL)", 0); -} - -/* - find a mpr component, allowing for sub objects, using the '.' convention -*/ - NTSTATUS mprGetVar(struct MprVar **v, const char *name) -{ - const char *p = strchr(name, '.'); - char *objname; - NTSTATUS status; - if (p == NULL) { - *v = mprGetProperty(*v, name, NULL); - if (*v == NULL) { - DEBUG(1,("mprGetVar unable to find '%s'\n", name)); - return NT_STATUS_INVALID_PARAMETER; - } - return NT_STATUS_OK; - } - objname = talloc_strndup(mprMemCtx(), name, p-name); - NT_STATUS_HAVE_NO_MEMORY(objname); - *v = mprGetProperty(*v, objname, NULL); - NT_STATUS_HAVE_NO_MEMORY(*v); - status = mprGetVar(v, p+1); - talloc_free(objname); - return status; -} - - -/* - set a mpr component, allowing for sub objects, using the '.' convention - destroys 'val' after setting -*/ - NTSTATUS mprSetVar(struct MprVar *v, const char *name, struct MprVar val) -{ - const char *p = strchr(name, '.'); - char *objname; - struct MprVar *v2; - NTSTATUS status; - if (p == NULL) { - v2 = mprSetProperty(v, name, &val); - if (v2 == NULL) { - DEBUG(1,("mprSetVar unable to set '%s'\n", name)); - return NT_STATUS_INVALID_PARAMETER_MIX; - } - mprDestroyVar(&val); - return NT_STATUS_OK; - } - objname = talloc_strndup(mprMemCtx(), name, p-name); - if (objname == NULL) { - return NT_STATUS_NO_MEMORY; - } - v2 = mprGetProperty(v, objname, NULL); - if (v2 == NULL) { - mprSetVar(v, objname, mprObject(objname)); - v2 = mprGetProperty(v, objname, NULL); - } - status = mprSetVar(v2, p+1, val); - talloc_free(objname); - return status; -} - - - -/* - add an indexed array element to a property -*/ - void mprAddArray(struct MprVar *var, int i, struct MprVar v) -{ - char idx[16]; - mprItoa(i, idx, sizeof(idx)); - mprSetVar(var, idx, v); -} - -/* - construct a MprVar from a list -*/ -struct MprVar mprList(const char *name, const char **list) -{ - struct MprVar var; - int i; - - var = mprArray(name); - for (i=0;list && list[i];i++) { - mprAddArray(&var, i, mprString(list[i])); - } - return var; -} - -/* - construct a MprVar from a string, using NULL if needed -*/ -struct MprVar mprString(const char *s) -{ - if (s == NULL) { - return mprCreatePtrVar(NULL); - } - return mprCreateStringVar(s, true); -} - -/* - construct a string MprVar from a lump of data -*/ -struct MprVar mprData(const uint8_t *p, size_t length) -{ - struct MprVar var; - char *s = talloc_strndup(mprMemCtx(), (const char *)p, length); - if (s == NULL) { - return mprCreateUndefinedVar(); - } - var = mprString(s); - talloc_free(s); - return var; -} - -/* - turn a ldb_message into a ejs object variable -*/ -static struct MprVar mprLdbMessage(struct ldb_context *ldb, struct ldb_message *msg) -{ - struct MprVar var; - int i; - /* we force some attributes to always be an array in the - returned structure. This makes the scripting easier, as you don't - need a special case for the single value case */ - const char *multivalued[] = { "objectClass", "memberOf", "privilege", - "member", NULL }; - - var = mprObject(ldb_dn_alloc_linearized(msg, msg->dn)); - - for (i=0;inum_elements;i++) { - struct ldb_message_element *el = &msg->elements[i]; - struct MprVar val; - const struct ldb_schema_attribute *a; - struct ldb_val v; - - a = ldb_schema_attribute_by_name(ldb, el->name); - if (a == NULL) { - goto failed; - } - - if (el->num_values == 1 && - !str_list_check_ci(multivalued, el->name)) { - if (a->syntax->ldif_write_fn(ldb, msg, &el->values[0], &v) != 0) { - goto failed; - } - /* FIXME: nasty hack, remove me when ejs will support - * arbitrary string and does not truncate on \0 */ - if (strlen((char *)v.data) != v.length) { - val = mprDataBlob(v); - } else { - val = mprData(v.data, v.length); - } - } else { - int j; - val = mprArray(el->name); - for (j=0;jnum_values;j++) { - if (a->syntax->ldif_write_fn(ldb, msg, - &el->values[j], &v) != 0) { - goto failed; - } - /* FIXME: nasty hack, remove me when ejs will support - * arbitrary string and does not truncate on \0 */ - if (strlen((char *)v.data) != v.length) { - mprAddArray(&val, j, mprDataBlob(v)); - } else { - mprAddArray(&val, j, mprData(v.data, v.length)); - } - } - } - mprSetVar(&var, el->name, val); - } - - /* add the dn if it is not already specified */ - if (mprGetProperty(&var, "dn", 0) == 0) { - mprSetVar(&var, "dn", mprString(ldb_dn_alloc_linearized(msg, msg->dn))); - } - - return var; -failed: - return mprCreateUndefinedVar(); -} - - -/* - build a MprVar result object for ldb operations with lots of funky properties -*/ -struct MprVar mprLdbResult(struct ldb_context *ldb, int err, struct ldb_result *result) -{ - struct MprVar ret; - struct MprVar ary; - - ret = mprObject("ldbret"); - - mprSetVar(&ret, "error", mprCreateIntegerVar(err)); - mprSetVar(&ret, "errstr", mprString(ldb_errstring(ldb))); - - ary = mprArray("ldb_message"); - if (result) { - int i; - - for (i = 0; i < result->count; i++) { - mprAddArray(&ary, i, mprLdbMessage(ldb, result->msgs[i])); - } - } - - mprSetVar(&ret, "msgs", ary); - - /* TODO: add referrals, exteded ops, and controls */ - - return ret; -} - - -/* - turn a MprVar string variable into a const char * - */ -const char *mprToString(struct MprVar *v) -{ - if (v->trigger) { - mprReadProperty(v, 0); - } - if (!mprVarIsString(v->type)) return NULL; - return v->string; -} - -/* - turn a MprVar integer variable into an int - */ -int mprToInt(struct MprVar *v) -{ - if (v->trigger) { - mprReadProperty(v, 0); - } - if (!mprVarIsNumber(v->type)) return 0; - return mprVarToNumber(v); -} - -/* - turn a MprVar object variable into a string list - this assumes the object variable consists only of strings -*/ -const char **mprToList(TALLOC_CTX *mem_ctx, struct MprVar *v) -{ - const char **list = NULL; - struct MprVar *el; - - if (v->type != MPR_TYPE_OBJECT || - v->properties == NULL) { - return NULL; - } - for (el=mprGetFirstProperty(v, MPR_ENUM_DATA); - el; - el=mprGetNextProperty(v, el, MPR_ENUM_DATA)) { - const char *s = mprToString(el); - if (s) { - list = str_list_add(list, s); - } - } - talloc_steal(mem_ctx, list); - return list; -} - - -/* - turn a MprVar object variable into a string list - this assumes the object variable is an array of strings -*/ -const char **mprToArray(TALLOC_CTX *mem_ctx, struct MprVar *v) -{ - const char **list = NULL; - struct MprVar *len; - int length, i; - - len = mprGetProperty(v, "length", NULL); - if (len == NULL) { - return NULL; - } - length = mprToInt(len); - - for (i=0;itype != MPR_TYPE_STRING) { - talloc_free(list); - return NULL; - } - list = str_list_add(list, mprToString(vs)); - } - talloc_steal(mem_ctx, list); - return list; -} - -/* - turn a NTSTATUS into a MprVar object with lots of funky properties -*/ -struct MprVar mprNTSTATUS(NTSTATUS status) -{ - struct MprVar res; - - res = mprObject("ntstatus"); - - mprSetVar(&res, "errstr", mprString(nt_errstr(status))); - mprSetVar(&res, "v", mprCreateIntegerVar(NT_STATUS_V(status))); - mprSetVar(&res, "is_ok", mprCreateBoolVar(NT_STATUS_IS_OK(status))); - mprSetVar(&res, "is_err", mprCreateBoolVar(NT_STATUS_IS_ERR(status))); - - return res; -} - -/* - create a data-blob in a mpr variable -*/ -struct MprVar mprDataBlob(DATA_BLOB blob) -{ - struct MprVar res; - struct datablob *pblob = talloc(mprMemCtx(), struct datablob); - *pblob = data_blob_talloc(pblob, blob.data, blob.length); - - res = mprObject("DATA_BLOB"); - - mprSetVar(&res, "size", mprCreateIntegerVar(blob.length)); - mprSetPtrChild(&res, "blob", pblob); - - return res; -} - -/* - return a data blob from a mpr var created using mprDataBlob -*/ -struct datablob *mprToDataBlob(struct MprVar *v) -{ - return talloc_get_type(mprGetPtr(v, "blob"), struct datablob); -} - -/* - turn a WERROR into a MprVar object with lots of funky properties -*/ -struct MprVar mprWERROR(WERROR status) -{ - struct MprVar res; - - res = mprObject("werror"); - - mprSetVar(&res, "errstr", mprString(win_errstr(status))); - mprSetVar(&res, "v", mprCreateIntegerVar(W_ERROR_V(status))); - mprSetVar(&res, "is_ok", mprCreateBoolVar(W_ERROR_IS_OK(status))); - mprSetVar(&res, "is_err", mprCreateBoolVar(!W_ERROR_IS_OK(status))); - - return res; -} - - -/* - set a pointer in a existing MprVar -*/ -void mprSetPtr(struct MprVar *v, const char *propname, const void *p) -{ - mprSetVar(v, propname, mprCreatePtrVar(discard_const(p))); -} - -/* - set a pointer in a existing MprVar, freeing it when the property goes away -*/ -void mprSetPtrChild(struct MprVar *v, const char *propname, const void *p) -{ - mprSetVar(v, propname, mprCreatePtrVar(discard_const(p))); - v = mprGetProperty(v, propname, NULL); - v->allocatedData = 1; - talloc_steal(mprMemCtx(), p); -} - -/* - get a pointer from a MprVar -*/ -void *mprGetPtr(struct MprVar *v, const char *propname) -{ - struct MprVar *val; - val = mprGetProperty(v, propname, NULL); - if (val == NULL) { - return NULL; - } - if (val->type != MPR_TYPE_PTR) { - return NULL; - } - return val->ptr; -} - -/* - set the return value then free the variable -*/ - void mpr_Return(int eid, struct MprVar v) -{ - ejsSetReturnValue(eid, v); - mprDestroyVar(&v); -} - -/* - set the return value then free the variable -*/ -void mpr_ReturnString(int eid, const char *s) -{ - mpr_Return(eid, mprString(s)); -} - - -/* - set a C function in a variable -*/ - void mprSetCFunction(struct MprVar *obj, const char *name, MprCFunction fn) -{ - mprSetVar(obj, name, mprCreateCFunctionVar(fn, obj, MPR_VAR_SCRIPT_HANDLE)); -} - -/* - set a string C function in a variable -*/ - void mprSetStringCFunction(struct MprVar *obj, const char *name, MprStringCFunction fn) -{ - mprSetVar(obj, name, mprCreateStringCFunctionVar(fn, obj, MPR_VAR_SCRIPT_HANDLE)); -} - -/* - get a pointer in the current object -*/ -void *mprGetThisPtr(int eid, const char *name) -{ - struct MprVar *this = mprGetProperty(ejsGetLocalObject(eid), "this", 0); - return mprGetPtr(this, name); -} - -/* - set a pointer as a child of the local object -*/ -void mprSetThisPtr(int eid, const char *name, void *ptr) -{ - struct MprVar *this = mprGetProperty(ejsGetLocalObject(eid), "this", 0); - mprSetPtrChild(this, name, ptr); -} - -/* - used by object xxx_init() routines to allow for the caller - to supply a pre-existing object to add properties to, - or create a new object. This makes inheritance easy -*/ -struct MprVar *mprInitObject(int eid, const char *name, int argc, struct MprVar **argv) -{ - if (argc > 0 && mprVarIsObject(argv[0]->type)) { - return argv[0]; - } - mpr_Return(eid, mprObject(name)); - return ejsGetReturnValue(eid); -} diff --git a/source4/scripting/ejs/smbcalls.c b/source4/scripting/ejs/smbcalls.c deleted file mode 100644 index 4314b51455..0000000000 --- a/source4/scripting/ejs/smbcalls.c +++ /dev/null @@ -1,220 +0,0 @@ -/* - Unix SMB/CIFS implementation. - - provide hooks into smbd C calls from ejs scripts - - Copyright (C) Andrew Tridgell 2005 - Copyright (C) Tim Potter 2005 - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#include "includes.h" -#include "param/param.h" -#include "scripting/ejs/smbcalls.h" -#include "version.h" - -/* - return the type of a variable -*/ -static int ejs_typeof(MprVarHandle eid, int argc, struct MprVar **argv) -{ - const struct { - MprType type; - const char *name; - } types[] = { - { MPR_TYPE_UNDEFINED, "undefined" }, - { MPR_TYPE_NULL, "object" }, - { MPR_TYPE_BOOL, "boolean" }, - { MPR_TYPE_CFUNCTION, "function" }, - { MPR_TYPE_FLOAT, "number" }, - { MPR_TYPE_INT, "number" }, - { MPR_TYPE_INT64, "number" }, - { MPR_TYPE_OBJECT, "object" }, - { MPR_TYPE_FUNCTION, "function" }, - { MPR_TYPE_STRING, "string" }, - { MPR_TYPE_STRING_CFUNCTION, "function" }, - { MPR_TYPE_PTR, "pointer" } - }; - int i; - const char *type = NULL; - - if (argc != 1) return -1; - - for (i=0;itype == types[i].type) { - type = types[i].name; - break; - } - } - if (type == NULL) return -1; - - mpr_ReturnString(eid, type); - return 0; -} - -/* - return the native type of a variable -*/ -static int ejs_typeof_native(MprVarHandle eid, int argc, struct MprVar **argv) -{ - const struct { - MprType type; - const char *name; - } types[] = { - { MPR_TYPE_UNDEFINED, "undefined" }, - { MPR_TYPE_NULL, "null" }, - { MPR_TYPE_BOOL, "boolean" }, - { MPR_TYPE_CFUNCTION, "c_function" }, - { MPR_TYPE_FLOAT, "float" }, - { MPR_TYPE_INT, "integer" }, - { MPR_TYPE_INT64, "integer64" }, - { MPR_TYPE_OBJECT, "object" }, - { MPR_TYPE_FUNCTION, "js_function" }, - { MPR_TYPE_STRING, "string" }, - { MPR_TYPE_STRING_CFUNCTION, "string_c_function" }, - { MPR_TYPE_PTR, "pointer" } - }; - int i; - const char *type = NULL; - - if (argc != 1) return -1; - - for (i=0;itype == types[i].type) { - type = types[i].name; - break; - } - } - if (type == NULL) return -1; - - mpr_ReturnString(eid, type); - return 0; -} - -/* - libinclude() allows you to include js files using a search path specified - in "js include =" in smb.conf. -*/ -static int ejs_libinclude(int eid, int argc, char **argv) -{ - int i, j; - const char **js_include = lp_js_include(mprLpCtx()); - - if (js_include == NULL || js_include[0] == NULL) { - ejsSetErrorMsg(eid, "js include path not set"); - return -1; - } - - for (i = 0; i < argc; i++) { - const char *script = argv[i]; - struct MprVar result; - char *path, *emsg; - int ret; - - /* use specfied path to search for requested file */ - for (j=0;js_include[j];j++) { - path = talloc_asprintf(mprMemCtx(), "%s/%s", js_include[j], script); - if (path == NULL) { - return -1; - } - if (file_exist(path)) { - - ret = ejsEvalFile(eid, path, &result, &emsg); - talloc_free(path); - if (ret < 0) { - ejsSetErrorMsg(eid, "%s: %s", script, emsg); - return -1; - } - break; - } - talloc_free(path); - } - - if (js_include[j] == NULL) { - ejsSetErrorMsg(eid, "unable to include '%s'", script); - return -1; - } - } - return 0; -} - -/* - return the current version -*/ -static int ejs_version(MprVarHandle eid, int argc, struct MprVar **argv) -{ - mpr_ReturnString(eid, SAMBA_VERSION_STRING); - return 0; -} - - -static void (*ejs_exception_handler) (const char *) = NULL; - -_PUBLIC_ void ejs_exception(const char *reason) -{ - ejs_exception_handler(reason); -} - -/* - setup C functions that be called from ejs -*/ -void smb_setup_ejs_functions(void (*exception_handler)(const char *)) -{ - extern NTSTATUS ejs_init_security(void); - extern NTSTATUS ejs_init_initshutdown(void); - extern NTSTATUS smb_setup_ejs_reg(void); - extern NTSTATUS smb_setup_ejs_string(void); - extern NTSTATUS ejs_init_lsarpc(void); - extern NTSTATUS ejs_init_rpcecho(void); - extern NTSTATUS ejs_init_winreg(void); - extern NTSTATUS smb_setup_ejs_random(void); - extern NTSTATUS smb_setup_ejs_config(void); - extern NTSTATUS ejs_init_misc(void); - extern NTSTATUS ejs_init_netdfs(void); - extern NTSTATUS smb_setup_ejs_datablob(void); - extern NTSTATUS smb_setup_ejs_auth(void); - extern NTSTATUS smb_setup_ejs_nss(void); - extern NTSTATUS ejs_init_samr(void); - extern NTSTATUS ejs_init_wkssvc(void); - extern NTSTATUS smb_setup_ejs_system(void); - extern NTSTATUS smb_setup_ejs_ldb(void); - extern NTSTATUS ejs_init_svcctl(void); - extern NTSTATUS smb_setup_ejs_net(void); - extern NTSTATUS ejs_init_srvsvc(void); - extern NTSTATUS ejs_init_netlogon(void); - extern NTSTATUS ejs_init_drsuapi(void); - extern NTSTATUS ejs_init_irpc(void); - extern NTSTATUS ejs_init_eventlog(void); - init_module_fn static_init[] = { STATIC_smbcalls_MODULES }; - init_module_fn *shared_init; - - ejs_exception_handler = exception_handler; - - smb_setup_ejs_options(); - smb_setup_ejs_credentials(); - - shared_init = load_samba_modules(NULL, mprLpCtx(), "smbcalls"); - - run_init_functions(static_init); - run_init_functions(shared_init); - - talloc_free(shared_init); - - ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE); - ejsDefineCFunction(-1, "nativeTypeOf", ejs_typeof_native, NULL, MPR_VAR_SCRIPT_HANDLE); - ejsDefineStringCFunction(-1, "libinclude", ejs_libinclude, NULL, MPR_VAR_SCRIPT_HANDLE); - ejsDefineCFunction(-1, "version", ejs_version, NULL, MPR_VAR_SCRIPT_HANDLE); -} - diff --git a/source4/scripting/ejs/smbcalls.h b/source4/scripting/ejs/smbcalls.h deleted file mode 100644 index 3aaf324b6e..0000000000 --- a/source4/scripting/ejs/smbcalls.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - Unix SMB/CIFS implementation. - - provide hooks into smbd C calls from ejs scripts - - Copyright (C) Andrew Tridgell 2005 - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#include "lib/appweb/ejs/ejs.h" -#include "lib/ldb/include/ldb.h" - -void mpr_Return(int eid, struct MprVar); -NTSTATUS mprSetVar(struct MprVar *v, const char *name, struct MprVar val); -NTSTATUS mprGetVar(struct MprVar **v, const char *name); -void mprAddArray(struct MprVar *var, int i, struct MprVar v); -void mprSetCFunction(struct MprVar *obj, const char *name, MprCFunction fn); -void mprSetStringCFunction(struct MprVar *obj, const char *name, MprStringCFunction fn); - -struct smbcalls_context { - struct event_context *event_ctx; - struct messaging_context *msg_ctx; -}; - -struct ldb_context; -struct ldb_message; -struct cli_credentials; - -#include "param/param.h" -#include "scripting/ejs/proto.h" diff --git a/source4/scripting/ejs/smbcalls_auth.c b/source4/scripting/ejs/smbcalls_auth.c deleted file mode 100644 index b67bb7ed5b..0000000000 --- a/source4/scripting/ejs/smbcalls_auth.c +++ /dev/null @@ -1,243 +0,0 @@ -/* - Unix SMB/CIFS implementation. - - ejs auth functions - - Copyright (C) Simo Sorce 2005 - Copyright (C) Andrew Tridgell 2005 - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#include "includes.h" -#include "lib/appweb/ejs/ejs.h" -#include "auth/auth.h" -#include "auth/credentials/credentials.h" -#include "scripting/ejs/smbcalls.h" -#include "lib/events/events.h" -#include "lib/messaging/irpc.h" -#include "libcli/security/security.h" - -static int ejs_doauth(MprVarHandle eid, - TALLOC_CTX *tmp_ctx, struct MprVar *auth, - const char *username, const char *password, - const char *domain, const char *workstation, - struct socket_address *remote_host, - const char **auth_types) -{ - struct auth_usersupplied_info *user_info = NULL; - struct auth_serversupplied_info *server_info = NULL; - struct auth_session_info *session_info = NULL; - struct auth_context *auth_context; - struct MprVar *session_info_obj; - NTSTATUS nt_status; - bool set; - - struct smbcalls_context *c; - struct event_context *ev; - struct messaging_context *msg; - - /* Hope we can find an smbcalls_context somewhere up there... */ - c = talloc_find_parent_bytype(tmp_ctx, struct smbcalls_context); - if (c) { - ev = c->event_ctx; - msg = c->msg_ctx; - } else { - /* Hope we can find the event context somewhere up there... */ - ev = mprEventCtx(); - msg = messaging_client_init(tmp_ctx, lp_messaging_path(tmp_ctx, mprLpCtx()), - lp_iconv_convenience(mprLpCtx()), ev); - } - - if (auth_types) { - nt_status = auth_context_create_methods(tmp_ctx, auth_types, ev, msg, mprLpCtx(), &auth_context); - } else { - nt_status = auth_context_create(tmp_ctx, ev, msg, mprLpCtx(), &auth_context); - } - if (!NT_STATUS_IS_OK(nt_status)) { - mprSetPropertyValue(auth, "result", mprCreateBoolVar(false)); - mprSetPropertyValue(auth, "report", mprString("Auth System Failure")); - goto done; - } - - user_info = talloc(tmp_ctx, struct auth_usersupplied_info); - if (!user_info) { - mprSetPropertyValue(auth, "result", mprCreateBoolVar(false)); - mprSetPropertyValue(auth, "report", mprString("talloc failed")); - goto done; - } - - user_info->mapped_state = true; - user_info->client.account_name = username; - user_info->mapped.account_name = username; - user_info->client.domain_name = domain; - user_info->mapped.domain_name = domain; - - user_info->workstation_name = workstation; - - user_info->remote_host = remote_host; - - user_info->password_state = AUTH_PASSWORD_PLAIN; - user_info->password.plaintext = talloc_strdup(user_info, password); - - user_info->flags = USER_INFO_CASE_INSENSITIVE_USERNAME | - USER_INFO_DONT_CHECK_UNIX_ACCOUNT; - - user_info->logon_parameters = 0; - - nt_status = auth_check_password(auth_context, tmp_ctx, user_info, &server_info); - - /* Don't give the game away (any difference between no such - * user and wrong password) */ - nt_status = auth_nt_status_squash(nt_status); - - if (!NT_STATUS_IS_OK(nt_status)) { - mprSetPropertyValue(auth, "report", - mprString(talloc_strdup(mprMemCtx(), get_friendly_nt_error_msg(nt_status)))); - mprSetPropertyValue(auth, "result", mprCreateBoolVar(false)); - goto done; - } - - nt_status = auth_generate_session_info(tmp_ctx, mprEventCtx(), mprLpCtx(), server_info, &session_info); - if (!NT_STATUS_IS_OK(nt_status)) { - mprSetPropertyValue(auth, "report", mprString("Session Info generation failed")); - mprSetPropertyValue(auth, "result", mprCreateBoolVar(false)); - goto done; - } - - if (security_token_has_nt_authenticated_users(session_info->security_token)) { - mprSetPropertyValue(auth, "user_class", mprString("USER")); - set = true; - } - - if (security_token_has_builtin_administrators(session_info->security_token)) { - mprSetPropertyValue(auth, "user_class", mprString("ADMINISTRATOR")); - set = true; - } - - if (security_token_is_system(session_info->security_token)) { - mprSetPropertyValue(auth, "user_class", mprString("SYSTEM")); - set = true; - } - - if (security_token_is_anonymous(session_info->security_token)) { - mprSetPropertyValue(auth, "report", mprString("Anonymous login not permitted")); - mprSetPropertyValue(auth, "result", mprCreateBoolVar(false)); - goto done; - } - - if (!set) { - mprSetPropertyValue(auth, "report", mprString("Session Info generation failed")); - mprSetPropertyValue(auth, "result", mprCreateBoolVar(false)); - } - - session_info_obj = mprInitObject(eid, "session_info", 0, NULL); - - mprSetPtrChild(session_info_obj, "session_info", session_info); - talloc_steal(mprMemCtx(), session_info); - - mprSetProperty(auth, "session_info", session_info_obj); - mprSetPropertyValue(auth, "result", mprCreateBoolVar(server_info->authenticated)); - mprSetPropertyValue(auth, "username", mprString(server_info->account_name)); - mprSetPropertyValue(auth, "domain", mprString(server_info->domain_name)); - -done: - return 0; -} - -/* - perform user authentication, returning an array of results - -*/ -static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv) -{ - TALLOC_CTX *tmp_ctx; - const char *username; - const char *password; - const char *domain; - const char *workstation; - struct MprVar auth; - struct cli_credentials *creds; - struct socket_address *remote_host; - const char *auth_types_unix[] = { "unix", NULL }; - - if (argc != 2 || argv[0]->type != MPR_TYPE_OBJECT || argv[1]->type != MPR_TYPE_OBJECT) { - ejsSetErrorMsg(eid, "userAuth invalid arguments, this function requires an object."); - return -1; - } - - /* get credential values from credentials object */ - creds = mprGetPtr(argv[0], "creds"); - if (creds == NULL) { - ejsSetErrorMsg(eid, "userAuth requires a 'creds' first parameter"); - return -1; - } - - remote_host = (struct socket_address *)mprGetPtr(argv[1], "socket_address"); - if (remote_host == NULL) { - ejsSetErrorMsg(eid, "userAuth requires a socket address second parameter"); - return -1; - } - - tmp_ctx = talloc_new(mprMemCtx()); - - username = cli_credentials_get_username(creds); - password = cli_credentials_get_password(creds); - domain = cli_credentials_get_domain(creds); - workstation = cli_credentials_get_workstation(creds); - - if (username == NULL || password == NULL || domain == NULL) { - mpr_Return(eid, mprCreateUndefinedVar()); - talloc_free(tmp_ctx); - return 0; - } - - auth = mprObject("auth"); - - if (domain && (strcmp("SYSTEM USER", domain) == 0)) { - ejs_doauth(eid, tmp_ctx, &auth, username, password, domain, workstation, remote_host, auth_types_unix); - } else { - ejs_doauth(eid, tmp_ctx, &auth, username, password, domain, workstation, remote_host, NULL); - } - - mpr_Return(eid, auth); - talloc_free(tmp_ctx); - return 0; -} - -/* - initialise credentials ejs object -*/ -static int ejs_system_session(MprVarHandle eid, int argc, struct MprVar **argv) -{ - struct MprVar *obj = mprInitObject(eid, "session_info", argc, argv); - struct auth_session_info *session_info = system_session(mprMemCtx(), mprLpCtx()); - - if (session_info == NULL) { - return -1; - } - - mprSetPtrChild(obj, "session_info", session_info); - return 0; -} - -/* - setup C functions that be called from ejs -*/ -NTSTATUS smb_setup_ejs_auth(void) -{ - ejsDefineCFunction(-1, "userAuth", ejs_userAuth, NULL, MPR_VAR_SCRIPT_HANDLE); - ejsDefineCFunction(-1, "system_session", ejs_system_session, NULL, MPR_VAR_SCRIPT_HANDLE); - return NT_STATUS_OK; -} diff --git a/source4/scripting/ejs/smbcalls_config.c b/source4/scripting/ejs/smbcalls_config.c deleted file mode 100644 index eb673b3a23..0000000000 --- a/source4/scripting/ejs/smbcalls_config.c +++ /dev/null @@ -1,228 +0,0 @@ -/* - Unix SMB/CIFS implementation. - - provide hooks into smbd C calls from ejs scripts - - Copyright (C) Andrew Tridgell 2005 - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#include "includes.h" -#include "scripting/ejs/smbcalls.h" -#include "lib/appweb/ejs/ejs.h" -#include "param/loadparm.h" -#include "system/network.h" -#include "lib/socket/netif.h" -#include "param/param.h" - -/* - return a list of defined services -*/ -static int ejs_lpServices(MprVarHandle eid, int argc, char **argv) -{ - int i; - const char **list = NULL; - if (argc != 0) return -1; - - for (i=0;iclass == P_GLOBAL) { - mpr_Return(eid, mprCreateUndefinedVar()); - return 0; - } - parm_ptr = lp_parm_ptr(mprLpCtx(), service, parm); - } else if (strchr(argv[0], ':')) { - /* its a global parametric option */ - const char *type = talloc_strndup(mprMemCtx(), - argv[0], strcspn(argv[0], ":")); - const char *option = strchr(argv[0], ':') + 1; - const char *value; - if (type == NULL || option == NULL) { - mpr_Return(eid, mprCreateUndefinedVar()); - return 0; - } - value = lp_get_parametric(mprLpCtx(), NULL, type, option); - if (value == NULL) { - mpr_Return(eid, mprCreateUndefinedVar()); - return 0; - } - mpr_ReturnString(eid, value); - return 0; - } else { - /* its a global parameter */ - parm = lp_parm_struct(argv[0]); - if (parm == NULL) { - mpr_Return(eid, mprCreateUndefinedVar()); - return 0; - } - parm_ptr = lp_parm_ptr(mprLpCtx(), NULL, parm); - } - - if (parm == NULL || parm_ptr == NULL) { - mpr_Return(eid, mprCreateUndefinedVar()); - return 0; - } - - /* construct and return the right type of ejs object */ - switch (parm->type) { - case P_STRING: - case P_USTRING: - mpr_ReturnString(eid, *(char **)parm_ptr); - break; - case P_BOOL: - mpr_Return(eid, mprCreateBoolVar(*(bool *)parm_ptr)); - break; - case P_INTEGER: - case P_OCTAL: - case P_BYTES: - mpr_Return(eid, mprCreateIntegerVar(*(int *)parm_ptr)); - break; - case P_ENUM: - for (i=0; parm->enum_list[i].name; i++) { - if (*(int *)parm_ptr == parm->enum_list[i].value) { - mpr_ReturnString(eid, parm->enum_list[i].name); - return 0; - } - } - mpr_Return(eid, mprCreateUndefinedVar()); - return 0; - case P_LIST: - mpr_Return(eid, mprList(parm->label, *(const char ***)parm_ptr)); - break; - } - return 0; -} - -/* - v = lp.filename(); obtain filename -*/ -static int ejs_lpFilename(MprVarHandle eid, int argc, char **argv) -{ - mpr_ReturnString(eid, lp_configfile(mprLpCtx())); - return 0; -} - -/* - set a smb.conf parameter. Only sets in memory, not permanent - - can be called in 4 ways: - - ok = lp.set("parm", "value"); -*/ -static int ejs_lpSet(MprVarHandle eid, int argc, char **argv) -{ - if (argc != 2) { - ejsSetErrorMsg(eid, "lp.set invalid arguments"); - return -1; - } - - mpr_Return(eid, mprCreateBoolVar(lp_set_cmdline(mprLpCtx(), argv[0], argv[1]))); - return 0; -} - -/* - reload smb.conf - - ok = lp.reload(); -*/ -static int ejs_lpReload(MprVarHandle eid, int argc, char **argv) -{ - bool ret; - const char *filename = lp_configfile(mprLpCtx()); - - ret = lp_load(mprLpCtx(), filename); - mpr_Return(eid, mprCreateBoolVar(ret)); - return 0; -} - -/* - initialise loadparm ejs subsystem -*/ -static int ejs_loadparm_init(MprVarHandle eid, int argc, struct MprVar **argv) -{ - struct MprVar *obj = mprInitObject(eid, "loadparm", argc, argv); - - mprSetStringCFunction(obj, "get", ejs_lpGet); - mprSetStringCFunction(obj, "set", ejs_lpSet); - mprSetStringCFunction(obj, "reload", ejs_lpReload); - mprSetStringCFunction(obj, "services", ejs_lpServices); - mprSetStringCFunction(obj, "filename", ejs_lpFilename); - return 0; -} - -/* - setup C functions that be called from ejs -*/ -NTSTATUS smb_setup_ejs_config(void) -{ - ejsDefineCFunction(-1, "loadparm_init", ejs_loadparm_init, NULL, MPR_VAR_SCRIPT_HANDLE); - return NT_STATUS_OK; -} diff --git a/source4/scripting/ejs/smbcalls_creds.c b/source4/scripting/ejs/smbcalls_creds.c deleted file mode 100644 index fd73f0751f..0000000000 --- a/source4/scripting/ejs/smbcalls_creds.c +++ /dev/null @@ -1,275 +0,0 @@ -/* - Unix SMB/CIFS implementation. - - provide hooks credentials calls - - Copyright (C) Andrew Tridgell 2005 - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#include "includes.h" -#include "scripting/ejs/smbcalls.h" -#include "lib/appweb/ejs/ejs.h" -#include "lib/cmdline/popt_common.h" -#include "auth/credentials/credentials.h" - -/* - helper function to get the local objects credentials ptr -*/ -static struct cli_credentials *ejs_creds_get_credentials(int eid) -{ - struct cli_credentials *creds = (struct cli_credentials *)mprGetThisPtr(eid, "creds"); - if (creds == NULL) { - ejsSetErrorMsg(eid, "NULL ejs credentials"); - } - return creds; -} - -/* - get a domain -*/ -static int ejs_creds_get_domain(MprVarHandle eid, int argc, struct MprVar **argv) -{ - struct cli_credentials *creds = ejs_creds_get_credentials(eid); - - mpr_Return(eid, mprString(cli_credentials_get_domain(creds))); - return 0; -} - - -/* - set a domain -*/ -static int ejs_creds_set_domain(MprVarHandle eid, int argc, char **argv) -{ - struct cli_credentials *creds = ejs_creds_get_credentials(eid); - if (argc != 1) { - ejsSetErrorMsg(eid, "bad arguments to set_domain"); - return -1; - } - - cli_credentials_set_domain(creds, argv[0], CRED_SPECIFIED); - mpr_Return(eid, mprCreateBoolVar(true)); - return 0; -} - - -/* - get a username -*/ -static int ejs_creds_get_username(MprVarHandle eid, int argc, struct MprVar **argv) -{ - struct cli_credentials *creds = ejs_creds_get_credentials(eid); - - mpr_Return(eid, mprString(cli_credentials_get_username(creds))); - return 0; -} - - -/* - set a username -*/ -static int ejs_creds_set_username(MprVarHandle eid, int argc, char **argv) -{ - struct cli_credentials *creds = ejs_creds_get_credentials(eid); - if (argc != 1) { - ejsSetErrorMsg(eid, "bad arguments to set_username"); - return -1; - } - - cli_credentials_set_username(creds, argv[0], CRED_SPECIFIED); - mpr_Return(eid, mprCreateBoolVar(true)); - return 0; -} - - -/* - get user password -*/ -static int ejs_creds_get_password(MprVarHandle eid, int argc, struct MprVar **argv) -{ - struct cli_credentials *creds = ejs_creds_get_credentials(eid); - - mpr_Return(eid, mprString(cli_credentials_get_password(creds))); - return 0; -} - - -/* - set user password -*/ -static int ejs_creds_set_password(MprVarHandle eid, int argc, char **argv) -{ - struct cli_credentials *creds = ejs_creds_get_credentials(eid); - if (argc != 1) { - ejsSetErrorMsg(eid, "bad arguments to set_password"); - return -1; - } - - cli_credentials_set_password(creds, argv[0], CRED_SPECIFIED); - mpr_Return(eid, mprCreateBoolVar(true)); - return 0; -} - - -/* - set realm -*/ -static int ejs_creds_set_realm(MprVarHandle eid, int argc, char **argv) -{ - struct cli_credentials *creds = ejs_creds_get_credentials(eid); - if (argc != 1) { - ejsSetErrorMsg(eid, "bad arguments to set_realm"); - return -1; - } - - cli_credentials_set_realm(creds, argv[0], CRED_SPECIFIED); - mpr_Return(eid, mprCreateBoolVar(true)); - return 0; -} - - -/* - get realm -*/ -static int ejs_creds_get_realm(MprVarHandle eid, int argc, struct MprVar **argv) -{ - struct cli_credentials *creds = ejs_creds_get_credentials(eid); - - mpr_Return(eid, mprString(cli_credentials_get_realm(creds))); - return 0; -} - - -/* - set workstation -*/ -static int ejs_creds_set_workstation(MprVarHandle eid, int argc, char **argv) -{ - struct cli_credentials *creds = ejs_creds_get_credentials(eid); - if (argc != 1) { - ejsSetErrorMsg(eid, "bad arguments to set_workstation"); - return -1; - } - - cli_credentials_set_workstation(creds, argv[0], CRED_SPECIFIED); - mpr_Return(eid, mprCreateBoolVar(true)); - return 0; -} - - -/* - get workstation -*/ -static int ejs_creds_get_workstation(MprVarHandle eid, int argc, struct MprVar **argv) -{ - struct cli_credentials *creds = ejs_creds_get_credentials(eid); - - mpr_Return(eid, mprString(cli_credentials_get_workstation(creds))); - return 0; -} - -/* - set machine account -*/ -static int ejs_creds_set_machine_account(MprVarHandle eid, int argc, struct MprVar **argv) -{ - struct cli_credentials *creds = ejs_creds_get_credentials(eid); - if (argc != 0) { - ejsSetErrorMsg(eid, "bad arguments to set_machine_account"); - return -1; - } - - if (NT_STATUS_IS_OK(cli_credentials_set_machine_account(creds, mprLpCtx()))) { - mpr_Return(eid, mprCreateBoolVar(true)); - } else { - mpr_Return(eid, mprCreateBoolVar(false)); - } - return 0; -} - - -/* - initialise credentials ejs object -*/ -static int ejs_credentials_obj(struct MprVar *obj, struct cli_credentials *creds) -{ - mprSetPtrChild(obj, "creds", creds); - - /* setup our object methods */ - mprSetCFunction(obj, "get_domain", ejs_creds_get_domain); - mprSetStringCFunction(obj, "set_domain", ejs_creds_set_domain); - mprSetCFunction(obj, "get_username", ejs_creds_get_username); - mprSetStringCFunction(obj, "set_username", ejs_creds_set_username); - mprSetCFunction(obj, "get_password", ejs_creds_get_password); - mprSetStringCFunction(obj, "set_password", ejs_creds_set_password); - mprSetCFunction(obj, "get_realm", ejs_creds_get_realm); - mprSetStringCFunction(obj, "set_realm", ejs_creds_set_realm); - mprSetCFunction(obj, "get_workstation", ejs_creds_get_workstation); - mprSetStringCFunction(obj, "set_workstation", ejs_creds_set_workstation); - mprSetCFunction(obj, "set_machine_account", ejs_creds_set_machine_account); - - return 0; -} - - -struct MprVar mprCredentials(struct cli_credentials *creds) -{ - struct MprVar mpv = mprObject("credentials"); - - ejs_credentials_obj(&mpv, creds); - - return mpv; -} - - -/* - initialise credentials ejs object -*/ -static int ejs_credentials_init(MprVarHandle eid, int argc, struct MprVar **argv) -{ - struct cli_credentials *creds; - struct MprVar *obj = mprInitObject(eid, "credentials", argc, argv); - - creds = cli_credentials_init(mprMemCtx()); - if (creds == NULL) { - return -1; - } - - cli_credentials_set_conf(creds, mprLpCtx()); - - return ejs_credentials_obj(obj, creds); -} - -/* - initialise cmdline credentials ejs object -*/ -int ejs_credentials_cmdline(int eid, int argc, struct MprVar **argv) -{ - struct MprVar *obj = mprInitObject(eid, "credentials", argc, argv); - if (talloc_reference(mprMemCtx(), cmdline_credentials) == NULL) { - return -1; - } - return ejs_credentials_obj(obj, cmdline_credentials); -} - -/* - setup C functions that be called from ejs -*/ -void smb_setup_ejs_credentials(void) -{ - ejsDefineCFunction(-1, "credentials_init", ejs_credentials_init, NULL, MPR_VAR_SCRIPT_HANDLE); -} - diff --git a/source4/scripting/ejs/smbcalls_ldb.c b/source4/scripting/ejs/smbcalls_ldb.c deleted file mode 100644 index 4a157945af..0000000000 --- a/source4/scripting/ejs/smbcalls_ldb.c +++ /dev/null @@ -1,772 +0,0 @@ -/* - Unix SMB/CIFS implementation. - - provide hooks into smbd C calls from ejs scripts - - Copyright (C) Andrew Tridgell 2005 - Copyright (C) Jelmer Vernooij 2005 - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#include "includes.h" -#include "scripting/ejs/smbcalls.h" -#include "lib/appweb/ejs/ejs.h" -#include "lib/ldb/include/ldb.h" -#include "lib/ldb/include/ldb_errors.h" -#include "ldb_wrap.h" -#include "dsdb/samdb/samdb.h" -#include "librpc/ndr/libndr.h" -#include "libcli/security/security.h" - -/* - get the connected db - */ -static struct ldb_context *ejs_get_ldb_context(int eid) -{ - struct ldb_context *ldb = (struct ldb_context *)mprGetThisPtr(eid, "db"); - if (ldb == NULL) { - ejsSetErrorMsg(eid, "invalid ldb connection"); - } - return ldb; -} - -/* - perform an ldb search, returning an array of results - - syntax: - res = ldb.search("expression"); - var attrs = new Array("attr1", "attr2", "attr3"); - ldb.search("expression", attrs); - var basedn = "cn=this,dc=is,dc=a,dc=test"; - ldb.search("expression", basedn, ldb.SCOPE_SUBTREE, attrs); - ldb.search("expression", basedn, ldb.SCOPE_SUBTREE, attrs, controls); -*/ -static int ejs_ldbSearch(MprVarHandle eid, int argc, struct MprVar **argv) -{ - const char **attrs = NULL; - const char *expression; - const char *base = NULL; - struct ldb_dn *basedn = NULL; - int scope = LDB_SCOPE_DEFAULT; - TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx()); - struct ldb_context *ldb; - int ret; - struct ldb_control **parsed_controls = NULL; - struct ldb_result *res=NULL; - struct ldb_request *req; - - /* validate arguments */ - if (argc < 1 || argc > 5) { - ejsSetErrorMsg(eid, "ldb.search invalid number of arguments"); - goto failed; - } - if (argc > 3 && argv[3]->type != MPR_TYPE_OBJECT) { - ejsSetErrorMsg(eid, "ldb.search attributes must be an object"); - goto failed; - } - - ldb = ejs_get_ldb_context(eid); - if (ldb == NULL) { - return -1; - } - - expression = mprToString(argv[0]); - if (argc > 1) { - base = mprToString(argv[1]); - /* a null basedn is valid */ - } - if (base != NULL) { - basedn = ldb_dn_new(tmp_ctx, ldb, base); - if ( ! ldb_dn_validate(basedn)) { - ejsSetErrorMsg(eid, "ldb.search malformed base dn"); - goto failed; - } - } else { - basedn = ldb_get_default_basedn(ldb); - } - if (argc > 2) { - scope = mprToInt(argv[2]); - switch (scope) { - case LDB_SCOPE_DEFAULT: - case LDB_SCOPE_BASE: - case LDB_SCOPE_ONELEVEL: - case LDB_SCOPE_SUBTREE: - break; /* ok */ - default: - ejsSetErrorMsg(eid, "ldb.search invalid scope"); - goto failed; - } - } - if (argc > 3) { - attrs = mprToList(tmp_ctx, argv[3]); - } - if (argc > 4) { - const char **controls; - controls = mprToList(tmp_ctx, argv[4]); - if (controls) { - parsed_controls = ldb_parse_control_strings(ldb, tmp_ctx, controls); - if (!parsed_controls) { - ejsSetErrorMsg(eid, "ldb.search cannot parse controls: %s", - ldb_errstring(ldb)); - goto failed; - } - } - } - - res = talloc_zero(tmp_ctx, struct ldb_result); - if (!res) { - return LDB_ERR_OPERATIONS_ERROR; - } - - ret = ldb_build_search_req(&req, ldb, tmp_ctx, - basedn, - scope, - expression, - attrs, - parsed_controls, - res, - ldb_search_default_callback); - - if (ret == LDB_SUCCESS) { - - ldb_set_timeout(ldb, req, 0); /* use default timeout */ - - ret = ldb_request(ldb, req); - - if (ret == LDB_SUCCESS) { - ret = ldb_wait(req->handle, LDB_WAIT_ALL); - } - } - - if (ret != LDB_SUCCESS) { - ejsSetErrorMsg(eid, "ldb.search failed - %s", ldb_errstring(ldb)); - mpr_Return(eid, mprLdbResult(ldb, ret, NULL)); - } else { - mpr_Return(eid, mprLdbResult(ldb, ret, res)); - } - - talloc_free(tmp_ctx); - return 0; - -failed: - talloc_free(tmp_ctx); - return -1; -} - - -/* - perform an ldb add or modify -*/ -static int ejs_ldbAddModify(MprVarHandle eid, int argc, struct MprVar **argv, - int fn(struct ldb_context *, const struct ldb_message *)) -{ - const char *ldifstring; - struct ldb_context *ldb; - struct ldb_ldif *ldif; - int ret = 0, count=0; - - if (argc != 1) { - ejsSetErrorMsg(eid, "ldb.add/modify invalid arguments"); - return -1; - } - - ldifstring = mprToString(argv[0]); - if (ldifstring == NULL) { - ejsSetErrorMsg(eid, "ldb.add/modify invalid arguments"); - return -1; - } - - ldb = ejs_get_ldb_context(eid); - if (ldb == NULL) { - return -1; - } - - while ((ldif = ldb_ldif_read_string(ldb, &ldifstring))) { - count++; - ret = fn(ldb, ldif->msg); - talloc_free(ldif); - if (ret != 0) break; - } - - if (count == 0) { - ejsSetErrorMsg(eid, "ldb.add/modify invalid ldif"); - return -1; - } - - mpr_Return(eid, mprLdbResult(ldb, ret, NULL)); - return 0; -} - - -/* - perform an ldb delete - usage: - ok = ldb.delete(dn); -*/ -static int ejs_ldbDelete(MprVarHandle eid, int argc, struct MprVar **argv) -{ - struct ldb_dn *dn; - struct ldb_context *ldb; - int ret; - - if (argc != 1) { - ejsSetErrorMsg(eid, "ldb.delete invalid arguments"); - return -1; - } - - ldb = ejs_get_ldb_context(eid); - if (ldb == NULL) { - return -1; - } - - dn = ldb_dn_new(ldb, ldb, mprToString(argv[0])); - if ( ! ldb_dn_validate(dn)) { - ejsSetErrorMsg(eid, "ldb.delete malformed dn"); - return -1; - } - - ret = ldb_delete(ldb, dn); - - talloc_free(dn); - - mpr_Return(eid, mprLdbResult(ldb, ret, NULL)); - return 0; -} - -/* - perform an ldb rename - usage: - ok = ldb.rename(dn1, dn2); -*/ -static int ejs_ldbRename(MprVarHandle eid, int argc, struct MprVar **argv) -{ - struct ldb_dn *dn1, *dn2; - struct ldb_context *ldb; - int ret; - - if (argc != 2) { - ejsSetErrorMsg(eid, "ldb.rename invalid arguments"); - return -1; - } - - ldb = ejs_get_ldb_context(eid); - if (ldb == NULL) { - return -1; - } - - dn1 = ldb_dn_new(ldb, ldb, mprToString(argv[0])); - dn2 = ldb_dn_new(ldb, ldb, mprToString(argv[1])); - if ( ! ldb_dn_validate(dn1) || ! ldb_dn_validate(dn2)) { - ejsSetErrorMsg(eid, "ldb.rename invalid or malformed arguments"); - return -1; - } - - ret = ldb_rename(ldb, dn1, dn2); - - talloc_free(dn1); - talloc_free(dn2); - - mpr_Return(eid, mprLdbResult(ldb, ret, NULL)); - return 0; -} - -/* - get last error message - usage: - ok = ldb.errstring(); -*/ -static int ejs_ldbErrstring(MprVarHandle eid, int argc, struct MprVar **argv) -{ - struct ldb_context *ldb; - - ldb = ejs_get_ldb_context(eid); - if (ldb == NULL) { - return -1; - } - - mpr_Return(eid, mprString(ldb_errstring(ldb))); - return 0; -} - -/* - base64 encode - usage: - dataout = ldb.encode(datain) - */ -static int ejs_base64encode(MprVarHandle eid, int argc, struct MprVar **argv) -{ - char *ret; - - if (argc != 1) { - ejsSetErrorMsg(eid, "ldb.base64encode invalid argument count"); - return -1; - } - - if (argv[0]->type == MPR_TYPE_STRING) { - const char *orig = mprToString(argv[0]); - ret = ldb_base64_encode(mprMemCtx(), orig, strlen(orig)); - } else { - DATA_BLOB *blob; - - blob = mprToDataBlob(argv[0]); - mprAssert(blob); - ret = ldb_base64_encode(mprMemCtx(), (char *)blob->data, blob->length); - } - - if (!ret) { - mpr_Return(eid, mprCreateUndefinedVar()); - } else { - mpr_Return(eid, mprString(ret)); - } - - talloc_free(ret); - - return 0; -} - -/* - base64 decode - usage: - dataout = ldb.decode(datain) - */ -static int ejs_base64decode(MprVarHandle eid, int argc, struct MprVar **argv) -{ - char *tmp; - int ret; - - if (argc != 1) { - ejsSetErrorMsg(eid, "ldb.base64encode invalid argument count"); - return -1; - } - - tmp = talloc_strdup(mprMemCtx(), mprToString(argv[0])); - ret = ldb_base64_decode(tmp); - if (ret == -1) { - mpr_Return(eid, mprCreateUndefinedVar()); - } else { - DATA_BLOB blob; - blob.data = (uint8_t *)tmp; - blob.length = ret; - mpr_Return(eid, mprDataBlob(blob)); - } - - talloc_free(tmp); - - return 0; -} - -/* - escape a DN - usage: - dataout = ldb.dn_escape(datain) - */ -static int ejs_dn_escape(MprVarHandle eid, int argc, struct MprVar **argv) -{ - char *ret; - struct ldb_val val; - - if (argc != 1) { - ejsSetErrorMsg(eid, "ldb.dn_escape invalid argument count"); - return -1; - } - - val = data_blob_string_const(mprToString(argv[0])); - - ret = ldb_dn_escape_value(mprMemCtx(), val); - if (ret == NULL) { - mpr_Return(eid, mprCreateUndefinedVar()); - } else { - mpr_Return(eid, mprString(ret)); - talloc_free(ret); - } - - return 0; -} - -/* - perform an ldb add - - syntax: - ok = ldb.add(ldifstring); -*/ -static int ejs_ldbAdd(MprVarHandle eid, int argc, struct MprVar **argv) -{ - return ejs_ldbAddModify(eid, argc, argv, ldb_add); -} - -/* - perform an ldb modify - - syntax: - ok = ldb.modify(ldifstring); -*/ -static int ejs_ldbModify(MprVarHandle eid, int argc, struct MprVar **argv) -{ - return ejs_ldbAddModify(eid, argc, argv, ldb_modify); -} - -/* - connect to a database - usage: - ok = ldb.connect(dbfile); - ok = ldb.connect(dbfile, "modules:modlist"); - - ldb.credentials or ldb.session_info may be setup first - -*/ -static int ejs_ldbConnect(MprVarHandle eid, int argc, char **argv) -{ - struct ldb_context *ldb; - struct auth_session_info *session_info = NULL; - struct cli_credentials *creds = NULL; - struct MprVar *credentials, *session; - struct MprVar *this = mprGetProperty(ejsGetLocalObject(eid), "this", 0); - - const char *dbfile; - - if (argc < 1) { - ejsSetErrorMsg(eid, "ldb.connect invalid arguments"); - return -1; - } - - credentials = mprGetProperty(this, "credentials", NULL); - if (credentials) { - creds = talloc_get_type(mprGetPtr(credentials, "creds"), struct cli_credentials); - } - - session = mprGetProperty(this, "session_info", NULL); - if (session) { - session_info = talloc_get_type(mprGetPtr(session, "session_info"), struct auth_session_info); - } - - dbfile = argv[0]; - - ldb = ldb_wrap_connect(mprMemCtx(), mprEventCtx(), mprLpCtx(), dbfile, - session_info, creds, - 0, (const char **)(argv+1)); - if (ldb == NULL) { - ejsSetErrorMsg(eid, "ldb.connect failed to open %s", dbfile); - } - - mprSetThisPtr(eid, "db", ldb); - mpr_Return(eid, mprCreateBoolVar(ldb != NULL)); - return 0; -} - - -/* - close a db connection -*/ -static int ejs_ldbClose(MprVarHandle eid, int argc, struct MprVar **argv) -{ - struct ldb_context *ldb; - - if (argc != 0) { - ejsSetErrorMsg(eid, "ldb.close invalid arguments"); - return -1; - } - - ldb = ejs_get_ldb_context(eid); - if (ldb == NULL) { - return -1; - } - - mprSetThisPtr(eid, "db", NULL); - mpr_Return(eid, mprCreateBoolVar(true)); - return 0; -} - - -/* - start a ldb transaction - usage: - ok = ldb.transaction_start(); -*/ -static int ejs_ldbTransactionStart(MprVarHandle eid, int argc, struct MprVar **argv) -{ - struct ldb_context *ldb; - int ret; - - if (argc != 0) { - ejsSetErrorMsg(eid, "ldb.transaction_start invalid arguments"); - return -1; - } - - ldb = ejs_get_ldb_context(eid); - if (ldb == NULL) { - return -1; - } - - ret = ldb_transaction_start(ldb); - - mpr_Return(eid, mprCreateBoolVar(ret == 0)); - return 0; -} - -/* - cancel a ldb transaction - usage: - ok = ldb.transaction_cancel(); -*/ -static int ejs_ldbTransactionCancel(MprVarHandle eid, int argc, struct MprVar **argv) -{ - struct ldb_context *ldb; - int ret; - - if (argc != 0) { - ejsSetErrorMsg(eid, "ldb.transaction_cancel invalid arguments"); - return -1; - } - - ldb = ejs_get_ldb_context(eid); - if (ldb == NULL) { - return -1; - } - - ret = ldb_transaction_cancel(ldb); - - mpr_Return(eid, mprCreateBoolVar(ret == 0)); - return 0; -} - -/* - commit a ldb transaction - usage: - ok = ldb.transaction_commit(); -*/ -static int ejs_ldbTransactionCommit(MprVarHandle eid, int argc, struct MprVar **argv) -{ - struct ldb_context *ldb; - int ret; - - if (argc != 0) { - ejsSetErrorMsg(eid, "ldb.transaction_commit invalid arguments"); - return -1; - } - - ldb = ejs_get_ldb_context(eid); - if (ldb == NULL) { - return -1; - } - - ret = ldb_transaction_commit(ldb); - - mpr_Return(eid, mprCreateBoolVar(ret == 0)); - return 0; -} - -/* - commit a ldb attach a dsdb_schema from ldif files - usage: - ok = ldb.attach_dsdb_schema_from_ldif("prefixMap ldif content", "definition ldif content") -*/ -static int ejs_ldb_attach_dsdb_schema_from_ldif(MprVarHandle eid, int argc, char **argv) -{ - struct ldb_context *ldb; - WERROR status; - const char *pf; - const char *df; - - if (argc != 2) { - ejsSetErrorMsg(eid, "ldb.attach_dsdb_schema_from_ldif invalid arguments"); - return -1; - } - - ldb = ejs_get_ldb_context(eid); - if (ldb == NULL) { - return -1; - } - - pf = argv[0]; - df = argv[1]; - - status = dsdb_attach_schema_from_ldif_file(ldb, pf, df); - - mpr_Return(eid, mprWERROR(status)); - return 0; -} - -/* - set a particular invocationId against the running LDB - usage: - ok = ldb.set_ntds_invocationId("7729aa4b-f990-41ad-b81a-8b6a14090f41"); -*/ -static int ejs_ldb_set_ntds_invocationId(MprVarHandle eid, int argc, char **argv) -{ - struct ldb_context *ldb; - NTSTATUS status; - struct GUID guid; - char *guid_str; - bool ok; - - if (argc != 1) { - ejsSetErrorMsg(eid, "ldb.set_ntds_invocationId invalid arguments"); - return -1; - } - - ldb = ejs_get_ldb_context(eid); - if (ldb == NULL) { - return -1; - } - - guid_str = argv[0]; - - status = GUID_from_string(guid_str, &guid); - if (!NT_STATUS_IS_OK(status)) { - ejsSetErrorMsg(eid, "ldb.set_ntds_invocationId - failed to parse GUID '%s' %s\n", - guid_str, nt_errstr(status)); - return -1; - } - - ok = samdb_set_ntds_invocation_id(ldb, &guid); - if (!ok) { - ejsSetErrorMsg(eid, "ldb.set_ntds_invocationId - failed to set cached ntds invocationId\n"); - return -1; - } - - mpr_Return(eid, mprCreateBoolVar(ok)); - return 0; -} - -/* - attach a particular ntds objectGUID against the current ldb - usage: - ok = ldb.set_ntds_objectGUID("7729aa4b-f990-41ad-b81a-8b6a14090f41"); -*/ -static int ejs_ldb_set_ntds_objectGUID(MprVarHandle eid, int argc, char **argv) -{ - struct ldb_context *ldb; - NTSTATUS status; - struct GUID guid; - char *guid_str; - bool ok; - - if (argc != 1) { - ejsSetErrorMsg(eid, "ldb.set_ntds_objectGUID invalid arguments"); - return -1; - } - - ldb = ejs_get_ldb_context(eid); - if (ldb == NULL) { - return -1; - } - - guid_str = argv[0]; - - status = GUID_from_string(guid_str, &guid); - if (!NT_STATUS_IS_OK(status)) { - ejsSetErrorMsg(eid, "ldb.set_ntds_objectGUID - failed to parse GUID '%s' %s\n", - guid_str, nt_errstr(status)); - return -1; - } - - ok = samdb_set_ntds_invocation_id(ldb, &guid); - if (!ok) { - ejsSetErrorMsg(eid, "ldb.set_ntds_objectGUID - failed to set cached ntds invocationId\n"); - return -1; - } - - mpr_Return(eid, mprCreateBoolVar(ok)); - return 0; -} - -/* - attach a particular domain SID against the current ldb - usage: - ok = ldb.set_domain_sid("S-S-1-5-21-3065342217-3567412576-2214182334"); -*/ -static int ejs_ldb_set_domain_sid(MprVarHandle eid, int argc, char **argv) -{ - struct ldb_context *ldb; - struct dom_sid *dom_sid; - char *dom_sid_str; - bool ok; - - if (argc != 1) { - ejsSetErrorMsg(eid, "ldb.set_domain_sid invalid arguments"); - return -1; - } - - ldb = ejs_get_ldb_context(eid); - if (ldb == NULL) { - return -1; - } - - dom_sid_str = argv[0]; - - dom_sid = dom_sid_parse_talloc(NULL, dom_sid_str); - if (!dom_sid) { - ejsSetErrorMsg(eid, "ldb.set_domain_sid - failed to parse domain sid '%s'\n", - dom_sid_str); - return -1; - } - - ok = samdb_set_domain_sid(ldb, dom_sid); - talloc_free(dom_sid); - if (!ok) { - ejsSetErrorMsg(eid, "ldb.set_domain_sid - failed to set cached ntds invocationId\n"); - return -1; - } - - mpr_Return(eid, mprCreateBoolVar(ok)); - return 0; -} - -/* - initialise ldb ejs subsystem -*/ -static int ejs_ldb_init(MprVarHandle eid, int argc, struct MprVar **argv) -{ - struct MprVar *ldb = mprInitObject(eid, "ldb", argc, argv); - - mprSetStringCFunction(ldb, "connect", ejs_ldbConnect); - mprSetCFunction(ldb, "search", ejs_ldbSearch); - mprSetCFunction(ldb, "add", ejs_ldbAdd); - mprSetCFunction(ldb, "modify", ejs_ldbModify); - mprSetCFunction(ldb, "del", ejs_ldbDelete); - mprSetCFunction(ldb, "rename", ejs_ldbRename); - mprSetCFunction(ldb, "errstring", ejs_ldbErrstring); - mprSetCFunction(ldb, "encode", ejs_base64encode); - mprSetCFunction(ldb, "decode", ejs_base64decode); - mprSetCFunction(ldb, "dn_escape", ejs_dn_escape); - mprSetCFunction(ldb, "close", ejs_ldbClose); - mprSetCFunction(ldb, "transaction_start", ejs_ldbTransactionStart); - mprSetCFunction(ldb, "transaction_cancel", ejs_ldbTransactionCancel); - mprSetCFunction(ldb, "transaction_commit", ejs_ldbTransactionCommit); - mprSetStringCFunction(ldb, "attach_dsdb_schema_from_ldif", - ejs_ldb_attach_dsdb_schema_from_ldif); - mprSetStringCFunction(ldb, "set_ntds_invocationId", - ejs_ldb_set_ntds_invocationId); - mprSetStringCFunction(ldb, "set_ntds_objectGUID", - ejs_ldb_set_ntds_objectGUID); - mprSetStringCFunction(ldb, "set_domain_sid", - ejs_ldb_set_domain_sid); - mprSetVar(ldb, "SCOPE_BASE", mprCreateNumberVar(LDB_SCOPE_BASE)); - mprSetVar(ldb, "SCOPE_ONE", mprCreateNumberVar(LDB_SCOPE_ONELEVEL)); - mprSetVar(ldb, "SCOPE_SUBTREE", mprCreateNumberVar(LDB_SCOPE_SUBTREE)); - mprSetVar(ldb, "SCOPE_DEFAULT", mprCreateNumberVar(LDB_SCOPE_DEFAULT)); - - return 0; -} - - -/* - setup C functions that be called from ejs -*/ -NTSTATUS smb_setup_ejs_ldb(void) -{ - ejsDefineCFunction(-1, "ldb_init", ejs_ldb_init, NULL, MPR_VAR_SCRIPT_HANDLE); - return NT_STATUS_OK; -} diff --git a/source4/scripting/ejs/smbcalls_options.c b/source4/scripting/ejs/smbcalls_options.c deleted file mode 100644 index 93872baa40..0000000000 --- a/source4/scripting/ejs/smbcalls_options.c +++ /dev/null @@ -1,193 +0,0 @@ -/* - Unix SMB/CIFS implementation. - - provide a command line options parsing function for ejs - - Copyright (C) Andrew Tridgell 2005 - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#include "includes.h" -#include "lib/cmdline/popt_common.h" -#include "scripting/ejs/smbcalls.h" - - -/* - usage: - options = GetOptions(argv, - "realm=s", - "enablexx", - "myint=i"); - - the special options POPT_COMMON_* options are recognised and replaced - with the Samba internal options - - resulting parsed options are placed in the options object - - additional command line arguments are placed in options.ARGV -*/ - -static int ejs_GetOptions(MprVarHandle eid, int argc, struct MprVar **argv) -{ - poptContext pc; - int opt; - struct { - const char *name; - struct poptOption *table; - const char *description; - } tables[] = { - { "POPT_AUTOHELP", poptHelpOptions, "Help options:" }, - { "POPT_COMMON_SAMBA", popt_common_samba, "Common Samba options:" }, - { "POPT_COMMON_CONNECTION", popt_common_connection, "Connection options:" }, - { "POPT_COMMON_CREDENTIALS", popt_common_credentials, "Authentication options:" }, - { "POPT_COMMON_VERSION", popt_common_version, "Common Samba options:" } - }; - - struct MprVar *options = mprInitObject(eid, "options", 0, NULL); - - TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx()); - struct poptOption *long_options = NULL; - int i, num_options = 0; - int opt_argc; - const char **opt_argv; - const char **opt_names = NULL; - const int BASE_OPTNUM = 0x100000; - - /* validate arguments */ - if (argc < 1 || argv[0]->type != MPR_TYPE_OBJECT) { - ejsSetErrorMsg(eid, "GetOptions invalid arguments"); - return -1; - } - - opt_argv = mprToArray(tmp_ctx, argv[0]); - opt_argc = str_list_length(opt_argv); - - long_options = talloc_array(tmp_ctx, struct poptOption, 1); - if (long_options == NULL) { - return -1; - } - - /* create the long_options array */ - for (i=1;itype != MPR_TYPE_STRING) { - ejsSetErrorMsg(eid, "GetOptions string argument"); - return -1; - } - - long_options = talloc_realloc(tmp_ctx, long_options, - struct poptOption, num_options+2); - if (long_options == NULL) { - return -1; - } - ZERO_STRUCT(long_options[num_options]); - - /* see if its one of the special samba option tables */ - for (t=0;t= num_options + BASE_OPTNUM) { - char *err; - err = talloc_asprintf(tmp_ctx, "%s: %s", - poptBadOption(pc, POPT_BADOPTION_NOALIAS), - poptStrerror(opt)); - mprSetVar(options, "ERROR", mprString(err)); - talloc_free(tmp_ctx); - mpr_Return(eid, mprCreateUndefinedVar()); - return 0; - } - opt -= BASE_OPTNUM; - arg = poptGetOptArg(pc); - if (arg == NULL) { - mprSetVar(options, opt_names[opt], mprCreateBoolVar(1)); - } else if (long_options[opt].argInfo == POPT_ARG_INT) { - int v = strtol(arg, NULL, 0); - mprSetVar(options, opt_names[opt], mprCreateIntegerVar(v)); - } else { - mprSetVar(options, opt_names[opt], mprString(arg)); - } - } - - /* setup options.argv list */ - mprSetVar(options, "ARGV", mprList("ARGV", poptGetArgs(pc))); - - poptFreeContext(pc); - - talloc_free(tmp_ctx); - - /* setup methods */ - mprSetCFunction(options, "get_credentials", ejs_credentials_cmdline); - - return 0; -} - - - -/* - setup C functions that be called from ejs -*/ -void smb_setup_ejs_options(void) -{ - ejsDefineCFunction(-1, "GetOptions", ejs_GetOptions, NULL, MPR_VAR_SCRIPT_HANDLE); -} diff --git a/source4/scripting/ejs/smbcalls_string.c b/source4/scripting/ejs/smbcalls_string.c deleted file mode 100644 index 541303ff2d..0000000000 --- a/source4/scripting/ejs/smbcalls_string.c +++ /dev/null @@ -1,529 +0,0 @@ -/* - Unix SMB/CIFS implementation. - - provide access to string functions - - Copyright (C) Andrew Tridgell 2005 - Copyright (C) Jelmer Vernooij 2005 (substr) - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#include "includes.h" -#include "scripting/ejs/smbcalls.h" -#include "lib/appweb/ejs/ejs.h" - -/* - usage: - var len = strlen(str); -*/ -static int ejs_strlen(MprVarHandle eid, int argc, char **argv) -{ - if (argc != 1) { - ejsSetErrorMsg(eid, "strlen invalid arguments"); - return -1; - } - mpr_Return(eid, mprCreateIntegerVar(strlen_m(argv[0]))); - return 0; -} - -/* - usage: - var s = strlower("UPPER"); -*/ -static int ejs_strlower(MprVarHandle eid, int argc, char **argv) -{ - char *s; - if (argc != 1) { - ejsSetErrorMsg(eid, "strlower invalid arguments"); - return -1; - } - s = strlower_talloc(mprMemCtx(), argv[0]); - mpr_Return(eid, mprString(s)); - talloc_free(s); - return 0; -} - -/* - usage: - var s = strupper("lower"); -*/ -static int ejs_strupper(MprVarHandle eid, int argc, char **argv) -{ - char *s; - if (argc != 1) { - ejsSetErrorMsg(eid, "strupper invalid arguments"); - return -1; - } - s = strupper_talloc(mprMemCtx(), argv[0]); - mpr_Return(eid, mprString(s)); - talloc_free(s); - return 0; -} - -/* - usage: - var s = strstr(string, substring); -*/ -static int ejs_strstr(MprVarHandle eid, int argc, char **argv) -{ - char *s; - if (argc != 2) { - ejsSetErrorMsg(eid, "strstr invalid arguments"); - return -1; - } - s = strstr(argv[0], argv[1]); - mpr_Return(eid, mprString(s)); - return 0; -} - -/* - usage: - var s = strspn(string, legal_chars_string); -*/ -static int ejs_strspn(MprVarHandle eid, int argc, char **argv) -{ - int len; - if (argc != 2) { - ejsSetErrorMsg(eid, "strspn invalid arguments"); - return -1; - } - len = strspn(argv[0], argv[1]); - mpr_Return(eid, mprCreateIntegerVar(len)); - return 0; -} - -/* - usage: - list = split(".", "a.foo.bar"); - list = split(".", "a.foo.bar", count); - - count is an optional count of how many splits to make - - NOTE: does not take a regular expression, unlike perl split() -*/ -static int ejs_split(MprVarHandle eid, int argc, struct MprVar **argv) -{ - const char *separator, *s; - char *p; - struct MprVar ret; - int count = 0, maxcount=0; - TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx()); - if (argc < 2 || - argv[0]->type != MPR_TYPE_STRING || - argv[1]->type != MPR_TYPE_STRING) { - ejsSetErrorMsg(eid, "split invalid arguments"); - return -1; - } - separator = mprToString(argv[0]); - s = mprToString(argv[1]); - if (argc == 3) { - maxcount = mprToInt(argv[2]); - } - - ret = mprArray("list"); - - while ((p = strstr(s, separator))) { - char *s2 = talloc_strndup(tmp_ctx, s, (int)(p-s)); - mprAddArray(&ret, count++, mprString(s2)); - talloc_free(s2); - s = p + strlen(separator); - if (maxcount != 0 && count >= maxcount) { - break; - } - } - if (*s) { - mprAddArray(&ret, count++, mprString(s)); - } - talloc_free(tmp_ctx); - mpr_Return(eid, ret); - return 0; -} - -/* - usage: - str = substr(orig[, start_offset[, length]]); - - special cases: - if start_offset < 0 then start_offset+=strlen(orig) - if length < 0 then length+=strlen(orig)-start_offset - - (as found in many other languages) -*/ -static int ejs_substr(MprVarHandle eid, int argc, struct MprVar **argv) -{ - int start_offset = 0; - int length = 0; - const char *orig; - char *target; - - if (argc < 1 || argc > 3 || - argv[0]->type != MPR_TYPE_STRING) { - ejsSetErrorMsg(eid, "substr invalid arguments"); - return -1; - } - - if (argc == 1) { - mpr_Return(eid, *argv[0]); - return 0; - } - - orig = mprToString(argv[0]); - start_offset = mprToInt(argv[1]); - length = strlen(orig); - if (start_offset < 0) start_offset += strlen(orig); - if (start_offset < 0 || start_offset > strlen(orig)) { - ejsSetErrorMsg(eid, "substr arg 2 out of bounds ([%s], %d)", orig, start_offset); - return -1; - } - - if (argc == 3) { - length = mprToInt(argv[2]); - if (length < 0) length += strlen(orig) - start_offset; - if (length < 0 || length+start_offset > strlen(orig)) { - ejsSetErrorMsg(eid, "substr arg 3 out of bounds ([%s], %d, %d)", orig, start_offset, length); - return -1; - } - } - - target = talloc_strndup(mprMemCtx(), orig+start_offset, length); - - mpr_Return(eid, mprString(target)); - - talloc_free(target); - - return 0; -} - -/* - usage: - str = join("DC=", list); -*/ -static int ejs_join(MprVarHandle eid, int argc, struct MprVar **argv) -{ - int i; - const char *separator; - char *ret = NULL; - const char **list; - TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx()); - if (argc != 2 || - argv[0]->type != MPR_TYPE_STRING || - argv[1]->type != MPR_TYPE_OBJECT) { - ejsSetErrorMsg(eid, "join invalid arguments"); - return -1; - } - - separator = mprToString(argv[0]); - list = mprToArray(tmp_ctx, argv[1]); - - if (list == NULL || list[0] == NULL) { - talloc_free(tmp_ctx); - mpr_Return(eid, mprString(NULL)); - return 0; - } - - ret = talloc_strdup(tmp_ctx, list[0]); - if (ret == NULL) { - goto failed; - } - for (i=1;list[i];i++) { - ret = talloc_asprintf_append_buffer(ret, "%s%s", separator, list[i]); - if (ret == NULL) { - goto failed; - } - } - mpr_Return(eid, mprString(ret)); - talloc_free(tmp_ctx); - return 0; -failed: - ejsSetErrorMsg(eid, "out of memory"); - return -1; -} - - -/* - blergh, C certainly makes this hard! - usage: - str = sprintf("i=%d s=%7s", 7, "foo"); -*/ -typedef char *(*_asprintf_append_t)(char *, const char *, ...); -static int ejs_sprintf(MprVarHandle eid, int argc, struct MprVar **argv) -{ - const char *format; - const char *p; - char *ret; - int a = 1; - _asprintf_append_t _asprintf_append; - TALLOC_CTX *tmp_ctx; - if (argc < 1 || argv[0]->type != MPR_TYPE_STRING) { - ejsSetErrorMsg(eid, "sprintf invalid arguments"); - return -1; - } - format = mprToString(argv[0]); - tmp_ctx = talloc_new(mprMemCtx()); - ret = talloc_strdup(tmp_ctx, ""); - - /* avoid all the format string warnings */ - _asprintf_append = (_asprintf_append_t)talloc_asprintf_append_buffer; - - /* - hackity hack ... - */ - while ((p = strchr(format, '%'))) { - char *fmt2; - int len, len_count=0; - char *tstr; - ret = talloc_asprintf_append_buffer(ret, "%*.*s", - (int)(p-format), (int)(p-format), - format); - if (ret == NULL) goto failed; - format += (int)(p-format); - len = strcspn(p+1, "dxuiofgGpXeEFcs%") + 1; - fmt2 = talloc_strndup(tmp_ctx, p, len+1); - if (fmt2 == NULL) goto failed; - len_count = count_chars(fmt2, '*'); - /* find the type string */ - tstr = &fmt2[len]; - while (tstr > fmt2 && isalpha((unsigned char)tstr[-1])) { - tstr--; - } - if (strcmp(tstr, "%") == 0) { - ret = talloc_asprintf_append_buffer(ret, "%%"); - if (ret == NULL) { - goto failed; - } - format += len+1; - continue; - } - if (len_count > 2 || - argc < a + len_count + 1) { - ejsSetErrorMsg(eid, "sprintf: not enough arguments for format"); - goto failed; - } -#define FMT_ARG(fn, type) do { \ - switch (len_count) { \ - case 0: \ - ret = _asprintf_append(ret, fmt2, \ - (type)fn(argv[a])); \ - break; \ - case 1: \ - ret = _asprintf_append(ret, fmt2, \ - (int)mprVarToNumber(argv[a]), \ - (type)fn(argv[a+1])); \ - break; \ - case 2: \ - ret = _asprintf_append(ret, fmt2, \ - (int)mprVarToNumber(argv[a]), \ - (int)mprVarToNumber(argv[a+1]), \ - (type)fn(argv[a+2])); \ - break; \ - } \ - a += len_count + 1; \ - if (ret == NULL) { \ - goto failed; \ - } \ -} while (0) - - if (strcmp(tstr, "s")==0) FMT_ARG(mprToString, const char *); - else if (strcmp(tstr, "c")==0) FMT_ARG(*mprToString, char); - else if (strcmp(tstr, "d")==0) FMT_ARG(mprVarToNumber, int); - else if (strcmp(tstr, "ld")==0) FMT_ARG(mprVarToNumber, long); - else if (strcmp(tstr, "lld")==0) FMT_ARG(mprVarToNumber, long long); - else if (strcmp(tstr, "x")==0) FMT_ARG(mprVarToNumber, int); - else if (strcmp(tstr, "lx")==0) FMT_ARG(mprVarToNumber, long); - else if (strcmp(tstr, "llx")==0) FMT_ARG(mprVarToNumber, long long); - else if (strcmp(tstr, "X")==0) FMT_ARG(mprVarToNumber, int); - else if (strcmp(tstr, "lX")==0) FMT_ARG(mprVarToNumber, long); - else if (strcmp(tstr, "llX")==0) FMT_ARG(mprVarToNumber, long long); - else if (strcmp(tstr, "u")==0) FMT_ARG(mprVarToNumber, int); - else if (strcmp(tstr, "lu")==0) FMT_ARG(mprVarToNumber, long); - else if (strcmp(tstr, "llu")==0) FMT_ARG(mprVarToNumber, long long); - else if (strcmp(tstr, "i")==0) FMT_ARG(mprVarToNumber, int); - else if (strcmp(tstr, "li")==0) FMT_ARG(mprVarToNumber, long); - else if (strcmp(tstr, "lli")==0) FMT_ARG(mprVarToNumber, long long); - else if (strcmp(tstr, "o")==0) FMT_ARG(mprVarToNumber, int); - else if (strcmp(tstr, "lo")==0) FMT_ARG(mprVarToNumber, long); - else if (strcmp(tstr, "llo")==0) FMT_ARG(mprVarToNumber, long long); - else if (strcmp(tstr, "f")==0) FMT_ARG(mprVarToFloat, double); - else if (strcmp(tstr, "lf")==0) FMT_ARG(mprVarToFloat, double); - else if (strcmp(tstr, "g")==0) FMT_ARG(mprVarToFloat, double); - else if (strcmp(tstr, "lg")==0) FMT_ARG(mprVarToFloat, double); - else if (strcmp(tstr, "e")==0) FMT_ARG(mprVarToFloat, double); - else if (strcmp(tstr, "le")==0) FMT_ARG(mprVarToFloat, double); - else if (strcmp(tstr, "E")==0) FMT_ARG(mprVarToFloat, double); - else if (strcmp(tstr, "lE")==0) FMT_ARG(mprVarToFloat, double); - else if (strcmp(tstr, "F")==0) FMT_ARG(mprVarToFloat, double); - else if (strcmp(tstr, "lF")==0) FMT_ARG(mprVarToFloat, double); - else { - ejsSetErrorMsg(eid, "sprintf: unknown format string '%s'", fmt2); - goto failed; - } - format += len+1; - } - - ret = talloc_asprintf_append_buffer(ret, "%s", format); - mpr_Return(eid, mprString(ret)); - talloc_free(tmp_ctx); - return 0; - -failed: - talloc_free(tmp_ctx); - return -1; -} - -/* - used to build your own print function - str = vsprintf(args); -*/ -static int ejs_vsprintf(MprVarHandle eid, int argc, struct MprVar **argv) -{ - struct MprVar **args, *len, *v; - int i, ret, length; - if (argc != 1 || argv[0]->type != MPR_TYPE_OBJECT) { - ejsSetErrorMsg(eid, "vsprintf invalid arguments"); - return -1; - } - v = argv[0]; - len = mprGetProperty(v, "length", NULL); - if (len == NULL) { - ejsSetErrorMsg(eid, "vsprintf takes an array"); - return -1; - } - length = mprToInt(len); - args = talloc_array(mprMemCtx(), struct MprVar *, length); - if (args == NULL) { - return -1; - } - - for (i=0;i. -*/ - -#include "includes.h" -#include "scripting/ejs/smbcalls.h" -#include "lib/appweb/ejs/ejs.h" -#include "lib/ldb/include/ldb.h" -#include "system/time.h" -#include "system/network.h" -#include "lib/socket/netif.h" - -/* - return the list of configured network interfaces -*/ -static int ejs_sys_interfaces(MprVarHandle eid, int argc, struct MprVar **argv) -{ - int i, count; - struct MprVar ret = mprArray("interfaces"); - struct interface *ifaces; - - load_interfaces(NULL, lp_interfaces(mprLpCtx()), &ifaces); - - count = iface_count(ifaces); - for (i=0;itype)) { - ejsSetErrorMsg(eid, "sys_unix2nttime invalid arguments"); - return -1; - } - unix_to_nt_time(&nt, mprVarToNumber(argv[0])); - v = mprCreateNumberVar(nt); - mpr_Return(eid, v); - return 0; -} - -/* - return the GMT time represented by the struct tm argument, as a time_t value -*/ -static int ejs_sys_gmmktime(MprVarHandle eid, int argc, struct MprVar **argv) -{ - struct MprVar *o; - struct tm tm; - if (argc != 1 || !mprVarIsObject(argv[0]->type)) { - ejsSetErrorMsg(eid, "sys_gmmktime invalid arguments"); - return -1; - } - - o = argv[0]; -#define TM_EL(n) tm.n = mprVarToNumber(mprGetProperty(o, #n, NULL)) - TM_EL(tm_sec); - TM_EL(tm_min); - TM_EL(tm_hour); - TM_EL(tm_mday); - TM_EL(tm_mon); - TM_EL(tm_year); - TM_EL(tm_wday); - TM_EL(tm_yday); - TM_EL(tm_isdst); -#undef TM_EL - - mpr_Return(eid, mprCreateIntegerVar(mktime(&tm))); - return 0; -} - -/* - return the given time as a gmtime structure -*/ -static int ejs_sys_gmtime(MprVarHandle eid, int argc, struct MprVar **argv) -{ - time_t t; - struct MprVar ret; - struct tm *tm; - if (argc != 1 || !mprVarIsNumber(argv[0]->type)) { - ejsSetErrorMsg(eid, "sys_gmtime invalid arguments"); - return -1; - } - t = (time_t) mprVarToNumber(argv[0]); - tm = gmtime(&t); - if (tm == NULL) { - mpr_Return(eid, mprCreateUndefinedVar()); - return 0; - } - ret = mprObject("gmtime"); -#define TM_EL(n) mprSetVar(&ret, #n, mprCreateIntegerVar(tm->n)) - TM_EL(tm_sec); - TM_EL(tm_min); - TM_EL(tm_hour); - TM_EL(tm_mday); - TM_EL(tm_mon); - TM_EL(tm_year); - TM_EL(tm_wday); - TM_EL(tm_yday); - TM_EL(tm_isdst); -#undef TM_EL - - mpr_Return(eid, ret); - return 0; -} - -/* - return the given NT time as a time_t value -*/ -static int ejs_sys_nttime2unix(MprVarHandle eid, int argc, struct MprVar **argv) -{ - time_t t; - struct MprVar v; - if (argc != 1 || !mprVarIsNumber(argv[0]->type)) { - ejsSetErrorMsg(eid, "sys_ntgmtime invalid arguments"); - return -1; - } - t = nt_time_to_unix(mprVarToNumber(argv[0])); - v = mprCreateNumberVar(t); - mpr_Return(eid, v); - return 0; -} - -/* - return the given NT time as a gmtime structure -*/ -static int ejs_sys_ntgmtime(MprVarHandle eid, int argc, struct MprVar **argv) -{ - time_t t; - struct MprVar ret; - struct tm *tm; - if (argc != 1 || !mprVarIsNumber(argv[0]->type)) { - ejsSetErrorMsg(eid, "sys_ntgmtime invalid arguments"); - return -1; - } - t = nt_time_to_unix(mprVarToNumber(argv[0])); - tm = gmtime(&t); - if (tm == NULL) { - mpr_Return(eid, mprCreateUndefinedVar()); - return 0; - } - ret = mprObject("gmtime"); -#define TM_EL(n) mprSetVar(&ret, #n, mprCreateIntegerVar(tm->n)) - TM_EL(tm_sec); - TM_EL(tm_min); - TM_EL(tm_hour); - TM_EL(tm_mday); - TM_EL(tm_mon); - TM_EL(tm_year); - TM_EL(tm_wday); - TM_EL(tm_yday); - TM_EL(tm_isdst); -#undef TM_EL - - mpr_Return(eid, ret); - return 0; -} - -/* - return a ldap time string from a nttime -*/ -static int ejs_sys_ldaptime(MprVarHandle eid, int argc, struct MprVar **argv) -{ - char *s; - time_t t; - if (argc != 1 || !mprVarIsNumber(argv[0]->type)) { - ejsSetErrorMsg(eid, "sys_ldaptime invalid arguments"); - return -1; - } - t = nt_time_to_unix(mprVarToNumber(argv[0])); - s = ldb_timestring(mprMemCtx(), t); - mpr_Return(eid, mprString(s)); - talloc_free(s); - return 0; -} - -/* - return a http time string from a nttime -*/ -static int ejs_sys_httptime(MprVarHandle eid, int argc, struct MprVar **argv) -{ - char *s; - time_t t; - if (argc != 1 || !mprVarIsNumber(argv[0]->type)) { - ejsSetErrorMsg(eid, "sys_httptime invalid arguments"); - return -1; - } - t = nt_time_to_unix(mprVarToNumber(argv[0])); - s = http_timestring(mprMemCtx(), t); - mpr_Return(eid, mprString(s)); - talloc_free(s); - return 0; -} - -/* - unlink a file - ok = sys.unlink(fname); -*/ -static int ejs_sys_unlink(MprVarHandle eid, int argc, char **argv) -{ - int ret; - if (argc != 1) { - ejsSetErrorMsg(eid, "sys_unlink invalid arguments"); - return -1; - } - ret = unlink(argv[0]); - mpr_Return(eid, mprCreateBoolVar(ret == 0)); - return 0; -} - -/* - load a file as a string - usage: - string = sys.file_load(filename); -*/ -static int ejs_sys_file_load(MprVarHandle eid, int argc, char **argv) -{ - char *s; - if (argc != 1) { - ejsSetErrorMsg(eid, "sys_file_load invalid arguments"); - return -1; - } - - s = file_load(argv[0], NULL, mprMemCtx()); - mpr_Return(eid, mprString(s)); - talloc_free(s); - return 0; -} - -/* - save a file from a string - usage: - ok = sys.file_save(filename, str); -*/ -static int ejs_sys_file_save(MprVarHandle eid, int argc, char **argv) -{ - bool ret; - if (argc != 2) { - ejsSetErrorMsg(eid, "sys_file_save invalid arguments"); - return -1; - } - ret = file_save(argv[0], argv[1], strlen(argv[1])); - mpr_Return(eid, mprCreateBoolVar(ret)); - return 0; -} - -/* - mkdir() - usage: - ok = sys.mkdir(dirname, mode); -*/ -static int ejs_sys_mkdir(MprVarHandle eid, int argc, struct MprVar **argv) -{ - bool ret; - char *name; - if (argc != 2) { - ejsSetErrorMsg(eid, "sys_mkdir invalid arguments, need mkdir(dirname, mode)"); - return -1; - } - if (!mprVarIsString(argv[0]->type)) { - ejsSetErrorMsg(eid, "sys_mkdir dirname not a string"); - return -1; - } - if (!mprVarIsNumber(argv[1]->type)) { - ejsSetErrorMsg(eid, "sys_mkdir mode not a number"); - return -1; - } - mprVarToString(&name, 0, NULL, argv[0]); - ret = mkdir(name, mprVarToNumber(argv[1])); - mpr_Return(eid, mprCreateBoolVar(ret == 0)); - return 0; -} - - -/* - return fields of a stat() call -*/ -static struct MprVar mpr_stat(struct stat *st) -{ - struct MprVar ret; - ret = mprObject("stat"); - -#define ST_EL(n) mprSetVar(&ret, #n, mprCreateNumberVar(st->n)) - ST_EL(st_dev); - ST_EL(st_ino); - ST_EL(st_mode); - ST_EL(st_nlink); - ST_EL(st_uid); - ST_EL(st_gid); - ST_EL(st_rdev); - ST_EL(st_size); - ST_EL(st_blksize); - ST_EL(st_blocks); - ST_EL(st_atime); - ST_EL(st_mtime); - ST_EL(st_ctime); - - return ret; -} - -/* - usage: - var st = sys.stat(filename); - returns an object containing struct stat elements -*/ -static int ejs_sys_stat(MprVarHandle eid, int argc, char **argv) -{ - struct stat st; - /* validate arguments */ - if (argc != 1) { - ejsSetErrorMsg(eid, "sys.stat invalid arguments"); - return -1; - } - if (stat(argv[0], &st) != 0) { - mpr_Return(eid, mprCreateUndefinedVar()); - } else { - mpr_Return(eid, mpr_stat(&st)); - } - return 0; -} - -/* - usage: - var st = sys.lstat(filename); - returns an object containing struct stat elements -*/ -static int ejs_sys_lstat(MprVarHandle eid, int argc, char **argv) -{ - struct stat st; - /* validate arguments */ - if (argc != 1) { - ejsSetErrorMsg(eid, "sys.stat invalid arguments"); - return -1; - } - if (lstat(argv[0], &st) != 0) { - mpr_Return(eid, mprCreateUndefinedVar()); - } else { - mpr_Return(eid, mpr_stat(&st)); - } - return 0; -} - -/* - bitwise AND - usage: - var z = sys.bitAND(x, 0x70); -*/ -static int ejs_sys_bitAND(MprVarHandle eid, int argc, struct MprVar **argv) -{ - int x, y, z; - - if (argc != 2 || - !mprVarIsNumber(argv[0]->type) || - !mprVarIsNumber(argv[1]->type)) { - ejsSetErrorMsg(eid, "bitand invalid arguments"); - return -1; - } - x = mprToInt(argv[0]); - y = mprToInt(argv[1]); - z = x & y; - - mpr_Return(eid, mprCreateIntegerVar(z)); - return 0; -} - -/* - bitwise OR - usage: - var z = sys.bitOR(x, 0x70); -*/ -static int ejs_sys_bitOR(MprVarHandle eid, int argc, struct MprVar **argv) -{ - int x, y, z; - - if (argc != 2 || - !mprVarIsNumber(argv[0]->type) || - !mprVarIsNumber(argv[1]->type)) { - ejsSetErrorMsg(eid, "bitand invalid arguments"); - return -1; - } - x = mprToInt(argv[0]); - y = mprToInt(argv[1]); - z = x | y; - - mpr_Return(eid, mprCreateIntegerVar(z)); - return 0; -} - -/* - initialise sys ejs subsystem -*/ -static int ejs_sys_init(MprVarHandle eid, int argc, struct MprVar **argv) -{ - struct MprVar *obj = mprInitObject(eid, "sys", argc, argv); - - mprSetCFunction(obj, "interfaces", ejs_sys_interfaces); - mprSetCFunction(obj, "hostname", ejs_sys_hostname); - mprSetCFunction(obj, "nttime", ejs_sys_nttime); - mprSetCFunction(obj, "gettimeofday", ejs_sys_gettimeofday); - mprSetCFunction(obj, "unix2nttime", ejs_sys_unix2nttime); - mprSetCFunction(obj, "gmmktime", ejs_sys_gmmktime); - mprSetCFunction(obj, "gmtime", ejs_sys_gmtime); - mprSetCFunction(obj, "nttime2unix", ejs_sys_nttime2unix); - mprSetCFunction(obj, "ntgmtime", ejs_sys_ntgmtime); - mprSetCFunction(obj, "ldaptime", ejs_sys_ldaptime); - mprSetCFunction(obj, "httptime", ejs_sys_httptime); - mprSetCFunction(obj, "mkdir", ejs_sys_mkdir); - mprSetStringCFunction(obj, "unlink", ejs_sys_unlink); - mprSetStringCFunction(obj, "file_load", ejs_sys_file_load); - mprSetStringCFunction(obj, "file_save", ejs_sys_file_save); - mprSetStringCFunction(obj, "stat", ejs_sys_stat); - mprSetStringCFunction(obj, "lstat", ejs_sys_lstat); - mprSetCFunction(obj, "bitAND", ejs_sys_bitAND); - mprSetCFunction(obj, "bitOR", ejs_sys_bitOR); - - return 0; -} - - -/* - setup C functions that be called from ejs -*/ -NTSTATUS smb_setup_ejs_system(void) -{ - ejsDefineCFunction(-1, "sys_init", ejs_sys_init, NULL, MPR_VAR_SCRIPT_HANDLE); - return NT_STATUS_OK; -} diff --git a/source4/scripting/ejs/smbscript.c b/source4/scripting/ejs/smbscript.c deleted file mode 100644 index db9fc9affa..0000000000 --- a/source4/scripting/ejs/smbscript.c +++ /dev/null @@ -1,129 +0,0 @@ -/* - Unix SMB/CIFS implementation. - - Standalone client for ejs scripting. - - Copyright (C) Tim Potter 2005 - Copyright (C) Andrew Tridgell 2005 - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#include "includes.h" -#include "lib/appweb/ejs/ejs.h" -#include "lib/appweb/ejs/ejsInternal.h" -#include "scripting/ejs/smbcalls.h" -#include "auth/gensec/gensec.h" -#include "ldb/include/ldb.h" -#include "dynconfig/dynconfig.h" - -static EjsId eid; - -_NORETURN_ static void smbscript_ejs_exception(const char *reason) -{ - Ejs *ep = ejsPtr(eid); - ejsSetErrorMsg(eid, "%s", reason); - fprintf(stderr, "%s", ep->error); - exit(127); -} - -int main(int argc, const char **argv) -{ - EjsHandle handle = 0; - MprVar result; - char *emsg, *script; - size_t script_size; - TALLOC_CTX *mem_ctx = talloc_new(NULL); - const char **argv_list = NULL; - const char *fname; - struct MprVar *return_var; - int exit_status, i; - struct loadparm_context *lp_ctx; - - fault_setup(argv[0]); - - global_loadparm = lp_ctx = loadparm_init(talloc_autofree_context()); - - if (getenv("SMB_CONF_PATH")) { - lp_load(lp_ctx, getenv("SMB_CONF_PATH")); - } else { - lp_load(lp_ctx, dyn_CONFIGFILE); - } - - gensec_init(lp_ctx); - mprSetCtx(mem_ctx); - - - if (argc < 2) { - fprintf(stderr, "You must supply a script name\n"); - exit(1); - } - - fname = argv[1]; - - if (ejsOpen(NULL, NULL, NULL) != 0) { - fprintf(stderr, "smbscript: ejsOpen(): unable to initialise " - "EJS subsystem\n"); - exit(127); - } - - smb_setup_ejs_functions(smbscript_ejs_exception); - - if ((eid = ejsOpenEngine(handle, 0)) == (EjsId)-1) { - fprintf(stderr, "smbscript: ejsOpenEngine(): unable to " - "initialise an EJS engine\n"); - exit(127); - } - - /* setup ARGV[] in the ejs environment */ - for (i=1;argv[i];i++) { - argv_list = str_list_add(argv_list, argv[i]); - } - talloc_steal(mem_ctx, argv_list); - mprSetVar(ejsGetGlobalObject(eid), "ARGV", mprList("ARGV", argv_list)); - - /* load the script and advance past interpreter line*/ - script = file_load(fname, &script_size, mem_ctx); - - if (!script) { - fprintf(stderr, "Unable to load script from '%s'\n", fname); - exit(1); - } - - /* allow scriptable js */ - if (strncmp(script, "#!", 2) == 0) { - script += strcspn(script, "\r\n"); - script += strspn(script, "\r\n"); - } - /* and this copes with the ugly exec hack */ - if (strncmp(script, "exec ", 5) == 0) { - script += strcspn(script, "\r\n"); - script += strspn(script, "\r\n"); - } - - /* run the script */ - if (ejsEvalScript(eid, script, &result, &emsg) == -1) { - fprintf(stderr, "smbscript: ejsEvalScript(): %s\n", emsg); - exit(127); - } - - return_var = ejsGetReturnValue(eid); - exit_status = mprVarToNumber(return_var); - - ejsClose(); - - talloc_free(mem_ctx); - - return exit_status; -} -- cgit From f594044d79346b0862336b9e23dc16736d16c9f7 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Tue, 16 Sep 2008 18:09:34 +0200 Subject: Remove remaining JavaScript code. --- source4/scripting/libjs/base.js | 50 ----------------------------------------- 1 file changed, 50 deletions(-) delete mode 100644 source4/scripting/libjs/base.js (limited to 'source4/scripting') diff --git a/source4/scripting/libjs/base.js b/source4/scripting/libjs/base.js deleted file mode 100644 index 790dfeb3e0..0000000000 --- a/source4/scripting/libjs/base.js +++ /dev/null @@ -1,50 +0,0 @@ -/* - base js library functions - Copyright Andrew Tridgell 2005 - released under the GNU GPL version 3 or later -*/ - -if (global["HAVE_BASE_JS"] != undefined) { - return; -} -HAVE_BASE_JS=1 - -/* bring the string functions into the global frame */ -string_init(global); - -/* - an essential function! -*/ -function printf() -{ - print(vsprintf(arguments)); -} - -/* - substitute strings of the form ${NAME} in str, replacing - with substitutions from subobj -*/ -function substitute_var(str, subobj) -{ - var list = split("${", str); - var i; - for (i=1;i Date: Thu, 18 Sep 2008 21:32:30 +0200 Subject: Remove python extension, simplify some code. --- source4/scripting/bin/autoidl | 161 ++++++++++ source4/scripting/bin/autoidl.py | 161 ---------- source4/scripting/bin/epdump | 24 ++ source4/scripting/bin/epdump.py | 24 -- source4/scripting/bin/minschema | 585 +++++++++++++++++++++++++++++++++++++ source4/scripting/bin/minschema.py | 585 ------------------------------------- source4/scripting/bin/smbstatus | 6 +- 7 files changed, 773 insertions(+), 773 deletions(-) create mode 100755 source4/scripting/bin/autoidl delete mode 100755 source4/scripting/bin/autoidl.py create mode 100755 source4/scripting/bin/epdump delete mode 100644 source4/scripting/bin/epdump.py create mode 100755 source4/scripting/bin/minschema delete mode 100755 source4/scripting/bin/minschema.py (limited to 'source4/scripting') diff --git a/source4/scripting/bin/autoidl b/source4/scripting/bin/autoidl new file mode 100755 index 0000000000..eed4ba3a80 --- /dev/null +++ b/source4/scripting/bin/autoidl @@ -0,0 +1,161 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# Unix SMB/CIFS implementation. +# Copyright © Jelmer Vernooij 2008 +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +import sys + +MAX_OPNUM = 1000 +MAX_BASE_SIZE = 0x1000 +MAX_IFACE_VERSION = 1000 + +DCERPC_FAULT_OP_RNG_ERROR = 0x1c010002 +DCERPC_FAULT_NDR = 0x6f7 +NT_STATUS_NET_WRITE_FAULT = -1073741614 + + +sys.path.insert(0, "bin/python") + +from samba.dcerpc import ClientConnection + +def find_num_funcs(conn): + for i in xrange(MAX_OPNUM): + try: + conn.request(i, "") + except RuntimeError, (num, msg): + if num == DCERPC_FAULT_OP_RNG_ERROR: + return i + raise Exception("More than %d functions" % MAX_OPNUM) + +class Function: + def __init__(self, conn, opnum): + self.conn = conn + self.opnum = opnum + + def request(self, data): + assert isinstance(data, str) + self.conn.request(self.opnum, data) + + def valid_ndr(self, data): + try: + self.request(data) + except RuntimeError, (num, msg): + if num == DCERPC_FAULT_NDR: + return False + return True + + def find_base_request(self, start=""): + """Find the smallest possible request that we can do that is + valid. + + This generally means sending all zeroes so we get NULL pointers where + possible.""" + # TODO: Don't try with just 0's as there may be switch_is() variables + # for which 0 is not a valid value or variables with range() set + # See how much input bytes we need + for i in range(MAX_BASE_SIZE): + data = start + chr(0) * i + if self.valid_ndr(data): + return data + return None + + def check_decision_byte(self, base_request, i): + """Check whether the specified byte is a possible "decision" byte, + e.g. a byte that is part of a pointer, array size variable or + discriminant. + + Note that this function only checks if a byte is definitely a decision + byte. It may return False in some cases while the byte is actually + a decision byte.""" + data = list(base_request) + data[i] = chr(1) + return not self.valid_ndr("".join(data)) + + def find_deferrant_data(self, base_request, idx): + data = list(base_request) + data[idx*4] = chr(1) + # Just check that this is a pointer to something non-empty: + assert not self.valid_ndr("".join(data)) + + newdata = self.find_base_request("".join(data)) + + if newdata is None: + return None + + assert newdata.startswith(data) + + return newdata[len(data):] + + def find_idl(self): + base_request = self.find_base_request() + + if base_request is None: + raise Exception("Unable to determine base size for opnum %d" % self.opnum) + + print "\tBase request is %r" % base_request + + decision_byte_map = map(lambda x: self.check_decision_byte(base_request, x), range(len(base_request))) + + print decision_byte_map + + # find pointers + possible_pointers = map(all, + [decision_byte_map[i*4:(i+1)*4] for i in range(int(len(base_request)/4))]) + print possible_pointers + + pointer_deferrant_bases = map( + lambda x: self.find_deferrant_data(base_request, x) if possible_pointers[x] else None, range(len(possible_pointers))) + + print pointer_deferrant_bases + + +if len(sys.argv) < 3: + print "Usage: autoidl []" + sys.exit(1) + +(binding, uuid) = sys.argv[1:3] +if len(sys.argv) == 4: + version = sys.argv[3] +else: + version = None + +if version is None: + for i in range(MAX_IFACE_VERSION): + try: + conn = ClientConnection(binding, (uuid, i)) + except RuntimeError, (num, msg): + if num == NT_STATUS_NET_WRITE_FAULT: + continue + raise + else: + break +else: + conn = ClientConnection(binding, (uuid, version)) + +print "Figuring out number of connections...", +num_funcs = find_num_funcs(conn) +print "%d" % num_funcs + +# Figure out the syntax for each one +for i in range(num_funcs): + print "Function %d" % i + data = Function(conn, i) + try: + data.find_idl() + except Exception, e: + print "Error: %r" % e diff --git a/source4/scripting/bin/autoidl.py b/source4/scripting/bin/autoidl.py deleted file mode 100755 index eed4ba3a80..0000000000 --- a/source4/scripting/bin/autoidl.py +++ /dev/null @@ -1,161 +0,0 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*- - -# Unix SMB/CIFS implementation. -# Copyright © Jelmer Vernooij 2008 -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -# - -import sys - -MAX_OPNUM = 1000 -MAX_BASE_SIZE = 0x1000 -MAX_IFACE_VERSION = 1000 - -DCERPC_FAULT_OP_RNG_ERROR = 0x1c010002 -DCERPC_FAULT_NDR = 0x6f7 -NT_STATUS_NET_WRITE_FAULT = -1073741614 - - -sys.path.insert(0, "bin/python") - -from samba.dcerpc import ClientConnection - -def find_num_funcs(conn): - for i in xrange(MAX_OPNUM): - try: - conn.request(i, "") - except RuntimeError, (num, msg): - if num == DCERPC_FAULT_OP_RNG_ERROR: - return i - raise Exception("More than %d functions" % MAX_OPNUM) - -class Function: - def __init__(self, conn, opnum): - self.conn = conn - self.opnum = opnum - - def request(self, data): - assert isinstance(data, str) - self.conn.request(self.opnum, data) - - def valid_ndr(self, data): - try: - self.request(data) - except RuntimeError, (num, msg): - if num == DCERPC_FAULT_NDR: - return False - return True - - def find_base_request(self, start=""): - """Find the smallest possible request that we can do that is - valid. - - This generally means sending all zeroes so we get NULL pointers where - possible.""" - # TODO: Don't try with just 0's as there may be switch_is() variables - # for which 0 is not a valid value or variables with range() set - # See how much input bytes we need - for i in range(MAX_BASE_SIZE): - data = start + chr(0) * i - if self.valid_ndr(data): - return data - return None - - def check_decision_byte(self, base_request, i): - """Check whether the specified byte is a possible "decision" byte, - e.g. a byte that is part of a pointer, array size variable or - discriminant. - - Note that this function only checks if a byte is definitely a decision - byte. It may return False in some cases while the byte is actually - a decision byte.""" - data = list(base_request) - data[i] = chr(1) - return not self.valid_ndr("".join(data)) - - def find_deferrant_data(self, base_request, idx): - data = list(base_request) - data[idx*4] = chr(1) - # Just check that this is a pointer to something non-empty: - assert not self.valid_ndr("".join(data)) - - newdata = self.find_base_request("".join(data)) - - if newdata is None: - return None - - assert newdata.startswith(data) - - return newdata[len(data):] - - def find_idl(self): - base_request = self.find_base_request() - - if base_request is None: - raise Exception("Unable to determine base size for opnum %d" % self.opnum) - - print "\tBase request is %r" % base_request - - decision_byte_map = map(lambda x: self.check_decision_byte(base_request, x), range(len(base_request))) - - print decision_byte_map - - # find pointers - possible_pointers = map(all, - [decision_byte_map[i*4:(i+1)*4] for i in range(int(len(base_request)/4))]) - print possible_pointers - - pointer_deferrant_bases = map( - lambda x: self.find_deferrant_data(base_request, x) if possible_pointers[x] else None, range(len(possible_pointers))) - - print pointer_deferrant_bases - - -if len(sys.argv) < 3: - print "Usage: autoidl []" - sys.exit(1) - -(binding, uuid) = sys.argv[1:3] -if len(sys.argv) == 4: - version = sys.argv[3] -else: - version = None - -if version is None: - for i in range(MAX_IFACE_VERSION): - try: - conn = ClientConnection(binding, (uuid, i)) - except RuntimeError, (num, msg): - if num == NT_STATUS_NET_WRITE_FAULT: - continue - raise - else: - break -else: - conn = ClientConnection(binding, (uuid, version)) - -print "Figuring out number of connections...", -num_funcs = find_num_funcs(conn) -print "%d" % num_funcs - -# Figure out the syntax for each one -for i in range(num_funcs): - print "Function %d" % i - data = Function(conn, i) - try: - data.find_idl() - except Exception, e: - print "Error: %r" % e diff --git a/source4/scripting/bin/epdump b/source4/scripting/bin/epdump new file mode 100755 index 0000000000..15dee33774 --- /dev/null +++ b/source4/scripting/bin/epdump @@ -0,0 +1,24 @@ +#!/usr/bin/python + +# Unix SMB/CIFS implementation. +# Copyright (C) Jelmer Vernooij 2008 +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +import sys + +if len(sys.argv) < 2: + print "Usage: %s " % sys.argv[0] + sys.exit(1) diff --git a/source4/scripting/bin/epdump.py b/source4/scripting/bin/epdump.py deleted file mode 100644 index 15dee33774..0000000000 --- a/source4/scripting/bin/epdump.py +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/python - -# Unix SMB/CIFS implementation. -# Copyright (C) Jelmer Vernooij 2008 -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -# - -import sys - -if len(sys.argv) < 2: - print "Usage: %s " % sys.argv[0] - sys.exit(1) diff --git a/source4/scripting/bin/minschema b/source4/scripting/bin/minschema new file mode 100755 index 0000000000..111557126d --- /dev/null +++ b/source4/scripting/bin/minschema @@ -0,0 +1,585 @@ +#!/usr/bin/python +# +# work out the minimal schema for a set of objectclasses +# + +import optparse + +import os, sys + +# Find right directory when running from source tree +sys.path.insert(0, "bin/python") + +import samba +from samba import getopt as options +import sys + +parser = optparse.OptionParser("minschema ") +sambaopts = options.SambaOptions(parser) +parser.add_option_group(sambaopts) +credopts = options.CredentialsOptions(parser) +parser.add_option_group(credopts) +parser.add_option_group(options.VersionOptions(parser)) +parser.add_option("--verbose", help="Be verbose", action="store_true") +parser.add_option("--dump-classes", action="store_true") +parser.add_option("--dump-attributes", action="store_true") +parser.add_option("--dump-subschema", action="store_true") +parser.add_option("--dump-subschema-auto", action="store_true") + +opts, args = parser.parse_args() +opts.dump_all = True + +if opts.dump_classes: + opts.dump_all = False +if opts.dump_attributes: + opts.dump_all = False +if opts.dump_subschema: + opts.dump_all = False +if opts.dump_subschema_auto: + opts.dump_all = False + opts.dump_subschema = True +if opts.dump_all: + opts.dump_classes = True + opts.dump_attributes = True + opts.dump_subschema = True + opts.dump_subschema_auto = True + +if len(args) != 2: + parser.print_usage() + sys.exit(1) + +(url, classfile) = args + +creds = credopts.get_credentials() +ldb = Ldb(url, credentials=creds) + +objectclasses = [] +attributes = [] + +objectclasses_expanded = set() + +# the attributes we need for objectclasses +class_attrs = ["objectClass", + "subClassOf", + "governsID", + "possSuperiors", + "possibleInferiors", + "mayContain", + "mustContain", + "auxiliaryClass", + "rDNAttID", + "showInAdvancedViewOnly", + "adminDisplayName", + "adminDescription", + "objectClassCategory", + "lDAPDisplayName", + "schemaIDGUID", + "systemOnly", + "systemPossSuperiors", + "systemMayContain", + "systemMustContain", + "systemAuxiliaryClass", + "defaultSecurityDescriptor", + "systemFlags", + "defaultHidingValue", + "objectCategory", + "defaultObjectCategory", + + # this attributes are not used by w2k3 + "schemaFlagsEx", + "msDs-IntId", + "msDs-Schema-Extensions", + "classDisplayName", + "isDefunct"] + +attrib_attrs = ["objectClass", + "attributeID", + "attributeSyntax", + "isSingleValued", + "rangeLower", + "rangeUpper", + "mAPIID", + "linkID", + "showInAdvancedViewOnly", + "adminDisplayName", + "oMObjectClass", + "adminDescription", + "oMSyntax", + "searchFlags", + "extendedCharsAllowed", + "lDAPDisplayName", + "schemaIDGUID", + "attributeSecurityGUID", + "systemOnly", + "systemFlags", + "isMemberOfPartialAttributeSet", + "objectCategory", + + # this attributes are not used by w2k3 + "schemaFlagsEx", + "msDs-IntId", + "msDs-Schema-Extensions", + "classDisplayName", + "isEphemeral", + "isDefunct"] + +# +# notes: +# +# objectClassCategory +# 1: structural +# 2: abstract +# 3: auxiliary + +# +# print only if verbose is set +# +def dprintf(text): + if verbose is not None: + print text + +def get_object_cn(ldb, name): + attrs = ["cn"] + + res = ldb.search("(ldapDisplayName=%s)" % name, rootDse["schemaNamingContext"], ldb.SCOPE_SUBTREE, attrs) + assert len(res) == 1 + + return res[0]["cn"] + +class Objectclass: + def __init__(self, ldb, name): + """create an objectclass object""" + self.name = name + self.cn = get_object_cn(ldb, name) + + +class Attribute: + def __init__(self, ldb, name): + """create an attribute object""" + self.name = name + self.cn = get_object_cn(ldb, name) + + +syntaxmap = dict() + +syntaxmap['2.5.5.1'] = '1.3.6.1.4.1.1466.115.121.1.12' +syntaxmap['2.5.5.2'] = '1.3.6.1.4.1.1466.115.121.1.38' +syntaxmap['2.5.5.3'] = '1.2.840.113556.1.4.1362' +syntaxmap['2.5.5.4'] = '1.2.840.113556.1.4.905' +syntaxmap['2.5.5.5'] = '1.3.6.1.4.1.1466.115.121.1.26' +syntaxmap['2.5.5.6'] = '1.3.6.1.4.1.1466.115.121.1.36' +syntaxmap['2.5.5.7'] = '1.2.840.113556.1.4.903' +syntaxmap['2.5.5.8'] = '1.3.6.1.4.1.1466.115.121.1.7' +syntaxmap['2.5.5.9'] = '1.3.6.1.4.1.1466.115.121.1.27' +syntaxmap['2.5.5.10'] = '1.3.6.1.4.1.1466.115.121.1.40' +syntaxmap['2.5.5.11'] = '1.3.6.1.4.1.1466.115.121.1.24' +syntaxmap['2.5.5.12'] = '1.3.6.1.4.1.1466.115.121.1.15' +syntaxmap['2.5.5.13'] = '1.3.6.1.4.1.1466.115.121.1.43' +syntaxmap['2.5.5.14'] = '1.2.840.113556.1.4.904' +syntaxmap['2.5.5.15'] = '1.2.840.113556.1.4.907' +syntaxmap['2.5.5.16'] = '1.2.840.113556.1.4.906' +syntaxmap['2.5.5.17'] = '1.3.6.1.4.1.1466.115.121.1.40' + + +def map_attribute_syntax(s): + """map some attribute syntaxes from some apparently MS specific + syntaxes to the standard syntaxes""" + if syntaxmap.has_key(s): + return syntaxmap[s] + return s + + +def fix_dn(dn): + """fix a string DN to use ${SCHEMADN}""" + return dn.replace(rootDse["schemaNamingContext"], "${SCHEMADN}") + + +def write_ldif_one(o, attrs): + """dump an object as ldif""" + print "dn: CN=%s,${SCHEMADN}\n" % o["cn"] + for a in attrs: + if not o.has_key(a): + continue + # special case for oMObjectClass, which is a binary object + if a == "oMObjectClass": + print "%s:: %s\n" % (a, o[a]) + continue + v = o[a] + if isinstance(v, str): + v = [v] + for j in v: + print "%s: %s\n" % (a, fix_dn(j)) + print "\n" + +def write_ldif(o, attrs): + """dump an array of objects as ldif""" + for i in o: + write_ldif_one(i, attrs) + + +def create_testdn(exampleDN): + """create a testDN based an an example DN + the idea is to ensure we obey any structural rules""" + a = exampleDN.split(",") + a[0] = "CN=TestDN" + return ",".join(a) + + +def find_objectclass_properties(ldb, o): + """the properties of an objectclass""" + res = ldb.search( + expression="(ldapDisplayName=%s)" % o.name, + basedn=rootDse["schemaNamingContext"], scope=ldb.SCOPE_SUBTREE, attrs=class_attrs) + assert(len(res) == 1) + msg = res[0] + for a in msg: + o[a] = msg[a] + +def find_attribute_properties(ldb, o): + """find the properties of an attribute""" + res = ldb.search( + expression="(ldapDisplayName=%s)" % o.name, + basedn=rootDse["schemaNamingContext"], scope=ldb.SCOPE_SUBTREE, + attrs=attrib_attrs) + assert(len(res) == 1) + msg = res[0] + for a in msg: + # special case for oMObjectClass, which is a binary object + if a == "oMObjectClass": + o[a] = ldb.encode(msg[a]) + continue + o[a] = msg[a] + + +def find_objectclass_auto(ldb, o): + """find the auto-created properties of an objectclass. Only works for + classes that can be created using just a DN and the objectclass""" + if not o.has_key("exampleDN"): + return + testdn = create_testdn(o.exampleDN) + + print "testdn is '%s'\n" % testdn + + ldif = "dn: " + testdn + ldif += "\nobjectClass: " + o.name + try: + ldb.add(ldif) + except LdbError, e: + print "error adding %s: %s\n" % (o.name, e) + print "%s\n" % ldif + return + + res = ldb.search("", testdn, ldb.SCOPE_BASE) + ldb.delete(testdn) + + for a in res.msgs[0]: + attributes[a].autocreate = True + + +def expand_objectclass(ldb, o): + """look at auxiliary information from a class to intuit the existance of + more classes needed for a minimal schema""" + attrs = ["auxiliaryClass", "systemAuxiliaryClass", + "possSuperiors", "systemPossSuperiors", + "subClassOf"] + res = ldb.search( + expression="(&(objectClass=classSchema)(ldapDisplayName=%s))" % o.name, + basedn=rootDse["schemaNamingContext"], scope=ldb.SCOPE_SUBTREE, + attrs=attrs) + print "Expanding class %s\n" % o.name + assert(len(res) == 1) + msg = res[0] + for a in attrs: + if not msg.has_key(aname): + continue + list = msg[aname] + if isinstance(list, str): + list = [msg[aname]] + for name in list: + if not objectclasses.has_key(name): + print "Found new objectclass '%s'\n" % name + objectclasses[name] = Objectclass(ldb, name) + + +def add_objectclass_attributes(ldb, objectclass): + """add the must and may attributes from an objectclass to the full list + of attributes""" + attrs = ["mustContain", "systemMustContain", + "mayContain", "systemMayContain"] + for aname in attrs: + if not objectclass.has_key(aname): + continue + alist = objectclass[aname] + if isinstance(alist, str): + alist = [alist] + for a in alist: + if not attributes.has_key(a): + attributes[a] = Attribute(ldb, a) + + +def walk_dn(ldb, dn): + """process an individual record, working out what attributes it has""" + # get a list of all possible attributes for this object + attrs = ["allowedAttributes"] + try: + res = ldb.search("objectClass=*", dn, ldb.SCOPE_BASE, attrs) + except LdbError, e: + print "Unable to fetch allowedAttributes for '%s' - %r\n" % (dn, e) + return + allattrs = res[0]["allowedAttributes"] + try: + res = ldb.search("objectClass=*", dn, ldb.SCOPE_BASE, allattrs) + except LdbError, e: + print "Unable to fetch all attributes for '%s' - %s\n" % (dn, e) + return + msg = res[0] + for a in msg: + if not attributes.has_key(a): + attributes[a] = Attribute(ldb, a) + +def walk_naming_context(ldb, namingContext): + """walk a naming context, looking for all records""" + try: + res = ldb.search("objectClass=*", namingContext, ldb.SCOPE_DEFAULT, + ["objectClass"]) + except LdbError, e: + print "Unable to fetch objectClasses for '%s' - %s\n" % (namingContext, e) + return + for msg in res: + msg = res.msgs[r]["objectClass"] + for objectClass in msg: + if not objectclasses.has_key(objectClass): + objectclasses[objectClass] = Objectclass(ldb, objectClass) + objectclasses[objectClass].exampleDN = res.msgs[r]["dn"] + walk_dn(ldb, res.msgs[r].dn) + +def trim_objectclass_attributes(ldb, objectclass): + """trim the may attributes for an objectClass""" + # trim possibleInferiors, + # include only the classes we extracted + if objectclass.has_key("possibleInferiors"): + possinf = objectclass["possibleInferiors"] + newpossinf = [] + if isinstance(possinf, str): + possinf = [possinf] + for x in possinf: + if objectclasses.has_key(x): + newpossinf[n] = x + n+=1 + objectclass["possibleInferiors"] = newpossinf + + # trim systemMayContain, + # remove duplicates + if objectclass.has_key("systemMayContain"): + sysmay = objectclass["systemMayContain"] + newsysmay = [] + if isinstance(sysmay, str): + sysmay = [sysmay] + for x in sysmay: + if not x in newsysmay: + newsysmay.append(x) + objectclass["systemMayContain"] = newsysmay + + # trim mayContain, + # remove duplicates + if not objectclass.has_key("mayContain"): + may = objectclass["mayContain"] + newmay = [] + if isinstance(may, str): + may = [may] + for x in may: + if not x in newmay: + newmay.append(x) + objectclass["mayContain"] = newmay + +def build_objectclass(ldb, name): + """load the basic attributes of an objectClass""" + attrs = ["name"] + try: + res = ldb.search( + expression="(&(objectClass=classSchema)(ldapDisplayName=%s))" % name, + basedn=rootDse["schemaNamingContext"], scope=ldb.SCOPE_SUBTREE, + attrs=attrs) + except LdbError, e: + print "unknown class '%s'\n" % name + return None + if len(res) == 0: + print "unknown class '%s'\n" % name + return None + return Objectclass(ldb, name) + +def attribute_list(objectclass, attr1, attr2): + """form a coalesced attribute list""" + a1 = objectclass[attr1] + a2 = objectclass[attr2] + if isinstance(a1, str): + a1 = [a1] + if isinstance(a2, str): + a2 = [a2] + return a1 + a2 + +def aggregate_list(name, list): + """write out a list in aggregate form""" + if list is None: + return + print "%s ( %s )" % (name, "$ ".join(list)) + +def write_aggregate_objectclass(objectclass): + """write the aggregate record for an objectclass""" + print "objectClasses: ( %s NAME '%s' " % (objectclass.governsID, objectclass.name) + if not objectclass.has_key('subClassOf'): + print "SUP %s " % objectclass['subClassOf'] + if objectclass.objectClassCategory == 1: + print "STRUCTURAL " + elif objectclass.objectClassCategory == 2: + print "ABSTRACT " + elif objectclass.objectClassCategory == 3: + print "AUXILIARY " + + list = attribute_list(objectclass, "systemMustContain", "mustContain") + aggregate_list("MUST", list) + + list = attribute_list(objectclass, "systemMayContain", "mayContain") + aggregate_list("MAY", list) + + print ")\n" + + +def write_aggregate_ditcontentrule(objectclass): + """write the aggregate record for an ditcontentrule""" + list = attribute_list(objectclass, "auxiliaryClass", "systemAuxiliaryClass") + if list is None: + return + + print "dITContentRules: ( %s NAME '%s' " % (objectclass.governsID, objectclass.name) + + aggregate_list("AUX", list) + + may_list = None + must_list = None + + for c in list: + list2 = attribute_list(objectclasses[c], + "mayContain", "systemMayContain") + may_list = may_list + list2 + list2 = attribute_list(objectclasses[c], + "mustContain", "systemMustContain") + must_list = must_list + list2 + + aggregate_list("MUST", must_list) + aggregate_list("MAY", may_list) + + print ")\n" + +def write_aggregate_attribute(attrib): + """write the aggregate record for an attribute""" + print "attributeTypes: ( %s NAME '%s' SYNTAX '%s' " % ( + attrib.attributeID, attrib.name, + map_attribute_syntax(attrib.attributeSyntax)) + if attrib['isSingleValued'] == "TRUE": + print "SINGLE-VALUE " + if attrib['systemOnly'] == "TRUE": + print "NO-USER-MODIFICATION " + + print ")\n" + + +def write_aggregate(): + """write the aggregate record""" + print "dn: CN=Aggregate,${SCHEMADN}\n" + print """objectClass: top +objectClass: subSchema +objectCategory: CN=SubSchema,${SCHEMADN} +""" + if not opts.dump_subschema_auto: + return + + for objectclass in objectclasses: + write_aggregate_objectclass(objectclass) + for attr in attributes: + write_aggregate_attribute(attr) + for objectclass in objectclasses: + write_aggregate_ditcontentrule(objectclass) + +def load_list(file): + """load a list from a file""" + return open(file, 'r').splitlines() + +# get the rootDSE +res = ldb.search("", "", ldb.SCOPE_BASE) +rootDse = res[0] + +# load the list of classes we are interested in +classes = load_list(classfile) +for classname in classes: + objectclass = build_objectclass(ldb, classname) + if objectclass is not None: + objectclasses[classname] = objectclass + + +# +# expand the objectclass list as needed +# +expanded = 0 + +# so EJS do not have while nor the break statement +# cannot find any other way than doing more loops +# than necessary to recursively expand all classes +# +for inf in range(500): + for n in objectclasses: + if not n in objectclasses_expanded: + expand_objectclass(ldb, objectclasses[i]) + objectclasses_expanded.add(n) + +# +# find objectclass properties +# +for objectclass in objectclasses: + find_objectclass_properties(ldb, objectclass) + + +# +# form the full list of attributes +# +for objectclass in objectclasses: + add_objectclass_attributes(ldb, objectclass) + +# and attribute properties +for attr in attributes: + find_attribute_properties(ldb, attr) + +# +# trim the 'may' attribute lists to those really needed +# +for objectclass in objectclasses: + trim_objectclass_attributes(ldb, objectclass) + +# +# dump an ldif form of the attributes and objectclasses +# +if opts.dump_attributes: + write_ldif(attributes, attrib_attrs) +if opts.dump_classes: + write_ldif(objectclasses, class_attrs) +if opts.dump_subschema: + write_aggregate() + +if not opts.verbose: + sys.exit(0) + +# +# dump list of objectclasses +# +print "objectClasses:\n" +for objectclass in objectclasses: + print "\t%s\n" % objectclass + +print "attributes:\n" +for attr in attributes: + print "\t%s\n" % attr + +print "autocreated attributes:\n" +for attr in attributes: + if attr.autocreate: + print "\t%s\n" % i diff --git a/source4/scripting/bin/minschema.py b/source4/scripting/bin/minschema.py deleted file mode 100755 index 111557126d..0000000000 --- a/source4/scripting/bin/minschema.py +++ /dev/null @@ -1,585 +0,0 @@ -#!/usr/bin/python -# -# work out the minimal schema for a set of objectclasses -# - -import optparse - -import os, sys - -# Find right directory when running from source tree -sys.path.insert(0, "bin/python") - -import samba -from samba import getopt as options -import sys - -parser = optparse.OptionParser("minschema ") -sambaopts = options.SambaOptions(parser) -parser.add_option_group(sambaopts) -credopts = options.CredentialsOptions(parser) -parser.add_option_group(credopts) -parser.add_option_group(options.VersionOptions(parser)) -parser.add_option("--verbose", help="Be verbose", action="store_true") -parser.add_option("--dump-classes", action="store_true") -parser.add_option("--dump-attributes", action="store_true") -parser.add_option("--dump-subschema", action="store_true") -parser.add_option("--dump-subschema-auto", action="store_true") - -opts, args = parser.parse_args() -opts.dump_all = True - -if opts.dump_classes: - opts.dump_all = False -if opts.dump_attributes: - opts.dump_all = False -if opts.dump_subschema: - opts.dump_all = False -if opts.dump_subschema_auto: - opts.dump_all = False - opts.dump_subschema = True -if opts.dump_all: - opts.dump_classes = True - opts.dump_attributes = True - opts.dump_subschema = True - opts.dump_subschema_auto = True - -if len(args) != 2: - parser.print_usage() - sys.exit(1) - -(url, classfile) = args - -creds = credopts.get_credentials() -ldb = Ldb(url, credentials=creds) - -objectclasses = [] -attributes = [] - -objectclasses_expanded = set() - -# the attributes we need for objectclasses -class_attrs = ["objectClass", - "subClassOf", - "governsID", - "possSuperiors", - "possibleInferiors", - "mayContain", - "mustContain", - "auxiliaryClass", - "rDNAttID", - "showInAdvancedViewOnly", - "adminDisplayName", - "adminDescription", - "objectClassCategory", - "lDAPDisplayName", - "schemaIDGUID", - "systemOnly", - "systemPossSuperiors", - "systemMayContain", - "systemMustContain", - "systemAuxiliaryClass", - "defaultSecurityDescriptor", - "systemFlags", - "defaultHidingValue", - "objectCategory", - "defaultObjectCategory", - - # this attributes are not used by w2k3 - "schemaFlagsEx", - "msDs-IntId", - "msDs-Schema-Extensions", - "classDisplayName", - "isDefunct"] - -attrib_attrs = ["objectClass", - "attributeID", - "attributeSyntax", - "isSingleValued", - "rangeLower", - "rangeUpper", - "mAPIID", - "linkID", - "showInAdvancedViewOnly", - "adminDisplayName", - "oMObjectClass", - "adminDescription", - "oMSyntax", - "searchFlags", - "extendedCharsAllowed", - "lDAPDisplayName", - "schemaIDGUID", - "attributeSecurityGUID", - "systemOnly", - "systemFlags", - "isMemberOfPartialAttributeSet", - "objectCategory", - - # this attributes are not used by w2k3 - "schemaFlagsEx", - "msDs-IntId", - "msDs-Schema-Extensions", - "classDisplayName", - "isEphemeral", - "isDefunct"] - -# -# notes: -# -# objectClassCategory -# 1: structural -# 2: abstract -# 3: auxiliary - -# -# print only if verbose is set -# -def dprintf(text): - if verbose is not None: - print text - -def get_object_cn(ldb, name): - attrs = ["cn"] - - res = ldb.search("(ldapDisplayName=%s)" % name, rootDse["schemaNamingContext"], ldb.SCOPE_SUBTREE, attrs) - assert len(res) == 1 - - return res[0]["cn"] - -class Objectclass: - def __init__(self, ldb, name): - """create an objectclass object""" - self.name = name - self.cn = get_object_cn(ldb, name) - - -class Attribute: - def __init__(self, ldb, name): - """create an attribute object""" - self.name = name - self.cn = get_object_cn(ldb, name) - - -syntaxmap = dict() - -syntaxmap['2.5.5.1'] = '1.3.6.1.4.1.1466.115.121.1.12' -syntaxmap['2.5.5.2'] = '1.3.6.1.4.1.1466.115.121.1.38' -syntaxmap['2.5.5.3'] = '1.2.840.113556.1.4.1362' -syntaxmap['2.5.5.4'] = '1.2.840.113556.1.4.905' -syntaxmap['2.5.5.5'] = '1.3.6.1.4.1.1466.115.121.1.26' -syntaxmap['2.5.5.6'] = '1.3.6.1.4.1.1466.115.121.1.36' -syntaxmap['2.5.5.7'] = '1.2.840.113556.1.4.903' -syntaxmap['2.5.5.8'] = '1.3.6.1.4.1.1466.115.121.1.7' -syntaxmap['2.5.5.9'] = '1.3.6.1.4.1.1466.115.121.1.27' -syntaxmap['2.5.5.10'] = '1.3.6.1.4.1.1466.115.121.1.40' -syntaxmap['2.5.5.11'] = '1.3.6.1.4.1.1466.115.121.1.24' -syntaxmap['2.5.5.12'] = '1.3.6.1.4.1.1466.115.121.1.15' -syntaxmap['2.5.5.13'] = '1.3.6.1.4.1.1466.115.121.1.43' -syntaxmap['2.5.5.14'] = '1.2.840.113556.1.4.904' -syntaxmap['2.5.5.15'] = '1.2.840.113556.1.4.907' -syntaxmap['2.5.5.16'] = '1.2.840.113556.1.4.906' -syntaxmap['2.5.5.17'] = '1.3.6.1.4.1.1466.115.121.1.40' - - -def map_attribute_syntax(s): - """map some attribute syntaxes from some apparently MS specific - syntaxes to the standard syntaxes""" - if syntaxmap.has_key(s): - return syntaxmap[s] - return s - - -def fix_dn(dn): - """fix a string DN to use ${SCHEMADN}""" - return dn.replace(rootDse["schemaNamingContext"], "${SCHEMADN}") - - -def write_ldif_one(o, attrs): - """dump an object as ldif""" - print "dn: CN=%s,${SCHEMADN}\n" % o["cn"] - for a in attrs: - if not o.has_key(a): - continue - # special case for oMObjectClass, which is a binary object - if a == "oMObjectClass": - print "%s:: %s\n" % (a, o[a]) - continue - v = o[a] - if isinstance(v, str): - v = [v] - for j in v: - print "%s: %s\n" % (a, fix_dn(j)) - print "\n" - -def write_ldif(o, attrs): - """dump an array of objects as ldif""" - for i in o: - write_ldif_one(i, attrs) - - -def create_testdn(exampleDN): - """create a testDN based an an example DN - the idea is to ensure we obey any structural rules""" - a = exampleDN.split(",") - a[0] = "CN=TestDN" - return ",".join(a) - - -def find_objectclass_properties(ldb, o): - """the properties of an objectclass""" - res = ldb.search( - expression="(ldapDisplayName=%s)" % o.name, - basedn=rootDse["schemaNamingContext"], scope=ldb.SCOPE_SUBTREE, attrs=class_attrs) - assert(len(res) == 1) - msg = res[0] - for a in msg: - o[a] = msg[a] - -def find_attribute_properties(ldb, o): - """find the properties of an attribute""" - res = ldb.search( - expression="(ldapDisplayName=%s)" % o.name, - basedn=rootDse["schemaNamingContext"], scope=ldb.SCOPE_SUBTREE, - attrs=attrib_attrs) - assert(len(res) == 1) - msg = res[0] - for a in msg: - # special case for oMObjectClass, which is a binary object - if a == "oMObjectClass": - o[a] = ldb.encode(msg[a]) - continue - o[a] = msg[a] - - -def find_objectclass_auto(ldb, o): - """find the auto-created properties of an objectclass. Only works for - classes that can be created using just a DN and the objectclass""" - if not o.has_key("exampleDN"): - return - testdn = create_testdn(o.exampleDN) - - print "testdn is '%s'\n" % testdn - - ldif = "dn: " + testdn - ldif += "\nobjectClass: " + o.name - try: - ldb.add(ldif) - except LdbError, e: - print "error adding %s: %s\n" % (o.name, e) - print "%s\n" % ldif - return - - res = ldb.search("", testdn, ldb.SCOPE_BASE) - ldb.delete(testdn) - - for a in res.msgs[0]: - attributes[a].autocreate = True - - -def expand_objectclass(ldb, o): - """look at auxiliary information from a class to intuit the existance of - more classes needed for a minimal schema""" - attrs = ["auxiliaryClass", "systemAuxiliaryClass", - "possSuperiors", "systemPossSuperiors", - "subClassOf"] - res = ldb.search( - expression="(&(objectClass=classSchema)(ldapDisplayName=%s))" % o.name, - basedn=rootDse["schemaNamingContext"], scope=ldb.SCOPE_SUBTREE, - attrs=attrs) - print "Expanding class %s\n" % o.name - assert(len(res) == 1) - msg = res[0] - for a in attrs: - if not msg.has_key(aname): - continue - list = msg[aname] - if isinstance(list, str): - list = [msg[aname]] - for name in list: - if not objectclasses.has_key(name): - print "Found new objectclass '%s'\n" % name - objectclasses[name] = Objectclass(ldb, name) - - -def add_objectclass_attributes(ldb, objectclass): - """add the must and may attributes from an objectclass to the full list - of attributes""" - attrs = ["mustContain", "systemMustContain", - "mayContain", "systemMayContain"] - for aname in attrs: - if not objectclass.has_key(aname): - continue - alist = objectclass[aname] - if isinstance(alist, str): - alist = [alist] - for a in alist: - if not attributes.has_key(a): - attributes[a] = Attribute(ldb, a) - - -def walk_dn(ldb, dn): - """process an individual record, working out what attributes it has""" - # get a list of all possible attributes for this object - attrs = ["allowedAttributes"] - try: - res = ldb.search("objectClass=*", dn, ldb.SCOPE_BASE, attrs) - except LdbError, e: - print "Unable to fetch allowedAttributes for '%s' - %r\n" % (dn, e) - return - allattrs = res[0]["allowedAttributes"] - try: - res = ldb.search("objectClass=*", dn, ldb.SCOPE_BASE, allattrs) - except LdbError, e: - print "Unable to fetch all attributes for '%s' - %s\n" % (dn, e) - return - msg = res[0] - for a in msg: - if not attributes.has_key(a): - attributes[a] = Attribute(ldb, a) - -def walk_naming_context(ldb, namingContext): - """walk a naming context, looking for all records""" - try: - res = ldb.search("objectClass=*", namingContext, ldb.SCOPE_DEFAULT, - ["objectClass"]) - except LdbError, e: - print "Unable to fetch objectClasses for '%s' - %s\n" % (namingContext, e) - return - for msg in res: - msg = res.msgs[r]["objectClass"] - for objectClass in msg: - if not objectclasses.has_key(objectClass): - objectclasses[objectClass] = Objectclass(ldb, objectClass) - objectclasses[objectClass].exampleDN = res.msgs[r]["dn"] - walk_dn(ldb, res.msgs[r].dn) - -def trim_objectclass_attributes(ldb, objectclass): - """trim the may attributes for an objectClass""" - # trim possibleInferiors, - # include only the classes we extracted - if objectclass.has_key("possibleInferiors"): - possinf = objectclass["possibleInferiors"] - newpossinf = [] - if isinstance(possinf, str): - possinf = [possinf] - for x in possinf: - if objectclasses.has_key(x): - newpossinf[n] = x - n+=1 - objectclass["possibleInferiors"] = newpossinf - - # trim systemMayContain, - # remove duplicates - if objectclass.has_key("systemMayContain"): - sysmay = objectclass["systemMayContain"] - newsysmay = [] - if isinstance(sysmay, str): - sysmay = [sysmay] - for x in sysmay: - if not x in newsysmay: - newsysmay.append(x) - objectclass["systemMayContain"] = newsysmay - - # trim mayContain, - # remove duplicates - if not objectclass.has_key("mayContain"): - may = objectclass["mayContain"] - newmay = [] - if isinstance(may, str): - may = [may] - for x in may: - if not x in newmay: - newmay.append(x) - objectclass["mayContain"] = newmay - -def build_objectclass(ldb, name): - """load the basic attributes of an objectClass""" - attrs = ["name"] - try: - res = ldb.search( - expression="(&(objectClass=classSchema)(ldapDisplayName=%s))" % name, - basedn=rootDse["schemaNamingContext"], scope=ldb.SCOPE_SUBTREE, - attrs=attrs) - except LdbError, e: - print "unknown class '%s'\n" % name - return None - if len(res) == 0: - print "unknown class '%s'\n" % name - return None - return Objectclass(ldb, name) - -def attribute_list(objectclass, attr1, attr2): - """form a coalesced attribute list""" - a1 = objectclass[attr1] - a2 = objectclass[attr2] - if isinstance(a1, str): - a1 = [a1] - if isinstance(a2, str): - a2 = [a2] - return a1 + a2 - -def aggregate_list(name, list): - """write out a list in aggregate form""" - if list is None: - return - print "%s ( %s )" % (name, "$ ".join(list)) - -def write_aggregate_objectclass(objectclass): - """write the aggregate record for an objectclass""" - print "objectClasses: ( %s NAME '%s' " % (objectclass.governsID, objectclass.name) - if not objectclass.has_key('subClassOf'): - print "SUP %s " % objectclass['subClassOf'] - if objectclass.objectClassCategory == 1: - print "STRUCTURAL " - elif objectclass.objectClassCategory == 2: - print "ABSTRACT " - elif objectclass.objectClassCategory == 3: - print "AUXILIARY " - - list = attribute_list(objectclass, "systemMustContain", "mustContain") - aggregate_list("MUST", list) - - list = attribute_list(objectclass, "systemMayContain", "mayContain") - aggregate_list("MAY", list) - - print ")\n" - - -def write_aggregate_ditcontentrule(objectclass): - """write the aggregate record for an ditcontentrule""" - list = attribute_list(objectclass, "auxiliaryClass", "systemAuxiliaryClass") - if list is None: - return - - print "dITContentRules: ( %s NAME '%s' " % (objectclass.governsID, objectclass.name) - - aggregate_list("AUX", list) - - may_list = None - must_list = None - - for c in list: - list2 = attribute_list(objectclasses[c], - "mayContain", "systemMayContain") - may_list = may_list + list2 - list2 = attribute_list(objectclasses[c], - "mustContain", "systemMustContain") - must_list = must_list + list2 - - aggregate_list("MUST", must_list) - aggregate_list("MAY", may_list) - - print ")\n" - -def write_aggregate_attribute(attrib): - """write the aggregate record for an attribute""" - print "attributeTypes: ( %s NAME '%s' SYNTAX '%s' " % ( - attrib.attributeID, attrib.name, - map_attribute_syntax(attrib.attributeSyntax)) - if attrib['isSingleValued'] == "TRUE": - print "SINGLE-VALUE " - if attrib['systemOnly'] == "TRUE": - print "NO-USER-MODIFICATION " - - print ")\n" - - -def write_aggregate(): - """write the aggregate record""" - print "dn: CN=Aggregate,${SCHEMADN}\n" - print """objectClass: top -objectClass: subSchema -objectCategory: CN=SubSchema,${SCHEMADN} -""" - if not opts.dump_subschema_auto: - return - - for objectclass in objectclasses: - write_aggregate_objectclass(objectclass) - for attr in attributes: - write_aggregate_attribute(attr) - for objectclass in objectclasses: - write_aggregate_ditcontentrule(objectclass) - -def load_list(file): - """load a list from a file""" - return open(file, 'r').splitlines() - -# get the rootDSE -res = ldb.search("", "", ldb.SCOPE_BASE) -rootDse = res[0] - -# load the list of classes we are interested in -classes = load_list(classfile) -for classname in classes: - objectclass = build_objectclass(ldb, classname) - if objectclass is not None: - objectclasses[classname] = objectclass - - -# -# expand the objectclass list as needed -# -expanded = 0 - -# so EJS do not have while nor the break statement -# cannot find any other way than doing more loops -# than necessary to recursively expand all classes -# -for inf in range(500): - for n in objectclasses: - if not n in objectclasses_expanded: - expand_objectclass(ldb, objectclasses[i]) - objectclasses_expanded.add(n) - -# -# find objectclass properties -# -for objectclass in objectclasses: - find_objectclass_properties(ldb, objectclass) - - -# -# form the full list of attributes -# -for objectclass in objectclasses: - add_objectclass_attributes(ldb, objectclass) - -# and attribute properties -for attr in attributes: - find_attribute_properties(ldb, attr) - -# -# trim the 'may' attribute lists to those really needed -# -for objectclass in objectclasses: - trim_objectclass_attributes(ldb, objectclass) - -# -# dump an ldif form of the attributes and objectclasses -# -if opts.dump_attributes: - write_ldif(attributes, attrib_attrs) -if opts.dump_classes: - write_ldif(objectclasses, class_attrs) -if opts.dump_subschema: - write_aggregate() - -if not opts.verbose: - sys.exit(0) - -# -# dump list of objectclasses -# -print "objectClasses:\n" -for objectclass in objectclasses: - print "\t%s\n" % objectclass - -print "attributes:\n" -for attr in attributes: - print "\t%s\n" % attr - -print "autocreated attributes:\n" -for attr in attributes: - if attr.autocreate: - print "\t%s\n" % i diff --git a/source4/scripting/bin/smbstatus b/source4/scripting/bin/smbstatus index bbd0e84826..a9265ead6a 100755 --- a/source4/scripting/bin/smbstatus +++ b/source4/scripting/bin/smbstatus @@ -22,7 +22,7 @@ def show_sessions(conn): sessions = conn.smbsrv_information(irpc.SMBSRV_INFO_SESSIONS).next() print "User Client Connected at" - print "-------------------------------------------------------------------------------" + print "-" * 79 for session in sessions: fulluser = "%s/%s" % (session.account_name, session.domain_name) print "%-30s %16s %s" % (fulluser, session.client_ip, sys.httptime(session.connect_time)) @@ -33,7 +33,7 @@ def show_tcons(open_connection): conn = open_connection("smb_server") tcons = conn.smbsrv_information(irpc.SMBSRV_INFO_TCONS).next() print "Share Client Connected at" - print "-------------------------------------------------------------------------------" + print "-" * 79 for tcon in tcons: print "%-30s %16s %s" % (tcon.share_name, tcon.client_ip, sys.httptime(tcon.connect_time)) @@ -76,7 +76,7 @@ else: try: conn = open_connection("smb_server") except RuntimeError, (num, msg): - if msg == 'NT_STATUS_OBJECT_NAME_NOT_FOUND': + if msg == 'NT_STATUS_OBJECT_NAME_NOT_FOUND': print "No active connections" else: show_sessions(conn) -- cgit From 6f6e42ce60e3f9adfebffa7db04eafe717942c1b Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Thu, 18 Sep 2008 23:55:50 +0200 Subject: Generate with 1.3.36. --- source4/scripting/python/config.m4 | 2 +- source4/scripting/python/misc.i | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'source4/scripting') diff --git a/source4/scripting/python/config.m4 b/source4/scripting/python/config.m4 index aaf32c5e4e..843c3f7873 100644 --- a/source4/scripting/python/config.m4 +++ b/source4/scripting/python/config.m4 @@ -5,7 +5,7 @@ AC_ARG_VAR([PYTHON_VERSION],[The installed Python will be appended to the Python interpreter canonical name.]) -AC_PROG_SWIG(1.3.35) +AC_PROG_SWIG(1.3.36) AC_PATH_PROG([PYTHON],[python[$PYTHON_VERSION]]) if test -z "$PYTHON"; then diff --git a/source4/scripting/python/misc.i b/source4/scripting/python/misc.i index 81be7d5c16..f40e88c6cd 100644 --- a/source4/scripting/python/misc.i +++ b/source4/scripting/python/misc.i @@ -31,7 +31,7 @@ %import "stdint.i" %include "exception.i" -%import "../../lib/talloc/talloc.i" +%import "../../../lib/talloc/talloc.i" %import "../../lib/ldb/ldb.i" %import "../../auth/credentials/credentials.i" %import "../../param/param.i" -- cgit From db6bd2a3d6311cf34a6af044ec37f31f7741c144 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Sun, 21 Sep 2008 03:42:27 +0200 Subject: Fix DNs - only one more samba3sam test failing now. --- source4/scripting/python/misc_wrap.c | 46 ++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 18 deletions(-) (limited to 'source4/scripting') diff --git a/source4/scripting/python/misc_wrap.c b/source4/scripting/python/misc_wrap.c index 3aee83f72c..b669f3e801 100644 --- a/source4/scripting/python/misc_wrap.c +++ b/source4/scripting/python/misc_wrap.c @@ -2494,24 +2494,26 @@ SWIG_Python_MustGetPtr(PyObject *obj, swig_type_info *ty, int argnum, int flags) #define SWIGTYPE_p_ldb_ldif swig_types[8] #define SWIGTYPE_p_ldb_message swig_types[9] #define SWIGTYPE_p_ldb_message_element swig_types[10] -#define SWIGTYPE_p_ldb_result swig_types[11] -#define SWIGTYPE_p_loadparm_context swig_types[12] -#define SWIGTYPE_p_loadparm_service swig_types[13] -#define SWIGTYPE_p_long_long swig_types[14] -#define SWIGTYPE_p_param_context swig_types[15] -#define SWIGTYPE_p_param_opt swig_types[16] -#define SWIGTYPE_p_param_section swig_types[17] -#define SWIGTYPE_p_security_descriptor swig_types[18] -#define SWIGTYPE_p_security_token swig_types[19] -#define SWIGTYPE_p_short swig_types[20] -#define SWIGTYPE_p_signed_char swig_types[21] -#define SWIGTYPE_p_unsigned_char swig_types[22] -#define SWIGTYPE_p_unsigned_int swig_types[23] -#define SWIGTYPE_p_unsigned_long swig_types[24] -#define SWIGTYPE_p_unsigned_long_long swig_types[25] -#define SWIGTYPE_p_unsigned_short swig_types[26] -static swig_type_info *swig_types[28]; -static swig_module_info swig_module = {swig_types, 27, 0, 0, 0, 0}; +#define SWIGTYPE_p_ldb_module swig_types[11] +#define SWIGTYPE_p_ldb_parse_tree swig_types[12] +#define SWIGTYPE_p_ldb_result swig_types[13] +#define SWIGTYPE_p_loadparm_context swig_types[14] +#define SWIGTYPE_p_loadparm_service swig_types[15] +#define SWIGTYPE_p_long_long swig_types[16] +#define SWIGTYPE_p_param_context swig_types[17] +#define SWIGTYPE_p_param_opt swig_types[18] +#define SWIGTYPE_p_param_section swig_types[19] +#define SWIGTYPE_p_security_descriptor swig_types[20] +#define SWIGTYPE_p_security_token swig_types[21] +#define SWIGTYPE_p_short swig_types[22] +#define SWIGTYPE_p_signed_char swig_types[23] +#define SWIGTYPE_p_unsigned_char swig_types[24] +#define SWIGTYPE_p_unsigned_int swig_types[25] +#define SWIGTYPE_p_unsigned_long swig_types[26] +#define SWIGTYPE_p_unsigned_long_long swig_types[27] +#define SWIGTYPE_p_unsigned_short swig_types[28] +static swig_type_info *swig_types[30]; +static swig_module_info swig_module = {swig_types, 29, 0, 0, 0, 0}; #define SWIG_TypeQuery(name) SWIG_TypeQueryModule(&swig_module, &swig_module, name) #define SWIG_MangledTypeQuery(name) SWIG_MangledTypeQueryModule(&swig_module, &swig_module, name) @@ -3340,6 +3342,8 @@ static swig_type_info _swigt__p_ldb_dn = {"_p_ldb_dn", "struct ldb_dn *|ldb_dn * static swig_type_info _swigt__p_ldb_ldif = {"_p_ldb_ldif", "struct ldb_ldif *|ldb_ldif *", 0, 0, (void*)0, 0}; static swig_type_info _swigt__p_ldb_message = {"_p_ldb_message", "ldb_msg *|struct ldb_message *", 0, 0, (void*)0, 0}; static swig_type_info _swigt__p_ldb_message_element = {"_p_ldb_message_element", "struct ldb_message_element *|ldb_message_element *", 0, 0, (void*)0, 0}; +static swig_type_info _swigt__p_ldb_module = {"_p_ldb_module", "struct ldb_module *|ldb_module *", 0, 0, (void*)0, 0}; +static swig_type_info _swigt__p_ldb_parse_tree = {"_p_ldb_parse_tree", "struct ldb_parse_tree *", 0, 0, (void*)0, 0}; static swig_type_info _swigt__p_ldb_result = {"_p_ldb_result", "struct ldb_result *", 0, 0, (void*)0, 0}; static swig_type_info _swigt__p_loadparm_context = {"_p_loadparm_context", "struct loadparm_context *|loadparm_context *", 0, 0, (void*)0, 0}; static swig_type_info _swigt__p_loadparm_service = {"_p_loadparm_service", "struct loadparm_service *|loadparm_service *", 0, 0, (void*)0, 0}; @@ -3369,6 +3373,8 @@ static swig_type_info *swig_type_initial[] = { &_swigt__p_ldb_ldif, &_swigt__p_ldb_message, &_swigt__p_ldb_message_element, + &_swigt__p_ldb_module, + &_swigt__p_ldb_parse_tree, &_swigt__p_ldb_result, &_swigt__p_loadparm_context, &_swigt__p_loadparm_service, @@ -3398,6 +3404,8 @@ static swig_cast_info _swigc__p_ldb_dn[] = { {&_swigt__p_ldb_dn, 0, 0, 0},{0, 0 static swig_cast_info _swigc__p_ldb_ldif[] = { {&_swigt__p_ldb_ldif, 0, 0, 0},{0, 0, 0, 0}}; static swig_cast_info _swigc__p_ldb_message[] = { {&_swigt__p_ldb_message, 0, 0, 0},{0, 0, 0, 0}}; static swig_cast_info _swigc__p_ldb_message_element[] = { {&_swigt__p_ldb_message_element, 0, 0, 0},{0, 0, 0, 0}}; +static swig_cast_info _swigc__p_ldb_module[] = { {&_swigt__p_ldb_module, 0, 0, 0},{0, 0, 0, 0}}; +static swig_cast_info _swigc__p_ldb_parse_tree[] = { {&_swigt__p_ldb_parse_tree, 0, 0, 0},{0, 0, 0, 0}}; static swig_cast_info _swigc__p_ldb_result[] = { {&_swigt__p_ldb_result, 0, 0, 0},{0, 0, 0, 0}}; static swig_cast_info _swigc__p_loadparm_context[] = { {&_swigt__p_loadparm_context, 0, 0, 0},{0, 0, 0, 0}}; static swig_cast_info _swigc__p_loadparm_service[] = { {&_swigt__p_loadparm_service, 0, 0, 0},{0, 0, 0, 0}}; @@ -3427,6 +3435,8 @@ static swig_cast_info *swig_cast_initial[] = { _swigc__p_ldb_ldif, _swigc__p_ldb_message, _swigc__p_ldb_message_element, + _swigc__p_ldb_module, + _swigc__p_ldb_parse_tree, _swigc__p_ldb_result, _swigc__p_loadparm_context, _swigc__p_loadparm_service, -- cgit From fae2fce47e28ccfe5f43744200ce2ae68da3a9aa Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Sat, 27 Sep 2008 01:49:22 +0200 Subject: s4:provision: don't do the full provision in the become_dc metze --- source4/scripting/python/samba/provision.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'source4/scripting') diff --git a/source4/scripting/python/samba/provision.py b/source4/scripting/python/samba/provision.py index 68f61532ad..427ca975b3 100644 --- a/source4/scripting/python/samba/provision.py +++ b/source4/scripting/python/samba/provision.py @@ -744,6 +744,8 @@ def setup_samdb(path, setup_path, session_info, credentials, lp, samdb = SamDB(path, session_info=session_info, credentials=credentials, lp=lp) + if fill == FILL_DRS: + return samdb message("Pre-loading the Samba 4 and AD schema") samdb.set_domain_sid(domainsid) -- cgit From 7f1c02cd7a8f4238041406acab061f34c587c69a Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Tue, 30 Sep 2008 15:24:46 +0200 Subject: Enable winreg Python tests - authentication works now. --- source4/scripting/python/samba/tests/dcerpc/rpcecho.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'source4/scripting') diff --git a/source4/scripting/python/samba/tests/dcerpc/rpcecho.py b/source4/scripting/python/samba/tests/dcerpc/rpcecho.py index 12638e2397..06790736b8 100644 --- a/source4/scripting/python/samba/tests/dcerpc/rpcecho.py +++ b/source4/scripting/python/samba/tests/dcerpc/rpcecho.py @@ -25,7 +25,8 @@ from samba.tests import RpcInterfaceTestCase class RpcEchoTests(RpcInterfaceTestCase): def setUp(self): - self.conn = echo.rpcecho("ncalrpc:", self.get_loadparm()) + self.conn = echo.rpcecho("ncalrpc:", self.get_loadparm(), + self.get_credentials()) def test_two_contexts(self): self.conn2 = echo.rpcecho("ncalrpc:", self.get_loadparm(), basis_connection=self.conn) -- cgit From 3ecde315d3ef6d13ea3811d34deb51f87cdeaa6d Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Wed, 8 Oct 2008 01:58:42 +0200 Subject: Import tests for subunit python module. --- source4/scripting/python/subunit/tests/TestUtil.py | 80 +++ source4/scripting/python/subunit/tests/__init__.py | 25 + .../python/subunit/tests/sample-script.py | 11 + .../python/subunit/tests/sample-two-script.py | 7 + .../python/subunit/tests/test_test_protocol.py | 730 +++++++++++++++++++++ 5 files changed, 853 insertions(+) create mode 100644 source4/scripting/python/subunit/tests/TestUtil.py create mode 100644 source4/scripting/python/subunit/tests/__init__.py create mode 100755 source4/scripting/python/subunit/tests/sample-script.py create mode 100755 source4/scripting/python/subunit/tests/sample-two-script.py create mode 100644 source4/scripting/python/subunit/tests/test_test_protocol.py (limited to 'source4/scripting') diff --git a/source4/scripting/python/subunit/tests/TestUtil.py b/source4/scripting/python/subunit/tests/TestUtil.py new file mode 100644 index 0000000000..1b5ba9c293 --- /dev/null +++ b/source4/scripting/python/subunit/tests/TestUtil.py @@ -0,0 +1,80 @@ +# Copyright (c) 2004 Canonical Limited +# Author: Robert Collins +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +import sys +import logging +import unittest + + +class LogCollector(logging.Handler): + def __init__(self): + logging.Handler.__init__(self) + self.records=[] + def emit(self, record): + self.records.append(record.getMessage()) + + +def makeCollectingLogger(): + """I make a logger instance that collects its logs for programmatic analysis + -> (logger, collector)""" + logger=logging.Logger("collector") + handler=LogCollector() + handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s")) + logger.addHandler(handler) + return logger, handler + + +def visitTests(suite, visitor): + """A foreign method for visiting the tests in a test suite.""" + for test in suite._tests: + #Abusing types to avoid monkey patching unittest.TestCase. + # Maybe that would be better? + try: + test.visit(visitor) + except AttributeError: + if isinstance(test, unittest.TestCase): + visitor.visitCase(test) + elif isinstance(test, unittest.TestSuite): + visitor.visitSuite(test) + visitTests(test, visitor) + else: + print "unvisitable non-unittest.TestCase element %r (%r)" % (test, test.__class__) + + +class TestSuite(unittest.TestSuite): + """I am an extended TestSuite with a visitor interface. + This is primarily to allow filtering of tests - and suites or + more in the future. An iterator of just tests wouldn't scale...""" + + def visit(self, visitor): + """visit the composite. Visiting is depth-first. + current callbacks are visitSuite and visitCase.""" + visitor.visitSuite(self) + visitTests(self, visitor) + + +class TestLoader(unittest.TestLoader): + """Custome TestLoader to set the right TestSuite class.""" + suiteClass = TestSuite + +class TestVisitor(object): + """A visitor for Tests""" + def visitSuite(self, aTestSuite): + pass + def visitCase(self, aTestCase): + pass diff --git a/source4/scripting/python/subunit/tests/__init__.py b/source4/scripting/python/subunit/tests/__init__.py new file mode 100644 index 0000000000..544d0e704f --- /dev/null +++ b/source4/scripting/python/subunit/tests/__init__.py @@ -0,0 +1,25 @@ +# +# subunit: extensions to python unittest to get test results from subprocesses. +# Copyright (C) 2005 Robert Collins +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +from subunit.tests import TestUtil, test_test_protocol + +def test_suite(): + result = TestUtil.TestSuite() + result.addTest(test_test_protocol.test_suite()) + return result diff --git a/source4/scripting/python/subunit/tests/sample-script.py b/source4/scripting/python/subunit/tests/sample-script.py new file mode 100755 index 0000000000..223d2f5d9f --- /dev/null +++ b/source4/scripting/python/subunit/tests/sample-script.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python +import sys +print "test old mcdonald" +print "success old mcdonald" +print "test bing crosby" +print "failure bing crosby [" +print "foo.c:53:ERROR invalid state" +print "]" +print "test an error" +print "error an error" +sys.exit(0) diff --git a/source4/scripting/python/subunit/tests/sample-two-script.py b/source4/scripting/python/subunit/tests/sample-two-script.py new file mode 100755 index 0000000000..d5550842bf --- /dev/null +++ b/source4/scripting/python/subunit/tests/sample-two-script.py @@ -0,0 +1,7 @@ +#!/usr/bin/env python +import sys +print "test old mcdonald" +print "success old mcdonald" +print "test bing crosby" +print "success bing crosby" +sys.exit(0) diff --git a/source4/scripting/python/subunit/tests/test_test_protocol.py b/source4/scripting/python/subunit/tests/test_test_protocol.py new file mode 100644 index 0000000000..af31584a97 --- /dev/null +++ b/source4/scripting/python/subunit/tests/test_test_protocol.py @@ -0,0 +1,730 @@ +# +# subunit: extensions to python unittest to get test results from subprocesses. +# Copyright (C) 2005 Robert Collins +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +import unittest +from StringIO import StringIO +import os +import subunit +import sys + +try: + class MockTestProtocolServerClient(object): + """A mock protocol server client to test callbacks.""" + + def __init__(self): + self.end_calls = [] + self.error_calls = [] + self.failure_calls = [] + self.start_calls = [] + self.success_calls = [] + super(MockTestProtocolServerClient, self).__init__() + + def addError(self, test, error): + self.error_calls.append((test, error)) + + def addFailure(self, test, error): + self.failure_calls.append((test, error)) + + def addSuccess(self, test): + self.success_calls.append(test) + + def stopTest(self, test): + self.end_calls.append(test) + + def startTest(self, test): + self.start_calls.append(test) + +except AttributeError: + MockTestProtocolServer = None + + +class TestMockTestProtocolServer(unittest.TestCase): + + def test_start_test(self): + protocol = MockTestProtocolServerClient() + protocol.startTest(subunit.RemotedTestCase("test old mcdonald")) + self.assertEqual(protocol.start_calls, + [subunit.RemotedTestCase("test old mcdonald")]) + self.assertEqual(protocol.end_calls, []) + self.assertEqual(protocol.error_calls, []) + self.assertEqual(protocol.failure_calls, []) + self.assertEqual(protocol.success_calls, []) + + def test_add_error(self): + protocol = MockTestProtocolServerClient() + protocol.addError(subunit.RemotedTestCase("old mcdonald"), + subunit.RemoteError("omg it works")) + self.assertEqual(protocol.start_calls, []) + self.assertEqual(protocol.end_calls, []) + self.assertEqual(protocol.error_calls, [( + subunit.RemotedTestCase("old mcdonald"), + subunit.RemoteError("omg it works"))]) + self.assertEqual(protocol.failure_calls, []) + self.assertEqual(protocol.success_calls, []) + + def test_add_failure(self): + protocol = MockTestProtocolServerClient() + protocol.addFailure(subunit.RemotedTestCase("old mcdonald"), + subunit.RemoteError("omg it works")) + self.assertEqual(protocol.start_calls, []) + self.assertEqual(protocol.end_calls, []) + self.assertEqual(protocol.error_calls, []) + self.assertEqual(protocol.failure_calls, [ + (subunit.RemotedTestCase("old mcdonald"), + subunit.RemoteError("omg it works"))]) + self.assertEqual(protocol.success_calls, []) + + def test_add_success(self): + protocol = MockTestProtocolServerClient() + protocol.addSuccess(subunit.RemotedTestCase("test old mcdonald")) + self.assertEqual(protocol.start_calls, []) + self.assertEqual(protocol.end_calls, []) + self.assertEqual(protocol.error_calls, []) + self.assertEqual(protocol.failure_calls, []) + self.assertEqual(protocol.success_calls, + [subunit.RemotedTestCase("test old mcdonald")]) + + def test_end_test(self): + protocol = MockTestProtocolServerClient() + protocol.stopTest(subunit.RemotedTestCase("test old mcdonald")) + self.assertEqual(protocol.end_calls, + [subunit.RemotedTestCase("test old mcdonald")]) + self.assertEqual(protocol.error_calls, []) + self.assertEqual(protocol.failure_calls, []) + self.assertEqual(protocol.success_calls, []) + self.assertEqual(protocol.start_calls, []) + + +class TestTestImports(unittest.TestCase): + + def test_imports(self): + from subunit import TestProtocolServer + from subunit import RemotedTestCase + from subunit import RemoteError + from subunit import ExecTestCase + from subunit import IsolatedTestCase + from subunit import TestProtocolClient + + +class TestTestProtocolServerPipe(unittest.TestCase): + + def test_story(self): + client = unittest.TestResult() + protocol = subunit.TestProtocolServer(client) + pipe = StringIO("test old mcdonald\n" + "success old mcdonald\n" + "test bing crosby\n" + "failure bing crosby [\n" + "foo.c:53:ERROR invalid state\n" + "]\n" + "test an error\n" + "error an error\n") + protocol.readFrom(pipe) + mcdonald = subunit.RemotedTestCase("old mcdonald") + bing = subunit.RemotedTestCase("bing crosby") + an_error = subunit.RemotedTestCase("an error") + self.assertEqual(client.errors, + [(an_error, 'RemoteException: \n\n')]) + self.assertEqual( + client.failures, + [(bing, "RemoteException: foo.c:53:ERROR invalid state\n\n")]) + self.assertEqual(client.testsRun, 3) + + +class TestTestProtocolServerStartTest(unittest.TestCase): + + def setUp(self): + self.client = MockTestProtocolServerClient() + self.protocol = subunit.TestProtocolServer(self.client) + + def test_start_test(self): + self.protocol.lineReceived("test old mcdonald\n") + self.assertEqual(self.client.start_calls, + [subunit.RemotedTestCase("old mcdonald")]) + + def test_start_testing(self): + self.protocol.lineReceived("testing old mcdonald\n") + self.assertEqual(self.client.start_calls, + [subunit.RemotedTestCase("old mcdonald")]) + + def test_start_test_colon(self): + self.protocol.lineReceived("test: old mcdonald\n") + self.assertEqual(self.client.start_calls, + [subunit.RemotedTestCase("old mcdonald")]) + + def test_start_testing_colon(self): + self.protocol.lineReceived("testing: old mcdonald\n") + self.assertEqual(self.client.start_calls, + [subunit.RemotedTestCase("old mcdonald")]) + + +class TestTestProtocolServerPassThrough(unittest.TestCase): + + def setUp(self): + from StringIO import StringIO + self.stdout = StringIO() + self.test = subunit.RemotedTestCase("old mcdonald") + self.client = MockTestProtocolServerClient() + self.protocol = subunit.TestProtocolServer(self.client, self.stdout) + + def keywords_before_test(self): + self.protocol.lineReceived("failure a\n") + self.protocol.lineReceived("failure: a\n") + self.protocol.lineReceived("error a\n") + self.protocol.lineReceived("error: a\n") + self.protocol.lineReceived("success a\n") + self.protocol.lineReceived("success: a\n") + self.protocol.lineReceived("successful a\n") + self.protocol.lineReceived("successful: a\n") + self.protocol.lineReceived("]\n") + self.assertEqual(self.stdout.getvalue(), "failure a\n" + "failure: a\n" + "error a\n" + "error: a\n" + "success a\n" + "success: a\n" + "successful a\n" + "successful: a\n" + "]\n") + + def test_keywords_before_test(self): + self.keywords_before_test() + self.assertEqual(self.client.start_calls, []) + self.assertEqual(self.client.error_calls, []) + self.assertEqual(self.client.failure_calls, []) + self.assertEqual(self.client.success_calls, []) + + def test_keywords_after_error(self): + self.protocol.lineReceived("test old mcdonald\n") + self.protocol.lineReceived("error old mcdonald\n") + self.keywords_before_test() + self.assertEqual(self.client.start_calls, [self.test]) + self.assertEqual(self.client.end_calls, [self.test]) + self.assertEqual(self.client.error_calls, + [(self.test, subunit.RemoteError(""))]) + self.assertEqual(self.client.failure_calls, []) + self.assertEqual(self.client.success_calls, []) + + def test_keywords_after_failure(self): + self.protocol.lineReceived("test old mcdonald\n") + self.protocol.lineReceived("failure old mcdonald\n") + self.keywords_before_test() + self.assertEqual(self.client.start_calls, [self.test]) + self.assertEqual(self.client.end_calls, [self.test]) + self.assertEqual(self.client.error_calls, []) + self.assertEqual(self.client.failure_calls, + [(self.test, subunit.RemoteError())]) + self.assertEqual(self.client.success_calls, []) + + def test_keywords_after_success(self): + self.protocol.lineReceived("test old mcdonald\n") + self.protocol.lineReceived("success old mcdonald\n") + self.keywords_before_test() + self.assertEqual(self.client.start_calls, [self.test]) + self.assertEqual(self.client.end_calls, [self.test]) + self.assertEqual(self.client.error_calls, []) + self.assertEqual(self.client.failure_calls, []) + self.assertEqual(self.client.success_calls, [self.test]) + + def test_keywords_after_test(self): + self.protocol.lineReceived("test old mcdonald\n") + self.protocol.lineReceived("test old mcdonald\n") + self.protocol.lineReceived("failure a\n") + self.protocol.lineReceived("failure: a\n") + self.protocol.lineReceived("error a\n") + self.protocol.lineReceived("error: a\n") + self.protocol.lineReceived("success a\n") + self.protocol.lineReceived("success: a\n") + self.protocol.lineReceived("successful a\n") + self.protocol.lineReceived("successful: a\n") + self.protocol.lineReceived("]\n") + self.protocol.lineReceived("failure old mcdonald\n") + self.assertEqual(self.stdout.getvalue(), "test old mcdonald\n" + "failure a\n" + "failure: a\n" + "error a\n" + "error: a\n" + "success a\n" + "success: a\n" + "successful a\n" + "successful: a\n" + "]\n") + self.assertEqual(self.client.start_calls, [self.test]) + self.assertEqual(self.client.end_calls, [self.test]) + self.assertEqual(self.client.failure_calls, + [(self.test, subunit.RemoteError())]) + self.assertEqual(self.client.error_calls, []) + self.assertEqual(self.client.success_calls, []) + + def test_keywords_during_failure(self): + self.protocol.lineReceived("test old mcdonald\n") + self.protocol.lineReceived("failure: old mcdonald [\n") + self.protocol.lineReceived("test old mcdonald\n") + self.protocol.lineReceived("failure a\n") + self.protocol.lineReceived("failure: a\n") + self.protocol.lineReceived("error a\n") + self.protocol.lineReceived("error: a\n") + self.protocol.lineReceived("success a\n") + self.protocol.lineReceived("success: a\n") + self.protocol.lineReceived("successful a\n") + self.protocol.lineReceived("successful: a\n") + self.protocol.lineReceived(" ]\n") + self.protocol.lineReceived("]\n") + self.assertEqual(self.stdout.getvalue(), "") + self.assertEqual(self.client.start_calls, [self.test]) + self.assertEqual(self.client.failure_calls, + [(self.test, subunit.RemoteError("test old mcdonald\n" + "failure a\n" + "failure: a\n" + "error a\n" + "error: a\n" + "success a\n" + "success: a\n" + "successful a\n" + "successful: a\n" + "]\n"))]) + self.assertEqual(self.client.end_calls, [self.test]) + self.assertEqual(self.client.error_calls, []) + self.assertEqual(self.client.success_calls, []) + + def test_stdout_passthrough(self): + """Lines received which cannot be interpreted as any protocol action + should be passed through to sys.stdout. + """ + bytes = "randombytes\n" + self.protocol.lineReceived(bytes) + self.assertEqual(self.stdout.getvalue(), bytes) + + +class TestTestProtocolServerLostConnection(unittest.TestCase): + + def setUp(self): + self.client = MockTestProtocolServerClient() + self.protocol = subunit.TestProtocolServer(self.client) + self.test = subunit.RemotedTestCase("old mcdonald") + + def test_lost_connection_no_input(self): + self.protocol.lostConnection() + self.assertEqual(self.client.start_calls, []) + self.assertEqual(self.client.error_calls, []) + self.assertEqual(self.client.failure_calls, []) + self.assertEqual(self.client.success_calls, []) + + def test_lost_connection_after_start(self): + self.protocol.lineReceived("test old mcdonald\n") + self.protocol.lostConnection() + self.assertEqual(self.client.start_calls, [self.test]) + self.assertEqual(self.client.end_calls, [self.test]) + self.assertEqual(self.client.error_calls, [ + (self.test, subunit.RemoteError("lost connection during " + "test 'old mcdonald'"))]) + self.assertEqual(self.client.failure_calls, []) + self.assertEqual(self.client.success_calls, []) + + def test_lost_connected_after_error(self): + self.protocol.lineReceived("test old mcdonald\n") + self.protocol.lineReceived("error old mcdonald\n") + self.protocol.lostConnection() + self.assertEqual(self.client.start_calls, [self.test]) + self.assertEqual(self.client.failure_calls, []) + self.assertEqual(self.client.end_calls, [self.test]) + self.assertEqual(self.client.error_calls, [ + (self.test, subunit.RemoteError(""))]) + self.assertEqual(self.client.success_calls, []) + + def test_lost_connection_during_error(self): + self.protocol.lineReceived("test old mcdonald\n") + self.protocol.lineReceived("error old mcdonald [\n") + self.protocol.lostConnection() + self.assertEqual(self.client.start_calls, [self.test]) + self.assertEqual(self.client.end_calls, [self.test]) + self.assertEqual(self.client.error_calls, [ + (self.test, subunit.RemoteError("lost connection during error " + "report of test 'old mcdonald'"))]) + self.assertEqual(self.client.failure_calls, []) + self.assertEqual(self.client.success_calls, []) + + def test_lost_connected_after_failure(self): + self.protocol.lineReceived("test old mcdonald\n") + self.protocol.lineReceived("failure old mcdonald\n") + self.protocol.lostConnection() + test = subunit.RemotedTestCase("old mcdonald") + self.assertEqual(self.client.start_calls, [self.test]) + self.assertEqual(self.client.end_calls, [self.test]) + self.assertEqual(self.client.error_calls, []) + self.assertEqual(self.client.failure_calls, + [(self.test, subunit.RemoteError())]) + self.assertEqual(self.client.success_calls, []) + + def test_lost_connection_during_failure(self): + self.protocol.lineReceived("test old mcdonald\n") + self.protocol.lineReceived("failure old mcdonald [\n") + self.protocol.lostConnection() + self.assertEqual(self.client.start_calls, [self.test]) + self.assertEqual(self.client.end_calls, [self.test]) + self.assertEqual(self.client.error_calls, + [(self.test, + subunit.RemoteError("lost connection during " + "failure report" + " of test 'old mcdonald'"))]) + self.assertEqual(self.client.failure_calls, []) + self.assertEqual(self.client.success_calls, []) + + def test_lost_connection_after_success(self): + self.protocol.lineReceived("test old mcdonald\n") + self.protocol.lineReceived("success old mcdonald\n") + self.protocol.lostConnection() + self.assertEqual(self.client.start_calls, [self.test]) + self.assertEqual(self.client.end_calls, [self.test]) + self.assertEqual(self.client.error_calls, []) + self.assertEqual(self.client.failure_calls, []) + self.assertEqual(self.client.success_calls, [self.test]) + + +class TestTestProtocolServerAddError(unittest.TestCase): + + def setUp(self): + self.client = MockTestProtocolServerClient() + self.protocol = subunit.TestProtocolServer(self.client) + self.protocol.lineReceived("test mcdonalds farm\n") + self.test = subunit.RemotedTestCase("mcdonalds farm") + + def simple_error_keyword(self, keyword): + self.protocol.lineReceived("%s mcdonalds farm\n" % keyword) + self.assertEqual(self.client.start_calls, [self.test]) + self.assertEqual(self.client.end_calls, [self.test]) + self.assertEqual(self.client.error_calls, [ + (self.test, subunit.RemoteError(""))]) + self.assertEqual(self.client.failure_calls, []) + + def test_simple_error(self): + self.simple_error_keyword("error") + + def test_simple_error_colon(self): + self.simple_error_keyword("error:") + + def test_error_empty_message(self): + self.protocol.lineReceived("error mcdonalds farm [\n") + self.protocol.lineReceived("]\n") + self.assertEqual(self.client.start_calls, [self.test]) + self.assertEqual(self.client.end_calls, [self.test]) + self.assertEqual(self.client.error_calls, [ + (self.test, subunit.RemoteError(""))]) + self.assertEqual(self.client.failure_calls, []) + + def error_quoted_bracket(self, keyword): + self.protocol.lineReceived("%s mcdonalds farm [\n" % keyword) + self.protocol.lineReceived(" ]\n") + self.protocol.lineReceived("]\n") + self.assertEqual(self.client.start_calls, [self.test]) + self.assertEqual(self.client.end_calls, [self.test]) + self.assertEqual(self.client.error_calls, [ + (self.test, subunit.RemoteError("]\n"))]) + self.assertEqual(self.client.failure_calls, []) + + def test_error_quoted_bracket(self): + self.error_quoted_bracket("error") + + def test_error_colon_quoted_bracket(self): + self.error_quoted_bracket("error:") + + +class TestTestProtocolServerAddFailure(unittest.TestCase): + + def setUp(self): + self.client = MockTestProtocolServerClient() + self.protocol = subunit.TestProtocolServer(self.client) + self.protocol.lineReceived("test mcdonalds farm\n") + self.test = subunit.RemotedTestCase("mcdonalds farm") + + def simple_failure_keyword(self, keyword): + self.protocol.lineReceived("%s mcdonalds farm\n" % keyword) + self.assertEqual(self.client.start_calls, [self.test]) + self.assertEqual(self.client.end_calls, [self.test]) + self.assertEqual(self.client.error_calls, []) + self.assertEqual(self.client.failure_calls, + [(self.test, subunit.RemoteError())]) + + def test_simple_failure(self): + self.simple_failure_keyword("failure") + + def test_simple_failure_colon(self): + self.simple_failure_keyword("failure:") + + def test_failure_empty_message(self): + self.protocol.lineReceived("failure mcdonalds farm [\n") + self.protocol.lineReceived("]\n") + self.assertEqual(self.client.start_calls, [self.test]) + self.assertEqual(self.client.end_calls, [self.test]) + self.assertEqual(self.client.error_calls, []) + self.assertEqual(self.client.failure_calls, + [(self.test, subunit.RemoteError())]) + + def failure_quoted_bracket(self, keyword): + self.protocol.lineReceived("%s mcdonalds farm [\n" % keyword) + self.protocol.lineReceived(" ]\n") + self.protocol.lineReceived("]\n") + self.assertEqual(self.client.start_calls, [self.test]) + self.assertEqual(self.client.end_calls, [self.test]) + self.assertEqual(self.client.error_calls, []) + self.assertEqual(self.client.failure_calls, + [(self.test, subunit.RemoteError("]\n"))]) + + def test_failure_quoted_bracket(self): + self.failure_quoted_bracket("failure") + + def test_failure_colon_quoted_bracket(self): + self.failure_quoted_bracket("failure:") + + +class TestTestProtocolServerAddSuccess(unittest.TestCase): + + def setUp(self): + self.client = MockTestProtocolServerClient() + self.protocol = subunit.TestProtocolServer(self.client) + self.protocol.lineReceived("test mcdonalds farm\n") + self.test = subunit.RemotedTestCase("mcdonalds farm") + + def simple_success_keyword(self, keyword): + self.protocol.lineReceived("%s mcdonalds farm\n" % keyword) + self.assertEqual(self.client.start_calls, [self.test]) + self.assertEqual(self.client.end_calls, [self.test]) + self.assertEqual(self.client.error_calls, []) + self.assertEqual(self.client.success_calls, [self.test]) + + def test_simple_success(self): + self.simple_success_keyword("failure") + + def test_simple_success_colon(self): + self.simple_success_keyword("failure:") + + def test_simple_success(self): + self.simple_success_keyword("successful") + + def test_simple_success_colon(self): + self.simple_success_keyword("successful:") + + +class TestRemotedTestCase(unittest.TestCase): + + def test_simple(self): + test = subunit.RemotedTestCase("A test description") + self.assertRaises(NotImplementedError, test.setUp) + self.assertRaises(NotImplementedError, test.tearDown) + self.assertEqual("A test description", + test.shortDescription()) + self.assertEqual("subunit.RemotedTestCase.A test description", + test.id()) + self.assertEqual("A test description (subunit.RemotedTestCase)", "%s" % test) + self.assertEqual("", "%r" % test) + result = unittest.TestResult() + test.run(result) + self.assertEqual([(test, "RemoteException: " + "Cannot run RemotedTestCases.\n\n")], + result.errors) + self.assertEqual(1, result.testsRun) + another_test = subunit.RemotedTestCase("A test description") + self.assertEqual(test, another_test) + different_test = subunit.RemotedTestCase("ofo") + self.assertNotEqual(test, different_test) + self.assertNotEqual(another_test, different_test) + + +class TestRemoteError(unittest.TestCase): + + def test_eq(self): + error = subunit.RemoteError("Something went wrong") + another_error = subunit.RemoteError("Something went wrong") + different_error = subunit.RemoteError("boo!") + self.assertEqual(error, another_error) + self.assertNotEqual(error, different_error) + self.assertNotEqual(different_error, another_error) + + def test_empty_constructor(self): + self.assertEqual(subunit.RemoteError(), subunit.RemoteError("")) + + +class TestExecTestCase(unittest.TestCase): + + class SampleExecTestCase(subunit.ExecTestCase): + + def test_sample_method(self): + """sample-script.py""" + # the sample script runs three tests, one each + # that fails, errors and succeeds + + + def test_construct(self): + test = self.SampleExecTestCase("test_sample_method") + self.assertEqual(test.script, + subunit.join_dir(__file__, 'sample-script.py')) + + def test_run(self): + runner = MockTestProtocolServerClient() + test = self.SampleExecTestCase("test_sample_method") + test.run(runner) + mcdonald = subunit.RemotedTestCase("old mcdonald") + bing = subunit.RemotedTestCase("bing crosby") + an_error = subunit.RemotedTestCase("an error") + self.assertEqual(runner.error_calls, + [(an_error, subunit.RemoteError())]) + self.assertEqual(runner.failure_calls, + [(bing, + subunit.RemoteError( + "foo.c:53:ERROR invalid state\n"))]) + self.assertEqual(runner.start_calls, [mcdonald, bing, an_error]) + self.assertEqual(runner.end_calls, [mcdonald, bing, an_error]) + + def test_debug(self): + test = self.SampleExecTestCase("test_sample_method") + test.debug() + + def test_count_test_cases(self): + """TODO run the child process and count responses to determine the count.""" + + def test_join_dir(self): + sibling = subunit.join_dir(__file__, 'foo') + expected = '%s/foo' % (os.path.split(__file__)[0],) + self.assertEqual(sibling, expected) + + +class DoExecTestCase(subunit.ExecTestCase): + + def test_working_script(self): + """sample-two-script.py""" + + +class TestIsolatedTestCase(unittest.TestCase): + + class SampleIsolatedTestCase(subunit.IsolatedTestCase): + + SETUP = False + TEARDOWN = False + TEST = False + + def setUp(self): + TestIsolatedTestCase.SampleIsolatedTestCase.SETUP = True + + def tearDown(self): + TestIsolatedTestCase.SampleIsolatedTestCase.TEARDOWN = True + + def test_sets_global_state(self): + TestIsolatedTestCase.SampleIsolatedTestCase.TEST = True + + + def test_construct(self): + test = self.SampleIsolatedTestCase("test_sets_global_state") + + def test_run(self): + result = unittest.TestResult() + test = self.SampleIsolatedTestCase("test_sets_global_state") + test.run(result) + self.assertEqual(result.testsRun, 1) + self.assertEqual(self.SampleIsolatedTestCase.SETUP, False) + self.assertEqual(self.SampleIsolatedTestCase.TEARDOWN, False) + self.assertEqual(self.SampleIsolatedTestCase.TEST, False) + + def test_debug(self): + pass + #test = self.SampleExecTestCase("test_sample_method") + #test.debug() + + +class TestIsolatedTestSuite(unittest.TestCase): + + class SampleTestToIsolate(unittest.TestCase): + + SETUP = False + TEARDOWN = False + TEST = False + + def setUp(self): + TestIsolatedTestSuite.SampleTestToIsolate.SETUP = True + + def tearDown(self): + TestIsolatedTestSuite.SampleTestToIsolate.TEARDOWN = True + + def test_sets_global_state(self): + TestIsolatedTestSuite.SampleTestToIsolate.TEST = True + + + def test_construct(self): + suite = subunit.IsolatedTestSuite() + + def test_run(self): + result = unittest.TestResult() + suite = subunit.IsolatedTestSuite() + sub_suite = unittest.TestSuite() + sub_suite.addTest(self.SampleTestToIsolate("test_sets_global_state")) + sub_suite.addTest(self.SampleTestToIsolate("test_sets_global_state")) + suite.addTest(sub_suite) + suite.addTest(self.SampleTestToIsolate("test_sets_global_state")) + suite.run(result) + self.assertEqual(result.testsRun, 3) + self.assertEqual(self.SampleTestToIsolate.SETUP, False) + self.assertEqual(self.SampleTestToIsolate.TEARDOWN, False) + self.assertEqual(self.SampleTestToIsolate.TEST, False) + + +class TestTestProtocolClient(unittest.TestCase): + + def setUp(self): + self.io = StringIO() + self.protocol = subunit.TestProtocolClient(self.io) + self.test = TestTestProtocolClient("test_start_test") + + + def test_start_test(self): + """Test startTest on a TestProtocolClient.""" + self.protocol.startTest(self.test) + self.assertEqual(self.io.getvalue(), "test: %s\n" % self.test.id()) + + def test_stop_test(self): + """Test stopTest on a TestProtocolClient.""" + self.protocol.stopTest(self.test) + self.assertEqual(self.io.getvalue(), "") + + def test_add_success(self): + """Test addSuccess on a TestProtocolClient.""" + self.protocol.addSuccess(self.test) + self.assertEqual( + self.io.getvalue(), "successful: %s\n" % self.test.id()) + + def test_add_failure(self): + """Test addFailure on a TestProtocolClient.""" + self.protocol.addFailure(self.test, subunit.RemoteError("boo")) + self.assertEqual( + self.io.getvalue(), + 'failure: %s [\nRemoteException: boo\n]\n' % self.test.id()) + + def test_add_error(self): + """Test stopTest on a TestProtocolClient.""" + self.protocol.addError(self.test, subunit.RemoteError("phwoar")) + self.assertEqual( + self.io.getvalue(), + 'error: %s [\n' + "RemoteException: phwoar\n" + "]\n" % self.test.id()) + + +def test_suite(): + loader = subunit.tests.TestUtil.TestLoader() + result = loader.loadTestsFromName(__name__) + return result -- cgit From 68837ff597bd39ff215ef30b4616692d2e31b1b4 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Tue, 7 Oct 2008 01:23:34 +0200 Subject: Fix syntax errors in minschema. --- source4/scripting/bin/minschema | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) (limited to 'source4/scripting') diff --git a/source4/scripting/bin/minschema b/source4/scripting/bin/minschema index 111557126d..e7d7ed4979 100755 --- a/source4/scripting/bin/minschema +++ b/source4/scripting/bin/minschema @@ -11,7 +11,8 @@ import os, sys sys.path.insert(0, "bin/python") import samba -from samba import getopt as options +from samba import getopt as options, Ldb +from ldb import SCOPE_SUBTREE, SCOPE_BASE, LdbError import sys parser = optparse.OptionParser("minschema ") @@ -50,7 +51,9 @@ if len(args) != 2: (url, classfile) = args -creds = credopts.get_credentials() +lp_ctx = sambaopts.get_loadparm() + +creds = credopts.get_credentials(lp_ctx) ldb = Ldb(url, credentials=creds) objectclasses = [] @@ -131,17 +134,10 @@ attrib_attrs = ["objectClass", # 2: abstract # 3: auxiliary -# -# print only if verbose is set -# -def dprintf(text): - if verbose is not None: - print text - def get_object_cn(ldb, name): attrs = ["cn"] - res = ldb.search("(ldapDisplayName=%s)" % name, rootDse["schemaNamingContext"], ldb.SCOPE_SUBTREE, attrs) + res = ldb.search("(ldapDisplayName=%s)" % name, rootDse["schemaNamingContext"], SCOPE_SUBTREE, attrs) assert len(res) == 1 return res[0]["cn"] @@ -229,7 +225,7 @@ def find_objectclass_properties(ldb, o): """the properties of an objectclass""" res = ldb.search( expression="(ldapDisplayName=%s)" % o.name, - basedn=rootDse["schemaNamingContext"], scope=ldb.SCOPE_SUBTREE, attrs=class_attrs) + base=rootDse["schemaNamingContext"], scope=SCOPE_SUBTREE, attrs=class_attrs) assert(len(res) == 1) msg = res[0] for a in msg: @@ -239,7 +235,7 @@ def find_attribute_properties(ldb, o): """find the properties of an attribute""" res = ldb.search( expression="(ldapDisplayName=%s)" % o.name, - basedn=rootDse["schemaNamingContext"], scope=ldb.SCOPE_SUBTREE, + base=rootDse["schemaNamingContext"], scope=SCOPE_SUBTREE, attrs=attrib_attrs) assert(len(res) == 1) msg = res[0] @@ -269,7 +265,7 @@ def find_objectclass_auto(ldb, o): print "%s\n" % ldif return - res = ldb.search("", testdn, ldb.SCOPE_BASE) + res = ldb.search(base=testdn, scope=ldb.SCOPE_BASE) ldb.delete(testdn) for a in res.msgs[0]: @@ -284,7 +280,7 @@ def expand_objectclass(ldb, o): "subClassOf"] res = ldb.search( expression="(&(objectClass=classSchema)(ldapDisplayName=%s))" % o.name, - basedn=rootDse["schemaNamingContext"], scope=ldb.SCOPE_SUBTREE, + base=rootDse["schemaNamingContext"], scope=SCOPE_SUBTREE, attrs=attrs) print "Expanding class %s\n" % o.name assert(len(res) == 1) @@ -322,13 +318,13 @@ def walk_dn(ldb, dn): # get a list of all possible attributes for this object attrs = ["allowedAttributes"] try: - res = ldb.search("objectClass=*", dn, ldb.SCOPE_BASE, attrs) + res = ldb.search("objectClass=*", dn, SCOPE_BASE, attrs) except LdbError, e: print "Unable to fetch allowedAttributes for '%s' - %r\n" % (dn, e) return allattrs = res[0]["allowedAttributes"] try: - res = ldb.search("objectClass=*", dn, ldb.SCOPE_BASE, allattrs) + res = ldb.search("objectClass=*", dn, SCOPE_BASE, allattrs) except LdbError, e: print "Unable to fetch all attributes for '%s' - %s\n" % (dn, e) return @@ -340,7 +336,7 @@ def walk_dn(ldb, dn): def walk_naming_context(ldb, namingContext): """walk a naming context, looking for all records""" try: - res = ldb.search("objectClass=*", namingContext, ldb.SCOPE_DEFAULT, + res = ldb.search("objectClass=*", namingContext, SCOPE_DEFAULT, ["objectClass"]) except LdbError, e: print "Unable to fetch objectClasses for '%s' - %s\n" % (namingContext, e) @@ -398,7 +394,7 @@ def build_objectclass(ldb, name): try: res = ldb.search( expression="(&(objectClass=classSchema)(ldapDisplayName=%s))" % name, - basedn=rootDse["schemaNamingContext"], scope=ldb.SCOPE_SUBTREE, + base=rootDse["schemaNamingContext"], scope=SCOPE_SUBTREE, attrs=attrs) except LdbError, e: print "unknown class '%s'\n" % name @@ -503,10 +499,10 @@ objectCategory: CN=SubSchema,${SCHEMADN} def load_list(file): """load a list from a file""" - return open(file, 'r').splitlines() + return open(file, 'r').readlines() # get the rootDSE -res = ldb.search("", "", ldb.SCOPE_BASE) +res = ldb.search(base="", expression="", scope=SCOPE_BASE, attrs=["schemaNamingContext"]) rootDse = res[0] # load the list of classes we are interested in -- cgit From f9facb51207713d1f294038e68909ba7701c61e4 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Wed, 8 Oct 2008 02:17:47 +0200 Subject: Move all subunit files to lib directory. --- source4/scripting/bin/subunitrun | 1 + source4/scripting/python/config.mk | 4 +- source4/scripting/python/subunit/__init__.py | 388 ----------- source4/scripting/python/subunit/tests/TestUtil.py | 80 --- source4/scripting/python/subunit/tests/__init__.py | 25 - .../python/subunit/tests/sample-script.py | 11 - .../python/subunit/tests/sample-two-script.py | 7 - .../python/subunit/tests/test_test_protocol.py | 730 --------------------- 8 files changed, 3 insertions(+), 1243 deletions(-) delete mode 100644 source4/scripting/python/subunit/__init__.py delete mode 100644 source4/scripting/python/subunit/tests/TestUtil.py delete mode 100644 source4/scripting/python/subunit/tests/__init__.py delete mode 100755 source4/scripting/python/subunit/tests/sample-script.py delete mode 100755 source4/scripting/python/subunit/tests/sample-two-script.py delete mode 100644 source4/scripting/python/subunit/tests/test_test_protocol.py (limited to 'source4/scripting') diff --git a/source4/scripting/bin/subunitrun b/source4/scripting/bin/subunitrun index 6f1086ad37..bc35c7ce22 100755 --- a/source4/scripting/bin/subunitrun +++ b/source4/scripting/bin/subunitrun @@ -21,6 +21,7 @@ import sys # Find right directory when running from source tree sys.path.insert(0, "bin/python") +sys.path.insert(0, "../lib/subunit/python") from subunit import SubunitTestRunner from unittest import TestProgram diff --git a/source4/scripting/python/config.mk b/source4/scripting/python/config.mk index b494ee6e8d..c88728a9fd 100644 --- a/source4/scripting/python/config.mk +++ b/source4/scripting/python/config.mk @@ -23,7 +23,7 @@ python_misc_OBJ_FILES = $(pyscriptsrcdir)/misc_wrap.o $(python_misc_OBJ_FILES): CFLAGS+=$(CFLAG_NO_UNUSED_MACROS) $(CFLAG_NO_CAST_QUAL) -_PY_FILES = $(shell find $(pyscriptsrcdir)/samba $(pyscriptsrcdir)/subunit -name "*.py") +_PY_FILES = $(shell find $(pyscriptsrcdir)/samba ../lib/subunit/python -name "*.py") $(foreach pyfile, $(_PY_FILES),$(eval $(call python_py_module_template,$(patsubst $(pyscriptsrcdir)/%,%,$(pyfile)),$(pyfile)))) @@ -32,6 +32,6 @@ $(eval $(call python_py_module_template,samba/misc.py,$(pyscriptsrcdir)/misc.py) EPYDOC_OPTIONS = --no-private --url http://www.samba.org/ --no-sourcecode epydoc:: pythonmods - PYTHONPATH=$(pythonbuilddir) epydoc $(EPYDOC_OPTIONS) samba tdb ldb subunit + PYTHONPATH=$(pythonbuilddir):../lib/subunit/python epydoc $(EPYDOC_OPTIONS) samba tdb ldb subunit install:: installpython diff --git a/source4/scripting/python/subunit/__init__.py b/source4/scripting/python/subunit/__init__.py deleted file mode 100644 index 406cd8765b..0000000000 --- a/source4/scripting/python/subunit/__init__.py +++ /dev/null @@ -1,388 +0,0 @@ -# -# subunit: extensions to python unittest to get test results from subprocesses. -# Copyright (C) 2005 Robert Collins -# Copyright (C) 2007 Jelmer Vernooij -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -# - -import os -from StringIO import StringIO -import sys -import unittest - -def test_suite(): - import subunit.tests - return subunit.tests.test_suite() - - -def join_dir(base_path, path): - """ - Returns an absolute path to C{path}, calculated relative to the parent - of C{base_path}. - - @param base_path: A path to a file or directory. - @param path: An absolute path, or a path relative to the containing - directory of C{base_path}. - - @return: An absolute path to C{path}. - """ - return os.path.join(os.path.dirname(os.path.abspath(base_path)), path) - - -class TestProtocolServer(object): - """A class for receiving results from a TestProtocol client.""" - - OUTSIDE_TEST = 0 - TEST_STARTED = 1 - READING_FAILURE = 2 - READING_ERROR = 3 - - def __init__(self, client, stream=sys.stdout): - """Create a TestProtocol server instance. - - client should be an object that provides - - startTest - - addSuccess - - addFailure - - addError - - stopTest - methods, i.e. a TestResult. - """ - self.state = TestProtocolServer.OUTSIDE_TEST - self.client = client - self._stream = stream - - def _addError(self, offset, line): - if (self.state == TestProtocolServer.TEST_STARTED and - self.current_test_description == line[offset:-1]): - self.state = TestProtocolServer.OUTSIDE_TEST - self.current_test_description = None - self.client.addError(self._current_test, RemoteError("")) - self.client.stopTest(self._current_test) - self._current_test = None - elif (self.state == TestProtocolServer.TEST_STARTED and - self.current_test_description + " [" == line[offset:-1]): - self.state = TestProtocolServer.READING_ERROR - self._message = "" - else: - self.stdOutLineReceived(line) - - def _addFailure(self, offset, line): - if (self.state == TestProtocolServer.TEST_STARTED and - self.current_test_description == line[offset:-1]): - self.state = TestProtocolServer.OUTSIDE_TEST - self.current_test_description = None - self.client.addFailure(self._current_test, RemoteError()) - self.client.stopTest(self._current_test) - elif (self.state == TestProtocolServer.TEST_STARTED and - self.current_test_description + " [" == line[offset:-1]): - self.state = TestProtocolServer.READING_FAILURE - self._message = "" - else: - self.stdOutLineReceived(line) - - def _addSuccess(self, offset, line): - if (self.state == TestProtocolServer.TEST_STARTED and - self.current_test_description == line[offset:-1]): - self.client.addSuccess(self._current_test) - self.client.stopTest(self._current_test) - self.current_test_description = None - self._current_test = None - self.state = TestProtocolServer.OUTSIDE_TEST - else: - self.stdOutLineReceived(line) - - def _appendMessage(self, line): - if line[0:2] == " ]": - # quoted ] start - self._message += line[1:] - else: - self._message += line - - def endQuote(self, line): - if self.state == TestProtocolServer.READING_FAILURE: - self.state = TestProtocolServer.OUTSIDE_TEST - self.current_test_description = None - self.client.addFailure(self._current_test, - RemoteError(self._message)) - self.client.stopTest(self._current_test) - elif self.state == TestProtocolServer.READING_ERROR: - self.state = TestProtocolServer.OUTSIDE_TEST - self.current_test_description = None - self.client.addError(self._current_test, - RemoteError(self._message)) - self.client.stopTest(self._current_test) - else: - self.stdOutLineReceived(line) - - def lineReceived(self, line): - """Call the appropriate local method for the received line.""" - if line == "]\n": - self.endQuote(line) - elif (self.state == TestProtocolServer.READING_FAILURE or - self.state == TestProtocolServer.READING_ERROR): - self._appendMessage(line) - else: - parts = line.split(None, 1) - if len(parts) == 2: - cmd, rest = parts - offset = len(cmd) + 1 - cmd = cmd.strip(':') - if cmd in ('test', 'testing'): - self._startTest(offset, line) - elif cmd == 'error': - self._addError(offset, line) - elif cmd == 'failure': - self._addFailure(offset, line) - elif cmd in ('success', 'successful'): - self._addSuccess(offset, line) - else: - self.stdOutLineReceived(line) - else: - self.stdOutLineReceived(line) - - def lostConnection(self): - """The input connection has finished.""" - if self.state == TestProtocolServer.TEST_STARTED: - self.client.addError(self._current_test, - RemoteError("lost connection during test '%s'" - % self.current_test_description)) - self.client.stopTest(self._current_test) - elif self.state == TestProtocolServer.READING_ERROR: - self.client.addError(self._current_test, - RemoteError("lost connection during " - "error report of test " - "'%s'" % - self.current_test_description)) - self.client.stopTest(self._current_test) - elif self.state == TestProtocolServer.READING_FAILURE: - self.client.addError(self._current_test, - RemoteError("lost connection during " - "failure report of test " - "'%s'" % - self.current_test_description)) - self.client.stopTest(self._current_test) - - def readFrom(self, pipe): - for line in pipe.readlines(): - self.lineReceived(line) - self.lostConnection() - - def _startTest(self, offset, line): - """Internal call to change state machine. Override startTest().""" - if self.state == TestProtocolServer.OUTSIDE_TEST: - self.state = TestProtocolServer.TEST_STARTED - self._current_test = RemotedTestCase(line[offset:-1]) - self.current_test_description = line[offset:-1] - self.client.startTest(self._current_test) - else: - self.stdOutLineReceived(line) - - def stdOutLineReceived(self, line): - self._stream.write(line) - - -class RemoteException(Exception): - """An exception that occured remotely to python.""" - - def __eq__(self, other): - try: - return self.args == other.args - except AttributeError: - return False - - -class TestProtocolClient(unittest.TestResult): - """A class that looks like a TestResult and informs a TestProtocolServer.""" - - def __init__(self, stream): - super(TestProtocolClient, self).__init__() - self._stream = stream - - def addError(self, test, error): - """Report an error in test test.""" - self._stream.write("error: %s [\n" % (test.shortDescription() or str(test))) - for line in self._exc_info_to_string(error, test).splitlines(): - self._stream.write("%s\n" % line) - self._stream.write("]\n") - super(TestProtocolClient, self).addError(test, error) - - def addFailure(self, test, error): - """Report a failure in test test.""" - self._stream.write("failure: %s [\n" % (test.shortDescription() or str(test))) - for line in self._exc_info_to_string(error, test).splitlines(): - self._stream.write("%s\n" % line) - self._stream.write("]\n") - super(TestProtocolClient, self).addFailure(test, error) - - def addSuccess(self, test): - """Report a success in a test.""" - self._stream.write("successful: %s\n" % (test.shortDescription() or str(test))) - super(TestProtocolClient, self).addSuccess(test) - - def startTest(self, test): - """Mark a test as starting its test run.""" - self._stream.write("test: %s\n" % (test.shortDescription() or str(test))) - super(TestProtocolClient, self).startTest(test) - - -def RemoteError(description=""): - if description == "": - description = "\n" - return (RemoteException, RemoteException(description), None) - - -class RemotedTestCase(unittest.TestCase): - """A class to represent test cases run in child processes.""" - - def __eq__ (self, other): - try: - return self.__description == other.__description - except AttributeError: - return False - - def __init__(self, description): - """Create a psuedo test case with description description.""" - self.__description = description - - def error(self, label): - raise NotImplementedError("%s on RemotedTestCases is not permitted." % - label) - - def setUp(self): - self.error("setUp") - - def tearDown(self): - self.error("tearDown") - - def shortDescription(self): - return self.__description - - def id(self): - return "%s.%s" % (self._strclass(), self.__description) - - def __str__(self): - return "%s (%s)" % (self.__description, self._strclass()) - - def __repr__(self): - return "<%s description='%s'>" % \ - (self._strclass(), self.__description) - - def run(self, result=None): - if result is None: result = self.defaultTestResult() - result.startTest(self) - result.addError(self, RemoteError("Cannot run RemotedTestCases.\n")) - result.stopTest(self) - - def _strclass(self): - cls = self.__class__ - return "%s.%s" % (cls.__module__, cls.__name__) - - -class ExecTestCase(unittest.TestCase): - """A test case which runs external scripts for test fixtures.""" - - def __init__(self, methodName='runTest'): - """Create an instance of the class that will use the named test - method when executed. Raises a ValueError if the instance does - not have a method with the specified name. - """ - unittest.TestCase.__init__(self, methodName) - testMethod = getattr(self, methodName) - self.script = join_dir(sys.modules[self.__class__.__module__].__file__, - testMethod.__doc__) - - def countTestCases(self): - return 1 - - def run(self, result=None): - if result is None: result = self.defaultTestResult() - self._run(result) - - def debug(self): - """Run the test without collecting errors in a TestResult""" - self._run(unittest.TestResult()) - - def _run(self, result): - protocol = TestProtocolServer(result) - output = os.popen(self.script, mode='r') - protocol.readFrom(output) - - -class IsolatedTestCase(unittest.TestCase): - """A TestCase which runs its tests in a forked process.""" - - def run(self, result=None): - if result is None: result = self.defaultTestResult() - run_isolated(unittest.TestCase, self, result) - - -class IsolatedTestSuite(unittest.TestSuite): - """A TestCase which runs its tests in a forked process.""" - - def run(self, result=None): - if result is None: result = unittest.TestResult() - run_isolated(unittest.TestSuite, self, result) - - -def run_isolated(klass, self, result): - """Run a test suite or case in a subprocess, using the run method on klass. - """ - c2pread, c2pwrite = os.pipe() - # fixme - error -> result - # now fork - pid = os.fork() - if pid == 0: - # Child - # Close parent's pipe ends - os.close(c2pread) - # Dup fds for child - os.dup2(c2pwrite, 1) - # Close pipe fds. - os.close(c2pwrite) - - # at this point, sys.stdin is redirected, now we want - # to filter it to escape ]'s. - ### XXX: test and write that bit. - - result = TestProtocolClient(sys.stdout) - klass.run(self, result) - sys.stdout.flush() - sys.stderr.flush() - # exit HARD, exit NOW. - os._exit(0) - else: - # Parent - # Close child pipe ends - os.close(c2pwrite) - # hookup a protocol engine - protocol = TestProtocolServer(result) - protocol.readFrom(os.fdopen(c2pread, 'rU')) - os.waitpid(pid, 0) - # TODO return code evaluation. - return result - - -class SubunitTestRunner(object): - def __init__(self, stream=sys.stdout): - self.stream = stream - - def run(self, test): - "Run the given test case or test suite." - result = TestProtocolClient(self.stream) - test(result) - return result - diff --git a/source4/scripting/python/subunit/tests/TestUtil.py b/source4/scripting/python/subunit/tests/TestUtil.py deleted file mode 100644 index 1b5ba9c293..0000000000 --- a/source4/scripting/python/subunit/tests/TestUtil.py +++ /dev/null @@ -1,80 +0,0 @@ -# Copyright (c) 2004 Canonical Limited -# Author: Robert Collins -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -# - -import sys -import logging -import unittest - - -class LogCollector(logging.Handler): - def __init__(self): - logging.Handler.__init__(self) - self.records=[] - def emit(self, record): - self.records.append(record.getMessage()) - - -def makeCollectingLogger(): - """I make a logger instance that collects its logs for programmatic analysis - -> (logger, collector)""" - logger=logging.Logger("collector") - handler=LogCollector() - handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s")) - logger.addHandler(handler) - return logger, handler - - -def visitTests(suite, visitor): - """A foreign method for visiting the tests in a test suite.""" - for test in suite._tests: - #Abusing types to avoid monkey patching unittest.TestCase. - # Maybe that would be better? - try: - test.visit(visitor) - except AttributeError: - if isinstance(test, unittest.TestCase): - visitor.visitCase(test) - elif isinstance(test, unittest.TestSuite): - visitor.visitSuite(test) - visitTests(test, visitor) - else: - print "unvisitable non-unittest.TestCase element %r (%r)" % (test, test.__class__) - - -class TestSuite(unittest.TestSuite): - """I am an extended TestSuite with a visitor interface. - This is primarily to allow filtering of tests - and suites or - more in the future. An iterator of just tests wouldn't scale...""" - - def visit(self, visitor): - """visit the composite. Visiting is depth-first. - current callbacks are visitSuite and visitCase.""" - visitor.visitSuite(self) - visitTests(self, visitor) - - -class TestLoader(unittest.TestLoader): - """Custome TestLoader to set the right TestSuite class.""" - suiteClass = TestSuite - -class TestVisitor(object): - """A visitor for Tests""" - def visitSuite(self, aTestSuite): - pass - def visitCase(self, aTestCase): - pass diff --git a/source4/scripting/python/subunit/tests/__init__.py b/source4/scripting/python/subunit/tests/__init__.py deleted file mode 100644 index 544d0e704f..0000000000 --- a/source4/scripting/python/subunit/tests/__init__.py +++ /dev/null @@ -1,25 +0,0 @@ -# -# subunit: extensions to python unittest to get test results from subprocesses. -# Copyright (C) 2005 Robert Collins -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -# - -from subunit.tests import TestUtil, test_test_protocol - -def test_suite(): - result = TestUtil.TestSuite() - result.addTest(test_test_protocol.test_suite()) - return result diff --git a/source4/scripting/python/subunit/tests/sample-script.py b/source4/scripting/python/subunit/tests/sample-script.py deleted file mode 100755 index 223d2f5d9f..0000000000 --- a/source4/scripting/python/subunit/tests/sample-script.py +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/env python -import sys -print "test old mcdonald" -print "success old mcdonald" -print "test bing crosby" -print "failure bing crosby [" -print "foo.c:53:ERROR invalid state" -print "]" -print "test an error" -print "error an error" -sys.exit(0) diff --git a/source4/scripting/python/subunit/tests/sample-two-script.py b/source4/scripting/python/subunit/tests/sample-two-script.py deleted file mode 100755 index d5550842bf..0000000000 --- a/source4/scripting/python/subunit/tests/sample-two-script.py +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/env python -import sys -print "test old mcdonald" -print "success old mcdonald" -print "test bing crosby" -print "success bing crosby" -sys.exit(0) diff --git a/source4/scripting/python/subunit/tests/test_test_protocol.py b/source4/scripting/python/subunit/tests/test_test_protocol.py deleted file mode 100644 index af31584a97..0000000000 --- a/source4/scripting/python/subunit/tests/test_test_protocol.py +++ /dev/null @@ -1,730 +0,0 @@ -# -# subunit: extensions to python unittest to get test results from subprocesses. -# Copyright (C) 2005 Robert Collins -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -# - -import unittest -from StringIO import StringIO -import os -import subunit -import sys - -try: - class MockTestProtocolServerClient(object): - """A mock protocol server client to test callbacks.""" - - def __init__(self): - self.end_calls = [] - self.error_calls = [] - self.failure_calls = [] - self.start_calls = [] - self.success_calls = [] - super(MockTestProtocolServerClient, self).__init__() - - def addError(self, test, error): - self.error_calls.append((test, error)) - - def addFailure(self, test, error): - self.failure_calls.append((test, error)) - - def addSuccess(self, test): - self.success_calls.append(test) - - def stopTest(self, test): - self.end_calls.append(test) - - def startTest(self, test): - self.start_calls.append(test) - -except AttributeError: - MockTestProtocolServer = None - - -class TestMockTestProtocolServer(unittest.TestCase): - - def test_start_test(self): - protocol = MockTestProtocolServerClient() - protocol.startTest(subunit.RemotedTestCase("test old mcdonald")) - self.assertEqual(protocol.start_calls, - [subunit.RemotedTestCase("test old mcdonald")]) - self.assertEqual(protocol.end_calls, []) - self.assertEqual(protocol.error_calls, []) - self.assertEqual(protocol.failure_calls, []) - self.assertEqual(protocol.success_calls, []) - - def test_add_error(self): - protocol = MockTestProtocolServerClient() - protocol.addError(subunit.RemotedTestCase("old mcdonald"), - subunit.RemoteError("omg it works")) - self.assertEqual(protocol.start_calls, []) - self.assertEqual(protocol.end_calls, []) - self.assertEqual(protocol.error_calls, [( - subunit.RemotedTestCase("old mcdonald"), - subunit.RemoteError("omg it works"))]) - self.assertEqual(protocol.failure_calls, []) - self.assertEqual(protocol.success_calls, []) - - def test_add_failure(self): - protocol = MockTestProtocolServerClient() - protocol.addFailure(subunit.RemotedTestCase("old mcdonald"), - subunit.RemoteError("omg it works")) - self.assertEqual(protocol.start_calls, []) - self.assertEqual(protocol.end_calls, []) - self.assertEqual(protocol.error_calls, []) - self.assertEqual(protocol.failure_calls, [ - (subunit.RemotedTestCase("old mcdonald"), - subunit.RemoteError("omg it works"))]) - self.assertEqual(protocol.success_calls, []) - - def test_add_success(self): - protocol = MockTestProtocolServerClient() - protocol.addSuccess(subunit.RemotedTestCase("test old mcdonald")) - self.assertEqual(protocol.start_calls, []) - self.assertEqual(protocol.end_calls, []) - self.assertEqual(protocol.error_calls, []) - self.assertEqual(protocol.failure_calls, []) - self.assertEqual(protocol.success_calls, - [subunit.RemotedTestCase("test old mcdonald")]) - - def test_end_test(self): - protocol = MockTestProtocolServerClient() - protocol.stopTest(subunit.RemotedTestCase("test old mcdonald")) - self.assertEqual(protocol.end_calls, - [subunit.RemotedTestCase("test old mcdonald")]) - self.assertEqual(protocol.error_calls, []) - self.assertEqual(protocol.failure_calls, []) - self.assertEqual(protocol.success_calls, []) - self.assertEqual(protocol.start_calls, []) - - -class TestTestImports(unittest.TestCase): - - def test_imports(self): - from subunit import TestProtocolServer - from subunit import RemotedTestCase - from subunit import RemoteError - from subunit import ExecTestCase - from subunit import IsolatedTestCase - from subunit import TestProtocolClient - - -class TestTestProtocolServerPipe(unittest.TestCase): - - def test_story(self): - client = unittest.TestResult() - protocol = subunit.TestProtocolServer(client) - pipe = StringIO("test old mcdonald\n" - "success old mcdonald\n" - "test bing crosby\n" - "failure bing crosby [\n" - "foo.c:53:ERROR invalid state\n" - "]\n" - "test an error\n" - "error an error\n") - protocol.readFrom(pipe) - mcdonald = subunit.RemotedTestCase("old mcdonald") - bing = subunit.RemotedTestCase("bing crosby") - an_error = subunit.RemotedTestCase("an error") - self.assertEqual(client.errors, - [(an_error, 'RemoteException: \n\n')]) - self.assertEqual( - client.failures, - [(bing, "RemoteException: foo.c:53:ERROR invalid state\n\n")]) - self.assertEqual(client.testsRun, 3) - - -class TestTestProtocolServerStartTest(unittest.TestCase): - - def setUp(self): - self.client = MockTestProtocolServerClient() - self.protocol = subunit.TestProtocolServer(self.client) - - def test_start_test(self): - self.protocol.lineReceived("test old mcdonald\n") - self.assertEqual(self.client.start_calls, - [subunit.RemotedTestCase("old mcdonald")]) - - def test_start_testing(self): - self.protocol.lineReceived("testing old mcdonald\n") - self.assertEqual(self.client.start_calls, - [subunit.RemotedTestCase("old mcdonald")]) - - def test_start_test_colon(self): - self.protocol.lineReceived("test: old mcdonald\n") - self.assertEqual(self.client.start_calls, - [subunit.RemotedTestCase("old mcdonald")]) - - def test_start_testing_colon(self): - self.protocol.lineReceived("testing: old mcdonald\n") - self.assertEqual(self.client.start_calls, - [subunit.RemotedTestCase("old mcdonald")]) - - -class TestTestProtocolServerPassThrough(unittest.TestCase): - - def setUp(self): - from StringIO import StringIO - self.stdout = StringIO() - self.test = subunit.RemotedTestCase("old mcdonald") - self.client = MockTestProtocolServerClient() - self.protocol = subunit.TestProtocolServer(self.client, self.stdout) - - def keywords_before_test(self): - self.protocol.lineReceived("failure a\n") - self.protocol.lineReceived("failure: a\n") - self.protocol.lineReceived("error a\n") - self.protocol.lineReceived("error: a\n") - self.protocol.lineReceived("success a\n") - self.protocol.lineReceived("success: a\n") - self.protocol.lineReceived("successful a\n") - self.protocol.lineReceived("successful: a\n") - self.protocol.lineReceived("]\n") - self.assertEqual(self.stdout.getvalue(), "failure a\n" - "failure: a\n" - "error a\n" - "error: a\n" - "success a\n" - "success: a\n" - "successful a\n" - "successful: a\n" - "]\n") - - def test_keywords_before_test(self): - self.keywords_before_test() - self.assertEqual(self.client.start_calls, []) - self.assertEqual(self.client.error_calls, []) - self.assertEqual(self.client.failure_calls, []) - self.assertEqual(self.client.success_calls, []) - - def test_keywords_after_error(self): - self.protocol.lineReceived("test old mcdonald\n") - self.protocol.lineReceived("error old mcdonald\n") - self.keywords_before_test() - self.assertEqual(self.client.start_calls, [self.test]) - self.assertEqual(self.client.end_calls, [self.test]) - self.assertEqual(self.client.error_calls, - [(self.test, subunit.RemoteError(""))]) - self.assertEqual(self.client.failure_calls, []) - self.assertEqual(self.client.success_calls, []) - - def test_keywords_after_failure(self): - self.protocol.lineReceived("test old mcdonald\n") - self.protocol.lineReceived("failure old mcdonald\n") - self.keywords_before_test() - self.assertEqual(self.client.start_calls, [self.test]) - self.assertEqual(self.client.end_calls, [self.test]) - self.assertEqual(self.client.error_calls, []) - self.assertEqual(self.client.failure_calls, - [(self.test, subunit.RemoteError())]) - self.assertEqual(self.client.success_calls, []) - - def test_keywords_after_success(self): - self.protocol.lineReceived("test old mcdonald\n") - self.protocol.lineReceived("success old mcdonald\n") - self.keywords_before_test() - self.assertEqual(self.client.start_calls, [self.test]) - self.assertEqual(self.client.end_calls, [self.test]) - self.assertEqual(self.client.error_calls, []) - self.assertEqual(self.client.failure_calls, []) - self.assertEqual(self.client.success_calls, [self.test]) - - def test_keywords_after_test(self): - self.protocol.lineReceived("test old mcdonald\n") - self.protocol.lineReceived("test old mcdonald\n") - self.protocol.lineReceived("failure a\n") - self.protocol.lineReceived("failure: a\n") - self.protocol.lineReceived("error a\n") - self.protocol.lineReceived("error: a\n") - self.protocol.lineReceived("success a\n") - self.protocol.lineReceived("success: a\n") - self.protocol.lineReceived("successful a\n") - self.protocol.lineReceived("successful: a\n") - self.protocol.lineReceived("]\n") - self.protocol.lineReceived("failure old mcdonald\n") - self.assertEqual(self.stdout.getvalue(), "test old mcdonald\n" - "failure a\n" - "failure: a\n" - "error a\n" - "error: a\n" - "success a\n" - "success: a\n" - "successful a\n" - "successful: a\n" - "]\n") - self.assertEqual(self.client.start_calls, [self.test]) - self.assertEqual(self.client.end_calls, [self.test]) - self.assertEqual(self.client.failure_calls, - [(self.test, subunit.RemoteError())]) - self.assertEqual(self.client.error_calls, []) - self.assertEqual(self.client.success_calls, []) - - def test_keywords_during_failure(self): - self.protocol.lineReceived("test old mcdonald\n") - self.protocol.lineReceived("failure: old mcdonald [\n") - self.protocol.lineReceived("test old mcdonald\n") - self.protocol.lineReceived("failure a\n") - self.protocol.lineReceived("failure: a\n") - self.protocol.lineReceived("error a\n") - self.protocol.lineReceived("error: a\n") - self.protocol.lineReceived("success a\n") - self.protocol.lineReceived("success: a\n") - self.protocol.lineReceived("successful a\n") - self.protocol.lineReceived("successful: a\n") - self.protocol.lineReceived(" ]\n") - self.protocol.lineReceived("]\n") - self.assertEqual(self.stdout.getvalue(), "") - self.assertEqual(self.client.start_calls, [self.test]) - self.assertEqual(self.client.failure_calls, - [(self.test, subunit.RemoteError("test old mcdonald\n" - "failure a\n" - "failure: a\n" - "error a\n" - "error: a\n" - "success a\n" - "success: a\n" - "successful a\n" - "successful: a\n" - "]\n"))]) - self.assertEqual(self.client.end_calls, [self.test]) - self.assertEqual(self.client.error_calls, []) - self.assertEqual(self.client.success_calls, []) - - def test_stdout_passthrough(self): - """Lines received which cannot be interpreted as any protocol action - should be passed through to sys.stdout. - """ - bytes = "randombytes\n" - self.protocol.lineReceived(bytes) - self.assertEqual(self.stdout.getvalue(), bytes) - - -class TestTestProtocolServerLostConnection(unittest.TestCase): - - def setUp(self): - self.client = MockTestProtocolServerClient() - self.protocol = subunit.TestProtocolServer(self.client) - self.test = subunit.RemotedTestCase("old mcdonald") - - def test_lost_connection_no_input(self): - self.protocol.lostConnection() - self.assertEqual(self.client.start_calls, []) - self.assertEqual(self.client.error_calls, []) - self.assertEqual(self.client.failure_calls, []) - self.assertEqual(self.client.success_calls, []) - - def test_lost_connection_after_start(self): - self.protocol.lineReceived("test old mcdonald\n") - self.protocol.lostConnection() - self.assertEqual(self.client.start_calls, [self.test]) - self.assertEqual(self.client.end_calls, [self.test]) - self.assertEqual(self.client.error_calls, [ - (self.test, subunit.RemoteError("lost connection during " - "test 'old mcdonald'"))]) - self.assertEqual(self.client.failure_calls, []) - self.assertEqual(self.client.success_calls, []) - - def test_lost_connected_after_error(self): - self.protocol.lineReceived("test old mcdonald\n") - self.protocol.lineReceived("error old mcdonald\n") - self.protocol.lostConnection() - self.assertEqual(self.client.start_calls, [self.test]) - self.assertEqual(self.client.failure_calls, []) - self.assertEqual(self.client.end_calls, [self.test]) - self.assertEqual(self.client.error_calls, [ - (self.test, subunit.RemoteError(""))]) - self.assertEqual(self.client.success_calls, []) - - def test_lost_connection_during_error(self): - self.protocol.lineReceived("test old mcdonald\n") - self.protocol.lineReceived("error old mcdonald [\n") - self.protocol.lostConnection() - self.assertEqual(self.client.start_calls, [self.test]) - self.assertEqual(self.client.end_calls, [self.test]) - self.assertEqual(self.client.error_calls, [ - (self.test, subunit.RemoteError("lost connection during error " - "report of test 'old mcdonald'"))]) - self.assertEqual(self.client.failure_calls, []) - self.assertEqual(self.client.success_calls, []) - - def test_lost_connected_after_failure(self): - self.protocol.lineReceived("test old mcdonald\n") - self.protocol.lineReceived("failure old mcdonald\n") - self.protocol.lostConnection() - test = subunit.RemotedTestCase("old mcdonald") - self.assertEqual(self.client.start_calls, [self.test]) - self.assertEqual(self.client.end_calls, [self.test]) - self.assertEqual(self.client.error_calls, []) - self.assertEqual(self.client.failure_calls, - [(self.test, subunit.RemoteError())]) - self.assertEqual(self.client.success_calls, []) - - def test_lost_connection_during_failure(self): - self.protocol.lineReceived("test old mcdonald\n") - self.protocol.lineReceived("failure old mcdonald [\n") - self.protocol.lostConnection() - self.assertEqual(self.client.start_calls, [self.test]) - self.assertEqual(self.client.end_calls, [self.test]) - self.assertEqual(self.client.error_calls, - [(self.test, - subunit.RemoteError("lost connection during " - "failure report" - " of test 'old mcdonald'"))]) - self.assertEqual(self.client.failure_calls, []) - self.assertEqual(self.client.success_calls, []) - - def test_lost_connection_after_success(self): - self.protocol.lineReceived("test old mcdonald\n") - self.protocol.lineReceived("success old mcdonald\n") - self.protocol.lostConnection() - self.assertEqual(self.client.start_calls, [self.test]) - self.assertEqual(self.client.end_calls, [self.test]) - self.assertEqual(self.client.error_calls, []) - self.assertEqual(self.client.failure_calls, []) - self.assertEqual(self.client.success_calls, [self.test]) - - -class TestTestProtocolServerAddError(unittest.TestCase): - - def setUp(self): - self.client = MockTestProtocolServerClient() - self.protocol = subunit.TestProtocolServer(self.client) - self.protocol.lineReceived("test mcdonalds farm\n") - self.test = subunit.RemotedTestCase("mcdonalds farm") - - def simple_error_keyword(self, keyword): - self.protocol.lineReceived("%s mcdonalds farm\n" % keyword) - self.assertEqual(self.client.start_calls, [self.test]) - self.assertEqual(self.client.end_calls, [self.test]) - self.assertEqual(self.client.error_calls, [ - (self.test, subunit.RemoteError(""))]) - self.assertEqual(self.client.failure_calls, []) - - def test_simple_error(self): - self.simple_error_keyword("error") - - def test_simple_error_colon(self): - self.simple_error_keyword("error:") - - def test_error_empty_message(self): - self.protocol.lineReceived("error mcdonalds farm [\n") - self.protocol.lineReceived("]\n") - self.assertEqual(self.client.start_calls, [self.test]) - self.assertEqual(self.client.end_calls, [self.test]) - self.assertEqual(self.client.error_calls, [ - (self.test, subunit.RemoteError(""))]) - self.assertEqual(self.client.failure_calls, []) - - def error_quoted_bracket(self, keyword): - self.protocol.lineReceived("%s mcdonalds farm [\n" % keyword) - self.protocol.lineReceived(" ]\n") - self.protocol.lineReceived("]\n") - self.assertEqual(self.client.start_calls, [self.test]) - self.assertEqual(self.client.end_calls, [self.test]) - self.assertEqual(self.client.error_calls, [ - (self.test, subunit.RemoteError("]\n"))]) - self.assertEqual(self.client.failure_calls, []) - - def test_error_quoted_bracket(self): - self.error_quoted_bracket("error") - - def test_error_colon_quoted_bracket(self): - self.error_quoted_bracket("error:") - - -class TestTestProtocolServerAddFailure(unittest.TestCase): - - def setUp(self): - self.client = MockTestProtocolServerClient() - self.protocol = subunit.TestProtocolServer(self.client) - self.protocol.lineReceived("test mcdonalds farm\n") - self.test = subunit.RemotedTestCase("mcdonalds farm") - - def simple_failure_keyword(self, keyword): - self.protocol.lineReceived("%s mcdonalds farm\n" % keyword) - self.assertEqual(self.client.start_calls, [self.test]) - self.assertEqual(self.client.end_calls, [self.test]) - self.assertEqual(self.client.error_calls, []) - self.assertEqual(self.client.failure_calls, - [(self.test, subunit.RemoteError())]) - - def test_simple_failure(self): - self.simple_failure_keyword("failure") - - def test_simple_failure_colon(self): - self.simple_failure_keyword("failure:") - - def test_failure_empty_message(self): - self.protocol.lineReceived("failure mcdonalds farm [\n") - self.protocol.lineReceived("]\n") - self.assertEqual(self.client.start_calls, [self.test]) - self.assertEqual(self.client.end_calls, [self.test]) - self.assertEqual(self.client.error_calls, []) - self.assertEqual(self.client.failure_calls, - [(self.test, subunit.RemoteError())]) - - def failure_quoted_bracket(self, keyword): - self.protocol.lineReceived("%s mcdonalds farm [\n" % keyword) - self.protocol.lineReceived(" ]\n") - self.protocol.lineReceived("]\n") - self.assertEqual(self.client.start_calls, [self.test]) - self.assertEqual(self.client.end_calls, [self.test]) - self.assertEqual(self.client.error_calls, []) - self.assertEqual(self.client.failure_calls, - [(self.test, subunit.RemoteError("]\n"))]) - - def test_failure_quoted_bracket(self): - self.failure_quoted_bracket("failure") - - def test_failure_colon_quoted_bracket(self): - self.failure_quoted_bracket("failure:") - - -class TestTestProtocolServerAddSuccess(unittest.TestCase): - - def setUp(self): - self.client = MockTestProtocolServerClient() - self.protocol = subunit.TestProtocolServer(self.client) - self.protocol.lineReceived("test mcdonalds farm\n") - self.test = subunit.RemotedTestCase("mcdonalds farm") - - def simple_success_keyword(self, keyword): - self.protocol.lineReceived("%s mcdonalds farm\n" % keyword) - self.assertEqual(self.client.start_calls, [self.test]) - self.assertEqual(self.client.end_calls, [self.test]) - self.assertEqual(self.client.error_calls, []) - self.assertEqual(self.client.success_calls, [self.test]) - - def test_simple_success(self): - self.simple_success_keyword("failure") - - def test_simple_success_colon(self): - self.simple_success_keyword("failure:") - - def test_simple_success(self): - self.simple_success_keyword("successful") - - def test_simple_success_colon(self): - self.simple_success_keyword("successful:") - - -class TestRemotedTestCase(unittest.TestCase): - - def test_simple(self): - test = subunit.RemotedTestCase("A test description") - self.assertRaises(NotImplementedError, test.setUp) - self.assertRaises(NotImplementedError, test.tearDown) - self.assertEqual("A test description", - test.shortDescription()) - self.assertEqual("subunit.RemotedTestCase.A test description", - test.id()) - self.assertEqual("A test description (subunit.RemotedTestCase)", "%s" % test) - self.assertEqual("", "%r" % test) - result = unittest.TestResult() - test.run(result) - self.assertEqual([(test, "RemoteException: " - "Cannot run RemotedTestCases.\n\n")], - result.errors) - self.assertEqual(1, result.testsRun) - another_test = subunit.RemotedTestCase("A test description") - self.assertEqual(test, another_test) - different_test = subunit.RemotedTestCase("ofo") - self.assertNotEqual(test, different_test) - self.assertNotEqual(another_test, different_test) - - -class TestRemoteError(unittest.TestCase): - - def test_eq(self): - error = subunit.RemoteError("Something went wrong") - another_error = subunit.RemoteError("Something went wrong") - different_error = subunit.RemoteError("boo!") - self.assertEqual(error, another_error) - self.assertNotEqual(error, different_error) - self.assertNotEqual(different_error, another_error) - - def test_empty_constructor(self): - self.assertEqual(subunit.RemoteError(), subunit.RemoteError("")) - - -class TestExecTestCase(unittest.TestCase): - - class SampleExecTestCase(subunit.ExecTestCase): - - def test_sample_method(self): - """sample-script.py""" - # the sample script runs three tests, one each - # that fails, errors and succeeds - - - def test_construct(self): - test = self.SampleExecTestCase("test_sample_method") - self.assertEqual(test.script, - subunit.join_dir(__file__, 'sample-script.py')) - - def test_run(self): - runner = MockTestProtocolServerClient() - test = self.SampleExecTestCase("test_sample_method") - test.run(runner) - mcdonald = subunit.RemotedTestCase("old mcdonald") - bing = subunit.RemotedTestCase("bing crosby") - an_error = subunit.RemotedTestCase("an error") - self.assertEqual(runner.error_calls, - [(an_error, subunit.RemoteError())]) - self.assertEqual(runner.failure_calls, - [(bing, - subunit.RemoteError( - "foo.c:53:ERROR invalid state\n"))]) - self.assertEqual(runner.start_calls, [mcdonald, bing, an_error]) - self.assertEqual(runner.end_calls, [mcdonald, bing, an_error]) - - def test_debug(self): - test = self.SampleExecTestCase("test_sample_method") - test.debug() - - def test_count_test_cases(self): - """TODO run the child process and count responses to determine the count.""" - - def test_join_dir(self): - sibling = subunit.join_dir(__file__, 'foo') - expected = '%s/foo' % (os.path.split(__file__)[0],) - self.assertEqual(sibling, expected) - - -class DoExecTestCase(subunit.ExecTestCase): - - def test_working_script(self): - """sample-two-script.py""" - - -class TestIsolatedTestCase(unittest.TestCase): - - class SampleIsolatedTestCase(subunit.IsolatedTestCase): - - SETUP = False - TEARDOWN = False - TEST = False - - def setUp(self): - TestIsolatedTestCase.SampleIsolatedTestCase.SETUP = True - - def tearDown(self): - TestIsolatedTestCase.SampleIsolatedTestCase.TEARDOWN = True - - def test_sets_global_state(self): - TestIsolatedTestCase.SampleIsolatedTestCase.TEST = True - - - def test_construct(self): - test = self.SampleIsolatedTestCase("test_sets_global_state") - - def test_run(self): - result = unittest.TestResult() - test = self.SampleIsolatedTestCase("test_sets_global_state") - test.run(result) - self.assertEqual(result.testsRun, 1) - self.assertEqual(self.SampleIsolatedTestCase.SETUP, False) - self.assertEqual(self.SampleIsolatedTestCase.TEARDOWN, False) - self.assertEqual(self.SampleIsolatedTestCase.TEST, False) - - def test_debug(self): - pass - #test = self.SampleExecTestCase("test_sample_method") - #test.debug() - - -class TestIsolatedTestSuite(unittest.TestCase): - - class SampleTestToIsolate(unittest.TestCase): - - SETUP = False - TEARDOWN = False - TEST = False - - def setUp(self): - TestIsolatedTestSuite.SampleTestToIsolate.SETUP = True - - def tearDown(self): - TestIsolatedTestSuite.SampleTestToIsolate.TEARDOWN = True - - def test_sets_global_state(self): - TestIsolatedTestSuite.SampleTestToIsolate.TEST = True - - - def test_construct(self): - suite = subunit.IsolatedTestSuite() - - def test_run(self): - result = unittest.TestResult() - suite = subunit.IsolatedTestSuite() - sub_suite = unittest.TestSuite() - sub_suite.addTest(self.SampleTestToIsolate("test_sets_global_state")) - sub_suite.addTest(self.SampleTestToIsolate("test_sets_global_state")) - suite.addTest(sub_suite) - suite.addTest(self.SampleTestToIsolate("test_sets_global_state")) - suite.run(result) - self.assertEqual(result.testsRun, 3) - self.assertEqual(self.SampleTestToIsolate.SETUP, False) - self.assertEqual(self.SampleTestToIsolate.TEARDOWN, False) - self.assertEqual(self.SampleTestToIsolate.TEST, False) - - -class TestTestProtocolClient(unittest.TestCase): - - def setUp(self): - self.io = StringIO() - self.protocol = subunit.TestProtocolClient(self.io) - self.test = TestTestProtocolClient("test_start_test") - - - def test_start_test(self): - """Test startTest on a TestProtocolClient.""" - self.protocol.startTest(self.test) - self.assertEqual(self.io.getvalue(), "test: %s\n" % self.test.id()) - - def test_stop_test(self): - """Test stopTest on a TestProtocolClient.""" - self.protocol.stopTest(self.test) - self.assertEqual(self.io.getvalue(), "") - - def test_add_success(self): - """Test addSuccess on a TestProtocolClient.""" - self.protocol.addSuccess(self.test) - self.assertEqual( - self.io.getvalue(), "successful: %s\n" % self.test.id()) - - def test_add_failure(self): - """Test addFailure on a TestProtocolClient.""" - self.protocol.addFailure(self.test, subunit.RemoteError("boo")) - self.assertEqual( - self.io.getvalue(), - 'failure: %s [\nRemoteException: boo\n]\n' % self.test.id()) - - def test_add_error(self): - """Test stopTest on a TestProtocolClient.""" - self.protocol.addError(self.test, subunit.RemoteError("phwoar")) - self.assertEqual( - self.io.getvalue(), - 'error: %s [\n' - "RemoteException: phwoar\n" - "]\n" % self.test.id()) - - -def test_suite(): - loader = subunit.tests.TestUtil.TestLoader() - result = loader.loadTestsFromName(__name__) - return result -- cgit From f8a02a1a804fdb8640989595e2b57df86281c0f7 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Wed, 8 Oct 2008 03:33:38 +0200 Subject: Fix subunit files location after cherrypicks. --- source4/scripting/bin/subunitrun | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source4/scripting') diff --git a/source4/scripting/bin/subunitrun b/source4/scripting/bin/subunitrun index bc35c7ce22..e7737bdbec 100755 --- a/source4/scripting/bin/subunitrun +++ b/source4/scripting/bin/subunitrun @@ -21,7 +21,7 @@ import sys # Find right directory when running from source tree sys.path.insert(0, "bin/python") -sys.path.insert(0, "../lib/subunit/python") +sys.path.insert(1, "../lib/subunit/python") from subunit import SubunitTestRunner from unittest import TestProgram -- cgit