sign_read_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package tests
  2. import (
  3. "testing"
  4. "golang.org/x/exp/maps"
  5. "notabug.org/Umnik/GoAndroidSDK/v2/components/sdk/buildTools/apksigner"
  6. )
  7. func newSignRead(t *testing.T, funcName string) *apksigner.Apksigner {
  8. t.Helper()
  9. a, err := apksigner.NewApkSignerLastVersion(*newSdk(t, t.Name()))
  10. if !testErr(t, funcName, err) {
  11. return nil
  12. }
  13. return a
  14. }
  15. func dumpSign(file string, funcName string, signer *apksigner.Apksigner, t *testing.T) []byte {
  16. t.Helper()
  17. b, err := signer.DumpSignatureInfo(file, false)
  18. if !testErr(t, funcName, err) {
  19. t.Errorf("data: %s\nerr: %s", string(b), err)
  20. return nil
  21. }
  22. return b
  23. }
  24. func verifyInfo(t *testing.T, file string, funcName string) *apksigner.VerifyInfo {
  25. t.Helper()
  26. sr := newSignRead(t, funcName)
  27. b := dumpSign(file, funcName, sr, t)
  28. res := apksigner.ParseDumpSignatureInfoFromBytes(b)
  29. return &res
  30. }
  31. func TestSignV31(t *testing.T) {
  32. t.Parallel()
  33. info := verifyInfo(t, sign31File, t.Name())
  34. t.Log(info)
  35. }
  36. func TestVirifyInfo1(t *testing.T) {
  37. t.Parallel()
  38. vi := verifyInfo(t, emptyValFile, t.Name())
  39. if !maps.Equal(vi.SignInfo[0].CertDN, map[string]string{"O": "Ledger SAS", "C": "FR", "L": "Paris"}) {
  40. t.Errorf("Bad signature: %s", vi.SignInfo[0].CertDN)
  41. }
  42. }
  43. func TestVirifyInfo2(t *testing.T) {
  44. t.Parallel()
  45. vi := verifyInfo(t, goodApkFile, t.Name())
  46. if !maps.Equal(vi.SignInfo[0].CertDN, map[string]string{"C": "UK", "CN": "FDroid", "L": "ORG", "O": "fdroid.org", "OU": "FDroid", "ST": "ORG"}) {
  47. t.Errorf("Bad signature: %s", vi.SignInfo[0].CertDN)
  48. }
  49. }
  50. func TestVerifyInfo3(t *testing.T) {
  51. t.Parallel()
  52. vi := verifyInfo(t, splitBaseFile, t.Name())
  53. if !maps.Equal(vi.SignInfo[0].CertDN, map[string]string{"CN": "Empatika LLC", "O": "Empatika LLC"}) {
  54. t.Errorf("Bad signature: %s", vi.SignInfo[0].CertDN)
  55. }
  56. }