github.com/3andne/restls-client-go@v0.1.6/u_clienthello_json_test.go (about)

     1  package tls
     2  
     3  import (
     4  	"encoding/json"
     5  	"os"
     6  	"reflect"
     7  	"testing"
     8  )
     9  
    10  func TestClientHelloSpecJSONUnmarshaler(t *testing.T) {
    11  	testClientHelloSpecJSONUnmarshaler(t, "testdata/ClientHello-JSON-Chrome102.json", HelloChrome_102)
    12  	testClientHelloSpecJSONUnmarshaler(t, "testdata/ClientHello-JSON-Firefox105.json", HelloFirefox_105)
    13  	testClientHelloSpecJSONUnmarshaler(t, "testdata/ClientHello-JSON-iOS14.json", HelloIOS_14)
    14  	testClientHelloSpecJSONUnmarshaler(t, "testdata/ClientHello-JSON-Edge106.json", HelloEdge_106)
    15  }
    16  
    17  func testClientHelloSpecJSONUnmarshaler(
    18  	t *testing.T,
    19  	jsonFilepath string,
    20  	truthClientHelloID ClientHelloID,
    21  ) {
    22  	jsonCH, err := os.ReadFile(jsonFilepath)
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  
    27  	var chsju ClientHelloSpecJSONUnmarshaler
    28  	if err := json.Unmarshal(jsonCH, &chsju); err != nil {
    29  		t.Fatal(err)
    30  	}
    31  
    32  	truthSpec, _ := utlsIdToSpec(truthClientHelloID)
    33  	jsonSpec := chsju.ClientHelloSpec()
    34  
    35  	// Compare CipherSuites
    36  	if !reflect.DeepEqual(jsonSpec.CipherSuites, truthSpec.CipherSuites) {
    37  		t.Errorf("JSONUnmarshaler %s: got %#v, want %#v", clientHelloSpecJSONTestIdentifier(truthClientHelloID), jsonSpec.CipherSuites, truthSpec.CipherSuites)
    38  	}
    39  
    40  	// Compare CompressionMethods
    41  	if !reflect.DeepEqual(jsonSpec.CompressionMethods, truthSpec.CompressionMethods) {
    42  		t.Errorf("JSONUnmarshaler %s: got %#v, want %#v", clientHelloSpecJSONTestIdentifier(truthClientHelloID), jsonSpec.CompressionMethods, truthSpec.CompressionMethods)
    43  	}
    44  
    45  	// Compare Extensions
    46  	if len(jsonSpec.Extensions) != len(truthSpec.Extensions) {
    47  		t.Errorf("JSONUnmarshaler %s: len(jsonExtensions) = %d != %d = len(truthExtensions)", clientHelloSpecJSONTestIdentifier(truthClientHelloID), len(jsonSpec.Extensions), len(truthSpec.Extensions))
    48  	}
    49  
    50  	for i := range jsonSpec.Extensions {
    51  		if !reflect.DeepEqual(jsonSpec.Extensions[i], truthSpec.Extensions[i]) {
    52  			if _, ok := jsonSpec.Extensions[i].(*UtlsPaddingExtension); ok {
    53  				testedPaddingExt := jsonSpec.Extensions[i].(*UtlsPaddingExtension)
    54  				savedPaddingExt := truthSpec.Extensions[i].(*UtlsPaddingExtension)
    55  				if testedPaddingExt.PaddingLen != savedPaddingExt.PaddingLen || testedPaddingExt.WillPad != savedPaddingExt.WillPad {
    56  					t.Errorf("got %#v, want %#v", testedPaddingExt, savedPaddingExt)
    57  				} else {
    58  					continue // UtlsPaddingExtension has non-nil function member
    59  				}
    60  			}
    61  			t.Errorf("JSONUnmarshaler %s: got %#v, want %#v", clientHelloSpecJSONTestIdentifier(truthClientHelloID), jsonSpec.Extensions[i], truthSpec.Extensions[i])
    62  		}
    63  	}
    64  }
    65  
    66  func TestClientHelloSpecUnmarshalJSON(t *testing.T) {
    67  	testClientHelloSpecUnmarshalJSON(t, "testdata/ClientHello-JSON-Chrome102.json", HelloChrome_102)
    68  	testClientHelloSpecUnmarshalJSON(t, "testdata/ClientHello-JSON-Firefox105.json", HelloFirefox_105)
    69  	testClientHelloSpecUnmarshalJSON(t, "testdata/ClientHello-JSON-iOS14.json", HelloIOS_14)
    70  	testClientHelloSpecUnmarshalJSON(t, "testdata/ClientHello-JSON-Edge106.json", HelloEdge_106)
    71  }
    72  
    73  func testClientHelloSpecUnmarshalJSON(
    74  	t *testing.T,
    75  	jsonFilepath string,
    76  	truthClientHelloID ClientHelloID,
    77  ) {
    78  	var jsonSpec ClientHelloSpec
    79  	jsonCH, err := os.ReadFile(jsonFilepath)
    80  	if err != nil {
    81  		t.Fatal(err)
    82  	}
    83  
    84  	if err := json.Unmarshal(jsonCH, &jsonSpec); err != nil {
    85  		t.Fatal(err)
    86  	}
    87  
    88  	truthSpec, _ := utlsIdToSpec(truthClientHelloID)
    89  
    90  	// Compare CipherSuites
    91  	if !reflect.DeepEqual(jsonSpec.CipherSuites, truthSpec.CipherSuites) {
    92  		t.Errorf("UnmarshalJSON %s: got %#v, want %#v", clientHelloSpecJSONTestIdentifier(truthClientHelloID), jsonSpec.CipherSuites, truthSpec.CipherSuites)
    93  	}
    94  
    95  	// Compare CompressionMethods
    96  	if !reflect.DeepEqual(jsonSpec.CompressionMethods, truthSpec.CompressionMethods) {
    97  		t.Errorf("UnmarshalJSON %s: got %#v, want %#v", clientHelloSpecJSONTestIdentifier(truthClientHelloID), jsonSpec.CompressionMethods, truthSpec.CompressionMethods)
    98  	}
    99  
   100  	// Compare Extensions
   101  	if len(jsonSpec.Extensions) != len(truthSpec.Extensions) {
   102  		t.Errorf("UnmarshalJSON %s: len(jsonExtensions) = %d != %d = len(truthExtensions)", jsonFilepath, len(jsonSpec.Extensions), len(truthSpec.Extensions))
   103  	}
   104  
   105  	for i := range jsonSpec.Extensions {
   106  		if !reflect.DeepEqual(jsonSpec.Extensions[i], truthSpec.Extensions[i]) {
   107  			if _, ok := jsonSpec.Extensions[i].(*UtlsPaddingExtension); ok {
   108  				testedPaddingExt := jsonSpec.Extensions[i].(*UtlsPaddingExtension)
   109  				savedPaddingExt := truthSpec.Extensions[i].(*UtlsPaddingExtension)
   110  				if testedPaddingExt.PaddingLen != savedPaddingExt.PaddingLen || testedPaddingExt.WillPad != savedPaddingExt.WillPad {
   111  					t.Errorf("got %#v, want %#v", testedPaddingExt, savedPaddingExt)
   112  				} else {
   113  					continue // UtlsPaddingExtension has non-nil function member
   114  				}
   115  			}
   116  			t.Errorf("UnmarshalJSON %s: got %#v, want %#v", clientHelloSpecJSONTestIdentifier(truthClientHelloID), jsonSpec.Extensions[i], truthSpec.Extensions[i])
   117  		}
   118  	}
   119  }
   120  
   121  func clientHelloSpecJSONTestIdentifier(id ClientHelloID) string {
   122  	return id.Client + id.Version
   123  }