OSDN Git Service

Ver.1.5.3: fixed mutex error occured at opening sqlite3
[opengate/opengate.git] / opengate / opengatesrv / test-comm-userdb.c
1
2 /**************************************************
3 opengate server test program 
4
5 Copyright (C) 2005 Opengate Project Team
6 Written by Yoshiaki Watanabe
7 Modified Katsuhiko Eguchi, 2005 
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 Email: watanaby@is.saga-u.ac.jp
24 **************************************************/
25
26 #include        "opengatesrv.h"
27 #include <sqlite3.h>
28
29 /********************/
30 /*  main routine    */
31 /********************/
32 int  main(int argc, char **argv)
33 {
34   char userid[USERMAXLN];
35   char clientAddr4[ADDRMAXLN];
36   char macAddr[ADDRMAXLN];
37   char language[WORDMAXLN];
38   int duration;
39   int durationEntered;  
40   char *pErrMsg;
41   sqlite3 *db;
42   sqlite3_stmt *stmt;
43  
44
45   printf("This is a test program for accessing sqlite db\n");
46
47   /******************************************/
48   /* First Test: stand-alone sqlite3 access */
49   printf("FIRST TEST:\n");
50   printf(" create and access /tmp/testsqlite3.db \n");
51   printf(" please remove /tmp/testsqlite3.db after test\n");
52   
53   printf("sqlite3_open\n");
54   if(sqlite3_open("/tmp/testsqlite3.db",&db)!=SQLITE_OK){
55     printf("ERR at %s#%d: sqlite3_open\n",__FILE__,__LINE__);
56     sqlite3_close(db);
57     return 1;
58   }
59       
60   printf("sqlite3_exec(create)\n");
61   if(sqlite3_exec(db, "CREATE TABLE testTable (id TEXT PRIMARY KEY,test TEXT)", 
62                   NULL, NULL, &pErrMsg)!=SQLITE_OK){
63     printf("ERR at %s#%d: sqlite3_exec: %s\n",__FILE__,__LINE__,pErrMsg);
64     printf(" Remove '/tmp/testsqlite3.db' and retry, when 'ERR..testTable already exists'\n");
65     sqlite3_close(db);
66     return 1;
67   }
68
69   printf("sqlite3_exec(insert)\n");
70   if(sqlite3_exec(db, "INSERT INTO testTable (test) values ('tttt')", 
71                   NULL, NULL, &pErrMsg)!=SQLITE_OK){
72     printf("ERR at %s#%d: sqlite3_exec: %s\n",__FILE__,__LINE__,pErrMsg);
73     sqlite3_close(db);
74     return 1;
75   }
76
77   printf("sqlite3_prepare\n");
78   if(sqlite3_prepare(db, "SELECT test FROM testTable WHERE test='tttt'", 
79                      BUFFMAXLN, &stmt, NULL)!=SQLITE_OK){
80     printf("ERR at %s#%d: sqlite3_prepare:%s\n",__FILE__,__LINE__,
81            sqlite3_errmsg(db));
82
83     /* finalize */
84     sqlite3_finalize(stmt);
85     sqlite3_close(db);
86     return 1;
87   }
88
89   printf("sqlite3_step\n");
90   if(sqlite3_step(stmt)==SQLITE_ROW){
91     printf("%s", (char*)sqlite3_column_text(stmt, 0));
92     printf(" <== should be 'tttt'\n");
93   }else{
94     sqlite3_finalize(stmt);
95     sqlite3_close(db);
96     return 1;
97   }
98   
99   /* finalize */
100   sqlite3_finalize(stmt);
101   sqlite3_close(db);
102   
103   printf("end of first test\n\n");
104
105   /************************************/
106   /* Second Test: get opengate config */
107
108   printf("SECOND TEST\n");
109   printf("create and access db file defined in conf file\n");
110
111   /* prepare config file */
112   OpenConfFile();
113  
114   /* start log */
115   errToSyslog(atoi(GetConfValue("Syslog/Enable")));
116   openlog(argv[0], LOG_PID, atoi(GetConfValue("Syslog/Facility")));
117
118   /* init config */
119   InitConf();
120
121   /* put session begin to db */
122   PutSessionBeginToDb("testcookie", "testuser", 
123                       "192.168.0.123", "2001::1", 
124                       "00:11:22:33:44:55", 
125                       "10000", "10002",
126                       600, 600,
127                       1, "ja");
128   /* put session end to db */
129   PutSessionEndToDb("testcookie", "HTTP");
130
131   /* get data from db */
132   GetSessionInfoFromDb("testcookie", userid, 
133                        clientAddr4, macAddr, 
134                        &duration, &durationEntered, language);
135
136   /* dump getting data */
137   printf("%s, %s, %s, %d, %d, %s\n",  userid, clientAddr4, macAddr, 
138          duration, durationEntered, language);
139
140   printf("<== sholud be 'testuser, 192.168.0.123, 00:11:22:33:44:55, 600, 600, ja'\n");
141
142   printf("end of second test\n\n");
143   return 0;
144 }