nfs-ganesha 1.4

test_mnt_proto.c

Go to the documentation of this file.
00001 /*
00002  * vim:expandtab:shiftwidth=8:tabstop=8:
00003  *
00004  * Copyright CEA/DAM/DIF  (2008)
00005  * contributeur : Philippe DENIEL   philippe.deniel@cea.fr
00006  *                Thomas LEIBOVICI  thomas.leibovici@cea.fr
00007  *
00008  *
00009  * This program is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 3 of the License, or (at your option) any later version.
00013  * 
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Lesser General Public License for more details.
00018  * 
00019  * You should have received a copy of the GNU Lesser General Public
00020  * License along with this library; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022  * 
00023  * ---------------------------------------
00024  */
00025 
00035 #ifdef HAVE_CONFIG_H
00036 #include "config.h"
00037 #endif
00038 
00039 #ifdef _SOLARIS
00040 #include "solaris_port.h"
00041 #endif
00042 
00043 #include <stdio.h>
00044 #include <string.h>
00045 #include <pthread.h>
00046 #include <fcntl.h>
00047 #include <sys/file.h>           /* for having FNDELAY */
00048 #include <sys/socket.h>         /* For getsockname */
00049 #include "HashData.h"
00050 #include "HashTable.h"
00051 #include "log.h"
00052 #include "ganesha_rpc.h"
00053 #include "nfs23.h"
00054 #include "nfs4.h"
00055 #include "mount.h"
00056 #include "nfs_core.h"
00057 #include "cache_inode.h"
00058 #include "nfs_exports.h"
00059 #include "nfs_creds.h"
00060 #include "nfs_proto_functions.h"
00061 #include "nfs_tools.h"
00062 
00063 /* prints an export list */
00064 void print_export_list(exports export_list)
00065 {
00066 
00067   exports p_expnode = export_list;
00068 
00069   while(p_expnode)
00070     {
00071 
00072       groups p_group;
00073 
00074       LogTest("exportnode.ex_dir = \"%s\"", p_expnode->ex_dir);
00075       LogTest("exportnode.ex_groups = {");
00076 
00077       p_group = p_expnode->ex_groups;
00078       while(p_group)
00079         {
00080           LogTest("  \"%s\"", p_group->gr_name);
00081           p_group = p_group->gr_next;
00082         }
00083       LogTest("}");
00084 
00085       p_expnode = p_expnode->ex_next;
00086     }
00087 
00088 }
00089 
00090 /* Test MNTPROC_NULL */
00091 int test_mnt_Null()
00092 {
00093 
00094   int rc;
00095 
00096   rc = mnt_Null(NULL, NULL, NULL, NULL, NULL, NULL, NULL);
00097   LogTest("MNTPROC_NULL()=%d", rc);
00098 
00099   /* Must return MNT3_OK */
00100   if(rc == MNT3_OK)
00101     {
00102       LogTest("TEST MNT_NULL : OK");
00103       return 0;
00104     }
00105   else
00106     {
00107       LogTest("TEST MNT_NULL : ERROR");
00108       return rc;
00109     }
00110 }
00111 
00112 # define NB_EXPORT_ENTRIES 5
00113 int test_mnt_Export()
00114 {
00115 
00116   int rc, i;
00117   int error = 0;
00118   exportlist_t export_entries[NB_EXPORT_ENTRIES];
00119   nfs_res_t result;
00120   int mysock;
00121   unsigned long size;
00122   struct sockaddr_in in_addr;
00123   struct sockaddr_in addr;
00124 
00125   /* TEST 1 : using a NULL export_list */
00126 
00127   rc = mnt_Export(NULL, NULL, NULL, NULL, NULL, NULL, &result);
00128   /* rc must be OK and result.res_mntexport must be NULL */
00129   LogTest("MNTPROC_EXPORT(NULL)=(%d,%p)", rc, result.res_mntexport);
00130 
00131   if((rc == MNT3_OK) && (result.res_mntexport == NULL))
00132     {
00133       LogTest("TEST MNT_EXPORT : OK");
00134     }
00135   else
00136     {
00137       LogTest("TEST MNT_EXPORT : ERROR");
00138       error++;
00139     }
00140 
00141   /* TEST 2 : MNT_EXPORT complex */
00142 
00143   if((mysock = socket(PF_INET, SOCK_STREAM, 0)) == -1)
00144     {
00145       LogTest("socket ERROR %d : %s", errno, strerror(errno));
00146     }
00147 
00148   in_addr.sin_family = AF_INET;
00149   in_addr.sin_addr.s_addr = INADDR_ANY;
00150   in_addr.sin_port = htons(5100);
00151 
00152   if(bind(mysock, (struct sockaddr *)&in_addr, sizeof(in_addr)) == -1)
00153     {
00154       LogTest("bind ERROR %d : %s", errno, strerror(errno));
00155     }
00156 
00157   size = sizeof(struct sockaddr);
00158   if(getsockname(mysock, (struct sockaddr *)&addr, (socklen_t *) & size) == -1)
00159     {
00160       LogTest("getsockname ERROR %d : %s", errno, strerror(errno));
00161     }
00162   /* we don't use the resource, only its adress. */
00163   close(mysock);
00164 
00165   /* Building an export list */
00166 
00167   for(i = 0; i < NB_EXPORT_ENTRIES; i++)
00168     {
00169 
00170       /* pour alleger les notations */
00171       exportlist_client_entry_t p_cli[5];
00172 
00173       /* setting paths */
00174       snprintf(export_entries[i].dirname, sizeof(export_entries[i].dirname), "/dirname-%d", i);
00175       snprintf(export_entries[i].fsname, sizeof(export_entries[i].fsname), "/fsname-%d", i);
00176       snprintf(export_entries[i].pseudopath, MAXPATHLEN, "/pseudopath-%d", i);
00177       snprintf(export_entries[i].fullpath, MAXPATHLEN, "/fullpath-%d", i);
00178 
00179       /* linking to the next element. */
00180       if((i + 1) < NB_EXPORT_ENTRIES)
00181         export_entries[i].next = &(export_entries[i + 1]);
00182       else
00183         export_entries[i].next = NULL;
00184 
00185       /* tests several clients list type */
00186       switch (i % 4)
00187         {
00188 
00189         case 0:
00190           /* empty list */
00191           export_entries[i].clients.num_clients = 0;
00192           break;
00193 
00194         case 1:
00195 
00196           /* one element list */
00197           export_entries[i].clients.num_clients = 1;
00198           p_cli[0].type = HOSTIF_CLIENT;
00199           p_cli[0].client.hostif.clientaddr = addr.sin_addr.s_addr;
00200           break;
00201 
00202         case 2:
00203 
00204           /* two elements list */
00205           export_entries[i].clients.num_clients = 2;
00206 
00207           p_cli[0].type = HOSTIF_CLIENT;
00208           p_cli[0].client.hostif.clientaddr = addr.sin_addr.s_addr;
00209 
00210           p_cli[1].type = NETGROUP_CLIENT;
00211           strcpy(p_cli[1].client.netgroup.netgroupname, "netgroup");
00212 
00213           break;
00214 
00215         case 3:
00216           /* several elements list */
00217 
00218           export_entries[i].clients.num_clients = 5;
00219 
00220           p_cli[0].type = HOSTIF_CLIENT;
00221           p_cli[0].client.hostif.clientaddr = addr.sin_addr.s_addr;
00222 
00223           p_cli[1].type = NETGROUP_CLIENT;
00224           strcpy(p_cli[1].client.netgroup.netgroupname, "netgroup");
00225 
00226           p_cli[2].type = WILDCARDHOST_CLIENT;
00227           strcpy(p_cli[2].client.wildcard.wildcard, "wilcard");
00228 
00229           p_cli[3].type = GSSPRINCIPAL_CLIENT;
00230           strcpy(p_cli[3].client.gssprinc.princname, "gssprincipal");
00231 
00232           p_cli[4].type = NETWORK_CLIENT;
00233           p_cli[4].client.network.netaddr = addr.sin_addr.s_addr;
00234           p_cli[4].client.network.netmask = 0xFFFFFF00;
00235 
00236           break;
00237         default:
00238           LogTest("!!!!!***** TEST ERROR *****!!!!!");
00239           return -1;
00240         }
00241 
00242     }
00243 
00244   rc = mnt_Export(NULL, export_entries, NULL, NULL, NULL, NULL, &result);
00245   /* rc must be OK and result.res_mntexport must be NULL */
00246   LogTest("MNTPROC_EXPORT(entries)=(%d,%p)", rc, result.res_mntexport);
00247 
00248   if((rc == MNT3_OK) && (result.res_mntexport != NULL))
00249     {
00250       LogTest("TEST MNT_EXPORT : OK");
00251     }
00252   else
00253     {
00254       LogTest("TEST MNT_EXPORT : ERROR");
00255       error++;
00256     }
00257 
00258   /* printing the export_list */
00259   print_export_list(result.res_mntexport);
00260 
00261   return (error);
00262 
00263 }
00264 
00265 #define Maketest(func,name) do {                      \
00266   int rc;                                             \
00267   LogTest("\n======== TEST %s =========",name);    \
00268   rc = func();                                        \
00269   if (rc)                                             \
00270     LogTest("\n-------- %s : %d ---------",name,rc); \
00271   else                                                \
00272     LogTest("\n-------- %s : OK ---------",name); \
00273   } while (0)
00274 
00275 int main(int argc, char **argv)
00276 {
00277   
00278   SetDefaultLogging("TEST");
00279   SetNamePgm("test_mnt_proto");
00280 
00281   Maketest(test_mnt_Null, "test_mnt_Null");
00282   Maketest(test_mnt_Export, "test_mnt_Export");
00283 
00284   exit(0);
00285 
00286 }