summaryrefslogtreecommitdiff
path: root/api/proc/ProcSimpleFileTests.py
blob: ab23a2467b81a245105e1076b179192efeca3f6b (plain)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
#
# Copyright (C) 2017 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import math

from parse import with_pattern
from vts.testcases.kernel.api.proc import KernelProcFileTestBase
from vts.utils.python.android import api
from vts.utils.python.file import target_file_utils

# Test for /proc/sys/kernel/*.

class ProcCorePattern(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/kernel/core_pattern is used to specify a core dumpfile pattern
    name.
    '''

    def parse_contents(self, contents):
        pass

    def get_path(self):
        return "/proc/sys/kernel/core_pattern"

    def get_permission_checker(self):
        return target_file_utils.IsReadWrite


class ProcCorePipeLimit(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/kernel/core_pipe_limit defines how many concurrent crashing
    processes may be piped to user space applications in parallel.
    '''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def get_path(self):
        return "/proc/sys/kernel/core_pipe_limit"

    def get_permission_checker(self):
        return target_file_utils.IsReadWrite


class ProcDmesgRestrict(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/kernel/dmesg_restrict indicates whether unprivileged users are
    prevented from using dmesg.
    '''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def result_correct(self, result):
        return result in [0, 1]

    def get_path(self):
        return "/proc/sys/kernel/dmesg_restrict"

    def get_permission_checker(self):
        return target_file_utils.IsReadWrite


class ProcDomainname(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/kernel/domainname determines YP/NIS domain name of the system.'''

    def parse_contents(self, contents):
        pass

    def get_path(self):
        return "/proc/sys/kernel/domainname"

    def get_permission_checker(self):
        return target_file_utils.IsReadWrite


class ProcHostname(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/kernel/hostname determines the system's host name.'''

    def parse_contents(self, contents):
        pass

    def get_path(self):
        return "/proc/sys/kernel/hostname"

    def get_permission_checker(self):
        return target_file_utils.IsReadWrite


class ProcHungTaskTimeoutSecs(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/kernel/hung_task_timeout_secs controls the default timeout
    (in seconds) used to determine when a task has become non-responsive and
    should be considered hung.
    '''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def get_path(self):
        return "/proc/sys/kernel/hung_task_timeout_secs"

    def get_permission_checker(self):
        return target_file_utils.IsReadWrite

    def file_optional(self):
        return True

class ProcKptrRestrictTest(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/kernel/kptr_restrict determines whether kernel pointers are printed
    in proc files.
    '''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def result_correct(self, result):
        return result >= 0 and result <= 4

    def get_path(self):
        return "/proc/sys/kernel/kptr_restrict"

    def get_permission_checker(self):
        """Get r/w file permission checker.
        """
        return target_file_utils.IsReadWrite


class ProcModulesDisabled(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/kernel/modules_disabled indicates if modules are allowed to be
    loaded.
    '''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def result_correct(self, result):
        return result in [0, 1]

    def get_path(self):
        return "/proc/sys/kernel/modules_disabled"

    def get_permission_checker(self):
        return target_file_utils.IsReadWrite


class ProcPanicOnOops(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/kernel/panic_on_oops controls kernel's behaviour on oops.'''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def result_correct(self, result):
        return result in [0, 1]

    def get_path(self):
        return "/proc/sys/kernel/panic_on_oops"

    def get_permission_checker(self):
        return target_file_utils.IsReadWrite


class ProcPerfEventMaxSampleRate(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/kernel/perf_event_max_sample_rate sets the maximum sample rate
    of performance events.
    '''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def get_path(self):
        return "/proc/sys/kernel/perf_event_max_sample_rate"

    def get_permission_checker(self):
        return target_file_utils.IsReadWrite


class ProcPerfEventParanoid(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/kernel/perf_event_paranoid controls use of the performance
    events system by unprivileged users.
    '''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def get_path(self):
        return "/proc/sys/kernel/perf_event_paranoid"

    def get_permission_checker(self):
        return target_file_utils.IsReadWrite


class ProcPidMax(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/kernel/pid_max is the pid allocation wrap value.'''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def get_path(self):
        return "/proc/sys/kernel/pid_max"

    def get_permission_checker(self):
        return target_file_utils.IsReadWrite


@with_pattern(
    r"^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
)
def token_uuid(text):
    return text

class ProcSysKernelRandomBootId(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/kernel/random/boot_id generates a random ID each boot.'''

    def parse_contents(self, contents):
        return self.parse_line("{:uuid}\n", contents, dict(uuid=token_uuid))[0]

    def get_path(self):
        return "/proc/sys/kernel/random/boot_id"

    def get_permission_checker(self):
        return target_file_utils.IsReadOnly


class ProcRandomizeVaSpaceTest(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/kernel/randomize_va_space determines the address layout randomization
    policy for the system.
    '''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def result_correct(self, result):
        return result >= 0 and result <= 2

    def get_path(self):
        return "/proc/sys/kernel/randomize_va_space"

    def get_permission_checker(self):
        """Get r/w file permission checker.
        """
        return target_file_utils.IsReadWrite


class ProcSchedChildRunsFirst(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/kernel/sched_child_runs_first causes newly forked tasks to
    be favored in scheduling over their parents.
    '''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def get_path(self):
        return "/proc/sys/kernel/sched_child_runs_first"

    def get_permission_checker(self):
        return target_file_utils.IsReadWrite


class ProcSchedLatencyNS(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/kernel/sched_latency_ns is the maximum latency in nanoseconds a
    task may incur prior to being scheduled.
    '''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def result_correct(self, result):
        return result >= 100000 and result <= 1000000000

    def get_path(self):
        return "/proc/sys/kernel/sched_latency_ns"

    def get_permission_checker(self):
        return target_file_utils.IsReadWrite


class ProcSchedRTPeriodUS(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/kernel/sched_rt_period_us defines the period length used by the
    system-wide RT execution limit in microseconds.
    '''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def result_correct(self, result):
        return result >= 1 and result <= math.pow(2,31)

    def get_path(self):
        return "/proc/sys/kernel/sched_rt_period_us"

    def get_permission_checker(self):
        return target_file_utils.IsReadWrite


class ProcSchedRTRuntimeUS(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/kernel/sched_rt_runtime_us defines the amount of time in
    microseconds relative to sched_rt_period_us that the system may execute RT
    tasks.
    '''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def result_correct(self, result):
        return result >= -1 and result <= (math.pow(2,31) - 1)

    def get_path(self):
        return "/proc/sys/kernel/sched_rt_runtime_us"

    def get_permission_checker(self):
        return target_file_utils.IsReadWrite


class ProcSchedTunableScaling(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/kernel/sched_tunable_scaling determines whether
    sched_latency_ns should be automatically adjusted by the scheduler based on
    the number of CPUs.
    '''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def result_correct(self, result):
        return result >= 0 and result <= 2

    def get_path(self):
        return "/proc/sys/kernel/sched_tunable_scaling"

    def get_permission_checker(self):
        return target_file_utils.IsReadWrite


class ProcSchedWakeupGranularityNS(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/kernel/sched_wakeup_granularity_ns defines how much more
    virtual runtime task A must have than task B in nanoseconds in order for
    task B to preempt it.
    '''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def result_correct(self, result):
        return result >= 0 and result <= 1000000000

    def get_path(self):
        return "/proc/sys/kernel/sched_wakeup_granularity_ns"

    def get_permission_checker(self):
        return target_file_utils.IsReadWrite


class ProcSysRqTest(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/kernel/sysrq controls the functions allowed to be invoked
    via the SysRq key.'''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def result_correct(self, result):
        return result >= 0 and result <= 511

    def get_path(self):
        return "/proc/sys/kernel/sysrq"

    def get_permission_checker(self):
        return target_file_utils.IsReadWrite


# Tests for /proc/sys/vm/*.

class ProcDirtyBackgroundBytes(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/vm/dirty_background_bytes contains the amount of dirty memory
    at which the background kernel flusher threads will start writeback.
    '''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def get_path(self):
        return "/proc/sys/vm/dirty_background_bytes"

    def get_permission_checker(self):
        return target_file_utils.IsReadWrite


class ProcDirtyBackgroundRatio(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/vm/dirty_background_ratio contains, as a percentage of total
    available memory that contains free pages and reclaimable pages, the number
    of pages at which the background kernel flusher threads will start writing
    out dirty data.
    '''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def result_correct(self, result):
        return result >= 0 and result <= 100

    def get_path(self):
        return "/proc/sys/vm/dirty_background_ratio"

    def get_permission_checker(self):
        return target_file_utils.IsReadWrite


class ProcDropCaches(KernelProcFileTestBase.KernelProcFileTestBase):
    '''Writing to /proc/sys/vm/drop_caches will cause the kernel to drop clean
    caches.
    '''

    def parse_contents(self, contents):
        # Format of this file is not documented, so don't check that.
        return ''

    def get_path(self):
        return "/proc/sys/vm/drop_caches"

    def IsReadWriteOrWriteOnly(self, permission_bits):
        return (target_file_utils.IsReadWrite(permission_bits) or
                target_file_utils.IsWriteOnly(permission_bits))

    def get_permission_checker(self):
        if self.api_level > api.PLATFORM_API_LEVEL_Q:
            return target_file_utils.IsWriteOnly
        else:
            return self.IsReadWriteOrWriteOnly

    def test_format(self):
        return False

class ProcExtraFreeKbytes(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/vm/extra_free_kbytes tells the VM to keep extra free memory
    between the threshold where background reclaim (kswapd) kicks in, and the
    threshold where direct reclaim (by allocating processes) kicks in.
    '''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def get_path(self):
        return "/proc/sys/vm/extra_free_kbytes"

    def get_permission_checker(self):
        return target_file_utils.IsReadWrite

    def file_optional(self):
        # This file isn't in Android common kernel.
        return True


class ProcOverCommitMemoryTest(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/vm/overcommit_memory determines the kernel virtual memory accounting mode.
    '''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def result_correct(self, result):
        return result >= 0 and result <= 2

    def get_path(self):
        return "/proc/sys/vm/overcommit_memory"

    def get_permission_checker(self):
        """Get r/w file permission checker.
        """
        return target_file_utils.IsReadWrite


class ProcMaxMapCount(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/vm/max_map_count contains the maximum number of memory map areas a process
    may have.
    '''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def get_path(self):
        return "/proc/sys/vm/max_map_count"

    def get_permission_checker(self):
        return target_file_utils.IsReadWrite


class ProcMmapMinAddrTest(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/vm/mmap_min_addr specifies the minimum address that can be mmap'd.
    '''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def get_path(self):
        return "/proc/sys/vm/mmap_min_addr"

    def get_permission_checker(self):
        """Get r/w file permission checker.
        """
        return target_file_utils.IsReadWrite


class ProcMmapRndBitsTest(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/vm/mmap_rnd_(compat_)bits specifies the amount of randomness in mmap'd
    addresses. Must be >= 8.
    '''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def result_correct(self, result):
        return result >= 8

    def get_path(self):
        return "/proc/sys/vm/mmap_rnd_bits"

    def get_permission_checker(self):
        """Get r/w file permission checker.
        """
        return target_file_utils.IsReadWrite


class ProcMmapRndCompatBitsTest(ProcMmapRndBitsTest):
    def get_path(self):
        return "/proc/sys/vm/mmap_rnd_compat_bits"


class ProcPageCluster(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/vm/page-cluster controls the number of pages up to which
    consecutive pages are read in from swap in a single attempt.
    '''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def get_path(self):
        return "/proc/sys/vm/page-cluster"

    def get_permission_checker(self):
        return target_file_utils.IsReadWrite


# Tests for /proc/sys/fs/*.

class ProcPipeMaxSize(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/fs/pipe-max-size reports the maximum size (in bytes) of
    individual pipes.
    '''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def get_path(self):
        return "/proc/sys/fs/pipe-max-size"

    def get_permission_checker(self):
        return target_file_utils.IsReadWrite


class ProcProtectedHardlinks(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/fs/protected_hardlinks reports hardlink creation behavior.'''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def result_correct(self, result):
        return result in [0, 1]

    def get_path(self):
        return "/proc/sys/fs/protected_hardlinks"

    def get_permission_checker(self):
        return target_file_utils.IsReadWrite


class ProcProtectedSymlinks(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/fs/protected_symlinks reports symlink following behavior.'''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def result_correct(self, result):
        return result in [0, 1]

    def get_path(self):
        return "/proc/sys/fs/protected_symlinks"

    def get_permission_checker(self):
        return target_file_utils.IsReadWrite


class ProcSuidDumpable(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/sys/fs/suid_dumpable value can be used to query and set the core
    dump mode for setuid or otherwise protected/tainted binaries.
    '''

    def parse_contents(self, contents):
        return self.parse_line("{:d}\n", contents)[0]

    def result_correct(self, result):
        return result in [0, 1, 2]

    def get_path(self):
        return "/proc/sys/fs/suid_dumpable"

    def get_permission_checker(self):
        return target_file_utils.IsReadWrite


class ProcUptime(KernelProcFileTestBase.KernelProcFileTestBase):
    '''/proc/uptime tells how long the system has been running.'''

    def parse_contents(self, contents):
        return self.parse_line("{:f} {:f}\n", contents)[0]

    def get_path(self):
        return "/proc/uptime"