Colin Cross | 36f55aa | 2022-03-21 18:46:41 -0700 | [diff] [blame^] | 1 | // Copyright 2022 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "strings" |
| 20 | "testing" |
| 21 | ) |
| 22 | |
| 23 | func Test_extractR8CompilerHash(t *testing.T) { |
| 24 | testCases := []struct { |
| 25 | name string |
| 26 | data string |
| 27 | |
| 28 | hash string |
| 29 | err string |
| 30 | }{ |
| 31 | { |
| 32 | name: "simple", |
| 33 | data: `# compiler: R8 |
| 34 | # compiler_version: 3.3.18-dev |
| 35 | # min_api: 10000 |
| 36 | # compiler_hash: bab44c1a04a2201b55fe10394f477994205c34e0 |
| 37 | # common_typos_disable |
| 38 | # {"id":"com.android.tools.r8.mapping","version":"2.0"} |
| 39 | # pg_map_id: 7fe8b95 |
| 40 | # pg_map_hash: SHA-256 7fe8b95ae71f179f63d2a585356fb9cf2c8fb94df9c9dd50621ffa6d9e9e88da |
| 41 | android.car.userlib.UserHelper -> android.car.userlib.UserHelper: |
| 42 | `, |
| 43 | hash: "7fe8b95ae71f179f63d2a585356fb9cf2c8fb94df9c9dd50621ffa6d9e9e88da", |
| 44 | }, |
| 45 | { |
| 46 | name: "empty", |
| 47 | data: ``, |
| 48 | hash: "", |
| 49 | }, |
| 50 | { |
| 51 | name: "non comment line", |
| 52 | data: `# compiler: R8 |
| 53 | # compiler_version: 3.3.18-dev |
| 54 | # min_api: 10000 |
| 55 | # compiler_hash: bab44c1a04a2201b55fe10394f477994205c34e0 |
| 56 | # common_typos_disable |
| 57 | # {"id":"com.android.tools.r8.mapping","version":"2.0"} |
| 58 | # pg_map_id: 7fe8b95 |
| 59 | android.car.userlib.UserHelper -> android.car.userlib.UserHelper: |
| 60 | # pg_map_hash: SHA-256 7fe8b95ae71f179f63d2a585356fb9cf2c8fb94df9c9dd50621ffa6d9e9e88da |
| 61 | `, |
| 62 | hash: "", |
| 63 | }, |
| 64 | { |
| 65 | name: "invalid hash", |
| 66 | data: `# pg_map_hash: foobar 7fe8b95ae71f179f63d2a585356fb9cf2c8fb94df9c9dd50621ffa6d9e9e88da`, |
| 67 | err: "invalid hash type", |
| 68 | }, |
| 69 | } |
| 70 | |
| 71 | for _, tt := range testCases { |
| 72 | t.Run(tt.name, func(t *testing.T) { |
| 73 | hash, err := extractR8CompilerHash(bytes.NewBufferString(tt.data)) |
| 74 | if err != nil { |
| 75 | if tt.err != "" { |
| 76 | if !strings.Contains(err.Error(), tt.err) { |
| 77 | t.Fatalf("incorrect error in extractR8CompilerHash, want %s got %s", tt.err, err) |
| 78 | } |
| 79 | } else { |
| 80 | t.Fatalf("unexpected error in extractR8CompilerHash: %s", err) |
| 81 | } |
| 82 | } else if tt.err != "" { |
| 83 | t.Fatalf("missing error in extractR8CompilerHash, want %s", tt.err) |
| 84 | } |
| 85 | |
| 86 | if g, w := hash, tt.hash; g != w { |
| 87 | t.Errorf("incorrect hash, want %q got %q", w, g) |
| 88 | } |
| 89 | }) |
| 90 | } |
| 91 | } |