|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package linux |
| 19 | + |
| 20 | +import ( |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | +) |
| 25 | + |
| 26 | +func TestParseKeyValueNoEOL(t *testing.T) { |
| 27 | + vals := [][2]string{} |
| 28 | + err := parseKeyValue([]byte( |
| 29 | + "Name: zsh\nUmask: 0022\nState: S (sleeping)\nUid: 1000 1000 1000 1000", |
| 30 | + ), ':', func(key, value []byte) error { |
| 31 | + vals = append(vals, [2]string{string(key), string(value)}) |
| 32 | + return nil |
| 33 | + }) |
| 34 | + assert.NoError(t, err) |
| 35 | + |
| 36 | + assert.Equal(t, [][2]string{ |
| 37 | + {"Name", "zsh"}, |
| 38 | + {"Umask", "0022"}, |
| 39 | + {"State", "S (sleeping)"}, |
| 40 | + {"Uid", "1000\t1000\t1000\t1000"}, |
| 41 | + }, vals) |
| 42 | +} |
| 43 | + |
| 44 | +func TestParseKeyValueEmptyLine(t *testing.T) { |
| 45 | + vals := [][2]string{} |
| 46 | + err := parseKeyValue([]byte( |
| 47 | + "Name: zsh\nUmask: 0022\nState: S (sleeping)\n\nUid: 1000 1000 1000 1000", |
| 48 | + ), ':', func(key, value []byte) error { |
| 49 | + vals = append(vals, [2]string{string(key), string(value)}) |
| 50 | + return nil |
| 51 | + }) |
| 52 | + assert.NoError(t, err) |
| 53 | + |
| 54 | + assert.Equal(t, [][2]string{ |
| 55 | + {"Name", "zsh"}, |
| 56 | + {"Umask", "0022"}, |
| 57 | + {"State", "S (sleeping)"}, |
| 58 | + {"Uid", "1000\t1000\t1000\t1000"}, |
| 59 | + }, vals) |
| 60 | +} |
| 61 | + |
| 62 | +func TestParseKeyValueEOL(t *testing.T) { |
| 63 | + vals := [][2]string{} |
| 64 | + err := parseKeyValue([]byte( |
| 65 | + "Name: zsh\nUmask: 0022\nState: S (sleeping)\nUid: 1000 1000 1000 1000\n", |
| 66 | + ), ':', func(key, value []byte) error { |
| 67 | + vals = append(vals, [2]string{string(key), string(value)}) |
| 68 | + return nil |
| 69 | + }) |
| 70 | + assert.NoError(t, err) |
| 71 | + |
| 72 | + assert.Equal(t, [][2]string{ |
| 73 | + {"Name", "zsh"}, |
| 74 | + {"Umask", "0022"}, |
| 75 | + {"State", "S (sleeping)"}, |
| 76 | + {"Uid", "1000\t1000\t1000\t1000"}, |
| 77 | + }, vals) |
| 78 | +} |
| 79 | + |
| 80 | +// from cat /proc/$$/status |
| 81 | +var testProcStatus = []byte(`Name: zsh |
| 82 | +Umask: 0022 |
| 83 | +State: S (sleeping) |
| 84 | +Tgid: 4023363 |
| 85 | +Ngid: 0 |
| 86 | +Pid: 4023363 |
| 87 | +PPid: 4023357 |
| 88 | +TracerPid: 0 |
| 89 | +Uid: 1000 1000 1000 1000 |
| 90 | +Gid: 1000 1000 1000 1000 |
| 91 | +FDSize: 64 |
| 92 | +Groups: 24 25 27 29 30 44 46 102 109 112 116 119 131 998 1000 |
| 93 | +NStgid: 4023363 |
| 94 | +NSpid: 4023363 |
| 95 | +NSpgid: 4023363 |
| 96 | +NSsid: 4023363 |
| 97 | +VmPeak: 15596 kB |
| 98 | +VmSize: 15144 kB |
| 99 | +VmLck: 0 kB |
| 100 | +VmPin: 0 kB |
| 101 | +VmHWM: 9060 kB |
| 102 | +VmRSS: 8716 kB |
| 103 | +RssAnon: 3828 kB |
| 104 | +RssFile: 4888 kB |
| 105 | +RssShmem: 0 kB |
| 106 | +VmData: 3500 kB |
| 107 | +VmStk: 328 kB |
| 108 | +VmExe: 600 kB |
| 109 | +VmLib: 2676 kB |
| 110 | +VmPTE: 68 kB |
| 111 | +VmSwap: 0 kB |
| 112 | +HugetlbPages: 0 kB |
| 113 | +CoreDumping: 0 |
| 114 | +THP_enabled: 1 |
| 115 | +Threads: 1 |
| 116 | +SigQ: 0/126683 |
| 117 | +SigPnd: 0000000000000000 |
| 118 | +ShdPnd: 0000000000000000 |
| 119 | +SigBlk: 0000000000000002 |
| 120 | +SigIgn: 0000000000384000 |
| 121 | +SigCgt: 0000000008013003 |
| 122 | +CapInh: 0000000000000000 |
| 123 | +CapPrm: 0000000000000000 |
| 124 | +CapEff: 0000000000000000 |
| 125 | +CapBnd: 000001ffffffffff |
| 126 | +CapAmb: 0000000000000000 |
| 127 | +NoNewPrivs: 0 |
| 128 | +Seccomp: 0 |
| 129 | +Seccomp_filters: 0 |
| 130 | +Speculation_Store_Bypass: thread vulnerable |
| 131 | +Cpus_allowed: fff |
| 132 | +Cpus_allowed_list: 0-11 |
| 133 | +Mems_allowed: 00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001 |
| 134 | +Mems_allowed_list: 0 |
| 135 | +voluntary_ctxt_switches: 223 |
| 136 | +nonvoluntary_ctxt_switches: 25 |
| 137 | +`) |
| 138 | + |
| 139 | +func BenchmarkParseKeyValue(b *testing.B) { |
| 140 | + for i := 0; i < b.N; i++ { |
| 141 | + _ = parseKeyValue(testProcStatus, ':', func(key, value []byte) error { |
| 142 | + return nil |
| 143 | + }) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +func FuzzParseKeyValue(f *testing.F) { |
| 148 | + testcases := []string{ |
| 149 | + "no_separator", |
| 150 | + "no_value:", |
| 151 | + "empty_value: ", |
| 152 | + "normal: 223", |
| 153 | + } |
| 154 | + for _, tc := range testcases { |
| 155 | + f.Add(tc) |
| 156 | + } |
| 157 | + f.Fuzz(func(t *testing.T, orig string) { |
| 158 | + _ = parseKeyValue([]byte(orig), ':', func(key, value []byte) error { |
| 159 | + return nil |
| 160 | + }) |
| 161 | + }) |
| 162 | +} |
0 commit comments