github.com/cloudflare/circl@v1.5.0/abe/cpabe/tkn20/format_test.go (about)

     1  package tkn20
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  )
     7  
     8  func TestPublicKeyFormat(t *testing.T) {
     9  	paramsData, err := os.ReadFile("testdata/publicKey")
    10  	if err != nil {
    11  		t.Fatalf("Unable to read public key")
    12  	}
    13  	pp := &PublicKey{}
    14  	err = pp.UnmarshalBinary(paramsData)
    15  	if err != nil {
    16  		t.Fatalf("unable to parse public key")
    17  	}
    18  }
    19  
    20  func TestSystemSecretKeyFormat(t *testing.T) {
    21  	secret, err := os.ReadFile("testdata/secretKey")
    22  	if err != nil {
    23  		t.Fatalf("Unable to read secret key")
    24  	}
    25  	sk := &SystemSecretKey{}
    26  	err = sk.UnmarshalBinary(secret)
    27  	if err != nil {
    28  		t.Fatalf("unable to parse system secret key")
    29  	}
    30  }
    31  
    32  func TestAttributeKeyFormat(t *testing.T) {
    33  	attributeKey, err := os.ReadFile("testdata/attributeKey")
    34  	if err != nil {
    35  		t.Fatalf("Unable to read secret key")
    36  	}
    37  	sk := &AttributeKey{}
    38  	err = sk.UnmarshalBinary(attributeKey)
    39  	if err != nil {
    40  		t.Fatalf("unable to parse secret key")
    41  	}
    42  }
    43  
    44  func TestCiphertext_v137(t *testing.T) {
    45  	// As of v1.3.8 ciphertext format changed to use wider prefixes.
    46  	// Ciphertexts in the previous format are still decryptable.
    47  	// The following functions are backwards-compatible:
    48  	// - AttributeKey.Decrypt
    49  	// - Attributes.CouldDecrypt
    50  	// - Policy.ExtractFromCiphertext
    51  	testCiphertext(t, "testdata/ciphertext_v137")
    52  }
    53  
    54  func TestCiphertext(t *testing.T) {
    55  	testCiphertext(t, "testdata/ciphertext")
    56  }
    57  
    58  func testCiphertext(t *testing.T, ctName string) {
    59  	t.Logf("Checking ciphertext: %v\n", ctName)
    60  	ciphertext, err := os.ReadFile(ctName)
    61  	if err != nil {
    62  		t.Fatalf("Unable to read ciphertext data")
    63  	}
    64  	attributeKey, err := os.ReadFile("testdata/attributeKey")
    65  	if err != nil {
    66  		t.Fatalf("Unable to read secret key")
    67  	}
    68  	sk := AttributeKey{}
    69  	err = sk.UnmarshalBinary(attributeKey)
    70  	if err != nil {
    71  		t.Fatalf("unable to parse secret key")
    72  	}
    73  	attrs := Attributes{}
    74  	attrs.FromMap(map[string]string{"country": "NL", "EU": "true"})
    75  	if !attrs.CouldDecrypt(ciphertext) {
    76  		t.Fatal("these attributes will be unable to decrypt message")
    77  	}
    78  	policy := Policy{}
    79  	err = policy.FromString("EU: true")
    80  	if err != nil {
    81  		t.Fatal("error creating policy from string")
    82  	}
    83  	gotPolicy := new(Policy)
    84  	err = gotPolicy.ExtractFromCiphertext(ciphertext)
    85  	if err != nil {
    86  		t.Fatal("error extracting policy from ciphertext")
    87  	}
    88  	if !policy.Equal(gotPolicy) {
    89  		t.Fatal("ciphertext's policy mismatches the original policy")
    90  	}
    91  	msg, err := sk.Decrypt(ciphertext)
    92  	if err != nil {
    93  		t.Fatal("unable to decrypt message")
    94  	}
    95  	if string(msg) != "Be sure to drink your ovaltine!" {
    96  		t.Fatal("message incorrect")
    97  	}
    98  }