Package pike :: Package test :: Module lease
[hide private]
[frames] | no frames]

Source Code for Module pike.test.lease

  1  # 
  2  # Copyright (c) 2013, EMC Corporation 
  3  # All rights reserved. 
  4  # 
  5  # Redistribution and use in source and binary forms, with or without 
  6  # modification, are permitted provided that the following conditions are met: 
  7  # 
  8  # 1. Redistributions of source code must retain the above copyright notice, 
  9  # this list of conditions and the following disclaimer. 
 10  # 2. Redistributions in binary form must reproduce the above copyright notice, 
 11  # this list of conditions and the following disclaimer in the documentation 
 12  # and/or other materials provided with the distribution. 
 13  # 
 14  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 15  # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 16  # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 17  # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
 18  # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 19  # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 20  # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 21  # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 22  # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 23  # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 24  # POSSIBILITY OF SUCH DAMAGE. 
 25  # 
 26  # Module Name: 
 27  # 
 28  #        lease.py 
 29  # 
 30  # Abstract: 
 31  # 
 32  #        Lease tests 
 33  # 
 34  # Authors: Brian Koropoff (brian.koropoff@emc.com) 
 35  # 
 36   
 37  import pike.model 
 38  import pike.smb2 
 39  import pike.test 
 40  import random 
 41  import array 
 42   
 43  @pike.test.RequireDialect(0x210) 
 44  @pike.test.RequireCapabilities(pike.smb2.SMB2_GLOBAL_CAP_LEASING) 
45 -class LeaseTest(pike.test.PikeTest):
46 - def __init__(self, *args, **kwargs):
47 super(LeaseTest, self).__init__(*args, **kwargs) 48 self.share_all = pike.smb2.FILE_SHARE_READ | pike.smb2.FILE_SHARE_WRITE | pike.smb2.FILE_SHARE_DELETE 49 self.lease1 = array.array('B',map(random.randint, [0]*16, [255]*16)) 50 self.lease2 = array.array('B',map(random.randint, [0]*16, [255]*16)) 51 self.r = pike.smb2.SMB2_LEASE_READ_CACHING 52 self.rw = self.r | pike.smb2.SMB2_LEASE_WRITE_CACHING 53 self.rh = self.r | pike.smb2.SMB2_LEASE_HANDLE_CACHING 54 self.rwh = self.rw | self.rh
55 56 # Upgrade lease from RW to RWH, then break it to R
57 - def test_lease_upgrade_break(self):
58 chan, tree = self.tree_connect() 59 60 # Request rw lease 61 handle1 = chan.create(tree, 62 'lease.txt', 63 share=self.share_all, 64 oplock_level=pike.smb2.SMB2_OPLOCK_LEVEL_LEASE, 65 lease_key = self.lease1, 66 lease_state = self.rw).result() 67 68 self.assertEqual(handle1.lease.lease_state, self.rw) 69 70 handle2 = chan.create(tree, 71 'lease.txt', 72 share=self.share_all, 73 oplock_level=pike.smb2.SMB2_OPLOCK_LEVEL_LEASE, 74 lease_key = self.lease1, 75 lease_state = self.rwh).result() 76 77 self.assertIs(handle2.lease, handle1.lease) 78 self.assertEqual(handle2.lease.lease_state, self.rwh) 79 80 # On break, voluntarily give up handle caching 81 handle2.lease.on_break(lambda state: state & ~pike.smb2.SMB2_LEASE_HANDLE_CACHING) 82 83 # Break our lease 84 handle3 = chan.create(tree, 85 'lease.txt', 86 share=self.share_all, 87 oplock_level=pike.smb2.SMB2_OPLOCK_LEVEL_LEASE, 88 lease_key = self.lease2, 89 lease_state = self.rwh).result() 90 91 # First lease should have broken to r 92 self.assertEqual(handle2.lease.lease_state, self.r) 93 # Should be granted rh on second lease 94 self.assertEqual(handle3.lease.lease_state, self.rh) 95 96 chan.close(handle1) 97 chan.close(handle2) 98 chan.close(handle3)
99 100 # Close handle associated with lease while a break is in progress
102 chan, tree = self.tree_connect() 103 # Request rw lease 104 handle1 = chan.create(tree, 105 'lease.txt', 106 share=self.share_all, 107 oplock_level=pike.smb2.SMB2_OPLOCK_LEVEL_LEASE, 108 lease_key = self.lease1, 109 lease_state = self.rw).result() 110 111 # Upgrade to rwh 112 handle2 = chan.create(tree, 113 'lease.txt', 114 share=self.share_all, 115 oplock_level=pike.smb2.SMB2_OPLOCK_LEVEL_LEASE, 116 lease_key = self.lease1, 117 lease_state = self.rwh).result() 118 119 # Break our lease 120 handle3_future = chan.create(tree, 121 'lease.txt', 122 share=self.share_all, 123 oplock_level=pike.smb2.SMB2_OPLOCK_LEVEL_LEASE, 124 lease_key = self.lease2, 125 lease_state = self.rwh) 126 127 # Wait for break 128 handle1.lease.future.wait() 129 130 # Close second handle 131 chan.close(handle2) 132 133 # Now ack break 134 handle1.lease.on_break(lambda state: state) 135 136 # Wait for handle3 137 handle3 = handle3_future.result() 138 139 chan.close(handle1) 140 chan.close(handle3)
141 142 # Test that a lease can be shared across connections
144 chan, tree = self.tree_connect() 145 146 # Request rwh lease 147 handle1 = chan.create(tree, 148 'lease.txt', 149 share=self.share_all, 150 oplock_level=pike.smb2.SMB2_OPLOCK_LEVEL_LEASE, 151 lease_key = self.lease1, 152 lease_state = self.rwh).result() 153 self.assertEqual(handle1.lease.lease_state, self.rwh) 154 155 chan2, tree2 = self.tree_connect() 156 157 # Request rwh lease to same file on separate connection, 158 # which we should get 159 handle2 = chan2.create(tree2, 160 'lease.txt', 161 share=self.share_all, 162 oplock_level=pike.smb2.SMB2_OPLOCK_LEVEL_LEASE, 163 lease_key = self.lease1, 164 lease_state = self.rwh).result() 165 self.assertEqual(handle2.lease.lease_state, self.rwh) 166 167 # Leases should be the same object 168 self.assertEqual(handle1.lease, handle2.lease) 169 170 chan2.close(handle2) 171 chan.close(handle1)
172