r8_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. package main
  15. import (
  16. "bytes"
  17. "strings"
  18. "testing"
  19. )
  20. func Test_extractR8CompilerHash(t *testing.T) {
  21. testCases := []struct {
  22. name string
  23. data string
  24. hash string
  25. err string
  26. }{
  27. {
  28. name: "simple",
  29. data: `# compiler: R8
  30. # compiler_version: 3.3.18-dev
  31. # min_api: 10000
  32. # compiler_hash: bab44c1a04a2201b55fe10394f477994205c34e0
  33. # common_typos_disable
  34. # {"id":"com.android.tools.r8.mapping","version":"2.0"}
  35. # pg_map_id: 7fe8b95
  36. # pg_map_hash: SHA-256 7fe8b95ae71f179f63d2a585356fb9cf2c8fb94df9c9dd50621ffa6d9e9e88da
  37. android.car.userlib.UserHelper -> android.car.userlib.UserHelper:
  38. `,
  39. hash: "7fe8b95ae71f179f63d2a585356fb9cf2c8fb94df9c9dd50621ffa6d9e9e88da",
  40. },
  41. {
  42. name: "empty",
  43. data: ``,
  44. hash: "",
  45. },
  46. {
  47. name: "non comment line",
  48. data: `# compiler: R8
  49. # compiler_version: 3.3.18-dev
  50. # min_api: 10000
  51. # compiler_hash: bab44c1a04a2201b55fe10394f477994205c34e0
  52. # common_typos_disable
  53. # {"id":"com.android.tools.r8.mapping","version":"2.0"}
  54. # pg_map_id: 7fe8b95
  55. android.car.userlib.UserHelper -> android.car.userlib.UserHelper:
  56. # pg_map_hash: SHA-256 7fe8b95ae71f179f63d2a585356fb9cf2c8fb94df9c9dd50621ffa6d9e9e88da
  57. `,
  58. hash: "",
  59. },
  60. {
  61. name: "invalid hash",
  62. data: `# pg_map_hash: foobar 7fe8b95ae71f179f63d2a585356fb9cf2c8fb94df9c9dd50621ffa6d9e9e88da`,
  63. err: "invalid hash type",
  64. },
  65. }
  66. for _, tt := range testCases {
  67. t.Run(tt.name, func(t *testing.T) {
  68. hash, err := extractR8CompilerHash(bytes.NewBufferString(tt.data))
  69. if err != nil {
  70. if tt.err != "" {
  71. if !strings.Contains(err.Error(), tt.err) {
  72. t.Fatalf("incorrect error in extractR8CompilerHash, want %s got %s", tt.err, err)
  73. }
  74. } else {
  75. t.Fatalf("unexpected error in extractR8CompilerHash: %s", err)
  76. }
  77. } else if tt.err != "" {
  78. t.Fatalf("missing error in extractR8CompilerHash, want %s", tt.err)
  79. }
  80. if g, w := hash, tt.hash; g != w {
  81. t.Errorf("incorrect hash, want %q got %q", w, g)
  82. }
  83. })
  84. }
  85. }