1// Copyright 2020 The Prometheus Authors 2// Licensed under the Apache License, Version 2.0 (the "License"); 3// you may not use this file except in compliance with the License. 4// You may obtain a copy of the License at 5// 6// http://www.apache.org/licenses/LICENSE-2.0 7// 8// Unless required by applicable law or agreed to in writing, software 9// distributed under the License is distributed on an "AS IS" BASIS, 10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11// See the License for the specific language governing permissions and 12// limitations under the License. 13 14package procfs 15 16import ( 17 "net" 18 "reflect" 19 "testing" 20) 21 22func Test_parseNetIPSocketLine(t *testing.T) { 23 tests := []struct { 24 fields []string 25 name string 26 want *netIPSocketLine 27 wantErr bool 28 }{ 29 { 30 name: "reading valid lines, no issue should happened", 31 fields: []string{"11:", "00000000:0000", "00000000:0000", "0A", "00000017:0000002A", "0:0", "0", "1000"}, 32 want: &netIPSocketLine{ 33 Sl: 11, 34 LocalAddr: net.IP{0, 0, 0, 0}, 35 LocalPort: 0, 36 RemAddr: net.IP{0, 0, 0, 0}, 37 RemPort: 0, 38 St: 10, 39 TxQueue: 23, 40 RxQueue: 42, 41 UID: 1000, 42 }, 43 }, 44 { 45 name: "error case - invalid line - number of fields/columns < 8", 46 fields: []string{"1:", "00000000:0000", "00000000:0000", "07", "0:0", "0"}, 47 want: nil, 48 wantErr: true, 49 }, 50 { 51 name: "error case - parse sl - not a valid uint", 52 fields: []string{"a:", "00000000:0000", "00000000:0000", "07", "00000000:00000001", "0:0", "0", "0"}, 53 want: nil, 54 wantErr: true, 55 }, 56 { 57 name: "error case - parse local_address - not a valid hex", 58 fields: []string{"1:", "0000000O:0000", "00000000:0000", "07", "00000000:00000001", "0:0", "0", "0"}, 59 want: nil, 60 wantErr: true, 61 }, 62 { 63 name: "error case - parse rem_address - not a valid hex", 64 fields: []string{"1:", "00000000:0000", "0000000O:0000", "07", "00000000:00000001", "0:0", "0", "0"}, 65 want: nil, 66 wantErr: true, 67 }, 68 { 69 name: "error case - cannot parse line - missing colon", 70 fields: []string{"1:", "00000000:0000", "00000000:0000", "07", "0000000000000001", "0:0", "0", "0"}, 71 want: nil, 72 wantErr: true, 73 }, 74 { 75 name: "error case - parse tx_queue - not a valid hex", 76 fields: []string{"1:", "00000000:0000", "00000000:0000", "07", "DEADCODE:00000001", "0:0", "0", "0"}, 77 want: nil, 78 wantErr: true, 79 }, 80 { 81 name: "error case - parse rx_queue - not a valid hex", 82 fields: []string{"1:", "00000000:0000", "00000000:0000", "07", "00000000:FEEDCODE", "0:0", "0", "0"}, 83 want: nil, 84 wantErr: true, 85 }, 86 { 87 name: "error case - parse UID - not a valid uint", 88 fields: []string{"1:", "00000000:0000", "00000000:0000", "07", "00000000:00000001", "0:0", "0", "-10"}, 89 want: nil, 90 wantErr: true, 91 }, 92 } 93 for _, tt := range tests { 94 t.Run(tt.name, func(t *testing.T) { 95 got, err := parseNetIPSocketLine(tt.fields) 96 if (err != nil) != tt.wantErr { 97 t.Errorf("parseNetIPSocketLine() error = %v, wantErr %v", err, tt.wantErr) 98 return 99 } 100 if tt.want == nil && got != nil { 101 t.Errorf("parseNetIPSocketLine() = %v, want %v", got, tt.want) 102 } 103 if !reflect.DeepEqual(got, tt.want) { 104 t.Errorf("parseNetIPSocketLine() = %#v, want %#v", got, tt.want) 105 } 106 }) 107 } 108} 109