...

Source file src/github.com/google/certificate-transparency-go/fixchain/fix_error_test.go

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

     1  // Copyright 2016 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 fixchain
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  	"testing"
    21  
    22  	"github.com/google/certificate-transparency-go/x509"
    23  )
    24  
    25  func TestEqual(t *testing.T) {
    26  	equalTests := []struct {
    27  		e        *FixError
    28  		f        *FixError
    29  		expEqual bool
    30  	}{
    31  		{
    32  			&FixError{},
    33  			&FixError{},
    34  			true,
    35  		},
    36  		{
    37  			&FixError{Type: LogPostFailed},
    38  			&FixError{},
    39  			false,
    40  		},
    41  		{
    42  			&FixError{Type: ParseFailure},
    43  			&FixError{Type: LogPostFailed},
    44  			false,
    45  		},
    46  		{
    47  			&FixError{Cert: GetTestCertificateFromPEM(t, googleLeaf)},
    48  			&FixError{},
    49  			false,
    50  		},
    51  		{
    52  			&FixError{Cert: GetTestCertificateFromPEM(t, googleLeaf)},
    53  			&FixError{Cert: GetTestCertificateFromPEM(t, megaLeaf)},
    54  			false,
    55  		},
    56  		{
    57  			&FixError{
    58  				Chain: []*x509.Certificate{
    59  					GetTestCertificateFromPEM(t, googleLeaf),
    60  					GetTestCertificateFromPEM(t, thawteIntermediate),
    61  					GetTestCertificateFromPEM(t, verisignRoot),
    62  				},
    63  			},
    64  			&FixError{},
    65  			false,
    66  		},
    67  		{ // Chains with only one cert different.
    68  			&FixError{
    69  				Chain: []*x509.Certificate{
    70  					GetTestCertificateFromPEM(t, googleLeaf),
    71  					GetTestCertificateFromPEM(t, thawteIntermediate),
    72  					GetTestCertificateFromPEM(t, verisignRoot),
    73  				},
    74  			},
    75  			&FixError{
    76  				Chain: []*x509.Certificate{
    77  					GetTestCertificateFromPEM(t, googleLeaf),
    78  					GetTestCertificateFromPEM(t, thawteIntermediate),
    79  					GetTestCertificateFromPEM(t, comodoRoot),
    80  				},
    81  			},
    82  			false,
    83  		},
    84  		{ // Completely different chains.
    85  			&FixError{
    86  				Chain: []*x509.Certificate{
    87  					GetTestCertificateFromPEM(t, googleLeaf),
    88  					GetTestCertificateFromPEM(t, thawteIntermediate),
    89  					GetTestCertificateFromPEM(t, verisignRoot),
    90  				},
    91  			},
    92  			&FixError{
    93  				Chain: []*x509.Certificate{
    94  					GetTestCertificateFromPEM(t, megaLeaf),
    95  					GetTestCertificateFromPEM(t, comodoIntermediate),
    96  					GetTestCertificateFromPEM(t, comodoRoot),
    97  				},
    98  			},
    99  			false,
   100  		},
   101  		{
   102  			&FixError{URL: "https://www.test.com"},
   103  			&FixError{},
   104  			false,
   105  		},
   106  		{
   107  			&FixError{URL: "https://www.test.com"},
   108  			&FixError{URL: "https://www.test1.com"},
   109  			false,
   110  		},
   111  		{
   112  			&FixError{Bad: []byte(googleLeaf)},
   113  			&FixError{},
   114  			false,
   115  		},
   116  		{
   117  			&FixError{Bad: []byte(googleLeaf)},
   118  			&FixError{Bad: []byte(megaLeaf)},
   119  			false,
   120  		},
   121  		{
   122  			&FixError{Error: errors.New("error1")},
   123  			&FixError{},
   124  			false,
   125  		},
   126  		{
   127  			&FixError{Error: errors.New("error1")},
   128  			&FixError{Error: errors.New("error2")},
   129  			false,
   130  		},
   131  		{
   132  			&FixError{
   133  				Type: LogPostFailed,
   134  				Cert: GetTestCertificateFromPEM(t, googleLeaf),
   135  				Chain: []*x509.Certificate{
   136  					GetTestCertificateFromPEM(t, googleLeaf),
   137  					GetTestCertificateFromPEM(t, thawteIntermediate),
   138  					GetTestCertificateFromPEM(t, verisignRoot),
   139  				},
   140  				URL:   "https://www.test.com",
   141  				Bad:   GetTestCertificateFromPEM(t, googleLeaf).Raw,
   142  				Error: errors.New("log post failed"),
   143  			},
   144  			&FixError{},
   145  			false,
   146  		},
   147  		{
   148  			&FixError{},
   149  			&FixError{
   150  				Type: LogPostFailed,
   151  				Cert: GetTestCertificateFromPEM(t, googleLeaf),
   152  				Chain: []*x509.Certificate{
   153  					GetTestCertificateFromPEM(t, googleLeaf),
   154  					GetTestCertificateFromPEM(t, thawteIntermediate),
   155  					GetTestCertificateFromPEM(t, verisignRoot),
   156  				},
   157  				URL:   "https://www.test.com",
   158  				Bad:   GetTestCertificateFromPEM(t, googleLeaf).Raw,
   159  				Error: errors.New("log post failed"),
   160  			},
   161  			false,
   162  		},
   163  		{
   164  			&FixError{
   165  				Type: LogPostFailed,
   166  				Cert: GetTestCertificateFromPEM(t, googleLeaf),
   167  				Chain: []*x509.Certificate{
   168  					GetTestCertificateFromPEM(t, googleLeaf),
   169  					GetTestCertificateFromPEM(t, thawteIntermediate),
   170  					GetTestCertificateFromPEM(t, verisignRoot),
   171  				},
   172  				URL:   "https://www.test.com",
   173  				Bad:   GetTestCertificateFromPEM(t, googleLeaf).Raw,
   174  				Error: errors.New("log post failed"),
   175  			},
   176  			&FixError{
   177  				Type: LogPostFailed,
   178  				Cert: GetTestCertificateFromPEM(t, googleLeaf),
   179  				Chain: []*x509.Certificate{
   180  					GetTestCertificateFromPEM(t, googleLeaf),
   181  					GetTestCertificateFromPEM(t, thawteIntermediate),
   182  					GetTestCertificateFromPEM(t, verisignRoot),
   183  				},
   184  				URL:   "https://www.test.com",
   185  				Bad:   GetTestCertificateFromPEM(t, googleLeaf).Raw,
   186  				Error: errors.New("log post failed"),
   187  			},
   188  			true,
   189  		},
   190  		{ // nil test
   191  			&FixError{
   192  				Type: LogPostFailed,
   193  				Cert: GetTestCertificateFromPEM(t, googleLeaf),
   194  				Chain: []*x509.Certificate{
   195  					GetTestCertificateFromPEM(t, googleLeaf),
   196  					GetTestCertificateFromPEM(t, thawteIntermediate),
   197  					GetTestCertificateFromPEM(t, verisignRoot),
   198  				},
   199  				URL:   "https://www.test.com",
   200  				Bad:   GetTestCertificateFromPEM(t, googleLeaf).Raw,
   201  				Error: errors.New("log post failed"),
   202  			},
   203  			nil,
   204  			false,
   205  		},
   206  	}
   207  
   208  	for i, test := range equalTests {
   209  		if test.e.Equal(test.f) != test.expEqual {
   210  			t.Errorf("#%d: expected FixError.Equal() to return %t, returned %t", i, test.expEqual, !test.expEqual)
   211  		}
   212  	}
   213  }
   214  
   215  func TestTypeString(t *testing.T) {
   216  	typeStringTests := []struct {
   217  		ferr     FixError
   218  		expected string
   219  	}{
   220  		{
   221  			FixError{Type: None},
   222  			"None",
   223  		},
   224  		{
   225  			FixError{Type: ParseFailure},
   226  			"ParseFailure",
   227  		},
   228  		{
   229  			FixError{Type: CannotFetchURL},
   230  			"CannotFetchURL",
   231  		},
   232  		{
   233  			FixError{Type: FixFailed},
   234  			"FixFailed",
   235  		},
   236  		{
   237  			FixError{Type: LogPostFailed},
   238  			"LogPostFailed",
   239  		},
   240  		{
   241  			FixError{Type: VerifyFailed},
   242  			"VerifyFailed",
   243  		},
   244  		{
   245  			FixError{},
   246  			"None",
   247  		},
   248  	}
   249  
   250  	for i, test := range typeStringTests {
   251  		if got, want := test.ferr.TypeString(), test.expected; got != want {
   252  			t.Errorf("#%d: TypeString() returned %s, expected %s.", i, got, want)
   253  		}
   254  	}
   255  }
   256  
   257  func TestString(t *testing.T) {
   258  	stringTests := []struct {
   259  		ferr *FixError
   260  		str  string
   261  	}{
   262  		{
   263  			&FixError{Type: None},
   264  			"None\n",
   265  		},
   266  		{
   267  			&FixError{
   268  				Type: LogPostFailed,
   269  				Cert: GetTestCertificateFromPEM(t, googleLeaf),
   270  				Chain: []*x509.Certificate{
   271  					GetTestCertificateFromPEM(t, googleLeaf),
   272  					GetTestCertificateFromPEM(t, thawteIntermediate),
   273  					GetTestCertificateFromPEM(t, verisignRoot),
   274  				},
   275  				URL:   "https://www.test.com",
   276  				Error: errors.New("log post failed"),
   277  			},
   278  			"LogPostFailed\n" +
   279  				"Error: log post failed\n" +
   280  				"URL: https://www.test.com\n" +
   281  				"Cert: " + googleLeaf +
   282  				"Chain: " + googleLeaf + thawteIntermediate + verisignRoot,
   283  		},
   284  	}
   285  
   286  	for i, test := range stringTests {
   287  		if got, want := test.ferr.String(), test.str; got != want {
   288  			t.Errorf("#%d: String() returned %s, expected %s.", i, got, want)
   289  		}
   290  	}
   291  }
   292  
   293  func TestMarshalJSON(t *testing.T) {
   294  	marshalJSONTests := []*FixError{
   295  		{},
   296  		{
   297  			Type: LogPostFailed,
   298  			Cert: GetTestCertificateFromPEM(t, googleLeaf),
   299  			Chain: []*x509.Certificate{
   300  				GetTestCertificateFromPEM(t, googleLeaf),
   301  				GetTestCertificateFromPEM(t, thawteIntermediate),
   302  				GetTestCertificateFromPEM(t, verisignRoot),
   303  			},
   304  			URL:   "https://www.test.com",
   305  			Bad:   GetTestCertificateFromPEM(t, googleLeaf).Raw,
   306  			Error: errors.New("log post failed"),
   307  		},
   308  	}
   309  
   310  	for i, test := range marshalJSONTests {
   311  		b, err := test.MarshalJSON()
   312  		if err != nil {
   313  			t.Errorf("#%d: Error marshaling json: %s", i, err.Error())
   314  		}
   315  
   316  		ferr, err := UnmarshalJSON(b)
   317  		if err != nil {
   318  			t.Errorf("#%d: Error unmarshaling json: %s", i, err.Error())
   319  		}
   320  
   321  		if !test.Equal(ferr) {
   322  			t.Errorf("#%d: Original FixError does not match marshaled-then-unmarshaled FixError", i)
   323  		}
   324  	}
   325  }
   326  
   327  func TestDumpPEM(t *testing.T) {
   328  	dumpPEMTests := []string{googleLeaf}
   329  
   330  	for i, test := range dumpPEMTests {
   331  		cert := GetTestCertificateFromPEM(t, test)
   332  		p := dumpPEM(cert.Raw)
   333  		certFromPEM := GetTestCertificateFromPEM(t, p)
   334  		if !cert.Equal(certFromPEM) {
   335  			t.Errorf("#%d: cert from output of dumpPEM() does not match original", i)
   336  		}
   337  	}
   338  }
   339  
   340  func TestDumpChainPEM(t *testing.T) {
   341  	dumpChainPEMTests := []struct {
   342  		chain    []string
   343  		expected string
   344  	}{
   345  		{
   346  			[]string{googleLeaf, thawteIntermediate},
   347  			fmt.Sprintf("%s%s", googleLeaf, thawteIntermediate),
   348  		},
   349  	}
   350  
   351  	for i, test := range dumpChainPEMTests {
   352  		chain := extractTestChain(t, i, test.chain)
   353  		if got := dumpChainPEM(chain); got != test.expected {
   354  			t.Errorf("#%d: dumpChainPEM() returned %s, expected %s", i, got, test.expected)
   355  		}
   356  	}
   357  }
   358  

View as plain text