...

Source file src/github.com/google/certificate-transparency-go/submission/loglist_manager_test.go

Documentation: github.com/google/certificate-transparency-go/submission

     1  // Copyright 2019 Google LLC. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package submission
    16  
    17  import (
    18  	"context"
    19  	"os"
    20  	"strings"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/google/certificate-transparency-go/testdata"
    25  	"github.com/google/trillian/monitoring"
    26  )
    27  
    28  func TestNoLLRefresher(t *testing.T) {
    29  	llm := NewLogListManager(nil, nil)
    30  	_, err := llm.RefreshLogList(context.Background())
    31  	if err == nil {
    32  		t.Errorf("llm.RefreshLogList() on nil LogListRefresher expected to get error, got none")
    33  	}
    34  }
    35  
    36  func TestNoLLRefresherAfterRun(t *testing.T) {
    37  	llm := NewLogListManager(nil, nil)
    38  	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
    39  	defer cancel()
    40  
    41  	llm.Run(ctx, 3*time.Second)
    42  	select {
    43  	case <-llm.Errors:
    44  		return
    45  	case <-ctx.Done():
    46  		t.Errorf("llm.Run() on nil LogListRefresher expected to emit error, got none")
    47  	}
    48  }
    49  
    50  func TestFirstRefresh(t *testing.T) {
    51  	f, err := createTempFile(testdata.SampleLogList3)
    52  	if err != nil {
    53  		t.Fatalf("createTempFile(%q) = (_, %q), want (_, nil)", testdata.SampleLogList3, err)
    54  	}
    55  	defer os.Remove(f)
    56  
    57  	llr := NewLogListRefresher(f)
    58  	llm := NewLogListManager(llr, nil)
    59  	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
    60  	defer cancel()
    61  
    62  	llm.Run(ctx, 3*time.Second)
    63  	select {
    64  	case <-llm.LLUpdates:
    65  		return
    66  	case err := <-llm.Errors:
    67  		t.Errorf("llm.Run() emitted error %q while expected none", err)
    68  	case <-ctx.Done():
    69  		t.Errorf("llm.Run() on stub LogListRefresher expected to emit update, got none")
    70  	}
    71  }
    72  
    73  func TestSecondRefresh(t *testing.T) {
    74  	f, err := createTempFile(testdata.SampleLogList3)
    75  	if err != nil {
    76  		t.Fatalf("createTempFile(%q) = (_, %q), want (_, nil)", testdata.SampleLogList3, err)
    77  	}
    78  	defer os.Remove(f)
    79  
    80  	llr := NewLogListRefresher(f)
    81  	llm := NewLogListManager(llr, monitoring.InertMetricFactory{})
    82  	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
    83  	defer cancel()
    84  	halfCtx, halfCancel := context.WithTimeout(context.Background(), time.Second)
    85  	defer halfCancel()
    86  
    87  	llm.Run(ctx, 300*time.Millisecond)
    88  	readFirst := false
    89  First:
    90  	for {
    91  		select {
    92  		case <-llm.LLUpdates:
    93  			if !readFirst {
    94  				readFirst = true
    95  			} else {
    96  				t.Errorf("llm.Run() emitted Log-list update when no updates happened")
    97  			}
    98  		case err := <-llm.Errors:
    99  			t.Errorf("llm.Run() remitted error %q while expected none", err)
   100  		case <-halfCtx.Done():
   101  			if !readFirst {
   102  				t.Errorf("llm.Run() didn't emit any Log-list updates on init. Expected one")
   103  			}
   104  			break First
   105  		}
   106  	}
   107  
   108  	sampleLogListUpdate := strings.Replace(testdata.SampleLogList3, "ct.googleapis.com/racketeer/", "ct.googleapis.com/racketeer/v2/", 1)
   109  	if err := os.WriteFile(f, []byte(sampleLogListUpdate), 0644); err != nil {
   110  		t.Fatalf("unable to update Log-list data file: %q", err)
   111  	}
   112  	select {
   113  	case <-llm.LLUpdates:
   114  		return
   115  	case err := <-llm.Errors:
   116  		t.Errorf("llm.Run() emitted error %q while expected none", err)
   117  	case <-ctx.Done():
   118  		t.Errorf("llm.Run() on stub LogListRefresher expected to emit update, got none")
   119  	}
   120  }
   121  

View as plain text