1 #!nfsh
   2 #
   3 # CDDL HEADER START
   4 #
   5 # The contents of this file are subject to the terms of the
   6 # Common Development and Distribution License (the "License").
   7 # You may not use this file except in compliance with the License.
   8 #
   9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10 # or http://www.opensolaris.org/os/licensing.
  11 # See the License for the specific language governing permissions
  12 # and limitations under the License.
  13 #
  14 # When distributing Covered Code, include this CDDL HEADER in each
  15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16 # If applicable, add the following below this CDDL HEADER, with the
  17 # fields enclosed by brackets "[]" replaced with your own identifying
  18 # information: Portions Copyright [yyyy] [name of copyright owner]
  19 #
  20 # CDDL HEADER END
  21 #
  22 
  23 #
  24 # Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  25 # Use is subject to license terms.
  26 #
  27 
  28 #
  29 # Test script to create a new file, test Open/Lock/Locku/Lockt/Close operations.
  30 #
  31 
  32 if { $argc != 2 } {
  33     puts "Usage: $argv0 <hostname> <pathname>"
  34     puts "       <pathname>: full path dir name to create test file."
  35     exit 1
  36 }
  37 
  38 # set host and file pathname (original)
  39 set host [ lindex $argv 0 ]
  40 set path [ path2comp [lindex $argv 1] "/" ]
  41 set fname "tfile.[pid]"
  42 
  43 # connect to the server
  44 #connect -t udp $host
  45 connect -t tcp $host
  46 
  47 # get dir file handle
  48 set dfh [get_fh $path]
  49 if {$dfh == ""} {
  50         puts stderr "ERROR: pathname=($path) not found."
  51         return 1
  52 }
  53 
  54 # negotiate the cleintid
  55 # set unique clientid and verifier
  56 set verifier "[clock seconds]01010"
  57 set owner "[pid]"
  58 
  59 set res [compound {Setclientid $verifier $owner {0 0 0}}]
  60 puts "\nSetclientid $verifier $owner ..."
  61 puts "Res: $res"
  62 set clientid [lindex [lindex [lindex $res 0] 2] 0]
  63 set cid_verifier [lindex [lindex [lindex $res 0] 2] 1]
  64 
  65 # confirm clientid
  66 set res [compound {Setclientid_confirm $clientid $cid_verifier}]
  67 puts "\nSetclientid_confirm $clientid $cid_verifier ..."
  68 puts "Res: $res"
  69 
  70 # Now try to create (open_type=1) a new test file under $path with Open op
  71 set oseqid 1
  72 set otype 1
  73 set mode 0664
  74 set oclaim 0
  75 set tag "OPEN1"
  76 set res [compound {Putfh $dfh; 
  77         Open $oseqid 3 0 {$clientid $owner} \
  78         {$otype 0 {{mode $mode} {size 0}}} {$oclaim $fname};
  79         Getfh; Getattr {mode size}}]
  80 puts "\nOpen to create $fname ..."
  81 puts -nonewline "  Open $oseqid 3 0 {$clientid $owner}"
  82 puts " {$otype 0 {{mode $mode} {size 0}}} {$oclaim $fname}"
  83 puts "Res: $res"
  84 if {$status != "OK"} {
  85         puts stderr "ERROR: Unable to create ($fname) with Open."
  86         return 2
  87 }
  88 # store the needed open info
  89 set open_sid [lindex [lindex $res 1] 2]
  90 set rflags [lindex [lindex $res 1] 4] 
  91 set nfh [lindex [lindex $res 2] 2]
  92 set attr [lindex [lindex $res 3] 2]
  93 
  94 # do open_confirm if needed, e.g. rflags==OPEN4_RESULT_CONFIRM=2
  95 if {[expr $rflags & 2] == 2} {
  96         incr oseqid
  97         set res [compound {Putfh $nfh; Open_confirm "$open_sid" $oseqid}]
  98         puts "\nOpen_confirm ($open_sid) $oseqid ..."
  99         puts "Res: $res"
 100         if {$status != "OK"} {
 101                 puts stderr "ERROR: unable to confirm created file $fname."
 102                 return 2
 103         }
 104         set open_sid [lindex [lindex $res 1] 2]
 105 }
 106 
 107 # Now try a WRITE Lock
 108 set lseqid 1
 109 set ltype 2
 110 set reclaim F
 111 set offset 0
 112 set length 1024
 113 set newlock T
 114 set tag "LOCK"
 115 incr oseqid
 116 set res [compound {Putfh $nfh; 
 117         Lock $ltype $reclaim $offset $length $newlock \
 118         $open_sid $lseqid {$oseqid $clientid $owner}}]
 119 puts "\nTry a WRITE lock on the file ..."
 120 puts "  Lock $ltype $reclaim $offset $length $newlock ($open_sid) $lseqid ($oseqid $clientid $owner)"
 121 puts "Res: $res"
 122 incr oseqid
 123 if {$status != "OK"} {
 124         puts stderr "ERROR: Failed to set Write lock the file, Close and exit."
 125         set res [compound {Putfh $nfh; Close $oseqid $open_sid}]
 126         puts "\nafter fail to lock, Close $oseqid ($open_sid) ..."
 127         puts "Res: $res"
 128         return 2;
 129 }
 130 set lock_sid [lindex [lindex $res 1] 2]
 131 
 132 set new_owner "fake_owner"
 133 # Now try LOCKT:
 134 set res [compound {Putfh $nfh; Lockt $ltype $clientid $new_owner 0 1024}]
 135 puts "\nfirst LOCKT with owner($clientid $new_owner) of region 0-1024"
 136 puts "Res: $res"
 137 if {$status != "DENIED"} {
 138         puts stderr "conflict Lockt(0-1024) was not denied"
 139         return 3
 140 }
 141 
 142 set res [compound {Putfh $nfh; Lockt $ltype $clientid $new_owner 1025 2048}]
 143 puts "\nsecond LOCKT with owner($clientid $new_owner) of region 1025-2048"
 144 puts "Res: $res"
 145 if {$status != "OK"} {
 146         puts stderr "Lockt(1025-2048) got status=$status"
 147         return 3
 148 }
 149 
 150 # try LOCKT with the original owner on the locked range, expect OK
 151 set res [compound {Putfh $nfh; Lockt $ltype $clientid $owner 0 1024}]
 152 puts "\nthird LOCKT with owner($clientid $owner) of region 0-1024"
 153 puts "Res: $res"
 154 if {$status != "OK"} {
 155         puts stderr "Lockt of original owner (0-1024) got status=$status"
 156         return 3
 157 }
 158 
 159 
 160 # Then unlock the lock
 161 incr lseqid
 162 set tag "UNLOCK"
 163 set res [compound {Putfh $nfh; 
 164         Locku $ltype $lseqid $lock_sid $offset $length}]
 165 puts "\nLocku $ltype $lseqid ($lock_sid) $offset $length"
 166 puts "Res: $res"
 167 if {$status != "OK"} {
 168         puts stderr "ERROR: Failed to unlock $offset-$length of the file"
 169         set res [compound {Putfh $nfh; Close $oseqid ($open_sid)}]
 170         puts "\nafter fail to unlock, Close $oseqid ($open_sid) ..."
 171         puts "Res: $res"
 172         return 2;
 173 }
 174 set lock_sid [lindex [lindex $res 1] 2]
 175 
 176 # Lockt again should be OK now:
 177 set res [compound {Putfh $nfh; Lockt $ltype $clientid $new_owner 0 1024}]
 178 puts "\nforth LOCKT with owner($clientid $new_owner) of region 0-1024"
 179 puts "Res: $res"
 180 if {$status != "OK"} {
 181         puts stderr "Lockt(0-1024) after Locku was not OK"
 182         return 3
 183 }
 184 
 185 # finally close this file
 186 set tag "CLOSE1"
 187 set res [compound {Putfh $nfh; Close $oseqid $open_sid}]
 188 puts "\nFinal test file for now: Close $oseqid ($open_sid) ..."
 189 puts "Res: $res"
 190 if {$status != "OK"} {
 191         puts stderr "ERROR: Final close failed."
 192         return 2
 193 }
 194 
 195 #check if the file is really there
 196 if [catch {set fh [get_fh "$path $fname"]} file_here ] {
 197         puts stderr "ERROR: File in <path/$fname> must exist at this point."
 198         return 1
 199 }
 200 
 201 # try open without create on the same file
 202 set otype 0
 203 set tag "OPEN2"
 204 incr oseqid
 205 set res [compound {Putfh $dfh; 
 206         Open $oseqid 3 0 {$clientid $owner} \
 207         {$otype 0 {{mode $mode}}} {$oclaim $fname}; Getfh}]
 208 puts "\nOpen again with/non-CREATE $fname (size > 0)..."
 209 puts -nonewline "  Open $oseqid 3 0 {$clientid $owner}"
 210 puts " {$otype 0 {{mode $mode} {size 188}}} {$oclaim $fname}"
 211 puts "Res: $res"
 212 if {$status != "OK"} {
 213         puts stderr "ERROR: Can not open file $fname under $path"
 214         return 3
 215 }
 216 # store the needed open info
 217 set open_sid2 [lindex [lindex $res 1] 2]
 218 set rflags [lindex [lindex $res 1] 4] 
 219 set nfh2 [lindex [lindex $res 2] 2]
 220 incr oseqid
 221 
 222 # do open_confirm if needed, e.g. rflags==OPEN4_RESULT_CONFIRM=2
 223 if {[expr $rflags & 2] == 2} {
 224         set res [compound {Putfh $nfh; Open_confirm "$open_sid2" $oseqid}]
 225         puts "\nOpen_confirm ($open_sid2) $oseqid ..."
 226         puts "Res: $res"
 227         if {$status != "OK"} {
 228                 puts stderr "ERROR: unable to confirm with open-non-create."
 229                 return 2
 230         }
 231         set open_sid2 [lindex [lindex $res 1] 2]
 232         incr oseqid
 233 }
 234 
 235 
 236 
 237 set tag "CLOSE2"
 238 set res [compound {Putfh $nfh; Close $oseqid $open_sid2}]
 239 puts "\nFinal Close $oseqid ($open_sid2) ..."
 240 puts "Res: $res"
 241 if {$status != "OK"} {
 242         puts stderr "ERROR: Final close2 failed."
 243         return 2
 244 }
 245 
 246 
 247 # If we get here, test is good; thus remove the created file
 248 set res [compound {Putfh $dfh; Remove $fname}]
 249 puts "\nRemove $fname ..."
 250 puts "Res: $res"
 251 if {$status != "OK"} {
 252         puts stderr "ERROR: Can not remove file $fname under $path"
 253         return 3
 254 }
 255 
 256 disconnect
 257 exit 0