This file is indexed.

/usr/share/checkbox/scripts/removable_storage_test is in checkbox 0.13.7.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/python

import dbus
import sys
import random
import os
import tempfile
import hashlib
import argparse
from shutil import copy2
import subprocess
import threading

class DiskTest():
    def __init__(self):
        self.process = None
        self.cmd = None
        self.timeout = 3
        self.returnCode = None
        self.rem_disks = {}     # mounted before the script running
        self.rem_disks_nm = {}  # not mounted before the script running

    ''' Class to contain various methods for testing USB disks '''
    def generate_test_data(self,size):
        '''Generate a random data file of a given size'''
        r = random
        min = 100
        max = 1000000
        self.tfile = tempfile.NamedTemporaryFile(delete=False)
        print "Creating Temp Data file"
        while os.path.getsize(self.tfile.name) < size:
            self.tfile.write(str(r.randint(min,max)))

        print "File name is :%s" % self.tfile.name
        print "File size is %s bytes" % os.path.getsize(self.tfile.name)

    def md5_hash_file(self,path):
        fh = open(path,'r')
        md5 = hashlib.md5()
        try:
            while True:
                data = fh.read(8192)
                if not data:
                    break
                md5.update(data)
        finally:
            fh.close()
        return md5.hexdigest()

    def copy_file(self,source,dest):
        try:
            copy2(source,dest)
        except IOError:
            print "ERROR: Unable to copy the file to %s" % dest
            return False
        else:
            return True

    def compare_hash(self,parent,child):
        if not parent == child:
            return False
        else:
            return True

    def clean_up(self,target):
        try:
            os.unlink(target)
        except:
            print "ERROR: Unable to remove tempfile %s" % target

    def get_disk_info(self, device):
        ''' Probes dbus to find any attached/mounted devices'''
        bus = dbus.SystemBus()
        ud_manager_obj = bus.get_object("org.freedesktop.UDisks", "/org/freedesktop/UDisks")
        ud_manager = dbus.Interface(ud_manager_obj, 'org.freedesktop.UDisks')
        self.rem_disks = {}
        self.rem_disks_nm = {}
        for dev in ud_manager.EnumerateDevices():
            device_obj = bus.get_object("org.freedesktop.UDisks", dev)
            device_props = dbus.Interface(device_obj, dbus.PROPERTIES_IFACE)
            if not device_props.Get('org.freedesktop.UDisks.Device',"DeviceIsDrive"):
                if device_props.Get('org.freedesktop.UDisks.Device', "DriveConnectionInterface") in device:
                    dev_file = str(device_props.Get('org.freedesktop.UDisks.Device',"DeviceFile"))

                    if len(device_props.Get('org.freedesktop.UDisks.Device',"DeviceMountPaths")) > 0:
                        devPath = str(device_props.Get('org.freedesktop.UDisks.Device',"DeviceMountPaths")[0])
                        self.rem_disks[dev_file] = devPath
                    else:
                        self.rem_disks_nm[dev_file] = None

    def mount(self):
        passed_mount = {}

        for k, _ in self.rem_disks_nm.items():
            t = tempfile.mkdtemp(dir='/tmp')
 
            r = False
            try:
                r = self.make_thread(self._mount(k, t))
            except:
                pass

            # remove those devices fail at mounting
            if r: 
                passed_mount[k] = t
                print "Now mounting %s on %s" % (k, t)
            else:
                print "Error: can't mount %s" % (k)
        
        if len(self.rem_disks_nm) == len(passed_mount):
            self.rem_disks_nm = passed_mount
            return 0
        else:
            c = len(self.rem_disks_nm) - len(passed_mount) 
            self.rem_disks_nm = passed_mount
            return c

    def _mount(self, dev_file, tmp_dir):
        cmd = ['/bin/mount', dev_file, tmp_dir]
        self.process = subprocess.Popen(cmd)
        self.process.communicate()

    def umount(self):
        errors = 0

        for d in self.rem_disks_nm:
            r = False
            try:
                r = self.make_thread(self._umount(d))
            except:
                errors += 1
                pass

            if r:
                print "Now umounting %s on %s" % (d, self.rem_disks_nm[d]) 
            else:
                print "Error: can't umount %s on %s" % (d, self.rem_disks_nm[d]) 

        return errors

    def _umount(self, dir):
        # '-l': lazy umount, dealing problem of unable to umount the device.
        cmd = ['/bin/umount', '-l', dir] 
        self.process = subprocess.Popen(cmd)
        self.process.communicate()

    def clean_tmp_dir(self):
        for d in self.rem_disks_nm:
            if not os.path.ismount(self.rem_disks_nm[d]):
                os.rmdir(self.rem_disks_nm[d])

    def make_thread(self, target):
        thread = threading.Thread(target=target)
        thread.start()
        thread.join(self.timeout)

        if thread.is_alive():
            self.process.terminate()
            thread.join()

        r = getattr(self.process,'returncode', 1)

        if r == 0:
            return True
        else:
            return False

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('device',
                        choices=['usb','firewire','sdio', 'scsi'],
                        nargs = '+',
                        help="The type of removable media (usb, firewire, sdio) to test.")
    parser.add_argument('-l','--list',
                        action='store_true',
                        default=False,
                        help="List the removable devices and mounting status")
    parser.add_argument('-c','--count',
                        action='store',
                        default='1',
                        type=int,
                        help='The number of times to run the test')
    parser.add_argument('-s','--size',
                        action='store',
                        type=int,
                        default=1048576,
                        help="The size of the test data file to use in Bytes. Default is %(default)s")
    parser.add_argument('-n','--skip-not-mount',
                        action='store_true',
                        default=False,
                        help="skip the removable devices which haven't been mounted before the test.")

    args = parser.parse_args()

    test = DiskTest()
    test.get_disk_info(args.device)

    errors = 0

    if len(test.rem_disks) > 0 or len(test.rem_disks_nm) > 0: #If we do have removable drives attached and mounted
        if args.list:   #Simply output a list of drives detected
            print '-' * 20 
            print "removable devices currently mounted:"
            if len(test.rem_disks) > 0:
                for k,v in test.rem_disks.iteritems():
                    print "%s : %s" % (k, v)
            else:
                print "None"

            print "removable devices currently not mounted:"
            if len(test.rem_disks_nm) > 0:
                for k, _ in test.rem_disks_nm.iteritems():
                    print k
            else:
                print "None"
            
            print '-' * 20

            return 0

        else: #Create a file, copy to USB and compare hashes
            if args.skip_not_mount: 
                disks_all = test.rem_disks
            else:
                # mount those haven't be mounted yet.
                errors_mount = test.mount()

                if errors_mount > 0:
                    print "There're total %d device(s) failed at mounting." % errors_mount
                    errors += errors_mount

                disks_all = dict(test.rem_disks.items() + test.rem_disks_nm.items())
        
            if len(disks_all) > 0:
                print "Found the following mounted %s partitions:" % args.device

                for k,v in disks_all.iteritems():
                    print "%s : %s" % (k, v)

                print '-' * 20
                print "Running %s file transfer test for %s iterations" % (args.device, args.count)
                
                try: 
                    for i in range(args.count):
                        test.generate_test_data(args.size)
                        parent_hash = test.md5_hash_file(test.tfile.name)
                        print "Parent hash is: %s" % parent_hash
                        for k,v in disks_all.iteritems():
                            print "Copying %s to %s" % (test.tfile.name, v)
                            if not test.copy_file(test.tfile.name, v):
                                errors += 1
                                continue
                            print "Hashing copy on %s" % v
                            target = ''.join((v,'/',os.path.basename(test.tfile.name)))
                            child_hash = test.md5_hash_file(target)
                            print "Hash of %s on %s is %s" % (target, v, child_hash)
                            if not test.compare_hash(parent_hash,child_hash):
                                print "[Iteration %s] Failure on file copy to %s" % (i, k)
                                errors += 1
                            test.clean_up(target)
                        test.clean_up(test.tfile.name)

                finally:
                    if( len(test.rem_disks_nm) > 0 ):
                        if test.umount() != 0:
                            errors += 1
                        test.clean_tmp_dir()

                if errors > 0:
                    print "Completed %s test iterations, but there were errors" % args.count
                    return 1
                else:
                    print "Successfully completed %s %s file transfer test iterations" % (args.count, args.device)
                    return 0
            else:
                print "No device being mounted successfully for testing, aborting"
                return 1
                   
    else:  #If we don't have removable drives attached and mounted
        print "No removable drives were detected, aborting"
        return 1

if __name__ == '__main__':
    sys.exit(main())