Android test from storage

I wrote an Android application that (among other things) writes files to disk. Now I wonder what happens when the file system starts from storage . I assume, for example, java.io.FileOutputStream.write() will IOException that I will have to catch.

The fact is that I do not want to rely on my powers as a super ninja - I could forget to try somewhere or not cope with it. So I want to test this scenario .

Unfortunately, I could not find a word about good practice at the same time - even on SO.

Of course, I coldly manually write the file system with something like this, I think:

 size = N dir = /some/path data = generateDataForSize(size) while( write(data, dir) ); while( round(size/=2) > 1 ) write(generateDataForSize(size), dir) 

Where write() will either generate separate files or add them to one. There may be some problems with generateDataForSize() and large sizes, but let it be delayed for now.

What makes me itchy is that I have to put this in my application and I have to clear this $% #! manually. Well, if I put it in my own directory, I can just drop it all with a single line in the adb shell.

Anyway, is there an easier way to go about this that I am missing? Does the android have any mechanics for this, for example, temporarily limiting the storage space available for each application? Any integrated unit test solutions or sroid tools for Android? How do other people (you) do it?


TL DR : how to use fs effectively to test the case of writing to fs when it is full.


edit: I am testing on a real device that is not deployed. The emulator is slow, even if HAXM is up and running. Unfortunately, using an emulator is not an option.

Tasos Moustakas hints at the limited space available for AVD. I think if you avoid the problems with android:installLocation="preferExternal" and move the application to sd / write files on a limited sd, this is an acceptable solution.

The accepted answer is pretty much what I did. But it still requires some manual work, so feel free to post more answers.

+5
source share
4 answers

Android runs on Linux, so you can go into the shell and use the dd to fill the disk. Connect the device via USB and enter adb shell , then

 dd if=/dev/zero of=/sdcard/deleteme bs=1m count=1024 

write a 1GB file named deleteme in /sdcard full of zeros.

  • if= is the input file
  • /dev/zero just returns infinite zeros.
  • of= is the output file
  • bs= - block size, here is one megabyte; some implementations prefer 1M instead.
  • count= - the number of blocks to write, here is 1024, which makes a file 1 GB in size.

Adjust the size to suit your needs or write multiple files. Get ready for Android to start complaining about low disk space when you press about 400 MB of free space. In my experience, Android starts complaining about low disk space long before applications crash.

+11
source

If you are testing your application on an AVD emulator, you can simply configure the space available in the emulator. See the documentation . Let me know if this solution works for you. Here is your tutorial on how to use AVD for Google Play services.

+1
source

If the executable program only creates POSIX system calls, you can use libfiu to inject errors and such things as returning ENOSPC calls to simulate a disk.

Another trick is to use the FUSE file system, such as charybdefs , which allows you to introduce errors at the file system level.

-1
source

Source: https://habr.com/ru/post/1234141/


All Articles