github.com/hpcng/singularity@v3.1.1+incompatible/pkg/sypgp/fetch_test.go (about)

     1  // Copyright (c) 2018, Sylabs Inc. All rights reserved.
     2  // This software is licensed under a 3-clause BSD license. Please consult the
     3  // LICENSE.md file distributed with the sources of this project regarding your
     4  // rights to use or distribute this software.
     5  
     6  package sypgp
     7  
     8  import (
     9  	"log"
    10  	"net/http"
    11  	"net/http/httptest"
    12  	"testing"
    13  
    14  	"github.com/sylabs/singularity/internal/pkg/test"
    15  	"golang.org/x/crypto/openpgp"
    16  	"golang.org/x/crypto/openpgp/armor"
    17  )
    18  
    19  type mockServer struct {
    20  	code int
    21  	el   openpgp.EntityList
    22  }
    23  
    24  func (ms *mockServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    25  	w.WriteHeader(ms.code)
    26  	if ms.code == http.StatusOK {
    27  		w.Header().Set("Content-Type", "application/pgp-keys")
    28  
    29  		wr, err := armor.Encode(w, openpgp.PublicKeyType, nil)
    30  		if err != nil {
    31  			log.Fatalf("failed to get encoder: %v", err)
    32  		}
    33  		defer wr.Close()
    34  
    35  		for _, e := range ms.el {
    36  			if err = e.Serialize(wr); err != nil {
    37  				log.Fatalf("failed to serialize entity: %v", err)
    38  			}
    39  		}
    40  	}
    41  }
    42  
    43  func TestFetchPubkey(t *testing.T) {
    44  	ms := &mockServer{}
    45  	srv := httptest.NewServer(ms)
    46  	defer srv.Close()
    47  
    48  	fp := string(testEntity.PrimaryKey.Fingerprint[:])
    49  
    50  	tests := []struct {
    51  		name        string
    52  		code        int
    53  		el          openpgp.EntityList
    54  		fingerprint string
    55  		uri         string
    56  		authToken   string
    57  		wantErr     bool
    58  	}{
    59  		{"Success", http.StatusOK, openpgp.EntityList{testEntity}, fp, srv.URL, "", false},
    60  		{"SuccessToken", http.StatusOK, openpgp.EntityList{testEntity}, fp, srv.URL, "token", false},
    61  		{"NoKeys", http.StatusOK, openpgp.EntityList{}, fp, srv.URL, "token", true},
    62  		{"TwoKeys", http.StatusOK, openpgp.EntityList{testEntity, testEntity}, fp, srv.URL, "token", true},
    63  		{"BadURL", http.StatusOK, openpgp.EntityList{testEntity}, fp, ":", "", true},
    64  		{"NotFound", http.StatusNotFound, nil, fp, srv.URL, "", true},
    65  		{"Unauthorized", http.StatusUnauthorized, nil, fp, srv.URL, "", true},
    66  	}
    67  	for _, tt := range tests {
    68  		t.Run(tt.name, test.WithoutPrivilege(func(t *testing.T) {
    69  			ms.code = tt.code
    70  			ms.el = tt.el
    71  
    72  			el, err := FetchPubkey(tt.fingerprint, tt.uri, tt.authToken, false)
    73  			if (err != nil) != tt.wantErr {
    74  				t.Fatalf("unexpected error: %v", err)
    75  				return
    76  			}
    77  
    78  			if !tt.wantErr {
    79  				if len(el) != 1 {
    80  					t.Fatalf("unexpected number of entities returned: %v", len(el))
    81  				}
    82  				for i := range tt.el {
    83  					if fp := el[i].PrimaryKey.Fingerprint; fp != tt.el[i].PrimaryKey.Fingerprint {
    84  						t.Errorf("fingerprint mismatch: %v / %v", fp, tt.el[i].PrimaryKey.Fingerprint)
    85  					}
    86  				}
    87  			}
    88  		}))
    89  	}
    90  }