go/src/crypto/internal/cryptotest/methods.go
Alan Donovan 4bfc3a9d14 std,cmd: go fix -any std cmd
This change mechanically replaces all occurrences of interface{}
by 'any' (where deemed safe by the 'any' modernizer) throughout
std and cmd, minus their vendor trees.

Since this fix is relatively numerous, it gets its own CL.

Also, 'go generate go/types'.

Change-Id: I14a6b52856c3291c1d27935409bca8d5fd4242a2
Reviewed-on: https://go-review.googlesource.com/c/go/+/719702
Commit-Queue: Alan Donovan <adonovan@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
Auto-Submit: Alan Donovan <adonovan@google.com>
2025-11-11 19:59:40 -08:00

62 lines
1.6 KiB
Go

// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cryptotest
import (
"fmt"
"reflect"
"slices"
"testing"
)
// NoExtraMethods checks that the concrete type of *ms has no exported methods
// beyond the methods of the interface type of *ms, and any others specified in
// the allowed list.
//
// These methods are accessible through interface upgrades, so they end up part
// of the API even if undocumented per Hyrum's Law.
//
// ms must be a pointer to a non-nil interface.
func NoExtraMethods(t *testing.T, ms any, allowed ...string) {
t.Helper()
extraMethods, err := extraMethods(ms)
if err != nil {
t.Fatal(err)
}
for _, m := range extraMethods {
if slices.Contains(allowed, m) {
continue
}
t.Errorf("unexpected method %q", m)
}
}
func extraMethods(ip any) ([]string, error) {
v := reflect.ValueOf(ip)
if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Interface || v.Elem().IsNil() {
return nil, fmt.Errorf("argument must be a pointer to a non-nil interface")
}
interfaceType := v.Elem().Type()
concreteType := v.Elem().Elem().Type()
interfaceMethods := make(map[string]bool)
for i := range interfaceType.NumMethod() {
interfaceMethods[interfaceType.Method(i).Name] = true
}
var extraMethods []string
for i := range concreteType.NumMethod() {
m := concreteType.Method(i)
if !m.IsExported() {
continue
}
if !interfaceMethods[m.Name] {
extraMethods = append(extraMethods, m.Name)
}
}
return extraMethods, nil
}