Revision: 2279
Author: rgnyman
Date: Thu Sep 10 06:23:24 2009
Log: Simplified C sample
http://code.google.com/p/robotframework/source/detail?r=2279

Added:
 /trunk/proto/c/LoginLibrary.py
 /trunk/proto/c/LoginTests.tsv
 /trunk/proto/c/login.c
Deleted:
 /trunk/proto/c/PasswordValidator.py
 /trunk/proto/c/PasswordValidator.tsv
 /trunk/proto/c/password-validator.c
 /trunk/proto/c/password-validator.h
Modified:
 /trunk/proto/c/Makefile

=======================================
--- /dev/null
+++ /trunk/proto/c/LoginLibrary.py      Thu Sep 10 06:23:24 2009
@@ -0,0 +1,29 @@
+"""Robot Framework test library example that calls C code.
+
+This example uses Python's standard `ctypes` module, which requires
+that the C code is compiled into a shared library.
+
+It is also possible to execute this file from the command line
+to test the C code manually.
+"""
+
+from ctypes import CDLL, c_char_p
+
+LIBRARY = CDLL('./liblogin.so')
+
+def check_user(username, password):
+    """Validates user name and password using imported shared C library."""
+    if not LIBRARY.validate_user(c_char_p(username), c_char_p(password)):
+        raise AssertionError('Wrong username/password combination')
+
+
+if __name__ == '__main__':
+    import sys
+    try:
+        check_user(*sys.argv[1:])
+    except TypeError:
+        print 'Usage:  %s username password' % sys.argv[0]
+    except AssertionError, err:
+        print err
+    else:
+        print 'Valid password'
=======================================
--- /dev/null
+++ /trunk/proto/c/LoginTests.tsv       Thu Sep 10 06:23:24 2009
@@ -0,0 +1,23 @@
+
+*** Settings ***
+Library        LoginLibrary.py
+
+
+*** Test Case ***
+Validate Users
+       Check Valid User        john    long
+       Check Valid User        demo    mode
+
+Login With Invalid User Should Fail
+       Check Invalid User      de      mo
+       Check Invalid User      invalid invalid
+       Check Invalid User      long    invalid
+       Check Invalid User      ${EMPTY}        ${EMPTY}
+
+
+*** Keyword ***
+Check Valid User       [arguments]     ${username}     ${password}
+       Check User      ${username}     ${password}
+
+Check Invalid User     [arguments]     ${username}     ${password}
+ Run Keyword And Expect Error Wrong username/password combination Check User ${username} ${password}
=======================================
--- /dev/null
+++ /trunk/proto/c/login.c      Thu Sep 10 06:23:24 2009
@@ -0,0 +1,24 @@
+/*
+Simple system that validates passwords and user names. There are two users in
+system with valid user name and password. "demo mode" and "john long". All
+other user names are invalid.
+*/
+
+#include <string.h>
+#define NR_USERS 2
+
+struct User {
+    const char* name;
+    const char* password;
+};
+const struct User VALID_USERS[NR_USERS] = { "john", "long", "demo", "mode" };
+
+int validate_user(const char* name, const char* password) {
+    int i;
+    for (i = 0; i < NR_USERS; i++) {
+ if (0 == strncmp(VALID_USERS[i].name, name, strlen(VALID_USERS[i].name))) + if (0 == strncmp(VALID_USERS[i].password, password, strlen(VALID_USERS[i].password)))
+                return 1;
+    }
+    return 0;
+}
=======================================
--- /trunk/proto/c/PasswordValidator.py Thu Sep  3 22:50:21 2009
+++ /dev/null
@@ -1,44 +0,0 @@
-
-#  Copyright 2009 Nokia Siemens Networks Oyj
-#
-#  Licensed under the Apache License, Version 2.0 (the "License");
-#  you may not use this file except in compliance with the License.
-#  You may obtain a copy of the License at
-#
-#      http://www.apache.org/licenses/LICENSE-2.0
-#
-#  Unless required by applicable law or agreed to in writing, software
-#  distributed under the License is distributed on an "AS IS" BASIS,
-#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-#  See the License for the specific language governing permissions and
-#  limitations under the License.
-
-
-"""Simple example how to call c code from Robot Framework.
-
-C code can be used with RF if is compiled as shared library.
-"""
-
-
-from ctypes import CDLL, c_char_p
-
-
-LIBRARY = CDLL('./libpasswordvalidatorstub.so.1.0.0')
-
-
-def check_user(username, password):
- """ Validates user name and password using imported shared C library."""
-    if not LIBRARY.validate_user(c_char_p(username), c_char_p(password)):
-        raise AssertionError('Wrong username/password combination')
-
-
-if __name__ == '__main__':
-    import sys
-    try:
-        check_user(*sys.argv[1:])
-    except AssertionError, err:
-        print err
-    except TypeError:
-        print 'Usage:  %s username password' % sys.argv[0]
-    else:
-        print 'Valid password'
=======================================
--- /trunk/proto/c/PasswordValidator.tsv        Thu Sep  3 22:50:21 2009
+++ /dev/null
@@ -1,39 +0,0 @@
-*
-*  Copyright 2009 Nokia Siemens Networks Oyj
-*
-*  Licensed under the Apache License, Version 2.0 (the "License");
-*  you may not use this file except in compliance with the License.
-*  You may obtain a copy of the License at
-*
-*      http://www.apache.org/licenses/LICENSE-2.0
-*
-*  Unless required by applicable law or agreed to in writing, software
-*  distributed under the License is distributed on an "AS IS" BASIS,
-*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-*  See the License for the specific language governing permissions and
-*  limitations under the License.
-
-
-
-*** Settings ***
-Library        PasswordValidator
-
-
-*** Test Case ***
-Validate Users
-       Check Valid User        john    long
-       Check Valid User        demo    mode
-
-Login With Invalid User Should Fail
-       Check Invalid User      de      mo
-       Check Invalid User      invalid invalid
-       Check Invalid User      long    invalid
-       Check Invalid User      ${EMPTY}        ${EMPTY}
-
-
-*** Keyword ***
-Check Valid User       [arguments]     ${username}     ${password}
-       Check User      ${username}     ${password}
-
-Check Invalid User     [arguments]     ${username}     ${password}
- Run Keyword And Expect Error Wrong username/password combination Check User ${username} ${password}
=======================================
--- /trunk/proto/c/password-validator.c Thu Sep  3 12:10:30 2009
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
-#  Copyright 2009 Nokia Siemens Networks Oyj
-#
-#  Licensed under the Apache License, Version 2.0 (the "License");
-#  you may not use this file except in compliance with the License.
-#  You may obtain a copy of the License at
-#
-#      http://www.apache.org/licenses/LICENSE-2.0
-#
-#  Unless required by applicable law or agreed to in writing, software
-#  distributed under the License is distributed on an "AS IS" BASIS,
-#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-#  See the License for the specific language governing permissions and
-#  limitations under the License.
-*/
-
-#include <stdio.h>
-#include <string.h>
-#include "password-validator.h"
-
-struct User
-{
-    const char* name;
-    const char* password;
-};
-
-#define NR_USERS 2
-
-const struct User VALID_USERS[NR_USERS] = { "john" , "long", "demo", "mode" };
-
-const int MAX_LEN = 100;
-
-/*
-Simple system that validates passwords and user names. There are two users in
-system with valid user name and password. "demo mode" and "john long". All
-other user names are invalid.
-
-System has command line interface and direct api for testing.
-*/
-
-
-/*
-This is api that is called from python class to validate user.
-*/
-int validate_user(const char* name, const char* password) {
-
-    int i;
-    for (i = 0; i < NR_USERS; ++i)
-    {
- if (0 == strncmp(VALID_USERS[i].name, name, strlen(VALID_USERS[i].name))) - if (0 == strncmp(VALID_USERS[i].password, password, strlen(VALID_USERS[i].password)))
-                return 1;
-    }
-
-    return 0;
-}
-
-int main(int argc, char* argv) {
-    char password[MAX_LEN];
-    char username[MAX_LEN];
-    printf("Give username: ");
-    scanf("%s", username);
-
-    printf("Give password: ");
-    scanf("%s", password);
-
-    if (validate_user(username, password))
-        printf("Hello %s you are now in system\n", username);
-    else
-        printf("Incorrect username and password combination\n");
-}
=======================================
--- /trunk/proto/c/password-validator.h Thu Sep  3 12:10:30 2009
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
-#  Copyright 2009 Nokia Siemens Networks Oyj
-#
-#  Licensed under the Apache License, Version 2.0 (the "License");
-#  you may not use this file except in compliance with the License.
-#  You may obtain a copy of the License at
-#
-#      http://www.apache.org/licenses/LICENSE-2.0
-#
-#  Unless required by applicable law or agreed to in writing, software
-#  distributed under the License is distributed on an "AS IS" BASIS,
-#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-#  See the License for the specific language governing permissions and
-#  limitations under the License.
-*/
-
-/*
-Simple system that validates passwords and user names. There are two users in
-system with valid user name and password. "demo mode" and "long john". All
-other user names are invalid.
-*/
-
-/* Valid user return 1 */
-int validate_user(const char* name, const char* password);
=======================================
--- /trunk/proto/c/Makefile     Thu Sep  3 12:10:30 2009
+++ /trunk/proto/c/Makefile     Thu Sep 10 06:23:24 2009
@@ -1,32 +1,10 @@
-#  Copyright 2009 Nokia Siemens Networks Oyj
-#
-#  Licensed under the Apache License, Version 2.0 (the "License");
-#  you may not use this file except in compliance with the License.
-#  You may obtain a copy of the License at
-#
-#      http://www.apache.org/licenses/LICENSE-2.0
-#
-#  Unless required by applicable law or agreed to in writing, software
-#  distributed under the License is distributed on an "AS IS" BASIS,
-#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-#  See the License for the specific language governing permissions and
-#  limitations under the License.
-
-
 CC=gcc
-CFLAGS=-I.
-OBJ = password-validator.o
-
-libpasswordvalidatorstub: $(OBJ)
- $(CC) -shared -Wl,-soname,libpasswordvalidatorstub.so.1 -o libpasswordvalidatorstub.so.1.0.0 password-validator.o
-
-password-validator: $(OBJ) password-validator.h
-       $(CC) -o $@ $^ $(CFLAGS)
-
-libpasswordvalidatorstub: password-validator
+SRC=login.c
+SO=liblogin.so
+
+$(SO): $(SRC)
+       $(CC) -shared -o $(SO) $(SRC)

 clean:
-       rm -f $(OBJ)
-       rm -f libpasswordvalidatorstub.so.1.0.0
-       rm -f password-validator
-       rm -f PasswordValidator.pyc
+       rm -f $(SO)
+

Reply via email to