github.com/psiphon-labs/psiphon-tunnel-core@v2.0.28+incompatible/psiphon/upstreamproxy/go-ntlm/ntlm/message_challenge_test.go (about)

     1  //Copyright 2013 Thomson Reuters Global Resources. BSD License please see License file for more information
     2  
     3  package ntlm
     4  
     5  import (
     6  	"bytes"
     7  	"encoding/base64"
     8  	"encoding/hex"
     9  	"fmt"
    10  	"testing"
    11  )
    12  
    13  func TestDecodeChallenge(t *testing.T) {
    14  	challengeMessage := "TlRMTVNTUAACAAAAAAAAADgAAADzgpjiuaopAbx9ejQAAAAAAAAAAKIAogA4AAAABQLODgAAAA8CAA4AUgBFAFUAVABFAFIAUwABABwAVQBLAEIAUAAtAEMAQgBUAFIATQBGAEUAMAA2AAQAFgBSAGUAdQB0AGUAcgBzAC4AbgBlAHQAAwA0AHUAawBiAHAALQBjAGIAdAByAG0AZgBlADAANgAuAFIAZQB1AHQAZQByAHMALgBuAGUAdAAFABYAUgBlAHUAdABlAHIAcwAuAG4AZQB0AAAAAAA="
    15  	challengeData, err := base64.StdEncoding.DecodeString(challengeMessage)
    16  
    17  	if err != nil {
    18  		t.Error("Could not base64 decode message data")
    19  	}
    20  
    21  	challenge, err := ParseChallengeMessage(challengeData)
    22  
    23  	if err != nil || challenge == nil {
    24  		t.Error("Failed to parse challenge message " + err.Error())
    25  	}
    26  
    27  	if challenge.TargetName.Len != 0 || challenge.TargetName.MaxLen != 0 || challenge.TargetName.Offset != 56 {
    28  		values := fmt.Sprintf("TargetName Len:%v MaxLen:%v Offset:%v", challenge.TargetName.Len, challenge.TargetName.MaxLen, challenge.TargetName.Offset)
    29  		t.Error("Failed to parse Target Name in challenge: " + values)
    30  	}
    31  
    32  	if challenge.NegotiateFlags != uint32(3801645811) {
    33  		t.Errorf("Challenge negotiate flags not correct should be %v got %d", uint32(3801645811), challenge.NegotiateFlags)
    34  	}
    35  
    36  	serverChallenge, err := hex.DecodeString("B9AA2901BC7D7A34")
    37  	if !bytes.Equal(challenge.ServerChallenge, serverChallenge) {
    38  		hex := hex.EncodeToString(challenge.ServerChallenge)
    39  		t.Error("Server challenge is not correct '" + hex + "'")
    40  	}
    41  
    42  	if challenge.Version.ProductMajorVersion != 5 || challenge.Version.ProductMinorVersion != 2 || challenge.Version.ProductBuild != 3790 || challenge.Version.NTLMRevisionCurrent != 15 {
    43  		t.Error("Version information is not correct: '" + challenge.Version.String() + "'")
    44  	}
    45  
    46  	if len(challenge.Payload) != int(challenge.TargetInfoPayloadStruct.Len) {
    47  		t.Error("Payload length is not long enough")
    48  	}
    49  
    50  	challenge.String()
    51  
    52  	outBytes := challenge.Bytes()
    53  
    54  	if len(outBytes) > 0 {
    55  		reparsed, err := ParseChallengeMessage(outBytes)
    56  		if err != nil {
    57  			t.Error("Could not re-parse challenge message")
    58  		}
    59  		if reparsed.String() != challenge.String() {
    60  			t.Error("Reparsed message is not the same")
    61  		}
    62  	} else {
    63  		t.Error("Invalid challenge messsage bytes")
    64  	}
    65  }