浏览代码

x/model: test UnmarshalText safe copy

Blake Mizerany 1 年之前
父节点
当前提交
a292cde2f3
共有 2 个文件被更改,包括 38 次插入0 次删除
  1. 25 0
      x/model/digest_test.go
  2. 13 0
      x/model/name_test.go

+ 25 - 0
x/model/digest_test.go

@@ -56,3 +56,28 @@ func TestDigestString(t *testing.T) {
 		}
 	}
 }
+
+func TestDigestUnmarshalText(t *testing.T) {
+	const testDigest = "sha256-1234"
+	t.Run("UnmarshalText (into Valid)", func(t *testing.T) {
+		d := ParseDigest(testDigest)
+		if !d.IsValid() {
+			panic("invalid test")
+		}
+		if err := d.UnmarshalText(nil); err == nil {
+			t.Errorf("UnmarshalText on valid Digest did not return error")
+		}
+		if d.String() != testDigest {
+			t.Errorf("UnmarshalText on valid Digest changed Digest: %q", d.String())
+		}
+	})
+	t.Run("UnmarshalText make safe copy", func(t *testing.T) {
+		data := []byte(testDigest)
+		var d Digest
+		d.UnmarshalText(data)
+		data[0] = 'x'
+		if d.String() != testDigest {
+			t.Errorf("UnmarshalText did not make a safe copy")
+		}
+	})
+}

+ 13 - 0
x/model/name_test.go

@@ -443,6 +443,19 @@ func TestNameTextMarshal(t *testing.T) {
 			t.Errorf("MarshalText allocs = %v; want <= 1", allocs)
 		}
 	})
+
+	t.Run("UnmarshalTest makes safe copy", func(t *testing.T) {
+		// UnmarshalText should make a copy of the data.
+		data := []byte("mistral:latest+Q4_0")
+		p := Name{}
+		if err := p.UnmarshalText(data); err != nil {
+			t.Fatal(err)
+		}
+		data[0] = 'x'
+		if p.String() != "mistral:latest+Q4_0" {
+			t.Errorf("UnmarshalText() did not make a copy")
+		}
+	})
 }
 
 func TestSQL(t *testing.T) {