Browse code

add TestDaemonwithwrongkey test case

Signed-off-by: Yuan Sun <sunyuan3@huawei.com>

Yuan Sun authored on 2015/04/06 09:57:19
Showing 1 changed files
... ...
@@ -834,3 +834,65 @@ func TestDaemonUnixSockCleanedUp(t *testing.T) {
834 834
 
835 835
 	logDone("daemon - unix socket is cleaned up")
836 836
 }
837
+
838
+func TestDaemonwithwrongkey(t *testing.T) {
839
+	type Config struct {
840
+		Crv string `json:"crv"`
841
+		D   string `json:"d"`
842
+		Kid string `json:"kid"`
843
+		Kty string `json:"kty"`
844
+		X   string `json:"x"`
845
+		Y   string `json:"y"`
846
+	}
847
+
848
+	os.Remove("/etc/docker/key.json")
849
+	d := NewDaemon(t)
850
+	if err := d.Start(); err != nil {
851
+		t.Fatalf("Failed to start daemon: %v", err)
852
+	}
853
+
854
+	if err := d.Stop(); err != nil {
855
+		t.Fatalf("Could not stop daemon: %v", err)
856
+	}
857
+
858
+	config := &Config{}
859
+	bytes, err := ioutil.ReadFile("/etc/docker/key.json")
860
+	if err != nil {
861
+		t.Fatalf("Error reading key.json file: %s", err)
862
+	}
863
+
864
+	// byte[] to Data-Struct
865
+	if err := json.Unmarshal(bytes, &config); err != nil {
866
+		t.Fatalf("Error Unmarshal: %s", err)
867
+	}
868
+
869
+	//replace config.Kid with the fake value
870
+	config.Kid = "VSAJ:FUYR:X3H2:B2VZ:KZ6U:CJD5:K7BX:ZXHY:UZXT:P4FT:MJWG:HRJ4"
871
+
872
+	// NEW Data-Struct to byte[]
873
+	newBytes, err := json.Marshal(&config)
874
+	if err != nil {
875
+		t.Fatalf("Error Marshal: %s", err)
876
+	}
877
+
878
+	// write back
879
+	if err := ioutil.WriteFile("/etc/docker/key.json", newBytes, 0400); err != nil {
880
+		t.Fatalf("Error ioutil.WriteFile: %s", err)
881
+	}
882
+
883
+	d1 := NewDaemon(t)
884
+
885
+	if err := d1.Start(); err == nil {
886
+		d1.Stop()
887
+		t.Fatalf("It should not be succssful to start daemon with wrong key: %v", err)
888
+	}
889
+
890
+	content, _ := ioutil.ReadFile(d1.logFile.Name())
891
+
892
+	if !strings.Contains(string(content), "Public Key ID does not match") {
893
+		t.Fatal("Missing KeyID message from daemon logs")
894
+	}
895
+
896
+	os.Remove("/etc/docker/key.json")
897
+	logDone("daemon - it should be failed to start daemon with wrong key")
898
+}